Skip to content

Commit

Permalink
feat(developer): Automatic sign up for email marketing
Browse files Browse the repository at this point in the history
refs #288
  • Loading branch information
steveoh committed May 24, 2024
1 parent 527238f commit 9f5267a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/developer/functions/.secret.local.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
LEGACY_PEPPER=some pepper value
SENDGRID_API_KEY=some sendgrid api key
30 changes: 23 additions & 7 deletions src/developer/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,32 @@ const cors = [
* @param {any} async(user - The user details from the OIDC flow from Utahid.
* @returns {bool} - The true or false result of the create user method.
*/
export const onCreateUser = auth.user().onCreate(async (user) => {
debug('[auth::user::onCreate] importing createUser');
const createUser = (await import('./auth/onCreate.js')).createUser;
export const onCreateUser = auth
.user({
secrets: ['SENDGRID_API_KEY'],
})
.onCreate(async (user) => {
debug('[auth::user::onCreate] importing createUser');
const createUser = (await import('./auth/onCreate.js')).createUser;

const result = await createUser(user);
const result = await createUser(user, process.env.SENDGRID_API_KEY ?? '');

This comment has been minimized.

Copy link
@stdavis

stdavis May 24, 2024

Member

Did you mean to send the API key to the createUser function?


debug('[auth::user::onCreate]', result);
debug('[auth::user::onCreate]', result);

return result;
});
debug('debug mode', process.env.NODE_ENV);
if (process.env.NODE_ENV !== 'development') {
const mailingListSignUp = (await import('./mail.js')).mailingListSignUp;

const mailListResult = await mailingListSignUp(
data,
process.env.SENDGRID_API_KEY ?? '',
);

debug('[auth::user::mailingListSignUp]', mailListResult);
}

return result;
});

// functions
/**
Expand Down
45 changes: 45 additions & 0 deletions src/developer/functions/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import client from '@sendgrid/client';

export const mailingListSignUp = async (data, apiKey) => {
client.setApiKey(apiKey);

let response;
const [first_name, last_name] = data.displayName.split(' ');

const body = {
list_ids: ['272a49d6-7d98-46bd-9312-7310e8e73ba9'],
contacts: [
{
email: data.email,
first_name,
last_name,
},
],
};

try {
response = await client.request({
method: 'PUT',
url: '/v3/marketing/contacts',
body,
});

if (response.statusCode !== 202) {
error('[mail::mailingListSignUp] sendgrid job error', {
response: response.body,
data,
});

return false;
}
} catch (errorMessage) {
error('[mail::mailingListSignUp] sendgrid error', {
errorMessage,
data,
});

return false;
}

return true;
};
78 changes: 72 additions & 6 deletions src/developer/functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/developer/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"start": "npm run shell"
},
"dependencies": {
"@sendgrid/client": "^8.1.3",
"firebase-admin": "^12.1.1",
"firebase-functions": "^5.0.1",
"ioredis": "^5.4.1",
Expand Down

0 comments on commit 9f5267a

Please sign in to comment.