2021-12-17 05:58:15 -05:00
|
|
|
import { HttpStatusCode, ResultList, UserNotification, UserNotificationSetting } from '@shared/models'
|
2021-07-09 10:23:01 -04:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-19 02:25:07 -05:00
|
|
|
async getLatest (options: OverrideCommandOptions = {}) {
|
2021-07-09 10:23:01 -04:00
|
|
|
const { total, data } = await this.list({
|
|
|
|
...options,
|
|
|
|
start: 0,
|
|
|
|
count: 1,
|
|
|
|
sort: '-createdAt'
|
|
|
|
})
|
|
|
|
|
|
|
|
if (total === 0) return undefined
|
|
|
|
|
|
|
|
return data[0]
|
|
|
|
}
|
|
|
|
}
|