Skip to content

Commit 4c563d4

Browse files
authored
feat(email): updateTemplate grpc (#1320)
* feat: update template grpc * feat: final changes
1 parent ec60d75 commit 4c563d4

File tree

7 files changed

+124
-55
lines changed

7 files changed

+124
-55
lines changed

libraries/grpc-sdk/src/modules/email/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ export class Email extends ConduitModule<typeof EmailDefinition> {
2929
});
3030
}
3131

32+
updateTemplate(
33+
templateId: string,
34+
params: {
35+
name?: string;
36+
subject?: string;
37+
body?: string;
38+
variables?: string[];
39+
sender?: string;
40+
},
41+
) {
42+
return this.client!.updateTemplate({
43+
id: templateId,
44+
name: params.name,
45+
subject: params.subject,
46+
body: params.body,
47+
variables: params.variables,
48+
sender: params.sender,
49+
}).then(res => {
50+
return JSON.parse(res.template);
51+
});
52+
}
53+
3254
sendEmail(
3355
templateName: string,
3456
params: {

modules/email/src/Email.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ResendEmailResponse,
2424
SendEmailRequest,
2525
SendEmailResponse,
26+
UpdateTemplateRequest,
2627
} from './protoTypes/email.js';
2728
import metricsSchema from './metrics/index.js';
2829
import { ConfigController, ManagedModule } from '@conduitplatform/module-tools';
@@ -40,6 +41,7 @@ export default class Email extends ManagedModule<Config> {
4041
protoDescription: 'email.Email',
4142
functions: {
4243
registerTemplate: this.registerTemplate.bind(this),
44+
updateTemplate: this.updateTemplate.bind(this),
4345
sendEmail: this.sendEmail.bind(this),
4446
resendEmail: this.resendEmail.bind(this),
4547
getEmailStatus: this.getEmailStatus.bind(this),
@@ -135,6 +137,26 @@ export default class Email extends ManagedModule<Config> {
135137
return callback(null, { template: JSON.stringify(template) });
136138
}
137139

140+
async updateTemplate(
141+
call: GrpcRequest<UpdateTemplateRequest>,
142+
callback: GrpcCallback<RegisterTemplateResponse>,
143+
) {
144+
const params = {
145+
name: call.request.name,
146+
subject: call.request.subject,
147+
body: call.request.body,
148+
variables: call.request.variables,
149+
sender: call.request.sender,
150+
};
151+
let errorMessage: string | null = null;
152+
const { template } = await this.emailService
153+
.updateTemplate(call.request.id, params)
154+
.catch(e => (errorMessage = e.message));
155+
if (!isNil(errorMessage))
156+
return callback({ code: status.INTERNAL, message: errorMessage });
157+
return callback(null, { template: JSON.stringify(template) });
158+
}
159+
138160
async sendEmail(
139161
call: GrpcRequest<SendEmailRequest>,
140162
callback: GrpcCallback<SendEmailResponse>,

modules/email/src/admin/index.ts

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -124,55 +124,10 @@ export class AdminHandlers {
124124
}
125125

126126
async patchTemplate(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
127-
const templateDocument = await EmailTemplate.getInstance().findOne({
128-
_id: call.request.params.id,
129-
});
130-
if (isNil(templateDocument)) {
131-
throw new GrpcError(status.NOT_FOUND, 'Template does not exist');
132-
}
133-
134-
['name', 'subject', 'body', 'sender'].forEach(key => {
135-
if (call.request.params[key]) {
136-
// @ts-ignore
137-
templateDocument[key] = call.request.params[key];
138-
}
139-
});
140-
141-
templateDocument.variables = Object.keys(
142-
getHandleBarsValues(call.request.params.body),
143-
).concat(Object.keys(getHandleBarsValues(call.request.params.subject)));
144-
if (templateDocument.variables) {
145-
templateDocument.variables = templateDocument.variables.filter(
146-
(value, index) => templateDocument.variables!.indexOf(value) === index,
147-
);
148-
}
149-
150-
const updatedTemplate = await EmailTemplate.getInstance()
151-
.findByIdAndUpdate(call.request.params.id, templateDocument)
152-
.catch((e: Error) => {
153-
throw new GrpcError(status.INTERNAL, e.message);
154-
});
155-
156-
if (templateDocument.externalManaged) {
157-
const template = await this.emailService.getExternalTemplate(
158-
updatedTemplate!.externalId!,
159-
);
160-
let versionId = undefined;
161-
if (!isNil(template?.versions[0].id)) {
162-
versionId = template?.versions[0].id;
163-
}
164-
const data = {
165-
id: updatedTemplate!.externalId!,
166-
subject: updatedTemplate!.subject,
167-
body: updatedTemplate!.body,
168-
versionId: versionId,
169-
};
170-
await this.emailService.updateTemplate(data)?.catch((e: Error) => {
171-
throw new GrpcError(status.INTERNAL, e.message);
172-
});
173-
}
174-
175-
return { updatedTemplate };
127+
return await this.emailService.updateTemplate(
128+
call.request.urlParams.id,
129+
call.request.bodyParams,
130+
);
176131
}
177132

178133
async deleteTemplate(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
@@ -485,10 +440,11 @@ export class AdminHandlers {
485440
name: ConduitString.Optional,
486441
subject: ConduitString.Optional,
487442
body: ConduitString.Optional,
443+
sender: ConduitString.Optional,
488444
},
489445
},
490446
new ConduitRouteReturnDefinition('PatchTemplate', {
491-
template: EmailTemplate.getInstance().fields, // @type-inconsistency
447+
template: EmailTemplate.name,
492448
}),
493449
this.patchTemplate.bind(this),
494450
);

modules/email/src/email.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ message RegisterTemplateRequest {
99
optional string sender = 5;
1010
}
1111

12+
message UpdateTemplateRequest {
13+
string id = 1;
14+
optional string name = 2;
15+
optional string subject = 3;
16+
optional string body = 4;
17+
repeated string variables = 5;
18+
optional string sender = 6;
19+
}
20+
1221
message RegisterTemplateResponse {
1322
string template = 1;
1423
}
@@ -48,6 +57,7 @@ message GetEmailStatusResponse {
4857

4958
service Email {
5059
rpc RegisterTemplate(RegisterTemplateRequest) returns (RegisterTemplateResponse);
60+
rpc UpdateTemplate(UpdateTemplateRequest) returns (RegisterTemplateResponse);
5161
rpc SendEmail(SendEmailRequest) returns (SendEmailResponse);
5262
rpc ResendEmail(ResendEmailRequest) returns (ResendEmailResponse);
5363
rpc GetEmailStatus(GetEmailStatusRequest) returns (GetEmailStatusResponse);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface IUpdateTemplateParams {
2+
name?: string;
3+
subject?: string;
4+
body?: string;
5+
variables?: string[];
6+
sender?: string;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './RegisterTemplateParams.js';
2+
export * from './UpdateTemplateParams.js';
23
export * from './SendEmailParams.js';

modules/email/src/services/email.service.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { isEmpty, isNil } from 'lodash-es';
22
import { EmailTemplate, EmailRecord } from '../models/index.js';
3-
import { IRegisterTemplateParams, ISendEmailParams } from '../interfaces/index.js';
3+
import {
4+
IRegisterTemplateParams,
5+
ISendEmailParams,
6+
IUpdateTemplateParams,
7+
} from '../interfaces/index.js';
48
import handlebars from 'handlebars';
59
import { EmailProvider } from '../email-provider/index.js';
610
import { CreateEmailTemplate } from '../email-provider/interfaces/CreateEmailTemplate.js';
@@ -12,6 +16,7 @@ import { ConfigController } from '@conduitplatform/module-tools';
1216
import { Config } from '../config/index.js';
1317
import { status } from '@grpc/grpc-js';
1418
import { storeEmail } from '../utils/index.js';
19+
import { getHandleBarsValues } from '../email-provider/utils/index.js';
1520

1621
export class EmailService {
1722
constructor(
@@ -38,10 +43,6 @@ export class EmailService {
3843
);
3944
}
4045

41-
updateTemplate(data: UpdateEmailTemplate) {
42-
return this.emailer._transport?.updateTemplate(data);
43-
}
44-
4546
deleteExternalTemplate(id: string) {
4647
return this.emailer._transport?.deleteTemplate(id);
4748
}
@@ -61,6 +62,56 @@ export class EmailService {
6162
});
6263
}
6364

65+
async updateTemplate(id: string, params: IUpdateTemplateParams) {
66+
const templateDocument = await EmailTemplate.getInstance().findOne({
67+
_id: id,
68+
});
69+
if (isNil(templateDocument)) {
70+
throw new GrpcError(status.NOT_FOUND, 'Template does not exist');
71+
}
72+
73+
['name', 'subject', 'body', 'sender'].forEach(key => {
74+
if (params[key as keyof IUpdateTemplateParams]) {
75+
// @ts-ignore
76+
templateDocument[key] = params[key];
77+
}
78+
});
79+
80+
templateDocument.variables = Object.keys(getHandleBarsValues(params.body)).concat(
81+
Object.keys(getHandleBarsValues(params.subject)),
82+
);
83+
if (templateDocument.variables) {
84+
templateDocument.variables = templateDocument.variables.filter(
85+
(value, index) => templateDocument.variables!.indexOf(value) === index,
86+
);
87+
}
88+
89+
const updatedTemplate = await EmailTemplate.getInstance()
90+
.findByIdAndUpdate(id, templateDocument)
91+
.catch((e: Error) => {
92+
throw new GrpcError(status.INTERNAL, e.message);
93+
});
94+
95+
if (templateDocument.externalManaged) {
96+
const template = await this.getExternalTemplate(updatedTemplate!.externalId!);
97+
let versionId = undefined;
98+
if (!isNil(template?.versions[0].id)) {
99+
versionId = template?.versions[0].id;
100+
}
101+
const data = {
102+
id: updatedTemplate!.externalId!,
103+
subject: updatedTemplate!.subject,
104+
body: updatedTemplate!.body,
105+
versionId: versionId,
106+
};
107+
await this.emailer._transport?.updateTemplate(data).catch((e: Error) => {
108+
throw new GrpcError(status.INTERNAL, e.message);
109+
});
110+
}
111+
112+
return { template: updatedTemplate };
113+
}
114+
64115
async sendEmail(
65116
template: string | undefined,
66117
params: ISendEmailParams,

0 commit comments

Comments
 (0)