Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Group membership #47

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion group-membership/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Sheet is edited. To learn more visit the

## Try it

1. Copy the [External Sustainability Group List](https://docs.google.com/spreadsheets/d/1toqdDkWSAOL7aIElil59RHJH0b1Efebg7GBlgjn3B2Y/edit#gid=0) from your _G Suite_ account.
1. Copy the [External Sustainability Group List](https://docs.google.com/spreadsheets/d/1toqdDkWSAOL7aIElil59RHJH0b1Efebg7GBlgjn3B2Y/copy) from your _G Suite_ account.
TechandEco marked this conversation as resolved.
Show resolved Hide resolved

1. Enter for testing purposes a _Gmail address you own_ and a Google Group you
have _rights to manage_ its membership. You can learn about
Expand Down
Binary file not shown.
31 changes: 15 additions & 16 deletions group-membership/src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const GOOGLE_GROUP = 'Google Group';
const ALLOWED = 'Allowed';
const EMAIL_TEMPLATE_DOC_URL = 'Email template doc URL';
const EMAIL_SUBJECT = 'Email subject';
const STATUS = 'Status';
const EMAIL_STATE = 'Email state';

const STATUS_VALUE = {
const EMAIL_STATE_VALUE = {
sent: 'Sent',
alreadyInGroup: 'Already in group',
notSent: 'Not sent',
requiredFieldMissing: 'Required field(s) missing: fill out all fields for this row',
emptyRow: '',
allowedFieldNotSpecified: '',
};

/**
Expand Down Expand Up @@ -57,20 +57,20 @@ function onEditInstallableTrigger(e) {
return result;
}, {});

// Update the entries Object with the status returned by addToGroup().
// Update the entries Object with the email state returned by addToGroup().
try {
const statusValue = addToGroup(
const emailState = addToGroup(
entries[EMAIL],
entries[GOOGLE_GROUP],
entries[ALLOWED],
entries[EMAIL_TEMPLATE_DOC_URL],
entries[EMAIL_SUBJECT]
);
entries[STATUS] = statusValue == STATUS_VALUE.sent ?
`${statusValue}: ${new Date()}` : statusValue;
entries[EMAIL_STATE] = emailState == EMAIL_STATE_VALUE.sent ?
`${emailState}: ${new Date()}` : emailState;
} catch (e) {
TechandEco marked this conversation as resolved.
Show resolved Hide resolved
// If there's an error, report that as the status.
entries[STATUS] = e;
// If there's an error, report that as the email state.
entries[EMAIL_STATE] = e;
}

// Convert the updated entries Object into a row Array.
TechandEco marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -92,17 +92,17 @@ function onEditInstallableTrigger(e) {
* of the welcome email sent to a user added to the group.
* @param {string} emailSubject - subject of welcome email sent to user added
* to group.
* @return {string} - status if email was sent to a user added in the sheet.
* @return {string} - state if email was sent to a user added in the sheet.
TechandEco marked this conversation as resolved.
Show resolved Hide resolved
*/
function addToGroup(userEmail, groupEmail, allowed, emailTemplateDocUrl, emailSubject) {
if (!allowed) {
return STATUS_VALUE.emptyRow;
return EMAIL_STATE_VALUE.allowedFieldNotSpecified;
}
if (!userEmail || !groupEmail || !emailTemplateDocUrl || !emailSubject) {
return STATUS_VALUE.requiredFieldMissing;
return EMAIL_STATE_VALUE.requiredFieldMissing;
}
if (allowed.toLowerCase() != 'yes') {
return STATUS_VALUE.notSent;
return EMAIL_STATE_VALUE.notSent;
}

// If the group does not contain the user's email, add it and send an email.
Expand All @@ -127,10 +127,9 @@ function addToGroup(userEmail, groupEmail, allowed, emailTemplateDocUrl, emailSu
htmlBody: emailBody,
});

// Set the status to the current date.
return STATUS_VALUE.sent;
return EMAIL_STATE_VALUE.sent;
}
return STATUS_VALUE.alreadyInGroup;
return EMAIL_STATE_VALUE.alreadyInGroup;
}

/**
Expand Down