Skip to content

Commit

Permalink
feat: #8340 ab email url (#8740)
Browse files Browse the repository at this point in the history
* update serverless

* fix yaml errors, use node 18, add lambda to vpc

* change landing url if they only have agentbox
  • Loading branch information
joshbalfour authored Mar 16, 2023
1 parent 4c0fb5a commit ec82a4f
Show file tree
Hide file tree
Showing 194 changed files with 1,752 additions and 401 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/cognito-custom-mail-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
},
"devDependencies": {
"@reapit/config-manager": "workspace:packages/config-manager",
"@reapit/foundations-ts-definitions": "workspace:^",
"@reapit/ts-scripts": "workspace:packages/ts-scripts",
"@reapit/ts-types": "workspace:packages/ts-types",
"@reapit/utils-node": "workspace:packages/utils-node",
"@types/mjml": "^4.7.0",
"@types/node": "^18.14.2",
Expand All @@ -57,9 +57,9 @@
"mjml": "^4.13.0",
"ncp": "^2.0.0",
"rimraf": "^3.0.2",
"serverless": "^2.72.3",
"serverless": "^3.28.1",
"serverless-deployment-bucket": "^1.6.0",
"serverless-offline": "^7.1.0",
"serverless-offline": "^12.0.4",
"serverless-plugin-log-retention": "^2.0.0",
"serverless-webpack": "^5.11.0",
"ts-loader": "^9.4.2",
Expand Down
34 changes: 31 additions & 3 deletions packages/cognito-custom-mail-lambda/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,50 @@ custom:
includeModules: true
packager: 'yarn'
packagerOptions:
noFrozenLockFile: false
noFrozenLockfile: false
excludeFiles: src/**/*.test.ts
includeFiles: src/mailer/templates/**
keepOutputDirectory: true
logRetentionInDays: 30

provider:
name: aws
runtime: 'nodejs16.x'
runtime: 'nodejs18.x'
stage: ${opt:stage, 'dev'}
region: eu-west-2
deploymentBucket:
name: ${self:custom.s3CloudFormBucket}
vpc:
securityGroupIds:
- Fn::ImportValue:
Fn::Join:
- ""
- - 'rpt-paas-'
- ${self:provider.stage}
- '-sg-LambdaSecurityGroup'
subnetIds:
- Fn::ImportValue:
Fn::Join:
- ""
- - 'rpt-paas-'
- ${self:provider.stage}
- '-vpc-PrivateSubnet1'
- Fn::ImportValue:
Fn::Join:
- ""
- - 'rpt-paas-'
- ${self:provider.stage}
- '-vpc-PrivateSubnet2'
- Fn::ImportValue:
Fn::Join:
- ""
- - 'rpt-paas-'
- ${self:provider.stage}
- '-vpc-PrivateSubnet3'
environment:
COGNITO_USERPOOL_ID: ${self:custom.env.COGNITO_USERPOOL_ID}
MARKET_PLACE_URL: ${self:custom.env.MARKET_PLACE_URL}
INTERNAL_ORG_SERVICE_URL: ${self:custom.env.INTERNAL_ORG_SERVICE_URL}
AGENTBOX_URL: ${self:custom.env.AGENTBOX_URL}

package:
include:
Expand Down
31 changes: 27 additions & 4 deletions packages/cognito-custom-mail-lambda/src/mailer/custom-mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { CognitoUserPoolTriggerHandler } from 'aws-lambda'
import forgotPasswordTemplate from './templates/forgot-password.html'
import confirmRegistrationTemplate from './templates/confirm-registration.html'
import adminUserInviteTemplate from './templates/admin-user-invite.html'
import { UserModel } from '@reapit/foundations-ts-definitions'

const confirmRegistrationUrl = `${process.env.MARKET_PLACE_URL}/login`
const resetPasswordUrl = `${process.env.MARKET_PLACE_URL}/reset-password`
const internalOrgServiceUrl = process.env.INTERNAL_ORG_SERVICE_URL
const agentboxUrl = process.env.AGENTBOX_URL

const replaceAll = (str: string, find: string, replace: string): string => {
return str.replace(new RegExp(find, 'g'), replace)
Expand All @@ -17,6 +20,27 @@ const format = (html: string, object: Object) => {
return html
}

const getConfirmRegistrationUrl = async (emailAddress: string) => {
const userId = Buffer.from(emailAddress).toString('base64').replace('==', '')
console.log(userId)
const res = await fetch(`${internalOrgServiceUrl}/Users/${userId}`, {
headers: {
'api-version': '2020-01-31',
},
})
if (res.status === 404) {
throw new Error('user not found')
}
if (!res.ok) {
throw new Error('error getting user')
}
const user: UserModel = await res.json()
if (user.products.length === 1 && user.products[0].id === 'agentbox') {
return agentboxUrl
}
return confirmRegistrationUrl
}

export const customMailer: CognitoUserPoolTriggerHandler = async (event, _context, callback) => {
if (event.userPoolId === process.env.COGNITO_USERPOOL_ID) {
switch (event.triggerSource) {
Expand All @@ -31,18 +55,17 @@ export const customMailer: CognitoUserPoolTriggerHandler = async (event, _contex
break

case 'CustomMessage_SignUp':
event.response.emailSubject = 'Reapit Connect - Forgotten Password'
event.response.emailSubject = 'Welcome to Reapit Connect'
event.response.emailMessage = format(confirmRegistrationTemplate, {
// userName: event.request.userAttributes.name,
url: confirmRegistrationUrl,
url: await getConfirmRegistrationUrl(event.request.userAttributes.email),
})
break

case 'CustomMessage_AdminCreateUser':
event.response.emailSubject = 'Welcome to Reapit Connect'
event.response.emailMessage = `${format(adminUserInviteTemplate, {
name: event.request.userAttributes.name,
url: confirmRegistrationUrl,
url: await getConfirmRegistrationUrl(event.request.userAttributes.email),
verificationCode: event.request.codeParameter as string,
})}`
break
Expand Down
Loading

0 comments on commit ec82a4f

Please sign in to comment.