vendredi 1 mai 2020

Сhange the class design of the email sending system to enable the ability to send messages to multiple recipients

Good day) I need help for making right decision, what pattern should I use, or how I should change my classes structure to make code more reusable.

So, I have a route, that process send email request to one recipient. We process two types of emails: SYSTEM and NON_SYSTEM. Process of these two types are bit different.

Controller:

async send(
    @Body() body: SendSystemMessageDto | SendNonSystemMessageDto,
    @User() user: User,
    @Applet() appletName: string,
  ) {
    let sendResponse = null;

    const emailSendPayload = {
      ...body,
      ...body.details,
    };

    if (appletName && body.type === MESSAGE_TYPE.SYSTEM) {
      sendResponse = await this.emailSystemSendService.send({ uid: appletName, ...emailSendPayload });
    } else if (body.type === MESSAGE_TYPE.NON_SYSTEM) {
      sendResponse = await this.emailPersonalSendService.send({ uid: String(user.uid), ...emailSendPayload });
    }

    return { success: true, ...sendResponse };
}

This functionality above sends email only to one recipient.

Now, we should add ability send email to many recipients.

So, in the request body, we send recipientsAddresses array with emails. And then call previous functionality in loop.

In addition, if email type === NON_SYSTEM and we want send multiple emails (recipientsAddresses > 1), we should check statistics for this user, who want to send this batch of emails, before looping and processing.

Summary, what we should do 1) Choose type of email - SYSTEM or NON_SYSTEM 2) Choose type of sending (single email or multiple) 3) If multiple email sending AND type of email equal to NON_SYSTEM - before processing in loop, we should call getStatistics function

I`m not profi in OOP, so I need some advice, how I should redesign my system.

I assume, that I can move logic of handling email type to the strategy, but I want avoid duplicated logic with checking email type for multiple sending.

Aucun commentaire:

Enregistrer un commentaire