-
Notifications
You must be signed in to change notification settings - Fork 39
/
stage.service.ts
221 lines (197 loc) · 5.84 KB
/
stage.service.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { Injectable } from '@nestjs/common';
import { PrismaService } from '@@core/prisma/prisma.service';
import { LoggerService } from '@@core/logger/logger.service';
import { v4 as uuidv4 } from 'uuid';
import { UnifiedStageOutput } from '../types/model.unified';
import { throwTypedError, UnifiedCrmError } from '@@core/utils/errors';
@Injectable()
export class StageService {
constructor(private prisma: PrismaService, private logger: LoggerService) {
this.logger.setContext(StageService.name);
}
async getStage(
id_stage: string,
remote_data?: boolean,
): Promise<UnifiedStageOutput> {
try {
const stage = await this.prisma.crm_deals_stages.findUnique({
where: {
id_crm_deals_stage: id_stage,
},
});
// Fetch field mappings for the stage
const values = await this.prisma.value.findMany({
where: {
entity: {
ressource_owner_id: stage.id_crm_deals_stage,
},
},
include: {
attribute: true,
},
});
const fieldMappingsMap = new Map();
values.forEach((value) => {
fieldMappingsMap.set(value.attribute.slug, value.data);
});
// Convert the map to an array of objects
const field_mappings = Array.from(fieldMappingsMap, ([key, value]) => ({
[key]: value,
}));
// Transform to UnifiedStageOutput format
const unifiedStage: UnifiedStageOutput = {
id: stage.id_crm_deals_stage,
stage_name: stage.stage_name,
field_mappings: field_mappings,
};
let res: UnifiedStageOutput = {
...unifiedStage,
};
if (remote_data) {
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: stage.id_crm_deals_stage,
},
});
const remote_data = JSON.parse(resp.data);
res = {
...res,
remote_data: remote_data,
};
}
return res;
} catch (error) {
throwTypedError(
new UnifiedCrmError({
name: 'GET_STAGE_ERROR',
message: 'StageService.getStage() call failed',
cause: error,
}),
);
}
}
async getStages(
integrationId: string,
linkedUserId: string,
pageSize: number,
remote_data?: boolean,
cursor?: string,
): Promise<{
data: UnifiedStageOutput[];
prev_cursor: null | string;
next_cursor: null | string;
}> {
try {
let prev_cursor = null;
let next_cursor = null;
if (cursor) {
const isCursorPresent = await this.prisma.crm_deals_stages.findFirst({
where: {
remote_platform: integrationId.toLowerCase(),
id_linked_user: linkedUserId,
id_crm_deals_stage: cursor,
},
});
if (!isCursorPresent) {
throw new ReferenceError(`The provided cursor does not exist!`);
}
}
const stages = await this.prisma.crm_deals_stages.findMany({
take: pageSize + 1,
cursor: cursor
? {
id_crm_deals_stage: cursor,
}
: undefined,
orderBy: {
created_at: 'asc',
},
where: {
remote_platform: integrationId.toLowerCase(),
id_linked_user: linkedUserId,
},
});
if (stages.length === pageSize + 1) {
next_cursor = Buffer.from(
stages[stages.length - 1].id_crm_deals_stage,
).toString('base64');
stages.pop();
}
if (cursor) {
prev_cursor = Buffer.from(cursor).toString('base64');
}
const unifiedStages: UnifiedStageOutput[] = await Promise.all(
stages.map(async (stage) => {
// Fetch field mappings for the ticket
const values = await this.prisma.value.findMany({
where: {
entity: {
ressource_owner_id: stage.id_crm_deals_stage,
},
},
include: {
attribute: true,
},
});
// Create a map to store unique field mappings
const fieldMappingsMap = new Map();
values.forEach((value) => {
fieldMappingsMap.set(value.attribute.slug, value.data);
});
// Convert the map to an array of objects
const field_mappings = Array.from(
fieldMappingsMap,
([key, value]) => ({ [key]: value }),
);
// Transform to UnifiedStageOutput format
return {
id: stage.id_crm_deals_stage,
stage_name: stage.stage_name,
field_mappings: field_mappings,
};
}),
);
let res: UnifiedStageOutput[] = unifiedStages;
if (remote_data) {
const remote_array_data: UnifiedStageOutput[] = await Promise.all(
res.map(async (stage) => {
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: stage.id,
},
});
const remote_data = JSON.parse(resp.data);
return { ...stage, remote_data };
}),
);
res = remote_array_data;
}
const event = await this.prisma.events.create({
data: {
id_event: uuidv4(),
status: 'success',
type: 'crm.stage.pulled',
method: 'GET',
url: '/crm/stages',
provider: integrationId,
direction: '0',
timestamp: new Date(),
id_linked_user: linkedUserId,
},
});
return {
data: res,
prev_cursor,
next_cursor,
};
} catch (error) {
throwTypedError(
new UnifiedCrmError({
name: 'GET_STAGES_ERROR',
message: 'StageService.getStages() call failed',
cause: error,
}),
);
}
}
}