Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(worker): Local instance selection handlebars #5622

Merged
merged 13 commits into from
Jun 12, 2024
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/APPLICATION_GENERIC.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/EE_AUTH.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/runConfigurations/RUN_LOCAL_ENV.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/SHARED_WEB.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"compounds": [
{
"name": "-- RUN FULL ENV - Local",
"configurations": ["API", "DAL", "EMBED", "SHARED", "TESTING LIB", "WS", "WEB", "WIDGET"]
"configurations": ["API", "DAL", "EMBED", "SHARED", "TESTING LIB", "WS", "WEB", "WIDGET", "worker"]
},
{
"name": "-- RUN FULL ENV - Test",
Expand All @@ -242,7 +242,8 @@
"TESTING LIB",
"WS - TEST ENV",
"WEB",
"WIDGET - test"
"WIDGET - test",
"worker"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export class ContentTemplatesController {
environmentId,
organizationId
);

await i18next.init({
const instance = i18next.createInstance();
await instance.init({
resources,
ns: namespaces,
defaultNS: false,
Expand All @@ -168,6 +168,8 @@ export class ContentTemplatesController {
},
},
});

return instance;
}
} catch (e) {
Logger.error(e, `Unexpected error while importing enterprise modules`, 'TranslationsService');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ export class InboundEmailParse {
);
}

const compiledDomain = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: currentParseWebhook as string,
data: job.payload,
})
);
const compiledDomain = await this.compileTemplate.execute({
template: currentParseWebhook as string,
data: job.payload,
});

const userPayload: IUserWebhookPayload = {
hmac: createHash(environment?.apiKeys[0]?.key, subscriber.subscriberId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export class SendMessageChat extends SendMessageBase {
if (!step?.template) throw new PlatformException('Chat channel template not found');

const { subscriber } = command.compileContext;
await this.initiateTranslations(command.environmentId, command.organizationId, subscriber.locale);
const i18nextInstance = await this.initiateTranslations(
command.environmentId,
command.organizationId,
subscriber.locale
);

const template = await this.processVariants(command);

Expand All @@ -92,12 +96,11 @@ export class SendMessageChat extends SendMessageBase {

try {
if (!command.chimeraData) {
content = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: step.template.content as string,
data: this.getCompilePayload(command.compileContext),
})
);
content = await this.compileTemplate.execute({
template: step.template.content as string,
data: this.getCompilePayload(command.compileContext),
i18next: i18nextInstance,
});
}
} catch (e) {
await this.sendErrorHandlebars(command.job, e.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class SendMessageEmail extends SendMessageBase {
});
}
} catch (e) {
Logger.error({ payload }, 'Compiling the email template or storing it or inlining it has failed', LOG_CONTEXT);
Logger.error({ payload, e }, 'Compiling the email template or storing it or inlining it has failed', LOG_CONTEXT);
await this.sendErrorHandlebars(command.job, e.message);

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export class SendMessagePush extends SendMessageBase {
const { subscriber, step: stepData } = command.compileContext;

const template = await this.processVariants(command);
await this.initiateTranslations(command.environmentId, command.organizationId, subscriber.locale);
const i18nInstance = await this.initiateTranslations(
command.environmentId,
command.organizationId,
subscriber.locale
);

if (template) {
step.template = template;
Expand All @@ -92,19 +96,17 @@ export class SendMessagePush extends SendMessageBase {

try {
if (!command.chimeraData) {
content = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: step.template?.content as string,
data,
})
);

title = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: step.template?.title as string,
data,
})
);
content = await this.compileTemplate.execute({
template: step.template?.content as string,
data,
i18next: i18nInstance,
});

