-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-push-notification-value-update-serial-operations.ts
169 lines (155 loc) · 4.17 KB
/
get-push-notification-value-update-serial-operations.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
DeepClient,
SerialOperation,
} from '@deep-foundation/deeplinks/imports/client.js';
import { createSerialOperation } from '@deep-foundation/deeplinks/imports/gql/index.js';
import { MutationInputValue } from '@deep-foundation/deeplinks/imports/client_types.js';
import { Link } from '@deep-foundation/deeplinks/imports/minilinks';
import { PushNotification } from './push-notification.js';
/**
* Gets serial operations to update {@link Package.PushNotification}
*
* @example
* #### Update {@link Package.PushNotification}
```ts
const {serialOperations, linkIds} = await getPushNotificationValueUpdateSerialOperations({
deep,
pushNotification: {
title,
body,
}
});
await deep.serial({
operations: serialOperations
})
```
*/
export async function getPushNotificationValueUpdateSerialOperations(
param: GetPushNotificationValueUpdateSerialOperationsParam
): Promise<Array<SerialOperation>> {
const { deep, pushNotification } = param;
const pushNotificationLink = await getPushNotificationLink();
const value = await getValue({
pushNotificationLink,
data: pushNotification,
});
const serialOperations = await getSerialOperations({
pushNotificationLink,
value,
});
return serialOperations;
async function getPushNotificationLink() {
let pushNotificationLink: Link<number>;
if ('pushNotificationLinkId' in param) {
if (!param.pushNotificationLinkId) {
throw new Error(`pushNotificationLinkId is undefined`);
}
const { data } = await deep.select({
id: param.pushNotificationLinkId,
});
pushNotificationLink = data[0];
} else if ('pushNotificationLink' in param) {
if (!param.pushNotificationLink) {
throw new Error(`pushNotificationLink is undefined`);
}
pushNotificationLink = param.pushNotificationLink;
} else {
throw new Error(`Either pushNotificationLink or pushNotificationLinkId must be passed`);
}
return pushNotificationLink;
}
async function getValueInsertSerialOperation({
pushNotificationLink,
value,
}: {
pushNotificationLink: Link<number>;
value: MutationInputValue<object>['value'];
}) {
return createSerialOperation({
table: 'objects',
type: 'insert',
objects: {
link_id: pushNotificationLink.id,
value: value,
},
});
}
async function getValueUpdateSerialOperation({
pushNotificationLink,
value,
}: {
pushNotificationLink: Link<number>;
value: MutationInputValue<object>['value'];
}) {
return createSerialOperation({
table: 'objects',
type: 'update',
exp: {
link_id: pushNotificationLink.id,
},
value: {
value: value,
},
});
}
async function getValue({
pushNotificationLink,
data,
}: {
pushNotificationLink: Link<number>;
data: Partial<PushNotification> | undefined;
}) {
return {
...(pushNotificationLink.value?.value ?? {}),
...(data),
} as MutationInputValue<object>['value'];
}
async function getSerialOperations({
pushNotificationLink,
value,
}: {
pushNotificationLink: Link<number>;
value: MutationInputValue<object>['value'];
}) {
let serialOperations: Array<SerialOperation> = [];
if (!pushNotificationLink.value) {
serialOperations.push(
await getValueUpdateSerialOperation({
pushNotificationLink,
value,
})
);
} else {
serialOperations.push(
await getValueInsertSerialOperation({
pushNotificationLink,
value,
})
);
}
return serialOperations;
}
}
export type GetPushNotificationValueUpdateSerialOperationsParam = {
/**
* DeepClient
*/
deep: DeepClient;
/**
* PushNotification
*/
pushNotification: PushNotification;
} & (
| {
/**
* PushNotification Link Id
*/
pushNotificationLinkId: number
}
| {
/**
* PushNotification Link
*/
pushNotificationLink: Link<number>
}
)