-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathpromote-message-template-change.ts
79 lines (71 loc) · 2.34 KB
/
promote-message-template-change.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
import { Injectable } from '@nestjs/common';
import { PromoteTypeChangeCommand } from '../promote-type-change.command';
import { MessageTemplateEntity, LayoutRepository, MessageTemplateRepository, FeedRepository } from '@novu/dal';
@Injectable()
export class PromoteMessageTemplateChange {
constructor(
private messageTemplateRepository: MessageTemplateRepository,
private feedRepository: FeedRepository,
private layoutRepository: LayoutRepository
) {}
async execute(command: PromoteTypeChangeCommand) {
const item = await this.messageTemplateRepository.findOne({
_environmentId: command.environmentId,
_parentId: command.item._id,
});
const newItem = command.item as MessageTemplateEntity;
const feedDev = await this.feedRepository.findOne({
_id: newItem._feedId,
_organizationId: command.organizationId,
});
const feed = await this.feedRepository.findOne({
_environmentId: command.environmentId,
identifier: feedDev?.identifier,
});
const layout = await this.layoutRepository.findOne({
_environmentId: command.environmentId,
_organizationId: command.organizationId,
_parentId: newItem._layoutId,
});
if (!item) {
return this.messageTemplateRepository.create({
type: newItem.type,
name: newItem.name,
subject: newItem.subject,
content: newItem.content,
contentType: newItem.contentType,
title: newItem.title,
cta: newItem.cta,
active: newItem.active,
actor: newItem.actor,
variables: newItem.variables,
_parentId: newItem._id,
_feedId: feed?._id,
_layoutId: layout?._id,
_environmentId: command.environmentId,
_creatorId: command.userId,
_organizationId: command.organizationId,
});
}
return this.messageTemplateRepository.update(
{
_environmentId: command.environmentId,
_id: item._id,
},
{
type: newItem.type,
name: newItem.name,
subject: newItem.subject,
content: newItem.content,
contentType: newItem.contentType,
title: newItem.title,
cta: newItem.cta,
active: newItem.active,
actor: newItem.actor,
variables: newItem.variables,
_feedId: feed?._id,
_layoutId: layout?._id,
}
);
}
}