/
content-templates.controller.ts
176 lines (168 loc) · 5.23 KB
/
content-templates.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { Body, Controller, Logger, Post, UseGuards } from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
import { format } from 'date-fns';
import * as i18next from 'i18next';
import { ModuleRef } from '@nestjs/core';
import {
ApiException,
CompileEmailTemplate,
CompileEmailTemplateCommand,
CompileInAppTemplate,
CompileInAppTemplateCommand,
CompileStepTemplate,
CompileStepTemplateCommand,
UserAuthGuard,
} from '@novu/application-generic';
import { IEmailBlock, IJwtPayload, MessageTemplateContentType, IMessageCTA } from '@novu/shared';
import { UserSession } from '../shared/framework/user.decorator';
@Controller('/content-templates')
@UseGuards(UserAuthGuard)
@ApiExcludeController()
export class ContentTemplatesController {
constructor(
private compileEmailTemplateUsecase: CompileEmailTemplate,
private compileInAppTemplate: CompileInAppTemplate,
private compileStepTemplate: CompileStepTemplate,
private moduleRef: ModuleRef
) {}
@Post('/preview/email')
public previewEmail(
@UserSession() user: IJwtPayload,
@Body('content') content: string | IEmailBlock[],
@Body('contentType') contentType: MessageTemplateContentType,
@Body('payload') payload: any,
@Body('subject') subject: string,
@Body('layoutId') layoutId: string,
@Body('locale') locale?: string
) {
return this.compileEmailTemplateUsecase.execute(
CompileEmailTemplateCommand.create({
userId: user._id,
organizationId: user.organizationId,
environmentId: user.environmentId,
content,
contentType,
payload,
subject,
layoutId,
locale,
}),
this.initiateTranslations.bind(this)
);
}
@Post('/preview/in-app')
public previewInApp(
@UserSession() user: IJwtPayload,
@Body('content') content: string,
@Body('payload') payload: any,
@Body('cta') cta: IMessageCTA,
@Body('locale') locale?: string
) {
return this.compileInAppTemplate.execute(
CompileInAppTemplateCommand.create({
userId: user._id,
organizationId: user.organizationId,
environmentId: user.environmentId,
content,
payload,
cta,
locale,
}),
this.initiateTranslations.bind(this)
);
}
// TODO: refactor this to use params and single endpoint to manage all the channels
@Post('/preview/sms')
public previewSms(
@UserSession() user: IJwtPayload,
@Body('content') content: string,
@Body('payload') payload: any,
@Body('locale') locale?: string
) {
return this.compileStepTemplate.execute(
CompileStepTemplateCommand.create({
userId: user._id,
organizationId: user.organizationId,
environmentId: user.environmentId,
content,
payload,
locale,
}),
this.initiateTranslations.bind(this)
);
}
@Post('/preview/chat')
public previewChat(
@UserSession() user: IJwtPayload,
@Body('content') content: string,
@Body('payload') payload: any,
@Body('locale') locale?: string
) {
return this.compileStepTemplate.execute(
CompileStepTemplateCommand.create({
userId: user._id,
organizationId: user.organizationId,
environmentId: user.environmentId,
content,
payload,
locale,
}),
this.initiateTranslations.bind(this)
);
}
@Post('/preview/push')
public previewPush(
@UserSession() user: IJwtPayload,
@Body('content') content: string,
@Body('title') title: string,
@Body('payload') payload: any,
@Body('locale') locale?: string
) {
return this.compileStepTemplate.execute(
CompileStepTemplateCommand.create({
userId: user._id,
organizationId: user.organizationId,
environmentId: user.environmentId,
content,
payload,
locale,
title,
}),
this.initiateTranslations.bind(this)
);
}
protected async initiateTranslations(environmentId: string, organizationId: string, locale: string | undefined) {
try {
if (process.env.NOVU_ENTERPRISE === 'true' || process.env.CI_EE_TEST === 'true') {
if (!require('@novu/ee-translation')?.TranslationsService) {
throw new ApiException('Translation module is not loaded');
}
const service = this.moduleRef.get(require('@novu/ee-translation')?.TranslationsService, { strict: false });
const { namespaces, resources, defaultLocale } = await service.getTranslationsList(
environmentId,
organizationId
);
await i18next.init({
resources,
ns: namespaces,
defaultNS: false,
nsSeparator: '.',
lng: locale || 'en',
compatibilityJSON: 'v2',
fallbackLng: defaultLocale,
interpolation: {
formatSeparator: ',',
format: function (value, formatting, lng) {
if (value && formatting && !isNaN(Date.parse(value))) {
return format(new Date(value), formatting);
}
return value.toString();
},
},
});
}
} catch (e) {
Logger.error(e, `Unexpected error while importing enterprise modules`, 'TranslationsService');
}
}
}