]>
Commit | Line | Data |
---|---|---|
dd0ebb71 | 1 | import { ResultList } from '@shared/models' |
c0e8b12e | 2 | import { HttpStatusCode } from '@shared/models' |
dd0ebb71 C |
3 | import { UserNotification, UserNotificationSetting } from '../../models/users' |
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
5 | ||
6 | export class NotificationsCommand extends AbstractCommand { | |
7 | ||
8 | updateMySettings (options: OverrideCommandOptions & { | |
9 | settings: UserNotificationSetting | |
10 | }) { | |
11 | const path = '/api/v1/users/me/notification-settings' | |
12 | ||
13 | return this.putBodyRequest({ | |
14 | ...options, | |
15 | ||
16 | path, | |
17 | fields: options.settings, | |
18 | implicitToken: true, | |
19 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
20 | }) | |
21 | } | |
22 | ||
23 | list (options: OverrideCommandOptions & { | |
24 | start?: number | |
25 | count?: number | |
26 | unread?: boolean | |
27 | sort?: string | |
28 | }) { | |
29 | const { start, count, unread, sort = '-createdAt' } = options | |
30 | const path = '/api/v1/users/me/notifications' | |
31 | ||
32 | return this.getRequestBody<ResultList<UserNotification>>({ | |
33 | ...options, | |
34 | ||
35 | path, | |
36 | query: { | |
37 | start, | |
38 | count, | |
39 | sort, | |
40 | unread | |
41 | }, | |
42 | implicitToken: true, | |
43 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
44 | }) | |
45 | } | |
46 | ||
47 | markAsRead (options: OverrideCommandOptions & { | |
48 | ids: number[] | |
49 | }) { | |
50 | const { ids } = options | |
51 | const path = '/api/v1/users/me/notifications/read' | |
52 | ||
53 | return this.postBodyRequest({ | |
54 | ...options, | |
55 | ||
56 | path, | |
57 | fields: { ids }, | |
58 | implicitToken: true, | |
59 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
60 | }) | |
61 | } | |
62 | ||
63 | markAsReadAll (options: OverrideCommandOptions) { | |
64 | const path = '/api/v1/users/me/notifications/read-all' | |
65 | ||
66 | return this.postBodyRequest({ | |
67 | ...options, | |
68 | ||
69 | path, | |
70 | implicitToken: true, | |
71 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
72 | }) | |
73 | } | |
74 | ||
75 | async getLastest (options: OverrideCommandOptions = {}) { | |
76 | const { total, data } = await this.list({ | |
77 | ...options, | |
78 | start: 0, | |
79 | count: 1, | |
80 | sort: '-createdAt' | |
81 | }) | |
82 | ||
83 | if (total === 0) return undefined | |
84 | ||
85 | return data[0] | |
86 | } | |
87 | } |