Skip to content

Commit

Permalink
style(justInCase): lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Jul 7, 2024
1 parent cddce52 commit 103049a
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions packages/sync26/justInCase.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @file Share a folder if owner is not available to keep it confidential.
*
*
* [Installable Triggers | Apps Script |  Google for Developers](https://developers.google.com/apps-script/guides/triggers/installable)
*
*
* see also [Dead man's switch](https://en.wikipedia.org/wiki/Dead_man%27s_switch)
*
*
* TODO: rework as CALENDAR trigger?
*/

Expand All @@ -15,7 +15,11 @@ function shareFolder(io = {}) {
emailAddress,
} = io;

console.log('adding viewer to folder', { emailAddress, folder: folder.getName(), folderId });
console.log('adding viewer to folder', {
emailAddress,
folder: folder.getName(),
folderId,
});
folder.addViewer(emailAddress);
}

Expand Down Expand Up @@ -47,7 +51,7 @@ function shareJustInCase(io = {}) {
sheetName = 'Triggers',
doc = SpreadsheetApp.getActive(),
sheet = doc.getSheetByName(sheetName),
clock = (() => new Date()),
clock = () => new Date(),
getFolderById = id => DriveApp.getFolderById(id),
} = io;

Expand All @@ -57,25 +61,37 @@ function shareJustInCase(io = {}) {
const getCell = colName => {
const colNum = hd.indexOf(colName) + 1;
return row => sheet.getRange(row, colNum, 1, 1);
}
};
let firstDeadline;
for (let row = sheet.getLastRow(); row > 1 ; row -= 1) {
for (let row = sheet.getLastRow(); row > 1; row -= 1) {
const detail = getRowRecord(sheet, row, hd);

const { folderId } = getFolderId(getCell('folder')(row));
if (!folderId) continue;
const folder = getFolderById(folderId);

getCell('viewers')(row).setValue(folder.getViewers().map(u => u.getEmail()).join(', '));

const note = detail.status ? (clock() >= detail.deadline ? 'Ready' : 'Pending') : 'Disabled';
getCell('viewers')(row).setValue(
folder
.getViewers()
.map(u => u.getEmail())
.join(', '),
);

const note = detail.status
? clock() >= detail.deadline
? 'Ready'
: 'Pending'
: 'Disabled';
console.log({ note, ...detail });
// TODO: consider a row proxy so we can do rowRecord.note = 'Off';
getCell('note')(row).setValue(note);
if (note !== 'Pending') continue;
if (note !== 'Pending') continue;

const { deadline } = detail;
if (firstDeadline === undefined || deadline.getTime() < firstDeadline.getTime()) {
if (
firstDeadline === undefined ||
deadline.getTime() < firstDeadline.getTime()
) {
firstDeadline = deadline;
// TODO: capture row; highlight it?
}
Expand All @@ -84,9 +100,10 @@ function shareJustInCase(io = {}) {

const { newTrigger = (...args) => ScriptApp.newTrigger(...args) } = io;
const trigger = newTrigger('onSharingTrigger')
.timeBased().at(firstDeadline)
.timeBased()
.at(firstDeadline)
.create();
const triggerId = trigger.getUniqueId()
const triggerId = trigger.getUniqueId();
console.log('created trigger', { triggerId, at: firstDeadline });
}

Expand All @@ -95,7 +112,7 @@ function onSharingTrigger(io = {}) {
sheetName = 'Triggers',
doc = SpreadsheetApp.getActive(),
sheet = doc.getSheetByName(sheetName),
clock = (() => new Date()),
clock = () => new Date(),
current = clock(),
getFolderById = id => DriveApp.getFolderById(id),
} = io;
Expand All @@ -106,24 +123,35 @@ function onSharingTrigger(io = {}) {
const getCell = colName => {
const colNum = hd.indexOf(colName) + 1;
return row => sheet.getRange(row, colNum, 1, 1);
}
for (let row = sheet.getLastRow(); row > 1 ; row -= 1) {
};
for (let row = sheet.getLastRow(); row > 1; row -= 1) {
const detail = getRowRecord(sheet, row, hd);

const { folderId } = getFolderId(getCell('folder')(row));
if (!folderId) continue;
const folder = getFolderById(folderId);

const note = detail.status ? (current >= detail.deadline ? 'Ready' : 'Pending') : 'Disabled';
const note = detail.status
? current >= detail.deadline
? 'Ready'
: 'Pending'
: 'Disabled';
console.log({ note, ...detail });
getCell('note')(row).setValue(note);
if (note !== 'Ready') continue;
if (note !== 'Ready') continue;

const viewersToAdd = detail.viewersToAdd.split(',').map(addr => addr.trim());
const viewersToAdd = detail.viewersToAdd
.split(',')
.map(addr => addr.trim());
for (const emailAddress of viewersToAdd) {
shareFolder({ folder, emailAddress });
}
const viewersPost = [...new Set([... folder.getViewers().map(u => u.getEmail()), ...viewersToAdd])]
const viewersPost = [
...new Set([
...folder.getViewers().map(u => u.getEmail()),
...viewersToAdd,
]),
];
getCell('viewers')(row).setValue(viewersPost.join(', '));
getCell('note')(row).setValue(`Shared`);
getCell('triggeredAt')(row).setValue(current);
Expand Down

0 comments on commit 103049a

Please sign in to comment.