-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-push-notification-insert-serial-operations.ts
202 lines (193 loc) · 5.72 KB
/
get-push-notification-insert-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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
DeepClient,
SerialOperation,
} from '@deep-foundation/deeplinks/imports/client.js';
import { createSerialOperation } from '@deep-foundation/deeplinks/imports/gql/index.js';
import { PushNotification } from './push-notification.js';
import { Package } from './package.js';
/**
* Gets serial operations to insert {@link Package.PushNotification}
*
* @example
* #### Insert {@link Package.PushNotification}
```ts
const {serialOperations, linkIds} = await getPushNotificationInsertSerialOperations({
deep
});
await deep.serial({
operations: serialOperations
})
```
* #### Insert {@link Package.PushNotification} with reserved link id
```ts
const reservedLinkIds = await deep.reserve(2);
const pushNotificationLinkId = reservedLinkIds.pop();
const containLinkId = reservedLinkIds.pop();
const {serialOperations, linkIds} = await getPushNotificationInsertSerialOperations({
deep,
pushNotification: {
title,
body,
},
reservedLinkIds: {
pushNotificationLinkId,
containLinkId,
}
});
await deep.serial({
operations: serialOperations
})
```
*/
export async function getPushNotificationInsertSerialOperations(
param: GetPushNotificationInsertSerialOperationsParam
): Promise<GetPushNotificationInsertSerialOperationsResult> {
const {
deep,
pushNotification,
containValue,
containerLinkId,
} = param;
const $package = new Package({deep});
const reservedLinkIds = await getReservedLinkIds();
const { containLinkId, pushNotificationLinkId } = reservedLinkIds;
const typeLinkIds = await getTypeLinkIds();
const { containTypeLinkId, pushNotificationTypeLinkId } = typeLinkIds;
const serialOperations = [];
const pushNotificationInsertSerialOperation = createSerialOperation({
type: 'insert',
table: 'links',
objects: {
id: pushNotificationLinkId,
type_id: pushNotificationTypeLinkId,
},
});
serialOperations.push(pushNotificationInsertSerialOperation);
const valueOfPushNotificationInsertSerialOperation = createSerialOperation({
type: 'insert',
table: 'objects',
objects: {
link_id: pushNotificationLinkId,
value: pushNotification,
},
});
serialOperations.push(valueOfPushNotificationInsertSerialOperation);
if (containerLinkId !== null) {
const containInsertSerialOperation = createSerialOperation({
type: 'insert',
table: 'links',
objects: {
type_id: containTypeLinkId,
from_id: containerLinkId || deep.linkId,
to_id: pushNotificationLinkId,
},
});
serialOperations.push(containInsertSerialOperation);
const valueOfContainInsertSerialOperation = createSerialOperation({
type: 'insert',
table: 'strings',
objects: {
link_id: containLinkId,
value: containValue,
},
});
serialOperations.push(valueOfContainInsertSerialOperation);
}
return {
serialOperations,
linkIds: reservedLinkIds
};
type GetReservedLinkIdsResult = Required<
Exclude<
GetPushNotificationInsertSerialOperationsParam['reservedLinkIds'],
undefined
>
>;
async function getReservedLinkIds(): Promise<GetReservedLinkIdsResult> {
let result: GetReservedLinkIdsResult = {
containLinkId: 0,
pushNotificationLinkId: 0,
};
const linksToReserveCount =
Object.keys(result).length -
Object.keys(param.reservedLinkIds || {}).length;
const reservedLinkIds: number[] =
linksToReserveCount > 0 ? await deep.reserve(linksToReserveCount) : [];
result = {
containLinkId:
param.reservedLinkIds?.containLinkId ?? reservedLinkIds.pop()!,
pushNotificationLinkId:
param.reservedLinkIds?.pushNotificationLinkId ?? reservedLinkIds.pop()!,
};
return result;
}
type GetTypeLinkIdsResult = Required<
Exclude<GetPushNotificationInsertSerialOperationsParam['typeLinkIds'], undefined>
>;
async function getTypeLinkIds(): Promise<GetTypeLinkIdsResult> {
const result: GetTypeLinkIdsResult = {
containTypeLinkId:
param.typeLinkIds?.containTypeLinkId ||
(await deep.id('@deep-foundation/core', 'Contain')),
pushNotificationTypeLinkId:
param.typeLinkIds?.pushNotificationTypeLinkId ||
await $package.PushNotification.id(),
};
return result;
}
}
export interface GetPushNotificationInsertSerialOperationsParam {
/**
* Reserved link ids that will be used in the serial operations
*/
reservedLinkIds?: {
/**
* Reserved link id for the pushNotification
*/
pushNotificationLinkId?: number;
/**
* Reserved link id for the contain
*/
containLinkId?: number;
};
/**
* Link ids of types that will be used in the serial operations
*/
typeLinkIds?: {
/**
* Link id of the contain type
*/
containTypeLinkId?: number;
/**
* Link id of the pushNotification type
*/
pushNotificationTypeLinkId?: number;
};
/**
* Deep Client
*/
deep: DeepClient;
/**
* PushNotification
*/
pushNotification: PushNotification;
/**
* Link id of the container
*
* @remarks
* If it is null, contain link will not be created
* @defaultValue {@link GetPushNotificationInsertSerialOperationsParam.deep.linkId} if not provided or undefined
*/
containerLinkId?: number | undefined | null;
/**
* Value of the contain link
*
* @remarks
* If {@link GetPushNotificationInsertSerialOperationsParam.containerLinkId} is null, this will be ignored
*/
containValue?: string | undefined;
}
export interface GetPushNotificationInsertSerialOperationsResult {
serialOperations: Array<SerialOperation>,
linkIds: Required<Exclude<GetPushNotificationInsertSerialOperationsParam['reservedLinkIds'], undefined>>,
}