Skip to content

Commit

Permalink
Parse config and extrac fromEmailAddress from there
Browse files Browse the repository at this point in the history
  • Loading branch information
kin0992 committed Nov 6, 2023
1 parent 1f51c75 commit 6fccf2b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 17 additions & 1 deletion apps/cognito-functions/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ export const customMessageHandler = pipe(
}, customMessage.makeHandler)
);

export const sensEmailHandler = pipe(new SES(), sendEmail.makeHandler);
export const sensEmailHandler = pipe(
{ fromEmailAddress: process.env.FROM_EMAIL_ADDRESS },
sendEmail.SendEmailConfig.decode,
E.fold(
(errors) => {
// eslint-disable-next-line functional/no-expression-statements
console.log(PR.failure(errors).join('\n'));
// eslint-disable-next-line functional/no-throw-statements
throw new Error();
},
(config) =>
sendEmail.makeHandler({
ses: new SES(),
config,
})
)
);
16 changes: 14 additions & 2 deletions apps/cognito-functions/src/send-email-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SendEmailCommand,
SendEmailCommandInput,
} from '@aws-sdk/client-ses';
import * as t from 'io-ts';

const emailBody = (firstName: string) => `
<h4>Finalmente sei dei nostri</h4>
Expand Down Expand Up @@ -42,11 +43,22 @@ const makeSesEmailParameters = (
Source: from,
});

export const SendEmailConfig = t.type({
fromEmailAddress: t.string,
});
type SendEmailConfig = t.TypeOf<typeof SendEmailConfig>;

export type SendEmailEnv = {
readonly config: SendEmailConfig;
readonly ses: SES;
};

export const makeHandler =
(ses: SES) => async (event: PostConfirmationConfirmSignUpTriggerEvent) => {
({ ses, config }: SendEmailEnv) =>
async (event: PostConfirmationConfirmSignUpTriggerEvent) => {
const { email, given_name } = event.request.userAttributes;
if (email) {
const fromEmail = 'Developer Portal <[email protected]>'; // FIXME
const fromEmail = config.fromEmailAddress;
const subject = 'Il tuo account è attivo';
const params = makeSesEmailParameters(
email,
Expand Down

0 comments on commit 6fccf2b

Please sign in to comment.