title = await this.compileTemplate.execute({
template: step.template?.title as string,
data,
i18next: i18nInstance,
});
}
} catch (e) {
await this.sendErrorHandlebars(command.job, e.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export class SendMessageSms extends SendMessageBase {

const { subscriber } = command.compileContext;
const template = await this.processVariants(command);
await this.initiateTranslations(command.environmentId, command.organizationId, subscriber.locale);
const i18nextInstance = await this.initiateTranslations(
command.environmentId,
command.organizationId,
subscriber.locale
);

if (template) {
step.template = template;
Expand All @@ -91,12 +95,11 @@ export class SendMessageSms extends SendMessageBase {

try {
if (!command.chimeraData) {
content = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: step.template.content as string,
data: this.getCompilePayload(command.compileContext),
})
);
content = await this.compileTemplate.execute({
template: step.template.content as string,
data: this.getCompilePayload(command.compileContext),
i18next: i18nextInstance,
});

if (!content) {
throw new PlatformException(`Unexpected error: SMS content is missing`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export abstract class SendMessageBase extends SendMessageType {
organizationId
);

await i18next.init({
const instance = i18next.createInstance();
await instance.init({
resources,
ns: namespaces,
defaultNS: false,
Expand All @@ -168,6 +169,8 @@ export abstract class SendMessageBase extends SendMessageType {
},
},
});

return instance;
}
} catch (e) {
Logger.error(e, `Unexpected error while importing enterprise modules`, 'TranslationsService');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export class CompileEmailTemplate extends CompileTemplateBase {
const verifyPayloadService = new VerifyPayloadService();
const organization = await this.getOrganization(command.organizationId);

let i18nInstance;
if (initiateTranslations) {
await initiateTranslations(
i18nInstance = await initiateTranslations(
command.environmentId,
command.organizationId,
command.locale ||
Expand Down Expand Up @@ -97,24 +98,48 @@ export class CompileEmailTemplate extends CompileTemplateBase {
};

try {
subject = await this.renderContent(command.subject, payload);
subject = await this.renderContent(
command.subject,
payload,
i18nInstance
);

if (preheader) {
preheader = await this.renderContent(preheader, payload);
preheader = await this.renderContent(preheader, payload, i18nInstance);
}

if (command.senderName) {
senderName = await this.renderContent(command.senderName, payload);
senderName = await this.renderContent(
command.senderName,
payload,
i18nInstance
);
}
} catch (e: any) {
throw new ApiException(
e?.message || `Message content could not be generated`
e?.message || `Email subject message content could not be generated`
);
}

const customLayout = CompileEmailTemplate.addPreheader(
layoutContent as string
);

if (isEditorMode) {
for (const block of content as IEmailBlock[]) {
block.content = await this.renderContent(
block.content,
payload,
i18nInstance
);
block.url = await this.renderContent(
block.url || '',
payload,
i18nInstance
);
}
}

const templateVariables = {
...payload,
subject,
Expand All @@ -123,48 +148,39 @@ export class CompileEmailTemplate extends CompileTemplateBase {
blocks: isEditorMode ? content : [],
};

if (isEditorMode) {
for (const block of content as IEmailBlock[]) {
block.content = await this.renderContent(block.content, payload);
block.url = await this.renderContent(block.url || '', payload);
}
}

const body = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: !isEditorMode
? (content as string)
: (helperBlocksContent as string),
data: templateVariables,
})
);
const body = await this.compileTemplate.execute({
i18next: i18nInstance,
template: !isEditorMode
? (content as string)
: (helperBlocksContent as string),
data: templateVariables,
});

templateVariables.body = body as string;

const html = customLayout
? await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: customLayout,
data: templateVariables,
})
)
? await this.compileTemplate.execute({
i18next: i18nInstance,
template: customLayout,
data: templateVariables,
})
: body;

return { html, content, subject, senderName };
}

private async renderContent(
content: string,
payload: Record<string, unknown>
payload: Record<string, unknown>,
i18nInstance: any
) {
const renderedContent = await this.compileTemplate.execute(
CompileTemplateCommand.create({
template: content,
data: {
...payload,
},
})
);
const renderedContent = await this.compileTemplate.execute({
i18next: i18nInstance,
template: content,
data: {
...payload,
},
});

return renderedContent?.trim() || '';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

{{#each blocks}}
<div style="margin-bottom: 10px" data-test-id="block-item-wrapper">
{{#equals type 'text'}}
Expand Down
Loading
Loading