-
Notifications
You must be signed in to change notification settings - Fork 69
/
alertController.ts
50 lines (48 loc) · 1.18 KB
/
alertController.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
import type { Alert } from '@prisma/client'
import { LoggedRequest } from '../../@types/helpers'
import {
AddAlertMutationVariables,
RemoveAlertMutationVariables
} from '../../graphql'
import { alerts } from './alerts'
import prisma from '../../prisma'
import { isAdminOrThrow } from '../../helpers/isAdmin'
export const addAlert = async (
_parent: void,
arg: AddAlertMutationVariables,
ctx: { req: LoggedRequest }
): Promise<Alert[]> => {
const { req } = ctx
try {
isAdminOrThrow(req)
const { text, type, url, urlCaption } = arg
if (!text || !type) {
throw new Error('Missing alert parameters')
}
await prisma.alert.create({
data: { text, type, url, urlCaption }
})
return alerts()
} catch (err) {
req.error(['Invalid data for alert creation', arg])
throw err
}
}
export const removeAlert = async (
_parent: void,
arg: RemoveAlertMutationVariables,
ctx: { req: LoggedRequest }
) => {
const { req } = ctx
try {
isAdminOrThrow(req)
const { id } = arg
await prisma.alert.delete({ where: { id } })
return {
success: true
}
} catch (err) {
req.warn(['Error deleting alert', arg])
throw err
}
}