Skip to content

Commit

Permalink
feat: add variant entity in step
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge committed Sep 12, 2023
1 parent 4fc7c5d commit 15989d1
Showing 1 changed file with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { InboundEmailParseCommand } from './inbound-email-parse.command';
import {
JobEntity,
Expand All @@ -12,6 +12,8 @@ import axios from 'axios';
import { createHash } from '../../../shared/helpers/hmac.service';
import { CompileTemplate, CompileTemplateCommand } from '@novu/application-generic';

const LOG_CONTEXT = 'InboundEmailParse';

@Injectable()
export class InboundEmailParse {
constructor(
Expand All @@ -21,43 +23,26 @@ export class InboundEmailParse {
) {}

async execute(command: InboundEmailParseCommand) {
const { toDomain, toTransactionId, toEnvironmentId } = this.splitTo(command.to[0].address);

Logger.debug('toDomain in InboundEmailParse is: ' + toDomain);
Logger.debug('toTransactionId in InboundEmailParse is: ' + toTransactionId);

if (!toTransactionId) {
Logger.warn(`missing transactionId on address ${command.to[0].address}`);

return;
}
const { domain, transactionId, environmentId } = this.splitTo(command.to[0].address);

Logger.debug('toEnvironmentId in InboundEmailParse is: ' + toEnvironmentId);

if (!toEnvironmentId) {
Logger.warn(`missing environmentId on address ${command.to[0].address}`);

return;
}
Logger.log({ domain, transactionId, environmentId }, `Received new email to parse`, LOG_CONTEXT);

const { template, notification, subscriber, environment, job, message } = await this.getEntities(
toTransactionId,
toEnvironmentId
transactionId,
environmentId
);

if (toDomain !== environment?.dns?.inboundParseDomain) {
Logger.warn('to domain is not in environment white list');

return;
if (domain !== environment?.dns?.inboundParseDomain) {
this.throwMiddleware('Domain is not in environment white list');
}

const currentParseWebhook = template.steps.find((step) => step?._id?.toString() === job?.step?._id)?.replyCallback
?.url;

if (!currentParseWebhook) {
Logger.warn(`missing parse webhook on template ${template._id} job ${job._id} transactionId ${toTransactionId}.`);

return;
this.throwMiddleware(
`Missing parse webhook on template ${template._id} job ${job._id} transactionId ${transactionId}.`
);
}

const compiledDomain = await this.compileTemplate.execute(
Expand All @@ -69,7 +54,7 @@ export class InboundEmailParse {

const userPayload: IUserWebhookPayload = {
hmac: createHash(environment?.apiKeys[0]?.key, subscriber.subscriberId),
transactionId: toTransactionId,
transactionId: transactionId,
payload: job.payload,
templateIdentifier: job.identifier,
template,
Expand All @@ -84,11 +69,29 @@ export class InboundEmailParse {
private splitTo(address: string) {
const userNameDelimiter = '-nv-e=';

const [toUser, toDomain] = address.split('@');
const toMetaIds = toUser.split('+')[1];
const [toTransactionId, toEnvironmentId] = toMetaIds.split(userNameDelimiter);
const [user, domain] = address.split('@');
const toMetaIds = user.split('+')[1];
const [transactionId, environmentId] = toMetaIds.split(userNameDelimiter);

if (!transactionId) {
this.throwMiddleware(`Missing transactionId on address ${address}`);
}

if (!domain) {
this.throwMiddleware(`Missing domain on address ${address}`);
}

if (!environmentId) {
this.throwMiddleware(`Missing environmentId on address ${address}`);
}

return { domain, transactionId, environmentId };
}

private throwMiddleware(error: string) {
Logger.error(error, LOG_CONTEXT);

return { toDomain, toTransactionId, toEnvironmentId };
throw new BadRequestException(error);
}

private async getEntities(transactionId: string, environmentId: string) {
Expand Down

0 comments on commit 15989d1

Please sign in to comment.