aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/notifications-command.ts
blob: dfe574ca1f5359e70c61e30f34d7a0ef86719171 (plain) (blame)
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
import { ResultList } from '@shared/models'
import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
import { UserNotification, UserNotificationSetting } from '../../models/users'
import { AbstractCommand, OverrideCommandOptions } from '../shared'

export class NotificationsCommand extends AbstractCommand {

  updateMySettings (options: OverrideCommandOptions & {
    settings: UserNotificationSetting
  }) {
    const path = '/api/v1/users/me/notification-settings'

    return this.putBodyRequest({
      ...options,

      path,
      fields: options.settings,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  list (options: OverrideCommandOptions & {
    start?: number
    count?: number
    unread?: boolean
    sort?: string
  }) {
    const { start, count, unread, sort = '-createdAt' } = options
    const path = '/api/v1/users/me/notifications'

    return this.getRequestBody<ResultList<UserNotification>>({
      ...options,

      path,
      query: {
        start,
        count,
        sort,
        unread
      },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  markAsRead (options: OverrideCommandOptions & {
    ids: number[]
  }) {
    const { ids } = options
    const path = '/api/v1/users/me/notifications/read'

    return this.postBodyRequest({
      ...options,

      path,
      fields: { ids },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  markAsReadAll (options: OverrideCommandOptions) {
    const path = '/api/v1/users/me/notifications/read-all'

    return this.postBodyRequest({
      ...options,

      path,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  async getLastest (options: OverrideCommandOptions = {}) {
    const { total, data } = await this.list({
      ...options,
      start: 0,
      count: 1,
      sort: '-createdAt'
    })

    if (total === 0) return undefined

    return data[0]
  }
}