-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
subscribers.controller.ts
482 lines (454 loc) · 16.1 KB
/
subscribers.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { CreateSubscriber, CreateSubscriberCommand } from './usecases/create-subscriber';
import { UpdateSubscriber, UpdateSubscriberCommand } from './usecases/update-subscriber';
import { RemoveSubscriber, RemoveSubscriberCommand } from './usecases/remove-subscriber';
import { JwtAuthGuard } from '../auth/framework/auth.guard';
import { ExternalApiAccessible } from '../auth/framework/external-api.decorator';
import { UserSession } from '../shared/framework/user.decorator';
import { ButtonTypeEnum, IJwtPayload, MessageActionStatusEnum } from '@novu/shared';
import {
CreateSubscriberRequestDto,
DeleteSubscriberResponseDto,
SubscriberResponseDto,
SubscribersResponseDto,
UpdateSubscriberChannelRequestDto,
UpdateSubscriberRequestDto,
} from './dtos';
import { UpdateSubscriberChannel, UpdateSubscriberChannelCommand } from './usecases/update-subscriber-channel';
import { GetSubscribers, GetSubscribersCommand } from './usecases/get-subscribers';
import { GetSubscriber, GetSubscriberCommand } from './usecases/get-subscriber';
import { ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { GetPreferencesCommand } from './usecases/get-preferences/get-preferences.command';
import { GetPreferences } from './usecases/get-preferences/get-preferences.usecase';
import { UpdatePreference } from './usecases/update-preference/update-preference.usecase';
import { UpdateSubscriberPreferenceCommand } from './usecases/update-subscriber-preference';
import { UpdateSubscriberPreferenceResponseDto } from '../widgets/dtos/update-subscriber-preference-response.dto';
import { UpdateSubscriberPreferenceRequestDto } from '../widgets/dtos/update-subscriber-preference-request.dto';
import { MessageResponseDto, MessagesResponseDto } from '../widgets/dtos/message-response.dto';
import { UnseenCountResponse } from '../widgets/dtos/unseen-count-response.dto';
import { MessageEntity } from '@novu/dal';
import { MarkEnum, MarkMessageAsCommand } from '../widgets/usecases/mark-message-as/mark-message-as.command';
import { UpdateMessageActionsCommand } from '../widgets/usecases/mark-action-as-done/update-message-actions.command';
import { GetNotificationsFeedCommand } from '../widgets/usecases/get-notifications-feed/get-notifications-feed.command';
import { GetNotificationsFeed } from '../widgets/usecases/get-notifications-feed/get-notifications-feed.usecase';
import { MarkMessageAs } from '../widgets/usecases/mark-message-as/mark-message-as.usecase';
import { UpdateMessageActions } from '../widgets/usecases/mark-action-as-done/update-message-actions.usecase';
import { StoreQuery } from '../widgets/queries/store.query';
import { GetFeedCount } from '../widgets/usecases/get-feed-count/get-feed-count.usecase';
import { GetFeedCountCommand } from '../widgets/usecases/get-feed-count/get-feed-count.command';
import { UpdateSubscriberOnlineFlagRequestDto } from './dtos/update-subscriber-online-flag-request.dto';
import {
UpdateSubscriberOnlineFlag,
UpdateSubscriberOnlineFlagCommand,
} from './usecases/update-subscriber-online-flag';
import { MarkMessageAsRequestDto } from '../widgets/dtos/mark-message-as-request.dto';
@Controller('/subscribers')
@ApiTags('Subscribers')
export class SubscribersController {
constructor(
private createSubscriberUsecase: CreateSubscriber,
private updateSubscriberUsecase: UpdateSubscriber,
private updateSubscriberChannelUsecase: UpdateSubscriberChannel,
private removeSubscriberUsecase: RemoveSubscriber,
private getSubscriberUseCase: GetSubscriber,
private getSubscribersUsecase: GetSubscribers,
private getPreferenceUsecase: GetPreferences,
private updatePreferenceUsecase: UpdatePreference,
private getNotificationsFeedUsecase: GetNotificationsFeed,
private genFeedCountUsecase: GetFeedCount,
private markMessageAsUsecase: MarkMessageAs,
private updateMessageActionsUsecase: UpdateMessageActions,
private updateSubscriberOnlineFlagUsecase: UpdateSubscriberOnlineFlag
) {}
@Get('')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: SubscribersResponseDto,
})
@ApiOperation({
summary: 'Get subscribers',
description: 'Returns a list of subscribers, could paginated using the `page` query parameter',
})
@ApiQuery({ name: 'page', type: Number, required: false, description: 'The page to fetch, defaults to 0' })
async getSubscribers(
@UserSession() user: IJwtPayload,
@Query('page') page = 0,
@Query('limit') limit = 10
): Promise<SubscribersResponseDto> {
return await this.getSubscribersUsecase.execute(
GetSubscribersCommand.create({
organizationId: user.organizationId,
environmentId: user.environmentId,
page: page ? Number(page) : 0,
limit: limit ? Number(limit) : 10,
})
);
}
@Get('/:subscriberId')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: SubscriberResponseDto,
})
@ApiOperation({
summary: 'Get subscriber',
description: 'Get subscriber by your internal id used to identify the subscriber',
})
async getSubscriber(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string
): Promise<SubscriberResponseDto> {
return await this.getSubscriberUseCase.execute(
GetSubscriberCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
})
);
}
@Post('/')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiCreatedResponse({
type: SubscriberResponseDto,
})
@ApiOperation({
summary: 'Create subscriber',
description:
'Creates a subscriber entity, in the Novu platform. ' +
'The subscriber will be later used to receive notifications, and access notification feeds. ' +
'Communication credentials such as email, phone number, and 3 rd party credentials i.e slack tokens could be later associated to this entity.',
})
async createSubscriber(
@UserSession() user: IJwtPayload,
@Body() body: CreateSubscriberRequestDto
): Promise<SubscriberResponseDto> {
return await this.createSubscriberUsecase.execute(
CreateSubscriberCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId: body.subscriberId,
firstName: body.firstName,
lastName: body.lastName,
email: body.email,
phone: body.phone,
avatar: body.avatar,
locale: body.locale,
})
);
}
@Put('/:subscriberId')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: SubscriberResponseDto,
})
@ApiOperation({
summary: 'Update subscriber',
description: 'Used to update the subscriber entity with new information',
})
async updateSubscriber(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Body() body: UpdateSubscriberRequestDto
): Promise<SubscriberResponseDto> {
return await this.updateSubscriberUsecase.execute(
UpdateSubscriberCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
firstName: body.firstName,
lastName: body.lastName,
email: body.email,
phone: body.phone,
avatar: body.avatar,
locale: body.locale,
})
);
}
@Put('/:subscriberId/credentials')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: SubscriberResponseDto,
})
@ApiOperation({
summary: 'Update subscriber credentials',
description: 'Subscriber credentials associated to the delivery methods such as slack and push tokens.',
})
async updateSubscriberChannel(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Body() body: UpdateSubscriberChannelRequestDto
): Promise<SubscriberResponseDto> {
return await this.updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
providerId: body.providerId,
credentials: body.credentials,
})
);
}
@Patch('/:subscriberId/online-status')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: SubscriberResponseDto,
})
@ApiOperation({
summary: 'Update subscriber online status',
description: 'Used to update the subscriber isOnline flag.',
})
async updateSubscriberOnlineFlag(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Body() body: UpdateSubscriberOnlineFlagRequestDto
): Promise<SubscriberResponseDto> {
return await this.updateSubscriberOnlineFlagUsecase.execute(
UpdateSubscriberOnlineFlagCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
isOnline: body.isOnline,
})
);
}
@Delete('/:subscriberId')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: DeleteSubscriberResponseDto,
})
@ApiOperation({
summary: 'Delete subscriber',
description: 'Deletes a subscriber entity from the Novu platform',
})
async removeSubscriber(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string
): Promise<DeleteSubscriberResponseDto> {
return await this.removeSubscriberUsecase.execute(
RemoveSubscriberCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
})
);
}
@Get('/:subscriberId/preferences')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: [UpdateSubscriberPreferenceResponseDto],
})
@ApiOperation({
summary: 'Get subscriber preferences',
})
async getSubscriberPreference(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string
): Promise<UpdateSubscriberPreferenceResponseDto[]> {
const command = GetPreferencesCommand.create({
organizationId: user.organizationId,
subscriberId: subscriberId,
environmentId: user.environmentId,
});
return await this.getPreferenceUsecase.execute(command);
}
@Patch('/:subscriberId/preferences/:templateId')
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@ApiOkResponse({
type: UpdateSubscriberPreferenceResponseDto,
})
@ApiOperation({
summary: 'Update subscriber preference',
})
async updateSubscriberPreference(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Param('templateId') templateId: string,
@Body() body: UpdateSubscriberPreferenceRequestDto
): Promise<UpdateSubscriberPreferenceResponseDto> {
const command = UpdateSubscriberPreferenceCommand.create({
organizationId: user.organizationId,
subscriberId: subscriberId,
environmentId: user.environmentId,
templateId: templateId,
channel: body.channel,
enabled: body.enabled,
});
return await this.updatePreferenceUsecase.execute(command);
}
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@Get('/:subscriberId/notifications/feed')
@ApiOperation({
summary: 'Get a notification feed for a particular subscriber',
})
@ApiOkResponse({
type: MessagesResponseDto,
})
@ApiQuery({
name: 'seen',
type: Boolean,
required: false,
})
@ApiQuery({
name: 'page',
type: Number,
required: false,
})
async getNotificationsFeed(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Query('page') page?: string,
@Query('feedIdentifier') feedId?: string,
@Query() query: StoreQuery = {}
) {
let feedsQuery: string[] | undefined;
if (feedId) {
feedsQuery = Array.isArray(feedId) ? feedId : [feedId];
}
const command = GetNotificationsFeedCommand.create({
organizationId: user.organizationId,
environmentId: user.environmentId,
subscriberId: subscriberId,
page: page != null ? parseInt(page) : 0,
feedId: feedsQuery,
query: query,
});
return await this.getNotificationsFeedUsecase.execute(command);
}
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@Get('/:subscriberId/notifications/unseen')
@ApiOkResponse({
type: UnseenCountResponse,
})
@ApiOperation({
summary: 'Get the unseen notification count for subscribers feed',
})
async getUnseenCount(
@UserSession() user: IJwtPayload,
@Query('feedIdentifier') feedId: string[] | string,
@Query('seen') seen: boolean,
@Param('subscriberId') subscriberId: string
): Promise<UnseenCountResponse> {
let feedsQuery: string[] | undefined;
if (feedId) {
feedsQuery = Array.isArray(feedId) ? feedId : [feedId];
}
const command = GetFeedCountCommand.create({
organizationId: user.organizationId,
subscriberId: subscriberId,
environmentId: user.environmentId,
feedId: feedsQuery,
seen,
});
return await this.genFeedCountUsecase.execute(command);
}
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@Post('/:subscriberId/messages/:messageId/seen')
@ApiOperation({
summary: 'Mark a subscriber feed message as seen',
description: 'This endpoint is deprecated please address /:subscriberId/messages/markAs instead',
deprecated: true,
})
@ApiCreatedResponse({
type: MessageResponseDto,
})
async markMessageAsSeen(
@UserSession() user: IJwtPayload,
@Param('messageId') messageId: string,
@Param('subscriberId') subscriberId: string
): Promise<MessageEntity> {
const messageIds = this.toArray(messageId);
if (!messageIds) throw new BadRequestException('messageId is required');
const command = MarkMessageAsCommand.create({
organizationId: user.organizationId,
environmentId: user.environmentId,
subscriberId: subscriberId,
messageIds,
mark: { [MarkEnum.SEEN]: true },
});
return (await this.markMessageAsUsecase.execute(command))[0];
}
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@Post('/:subscriberId/messages/markAs')
@ApiOperation({
summary: 'Mark a subscriber feed message as seen',
})
@ApiCreatedResponse({
type: MessageResponseDto,
})
async markMessageAs(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Body() body: MarkMessageAsRequestDto
): Promise<MessageEntity[]> {
if (!body.messageId) throw new BadRequestException('messageId is required');
const messageIds = this.toArray(body.messageId);
if (!messageIds) throw new BadRequestException('messageId is required');
const command = MarkMessageAsCommand.create({
organizationId: user.organizationId,
subscriberId: subscriberId,
environmentId: user.environmentId,
messageIds,
mark: body.mark,
});
return await this.markMessageAsUsecase.execute(command);
}
@ExternalApiAccessible()
@UseGuards(JwtAuthGuard)
@Post('/:subscriberId/messages/:messageId/actions/:type')
@ApiOperation({
summary: 'Mark message action as seen',
})
@ApiCreatedResponse({
type: MessageResponseDto,
})
async markActionAsSeen(
@UserSession() user: IJwtPayload,
@Param('messageId') messageId: string,
@Param('type') type: ButtonTypeEnum,
@Body() body: { payload: any; status: MessageActionStatusEnum }, // eslint-disable-line @typescript-eslint/no-explicit-any
@Param('subscriberId') subscriberId: string
): Promise<MessageEntity> {
return await this.updateMessageActionsUsecase.execute(
UpdateMessageActionsCommand.create({
organizationId: user.organizationId,
environmentId: user.environmentId,
subscriberId: subscriberId,
messageId,
type,
payload: body.payload,
status: body.status,
})
);
}
private toArray(param: string[] | string): string[] | undefined {
let paramArray: string[] | undefined;
if (param) {
paramArray = Array.isArray(param) ? param : param.split(',');
}
return paramArray as string[];
}
}