-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeep.json
306 lines (306 loc) · 22 KB
/
deep.json
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{
"package": {
"name": "@deep-foundation/firebase-push-notification",
"version": "18.2.2-dev.4"
},
"data": [
{
"package": {
"dependencyId": 0,
"containValue": "Tree"
},
"id": 1
},
{
"package": {
"dependencyId": 0,
"containValue": "SyncTextFile"
},
"id": 2
},
{
"package": {
"dependencyId": 1,
"containValue": "TSX"
},
"id": 3
},
{
"package": {
"dependencyId": 0,
"containValue": "GeneratedFrom"
},
"id": 4
},
{
"package": {
"dependencyId": 0,
"containValue": "Handler"
},
"id": 5
},
{
"package": {
"dependencyId": 0,
"containValue": "dockerSupportsJs"
},
"id": 6
},
{
"package": {
"dependencyId": 0,
"containValue": "Type"
},
"id": 7
},
{
"package": {
"dependencyId": 0,
"containValue": "Value"
},
"id": 8
},
{
"package": {
"dependencyId": 0,
"containValue": "Object"
},
"id": 9
},
{
"package": {
"dependencyId": 0,
"containValue": "User"
},
"id": 10
},
{
"package": {
"dependencyId": 0,
"containValue": "String"
},
"id": 11
},
{
"package": {
"dependencyId": 0,
"containValue": "TreeIncludeFromCurrent"
},
"id": 12
},
{
"package": {
"dependencyId": 2,
"containValue": "Notify"
},
"id": 13
},
{
"package": {
"dependencyId": 3,
"containValue": "Device"
},
"id": 14
},
{
"package": {
"dependencyId": 0,
"containValue": "HandleInsert"
},
"id": 15
},
{
"package": {
"dependencyId": 2,
"containValue": "Notified"
},
"id": 16
},
{
"package": {
"dependencyId": 0,
"containValue": "TreeIncludeNode"
},
"id": 17
},
{
"package": {
"dependencyId": 0,
"containValue": "HandleUpdate"
},
"id": 18
},
{
"package": {
"dependencyId": 4,
"containValue": "UpdateHandler"
},
"id": 19
},
{
"id": "PushNotificationTree",
"type": 1
},
{
"id": "PushNotificationNotifyInsertHandlerCodeGenerated",
"type": 2,
"value": {
"value": "async ({ deep, data: { newLink: notifyLink, triggeredByLinkId }, }) => {\n const { default: firebaseAdmin } = await import('firebase-admin');\n const util = await import('util');\n const { createSerialOperation } = await import('@deep-foundation/deeplinks/imports/gql/index.js');\n const logs = [];\n const DEFAULT_LOG_DEPTH = 3;\n try {\n const result = await main();\n return {\n result,\n logs\n };\n }\n catch (error) {\n return {\n error,\n logs\n };\n }\n async function main() {\n const log = getNamespacedLogger({ namespace: main.name });\n log({ notifyLink });\n const notificationLinkId = notifyLink.from_id;\n log({ notificationLinkId });\n const { data: [notificationLink] } = await deep.select(notificationLinkId);\n log({ notificationLink });\n if (!notificationLink.value?.value) {\n throw new Error(`##${notificationLinkId} must have value`);\n }\n const title = notificationLink.value.value.title;\n log({ title });\n if (!title) {\n throw new Error(`Object value of ##${notificationLinkId} must have title property`);\n }\n const body = notificationLink.value.value.body;\n log({ body });\n if (!body) {\n throw new Error(`Object value of ##${notificationLinkId} must have body property`);\n }\n const deviceLinkId = notifyLink.to_id;\n log({ deviceLinkId });\n const containTypeLinkId = await deep.id('@deep-foundation/core', 'Contain');\n log({ containTypeLinkId });\n const serviceAccount = await getServiceAccount({\n containTypeLinkId,\n triggeredByLinkId,\n });\n log({ serviceAccount });\n const firebaseApplication = await getFirebaseApplication({\n firebaseAdmin,\n serviceAccount,\n });\n log({ firebaseApplication });\n const deviceRegistrationToken = await getDeviceRegistrationToken({\n containTypeLinkId,\n deviceLinkId,\n });\n log({ deviceRegistrationToken });\n const pushNotificationData = {\n token: deviceRegistrationToken,\n notification: {\n title: title,\n body: body,\n },\n };\n log({ pushNotificationData });\n await firebaseAdmin.messaging(firebaseApplication).send(pushNotificationData);\n await deep.insert({\n type_id: await deep.id(deep.linkId, 'Notified'),\n in: {\n data: {\n type_id: containTypeLinkId,\n from_id: triggeredByLinkId,\n },\n },\n from_id: notifyLink.id,\n to_id: deviceLinkId,\n });\n firebaseApplication.delete();\n }\n async function getServiceAccount({ containTypeLinkId, triggeredByLinkId }) {\n const log = getNamespacedLogger({ namespace: getServiceAccount.name });\n const serviceAccountTypeLinkId = await deep.id(deep.linkId, 'ServiceAccount');\n log({ serviceAccountTypeLinkId });\n const usesServiceAccountTypeLinkId = await deep.id(deep.linkId, 'UsesServiceAccount');\n log({ usesServiceAccountTypeLinkId });\n const selectData = {\n _or: [\n {\n type_id: serviceAccountTypeLinkId,\n in: {\n type_id: containTypeLinkId,\n from_id: triggeredByLinkId,\n },\n },\n {\n type_id: usesServiceAccountTypeLinkId,\n from_id: triggeredByLinkId,\n },\n ],\n };\n const { data } = await deep.select(selectData);\n log({ data });\n if (data.length === 0) {\n throw new Error(`Select with data ${JSON.stringify(selectData)} returned empty result`);\n }\n let serviceAccountLink;\n const usesServiceAccountLinks = data.filter((link) => link.type_id === usesServiceAccountTypeLinkId);\n if (usesServiceAccountLinks.length > 1) {\n throw new Error(`There must be only one link of type ${usesServiceAccountTypeLinkId} and from ${triggeredByLinkId}, instead there are ${usesServiceAccountLinks\n .map((link) => `##${link.id}`)\n .join(', ')}`);\n }\n else if (usesServiceAccountLinks.length === 1) {\n const usesServiceAccountLink = usesServiceAccountLinks[0];\n serviceAccountLink = data.find((link) => link.id === usesServiceAccountLink.to_id);\n }\n else if (usesServiceAccountLinks.length === 0) {\n const serviceAccountLinks = data.filter((link) => link.type_id === serviceAccountTypeLinkId);\n if (serviceAccountLinks.length > 1) {\n throw new Error(`There must be only one link of type ##${serviceAccountTypeLinkId} and contained by ##${triggeredByLinkId}, instead there are ${serviceAccountLinks\n .map((link) => `##${link.id}`)\n .join(', ')}`);\n }\n else if (serviceAccountLinks.length === 1) {\n serviceAccountLink = serviceAccountLinks[0];\n }\n else if (serviceAccountLinks.length === 0) {\n throw new Error(`A link of type ##${serviceAccountTypeLinkId} and contained by ##${triggeredByLinkId} is not found`);\n }\n }\n log({ serviceAccountLink });\n if (!serviceAccountLink) {\n throw new Error(`A link of type ##${usesServiceAccountTypeLinkId} and from ##${triggeredByLinkId} is not found`);\n }\n if (!serviceAccountLink.value?.value) {\n throw new Error(`##${serviceAccountLink.id} must have value`);\n }\n const result = serviceAccountLink.value.value;\n log({ result });\n return result;\n }\n async function getDeviceRegistrationToken({ containTypeLinkId, deviceLinkId, }) {\n const log = getNamespacedLogger({ namespace: getDeviceRegistrationToken.name });\n const deviceRegistrationTokenTypeLinkId = await deep.id(deep.linkId, 'DeviceRegistrationToken');\n log({ deviceRegistrationTokenTypeLinkId });\n const selectData = {\n type_id: deviceRegistrationTokenTypeLinkId,\n in: {\n type_id: containTypeLinkId,\n from_id: deviceLinkId,\n },\n };\n log({ selectData });\n const { data: [deviceRegistrationTokenLink], } = await deep.select(selectData);\n if (!deviceRegistrationTokenLink) {\n throw new Error(`##${deviceLinkId} must have contained a link of type ##${deviceRegistrationTokenTypeLinkId}. Select with data ${JSON.stringify(selectData)} returned empty result`);\n }\n log({ deviceRegistrationTokenLink });\n if (!deviceRegistrationTokenLink.value?.value) {\n throw new Error(`##${deviceRegistrationTokenLink.id} must have value`);\n }\n const result = deviceRegistrationTokenLink.value.value;\n log({ result });\n return result;\n }\n async function getFirebaseApplication(options) {\n const log = getNamespacedLogger({ namespace: getFirebaseApplication.name });\n log({ options });\n const { firebaseAdmin, serviceAccount, } = options;\n firebaseAdmin.apps.forEach((app) => app?.delete());\n return firebaseAdmin.initializeApp({\n credential: firebaseAdmin.credential.cert(serviceAccount),\n });\n }\n function getNamespacedLogger({ namespace, depth = DEFAULT_LOG_DEPTH, }) {\n return function (content) {\n const message = util.inspect(content, { depth });\n logs.push(`${namespace}: ${message}`);\n };\n }\n};\nexport {};\n//# sourceMappingURL=module.js.map"
}
},
{
"id": "PushNotificationNotifyInsertHandlerCode",
"type": 3,
"value": {
"value": "import {\r\n DeepClient,\r\n SerialOperation,\r\n Table,\r\n} from '@deep-foundation/deeplinks/imports/client';\r\nimport { Link } from '@deep-foundation/deeplinks/imports/minilinks';\r\nimport * as FirebaseAdmin from 'firebase-admin';\r\n\r\nasync ({\r\n deep,\r\n data: { newLink: notifyLink, triggeredByLinkId },\r\n}: {\r\n deep: DeepClient;\r\n data: { newLink: Link<number>, triggeredByLinkId: number };\r\n}) => {\r\n const {default: firebaseAdmin} = await import('firebase-admin');\r\n const util = await import('util');\r\n const { createSerialOperation } = await import('@deep-foundation/deeplinks/imports/gql/index.js')\r\n const logs: Array<any> = [];\r\n const DEFAULT_LOG_DEPTH = 3;\r\n\r\n try {\r\n const result = await main();\r\n return {\r\n result,\r\n logs\r\n }\r\n } catch (error) {\r\n return {\r\n error,\r\n logs\r\n }\r\n }\r\n\r\n async function main() {\r\n const log = getNamespacedLogger({ namespace: main.name });\r\n log({notifyLink})\r\n const notificationLinkId = notifyLink.from_id!;\r\n log({ notificationLinkId })\r\n const { data: [notificationLink] } = await deep.select(notificationLinkId)\r\n log({ notificationLink })\r\n if (!notificationLink.value?.value) {\r\n throw new Error(`##${notificationLinkId} must have value`)\r\n }\r\n const title = notificationLink.value.value.title;\r\n log({ title })\r\n if (!title) {\r\n throw new Error(`Object value of ##${notificationLinkId} must have title property`)\r\n }\r\n\r\n const body = notificationLink.value.value.body;\r\n log({ body })\r\n if (!body) {\r\n throw new Error(`Object value of ##${notificationLinkId} must have body property`)\r\n }\r\n\r\n const deviceLinkId = notifyLink.to_id!;\r\n log({ deviceLinkId })\r\n\r\n const containTypeLinkId = await deep.id('@deep-foundation/core', 'Contain');\r\n log({ containTypeLinkId })\r\n\r\n const serviceAccount = await getServiceAccount({\r\n containTypeLinkId,\r\n triggeredByLinkId,\r\n });\r\n log({ serviceAccount })\r\n\r\n const firebaseApplication = await getFirebaseApplication({\r\n firebaseAdmin,\r\n serviceAccount,\r\n });\r\n log({ firebaseApplication })\r\n\r\n const deviceRegistrationToken = await getDeviceRegistrationToken({\r\n containTypeLinkId,\r\n deviceLinkId,\r\n });\r\n log({ deviceRegistrationToken })\r\n\r\n const pushNotificationData = {\r\n token: deviceRegistrationToken,\r\n notification: {\r\n title: title,\r\n body: body,\r\n },\r\n };\r\n log({ pushNotificationData })\r\n\r\n await firebaseAdmin.messaging(firebaseApplication).send(pushNotificationData);\r\n await deep.insert({\r\n type_id: await deep.id(deep.linkId!, 'Notified'),\r\n in: {\r\n data: {\r\n type_id: containTypeLinkId,\r\n from_id: triggeredByLinkId,\r\n },\r\n },\r\n from_id: notifyLink.id,\r\n to_id: deviceLinkId,\r\n });\r\n\r\n firebaseApplication.delete();\r\n }\r\n\r\n async function getServiceAccount({ containTypeLinkId, triggeredByLinkId }: {containTypeLinkId: number, triggeredByLinkId: number}) {\r\n const log = getNamespacedLogger({ namespace: getServiceAccount.name });\r\n const serviceAccountTypeLinkId = await deep.id(\r\n deep.linkId!,\r\n 'ServiceAccount'\r\n );\r\n log({ serviceAccountTypeLinkId })\r\n const usesServiceAccountTypeLinkId = await deep.id(\r\n deep.linkId!,\r\n 'UsesServiceAccount'\r\n );\r\n log({ usesServiceAccountTypeLinkId })\r\n const selectData = {\r\n _or: [\r\n {\r\n type_id: serviceAccountTypeLinkId,\r\n in: {\r\n type_id: containTypeLinkId,\r\n from_id: triggeredByLinkId,\r\n },\r\n },\r\n {\r\n type_id: usesServiceAccountTypeLinkId,\r\n from_id: triggeredByLinkId,\r\n },\r\n ],\r\n };\r\n const { data } = await deep.select(selectData);\r\n log({ data })\r\n if (data.length === 0) {\r\n throw new Error(\r\n `Select with data ${JSON.stringify(selectData)} returned empty result`\r\n );\r\n }\r\n let serviceAccountLink;\r\n const usesServiceAccountLinks = data.filter(\r\n (link) => link.type_id === usesServiceAccountTypeLinkId\r\n );\r\n if (usesServiceAccountLinks.length > 1) {\r\n throw new Error(\r\n `There must be only one link of type ${usesServiceAccountTypeLinkId} and from ${triggeredByLinkId}, instead there are ${usesServiceAccountLinks\r\n .map((link) => `##${link.id}`)\r\n .join(', ')}`\r\n );\r\n } else if (usesServiceAccountLinks.length === 1) {\r\n const usesServiceAccountLink = usesServiceAccountLinks[0];\r\n serviceAccountLink = data.find(\r\n (link) => link.id === usesServiceAccountLink.to_id\r\n );\r\n } else if (usesServiceAccountLinks.length === 0) {\r\n const serviceAccountLinks = data.filter(\r\n (link) => link.type_id === serviceAccountTypeLinkId\r\n );\r\n if (serviceAccountLinks.length > 1) {\r\n throw new Error(\r\n `There must be only one link of type ##${serviceAccountTypeLinkId} and contained by ##${triggeredByLinkId}, instead there are ${serviceAccountLinks\r\n .map((link) => `##${link.id}`)\r\n .join(', ')}`\r\n );\r\n } else if (serviceAccountLinks.length === 1) {\r\n serviceAccountLink = serviceAccountLinks[0];\r\n } else if (serviceAccountLinks.length === 0) {\r\n throw new Error(\r\n `A link of type ##${serviceAccountTypeLinkId} and contained by ##${triggeredByLinkId} is not found`\r\n );\r\n }\r\n }\r\n log({ serviceAccountLink })\r\n if (!serviceAccountLink) {\r\n throw new Error(\r\n `A link of type ##${usesServiceAccountTypeLinkId} and from ##${triggeredByLinkId} is not found`\r\n );\r\n }\r\n if (!serviceAccountLink.value?.value) {\r\n throw new Error(`##${serviceAccountLink.id} must have value`);\r\n }\r\n const result = serviceAccountLink.value.value;\r\n log({ result })\r\n return result;\r\n }\r\n\r\n async function getDeviceRegistrationToken({\r\n containTypeLinkId,\r\n deviceLinkId,\r\n }: {\r\n containTypeLinkId: number;\r\n deviceLinkId: number;\r\n }) {\r\n const log = getNamespacedLogger({ namespace: getDeviceRegistrationToken.name });\r\n const deviceRegistrationTokenTypeLinkId = await deep.id(\r\n deep.linkId!,\r\n 'DeviceRegistrationToken'\r\n );\r\n log({ deviceRegistrationTokenTypeLinkId })\r\n const selectData = {\r\n type_id: deviceRegistrationTokenTypeLinkId,\r\n in: {\r\n type_id: containTypeLinkId,\r\n from_id: deviceLinkId,\r\n },\r\n };\r\n log({ selectData })\r\n const {\r\n data: [deviceRegistrationTokenLink],\r\n } = await deep.select(selectData);\r\n if (!deviceRegistrationTokenLink) {\r\n throw new Error(\r\n `##${deviceLinkId} must have contained a link of type ##${deviceRegistrationTokenTypeLinkId}. Select with data ${JSON.stringify(\r\n selectData\r\n )} returned empty result`\r\n );\r\n }\r\n log({ deviceRegistrationTokenLink })\r\n if (!deviceRegistrationTokenLink.value?.value) {\r\n throw new Error(`##${deviceRegistrationTokenLink.id} must have value`);\r\n }\r\n const result = deviceRegistrationTokenLink.value.value;\r\n log({ result })\r\n return result;\r\n }\r\n\r\n async function getFirebaseApplication(options: {\r\n firebaseAdmin: typeof FirebaseAdmin,\r\n serviceAccount: FirebaseAdmin.ServiceAccount,\r\n }): Promise<FirebaseAdmin.app.App> {\r\n const log = getNamespacedLogger({ namespace: getFirebaseApplication.name });\r\n log({ options })\r\n const {\r\n firebaseAdmin,\r\n serviceAccount,\r\n } = options;\r\n firebaseAdmin.apps.forEach((app) => app?.delete());\r\n return firebaseAdmin.initializeApp({\r\n credential: firebaseAdmin.credential.cert(serviceAccount),\r\n });\r\n }\r\n\r\n\r\n function getNamespacedLogger({\r\n namespace,\r\n depth = DEFAULT_LOG_DEPTH,\r\n }: {\r\n namespace: string;\r\n depth?: number;\r\n }) {\r\n return function (content: any) {\r\n const message = util.inspect(content, { depth });\r\n logs.push(`${namespace}: ${message}`);\r\n };\r\n }\r\n};\r\n"
}
},
{
"id": "PushNotificationNotifyInsertHandlerCodeGeneratedFrom",
"type": 4,
"from": "PushNotificationNotifyInsertHandlerCodeGenerated",
"to": "PushNotificationNotifyInsertHandlerCode"
},
{
"id": "PushNotificationNotifyInsertHandler",
"type": 5,
"from": 6,
"to": "PushNotificationNotifyInsertHandlerCodeGenerated"
},
{
"id": "ServiceAccount",
"type": 7
},
{
"id": "TypeOfValueOfServiceAccount",
"type": 8,
"from": "ServiceAccount",
"to": 9
},
{
"id": "UsesServiceAccount",
"type": 7,
"from": 10,
"to": "ServiceAccount"
},
{
"id": "WebPushCertificate",
"type": 7
},
{
"id": "TypeOfValueOfWebPushCertificate",
"type": 8,
"from": "WebPushCertificate",
"to": 11
},
{
"id": "UsesWebPushCertificate",
"type": 7,
"from": 10,
"to": "WebPushCertificate"
},
{
"id": "DeviceRegistrationToken",
"type": 7
},
{
"id": "TreeIncludeFromCurrentPushNotificationBody",
"type": 12,
"from": "PushNotificationTree",
"to": "PushNotificationBody"
},
{
"id": "PushNotificationBody",
"type": 7,
"from": "PushNotification",
"to": "PushNotification"
},
{
"id": "TypeOfValueOfPushNotificationBody",
"type": 8,
"from": "PushNotificationBody",
"to": 11
},
{
"id": "PushNotification",
"type": 7
},
{
"id": "TypeOfValueOfPushNotification",
"type": 8,
"from": "PushNotification",
"to": 9
},
{
"id": "PushNotificationTitle",
"type": 7,
"from": "PushNotification",
"to": "PushNotification"
},
{
"id": "TypeOfValueOfPushNotificationTitle",
"type": 8,
"from": "PushNotificationTitle",
"to": 11
},
{
"id": "TreeIncludeFromCurrentPushNotificationTitle",
"type": 12,
"from": "PushNotificationTree",
"to": "PushNotificationTitle"
},
{
"id": "Notify",
"type": 13,
"from": "PushNotification",
"to": 14
},
{
"id": "HandlePushNotificationNotifyInsert",
"type": 15,
"from": "Notify",
"to": "PushNotificationNotifyInsertHandler"
},
{
"id": "Notified",
"type": 16,
"from": "Notify",
"to": 14
},
{
"id": "TreeIncludeNodePushNotification",
"type": 17,
"from": "PushNotificationTree",
"to": "PushNotification"
},
{
"id": "HandleUpdateOfPushNotification",
"type": 18,
"from": "PushNotification",
"to": 19
}
],
"errors": [],
"dependencies": [
{
"name": "@deep-foundation/core",
"version": "0.0.2"
},
{
"name": "@deep-foundation/tsx",
"version": "0.0.4"
},
{
"name": "@deep-foundation/notification",
"version": "1.0.1"
},
{
"name": "@deep-foundation/capacitor-device",
"version": "17.1.0"
},
{
"name": "@deep-foundation/object-to-links-async-converter",
"version": "0.0.1-dev.5"
}
]
}