-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuserNotifications.ts
More file actions
107 lines (96 loc) · 3.64 KB
/
userNotifications.ts
File metadata and controls
107 lines (96 loc) · 3.64 KB
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
import { ResolverContextWithUser } from '../types/graphql';
import { UserNotificationsDBScheme, UserNotificationType } from '../models/user';
import { NotificationsChannelsDBScheme } from '../types/notification-channels';
import { UserDBScheme } from '@hawk.so/types';
import { UserInputError } from 'apollo-server-express';
import { validateWebhookEndpoint } from '../utils/webhookEndpointValidator';
/**
* We will get this structure from the client to update Channel settings
*/
interface ChangeUserNotificationsChannelPayload {
/**
* Channels with their settings
*/
input: NotificationsChannelsDBScheme;
}
/**
* We will get this structure from the client to toggle Receive type
*/
interface ChangeUserNotificationsReceiveTypePayload {
/**
* Types of notifications to receive
*/
input: {[key in UserNotificationType]: boolean};
}
/**
* This response will be sent on changeUserNotificationsChannel and changeUserNotificationsReceiveType mutations
*/
interface ChangeNotificationsResponse {
notifications: UserNotificationsDBScheme;
}
export default {
Mutation: {
/**
* Change settings of passed channel of the user notification
*
* @param _obj - parent object
* @param user - current authorized user {@see ../index.js}
* @param factories - factories for working with models
* @param input - payload with channel data
*/
async changeUserNotificationsChannel(
_obj: undefined,
{ input }: ChangeUserNotificationsChannelPayload,
{ user, factories }: ResolverContextWithUser
): Promise<ChangeNotificationsResponse> {
if (input.webhook?.isEnabled) {
const webhookError = await validateWebhookEndpoint(input.webhook.endpoint);
if (webhookError !== null) {
throw new UserInputError(webhookError);
}
}
const currentUser = await factories.usersFactory.findById(user.id);
const currentNotifySet = currentUser?.notifications || {} as UserNotificationsDBScheme;
const oldChannels = currentNotifySet.channels || {};
const newChannels = Object.assign(oldChannels, input);
const newNotifySet = Object.assign(currentNotifySet, {
channels: newChannels,
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await currentUser!.updateProfile({
notifications: newNotifySet,
} as Pick<UserDBScheme, 'notifications'>);
return {
notifications: newNotifySet,
};
},
/**
* Toggle receive type of user notifications
*
* @param _obj - parent object
* @param user - current authorized user {@see ../index.js}
* @param factories - factories for working with models
* @param input - payload with receive type and it's active status
*/
async changeUserNotificationsReceiveType(
_obj: undefined,
{ input }: ChangeUserNotificationsReceiveTypePayload,
{ user, factories }: ResolverContextWithUser
): Promise<ChangeNotificationsResponse> {
const currentUser = await factories.usersFactory.findById(user.id);
const currentNotifySet = currentUser?.notifications || {} as UserNotificationsDBScheme;
const oldReceiveTypes = currentNotifySet.whatToReceive || {};
const newReceiveTypes = Object.assign(oldReceiveTypes, input);
const newNotifySet = Object.assign(currentNotifySet, {
whatToReceive: newReceiveTypes,
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await currentUser!.updateProfile({
notifications: newNotifySet,
} as Pick<UserDBScheme, 'notifications'>);
return {
notifications: newNotifySet,
};
},
},
};