All files / amplify/functions/email-transcript handler.ts

0% Statements 0/79
0% Branches 0/1
0% Functions 0/1
0% Lines 0/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import {CognitoIdentityProvider} from "@aws-sdk/client-cognito-identity-provider";
import {SendRawEmailCommand, SES} from "@aws-sdk/client-ses";
import {createTransport} from "nodemailer";
import type {Schema} from "../../data/resource";
import type {BaseTranscriptPart, Font, TranscriptComment} from "../../utils/types";
import {Languages} from "../../utils/languages";
import {createPdf} from "../../utils/transcript-pdf";
import {notoArabic} from "../../fonts/noto-arabic-normal";
import {notoChinese} from "../../fonts/noto-chinese-normal";
import {notoJapanese} from "../../fonts/noto-japanese-normal";
import {notoKorean} from "../../fonts/noto-korean-normal";
 
const cognito = new CognitoIdentityProvider();
const emailTransporter = createTransport({
    SES: {
        ses: new SES({
            region: process.env.AWS_REGION,
        }),
        aws: {
            SendRawEmailCommand,
        },
    },
});
 
// noinspection JSUnusedGlobalSymbols
export const handler: Schema["emailTranscript"]["functionHandler"] = async (event) => {
    try {
        const user = await cognito.getUser({
            AccessToken: event.request.headers.authorization,
        });
        const email = user.UserAttributes
            ?.find(attribute => attribute.Name === "email")
            ?.Value;
        if (email === undefined) {
            return {
                status: "error",
                error: "User does not have an email address",
            };
        }
        const transcriptParts = event.arguments.transcriptParts as BaseTranscriptPart[];
        const comments = event.arguments.comments as TranscriptComment[];
        const languageCode = event.arguments.languageCode;
 
        const font = getFont(languageCode);
        const pdf = createPdf(transcriptParts, comments, font);
        const pdfBuffer = pdf.output("arraybuffer");
 
        await emailTransporter.sendMail({
            from: {
                name: "Conversate",
                address: "transcription@conversateapp.com",
            },
            to: email,
            subject: "Transcript",
            attachments: [
                {
                    filename: "transcript.pdf",
                    content: Buffer.from(pdfBuffer),
                },
            ],
        });
        return {
            status: "success",
        };
    } catch (e) {
        return {
            status: "error",
            error: e,
        };
    }
};
 
function getFont(languageCode: string): Font | undefined {
    switch (languageCode) {
        case Languages.ARABIC.transcribeCode:
            return notoArabic;
        case Languages.CHINESE.transcribeCode:
        case Languages.RUSSIAN.transcribeCode:
            return notoChinese;
        case Languages.JAPANESE.transcribeCode:
            return notoJapanese;
        case Languages.KOREAN.transcribeCode:
            return notoKorean;
        default:
            return undefined;
    }
}