Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
added split option #801
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Jun 26, 2020
1 parent a5f4a2c commit 3922218
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
4 changes: 1 addition & 3 deletions engages-email-sender/src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export const start = async data => {
const { user, email, engageMessageId, customers } = data;
const { content, subject, attachments } = email;

if (!(await Stats.findOne({ engageMessageId }))) {
await Stats.create({ engageMessageId });
}
await Stats.findOneAndUpdate({ engageMessageId }, { engageMessageId }, { upsert: true });

const transporter = await createTransporter();

Expand Down
35 changes: 19 additions & 16 deletions src/data/resolvers/mutations/engageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { debugBase } from '../../../debuggers';
import { sendMessage } from '../../../messageBroker';
import { MESSAGE_KINDS } from '../../constants';
import { fetchBySegments } from '../../modules/segments/queryBuilder';
import { chunkArray } from '../../utils';

/**
* Find customers
Expand Down Expand Up @@ -114,17 +115,6 @@ export const sendViaEmail = async (engageMessage: IEngageMessageDocument, custom
const customerInfos: Array<{ _id: string; name: string; email: string; emailValidationStatus: string }> = [];

const onFinishPiping = async () => {
const data = {
email: engageMessage.email,
customers: customerInfos,
user: {
email: user.email,
name: user.details && user.details.fullName,
position: user.details && user.details.position,
},
engageMessageId,
};

if (engageMessage.kind === MESSAGE_KINDS.MANUAL && customerInfos.length === 0) {
await EngageMessages.deleteOne({ _id: engageMessage._id });
throw new Error('No customers found');
Expand All @@ -143,12 +133,25 @@ export const sendViaEmail = async (engageMessage: IEngageMessageDocument, custom

await EngageMessages.setCustomersCount(engageMessage._id, 'validCustomersCount', customerInfos.length);

if (data.customers.length > 0) {
if (process.env.ENGAGE_ADMINS) {
data.customers = [...customerInfos, ...JSON.parse(process.env.ENGAGE_ADMINS)];
}
if (customerInfos.length > 0) {
const data: any = {
email: engageMessage.email,
customers: [],
user: {
email: user.email,
name: user.details && user.details.fullName,
position: user.details && user.details.position,
},
engageMessageId,
};

const chunks = chunkArray(customerInfos, 500);

await sendQueueMessage({ action: 'sendEngage', data });
for (const chunk of chunks) {
data.customers = chunk;

await sendQueueMessage({ action: 'sendEngage', data });
}
}
};

Expand Down
16 changes: 16 additions & 0 deletions src/data/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,3 +976,19 @@ export const getSubServiceDomain = ({ name }: { name: string }): string => {

return defaultMappings[name];
};

export const chunkArray = (myArray, chunkSize: number) => {
let index = 0;

const arrayLength = myArray.length;
const tempArray: any[] = [];

for (index = 0; index < arrayLength; index += chunkSize) {
const myChunk = myArray.slice(index, index + chunkSize);

// Do something if you want with the group
tempArray.push(myChunk);
}

return tempArray;
};

0 comments on commit 3922218

Please sign in to comment.