From 530e3d17a2a14bd789c699f65dda9e351501240f Mon Sep 17 00:00:00 2001 From: "francisco.peralta" Date: Sun, 27 Feb 2022 21:01:08 -0300 Subject: [PATCH] add function consumers --- queues/communications.ts | 82 ++++++++++++++++++++++++++++++++++++++++ types/index.ts | 47 +++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 queues/communications.ts diff --git a/queues/communications.ts b/queues/communications.ts new file mode 100644 index 0000000..2fa3eb2 --- /dev/null +++ b/queues/communications.ts @@ -0,0 +1,82 @@ +import { + SQSBatchItemFailure, + SQSHandler, +} from "aws-lambda"; +import { AWSError, SQS } from "aws-sdk"; +import { SendMessageResult } from "aws-sdk/clients/sqs"; +import { PromiseResult } from "aws-sdk/lib/request"; +import { QueueData, QUEUETYPES } from "../types"; + +const sqs: SQS = new SQS(); + +export const senderQueueMail: ( + data: QueueData +) => Promise<{ data: PromiseResult }> = async ( + data +) => { + try { + const sqsSender = await sqs + .sendMessage({ + QueueUrl: process.env.QUEUE_COMMUNICATIONS_URL, + MessageBody: JSON.stringify(data), + }) + .promise(); + return { + data: sqsSender, + }; + } catch (error) { + throw new Error("Unexpected error sending the message to a queue."); + } +}; + +export const receiverQueueMail: SQSHandler = (event, _, callback) => { + const batchItemFailures: SQSBatchItemFailure[] = []; + + for (const record of event.Records) { + const body = JSON.parse(record.body); + switch (body.type) { + case QUEUETYPES.MENTEE_MAIL: { + try { + //TODO: send mentee mail + } catch (error) { + batchItemFailures.push({ itemIdentifier: record.messageId }); + } + break; + } + case QUEUETYPES.MENTOR_MAIL: { + try { + //TODO: send mentor mail + } catch (error) { + batchItemFailures.push({ itemIdentifier: record.messageId }); + } + break; + } + case QUEUETYPES.MENTEE_DM: { + try { + //TODO: send mentee dm + } catch (error) { + batchItemFailures.push({ itemIdentifier: record.messageId }); + } + break; + } + case QUEUETYPES.MENTOR_DM: { + try { + //TODO: send mentor dm + } catch (error) { + batchItemFailures.push({ itemIdentifier: record.messageId }); + } + break; + } + case QUEUETYPES.NOTIFICATION: { + try { + //TODO: send notification + } catch (error) { + batchItemFailures.push({ itemIdentifier: record.messageId }); + } + break; + } + } + } + + return callback(null, { batchItemFailures }); +}; diff --git a/types/index.ts b/types/index.ts index c84e64b..016da85 100644 --- a/types/index.ts +++ b/types/index.ts @@ -107,3 +107,50 @@ export interface MentorshipResponse { confirmationAttempt?: number; reminderAttempt?: number; } + + +export enum QUEUETYPES { + MENTEE_MAIL = "MENTEE_MAIL", + MENTEE_DM = "MENTEE_DM", + MENTOR_MAIL = "MENTOR_MAIL", + MENTOR_DM = "MENTOR_DM", + NOTIFICATION = "NOTIFICATION", +} + +export enum COMMUNICATIONTYPES { + CREATE = "CREATE", + REMINDER = "REMINDER", + FEEDBACK = "FEEDBACK", + CANCEL = "CANCEL", + CONFIRMATION_REQUEST = "CONFIRMATION_REQUEST", + WARNING = "WARNING", +} + +type QueueDataBase = { + type: QUEUETYPES; + mentee_id: string; + mentor_id: string; + mentorship_date: Date; +}; + +type QueueMail = QueueDataBase & { + mentee_email: string; + mentee_name: string; + mentor_email: string; + mentor_name: string; + duration: 30 | 45 | 60; + mentorship_id: string; + mentee_timezone: string; + mentor_timezone: string; + mail_type: COMMUNICATIONTYPES; +}; + +type QueueNotification = QueueDataBase & { + notification_type: COMMUNICATIONTYPES; +}; + +type QueueDM = QueueDataBase & { + dm_type: COMMUNICATIONTYPES; +}; + +export type QueueData = QueueDM | QueueMail | QueueNotification; \ No newline at end of file