@prefabs.tech/fastify-firebase 0.92.0 → 0.92.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/prefabs-tech-fastify-firebase.js +108 -108
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -18
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import I from "fastify-plugin";
|
|
2
|
+
import h from "firebase-admin";
|
|
3
3
|
import { sql as l } from "slonik";
|
|
4
|
-
import { DefaultSqlFactory as
|
|
4
|
+
import { DefaultSqlFactory as N, BaseService as R } from "@prefabs.tech/fastify-slonik";
|
|
5
5
|
import { mercurius as a } from "mercurius";
|
|
6
|
-
import { gql as
|
|
7
|
-
const
|
|
8
|
-
if (!(
|
|
6
|
+
import { gql as m, mergeTypeDefs as k, baseSchema as F } from "@prefabs.tech/fastify-graphql";
|
|
7
|
+
const A = (e, r) => {
|
|
8
|
+
if (!(h.apps.length > 0)) {
|
|
9
9
|
if (e.firebase?.enabled !== !1 && !e.firebase.credentials) {
|
|
10
10
|
r.log.error("Firebase credentials are missing");
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
try {
|
|
14
|
-
|
|
15
|
-
credential:
|
|
14
|
+
h.initializeApp({
|
|
15
|
+
credential: h.credential.cert({
|
|
16
16
|
projectId: e.firebase.credentials?.projectId,
|
|
17
17
|
privateKey: e.firebase.credentials?.privateKey.replaceAll(
|
|
18
18
|
String.raw`\n`,
|
|
@@ -26,10 +26,10 @@ const P = (e, r) => {
|
|
|
26
26
|
r.log.error("Failed to initialize firebase"), r.log.error(t);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
},
|
|
30
|
-
await
|
|
31
|
-
},
|
|
32
|
-
const r = e.firebase.table?.userDevices?.name ||
|
|
29
|
+
}, D = async (e) => {
|
|
30
|
+
await h.messaging().sendEachForMulticast(e);
|
|
31
|
+
}, $ = "/send-notification", _ = "/user-device", P = "/user-device", T = "user_devices", O = (e) => {
|
|
32
|
+
const r = e.firebase.table?.userDevices?.name || T;
|
|
33
33
|
return l.unsafe`
|
|
34
34
|
DO $$
|
|
35
35
|
BEGIN
|
|
@@ -46,13 +46,13 @@ const P = (e, r) => {
|
|
|
46
46
|
])} (user_id, device_token);
|
|
47
47
|
END $$;
|
|
48
48
|
`;
|
|
49
|
-
},
|
|
49
|
+
}, M = async (e, r) => {
|
|
50
50
|
await e.connect(async (t) => {
|
|
51
|
-
await t.query(
|
|
51
|
+
await t.query(O(r));
|
|
52
52
|
});
|
|
53
53
|
};
|
|
54
|
-
class
|
|
55
|
-
static TABLE =
|
|
54
|
+
class C extends N {
|
|
55
|
+
static TABLE = T;
|
|
56
56
|
getDeleteExistingTokenSql(r) {
|
|
57
57
|
return l.type(this.validationSchema)`
|
|
58
58
|
DELETE
|
|
@@ -72,7 +72,7 @@ class W extends F {
|
|
|
72
72
|
return this.config.firebase.table?.userDevices?.name || super.table;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
class v extends
|
|
75
|
+
class v extends R {
|
|
76
76
|
async getByUserId(r) {
|
|
77
77
|
const t = this.factory.getFindByUserIdSql(r);
|
|
78
78
|
return await this.database.connect((i) => i.any(t));
|
|
@@ -85,31 +85,31 @@ class v extends A {
|
|
|
85
85
|
return super.factory;
|
|
86
86
|
}
|
|
87
87
|
get sqlFactoryClass() {
|
|
88
|
-
return
|
|
88
|
+
return C;
|
|
89
89
|
}
|
|
90
90
|
async preCreate(r) {
|
|
91
91
|
const { deviceToken: t } = r;
|
|
92
92
|
return await this.removeByDeviceToken(t), r;
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
|
-
const
|
|
95
|
+
const B = async (e, r) => {
|
|
96
96
|
if (!e.user)
|
|
97
97
|
throw e.server.httpErrors.unauthorized("Unauthorised");
|
|
98
98
|
const {
|
|
99
|
-
body:
|
|
99
|
+
body: s,
|
|
100
100
|
title: i,
|
|
101
101
|
data: o,
|
|
102
102
|
userId: d
|
|
103
|
-
} = e.body,
|
|
103
|
+
} = e.body, n = await new v(
|
|
104
104
|
e.config,
|
|
105
105
|
e.slonik,
|
|
106
106
|
e.dbSchema
|
|
107
107
|
).getByUserId(d);
|
|
108
|
-
if (!
|
|
108
|
+
if (!n || n.length === 0)
|
|
109
109
|
throw e.server.httpErrors.unprocessableEntity(
|
|
110
110
|
"No devices found for the receiver"
|
|
111
111
|
);
|
|
112
|
-
const u =
|
|
112
|
+
const u = n.map((f) => f.deviceToken), p = {
|
|
113
113
|
android: {
|
|
114
114
|
priority: "high",
|
|
115
115
|
notification: {
|
|
@@ -126,18 +126,18 @@ const z = async (e, r) => {
|
|
|
126
126
|
tokens: u,
|
|
127
127
|
notification: {
|
|
128
128
|
title: i,
|
|
129
|
-
body:
|
|
129
|
+
body: s
|
|
130
130
|
},
|
|
131
131
|
data: {
|
|
132
132
|
...o,
|
|
133
133
|
title: i,
|
|
134
|
-
body:
|
|
134
|
+
body: s
|
|
135
135
|
}
|
|
136
136
|
};
|
|
137
|
-
await
|
|
138
|
-
},
|
|
139
|
-
sendNotification:
|
|
140
|
-
},
|
|
137
|
+
await D(p), r.send({ message: "Notification sent successfully" });
|
|
138
|
+
}, L = {
|
|
139
|
+
sendNotification: B
|
|
140
|
+
}, W = {
|
|
141
141
|
description: "Send a notification to a specific user",
|
|
142
142
|
operationId: "sendNotification",
|
|
143
143
|
body: {
|
|
@@ -166,43 +166,43 @@ const z = async (e, r) => {
|
|
|
166
166
|
}
|
|
167
167
|
},
|
|
168
168
|
tags: ["notifications"]
|
|
169
|
-
},
|
|
169
|
+
}, b = (e) => async () => {
|
|
170
170
|
if (e.config.firebase.enabled === !1)
|
|
171
171
|
throw e.httpErrors.notFound("Firebase is disabled");
|
|
172
|
-
},
|
|
172
|
+
}, z = async (e) => {
|
|
173
173
|
const r = e.config.firebase.handlers?.userDevice, t = e.config.firebase.notification;
|
|
174
174
|
t?.test?.enabled && e.post(
|
|
175
|
-
t.test.path ||
|
|
175
|
+
t.test.path || $,
|
|
176
176
|
{
|
|
177
|
-
preHandler: [e.verifySession(),
|
|
178
|
-
schema:
|
|
177
|
+
preHandler: [e.verifySession(), b(e)],
|
|
178
|
+
schema: W
|
|
179
179
|
},
|
|
180
|
-
r?.addUserDevice ||
|
|
180
|
+
r?.addUserDevice || L.sendNotification
|
|
181
181
|
);
|
|
182
|
-
},
|
|
183
|
-
const { body: t, config:
|
|
182
|
+
}, x = async (e, r) => {
|
|
183
|
+
const { body: t, config: s, dbSchema: i, slonik: o, user: d } = e;
|
|
184
184
|
if (!d)
|
|
185
185
|
throw e.server.httpErrors.unauthorized("Unauthorised");
|
|
186
|
-
const { deviceToken: c } = t,
|
|
187
|
-
r.send(await
|
|
188
|
-
},
|
|
186
|
+
const { deviceToken: c } = t, n = new v(s, o, i);
|
|
187
|
+
r.send(await n.create({ userId: d.id, deviceToken: c }));
|
|
188
|
+
}, j = async (e, r) => {
|
|
189
189
|
const t = e.user;
|
|
190
190
|
if (!t)
|
|
191
191
|
throw e.server.httpErrors.unauthorized("Unauthorised");
|
|
192
|
-
const { deviceToken:
|
|
192
|
+
const { deviceToken: s } = e.body, i = new v(e.config, e.slonik, e.dbSchema), o = await i.getByUserId(t.id);
|
|
193
193
|
if (!o || o.length === 0)
|
|
194
194
|
throw e.server.httpErrors.notFound("No devices found for the user");
|
|
195
195
|
if (!o.find(
|
|
196
|
-
(c) => c.deviceToken ===
|
|
196
|
+
(c) => c.deviceToken === s
|
|
197
197
|
))
|
|
198
198
|
throw e.server.httpErrors.unprocessableEntity(
|
|
199
199
|
"Device requested to delete not owned by user"
|
|
200
200
|
);
|
|
201
|
-
r.send(await i.removeByDeviceToken(
|
|
202
|
-
},
|
|
203
|
-
addUserDevice:
|
|
204
|
-
removeUserDevice:
|
|
205
|
-
},
|
|
201
|
+
r.send(await i.removeByDeviceToken(s));
|
|
202
|
+
}, E = {
|
|
203
|
+
addUserDevice: x,
|
|
204
|
+
removeUserDevice: j
|
|
205
|
+
}, S = {
|
|
206
206
|
type: "object",
|
|
207
207
|
properties: {
|
|
208
208
|
userId: { type: "string" },
|
|
@@ -211,7 +211,7 @@ const z = async (e, r) => {
|
|
|
211
211
|
updatedAt: { type: "number" }
|
|
212
212
|
},
|
|
213
213
|
required: ["userId", "deviceToken", "createdAt", "updatedAt"]
|
|
214
|
-
},
|
|
214
|
+
}, V = {
|
|
215
215
|
description: "Delete a user device by device token",
|
|
216
216
|
operationId: "deleteUserDevice",
|
|
217
217
|
body: {
|
|
@@ -223,7 +223,7 @@ const z = async (e, r) => {
|
|
|
223
223
|
},
|
|
224
224
|
response: {
|
|
225
225
|
200: {
|
|
226
|
-
...
|
|
226
|
+
...S,
|
|
227
227
|
nullable: !0
|
|
228
228
|
},
|
|
229
229
|
401: {
|
|
@@ -235,7 +235,7 @@ const z = async (e, r) => {
|
|
|
235
235
|
}
|
|
236
236
|
},
|
|
237
237
|
tags: ["user-devices"]
|
|
238
|
-
},
|
|
238
|
+
}, q = {
|
|
239
239
|
description: "Register a new user device",
|
|
240
240
|
operationId: "postUserDevice",
|
|
241
241
|
body: {
|
|
@@ -247,7 +247,7 @@ const z = async (e, r) => {
|
|
|
247
247
|
},
|
|
248
248
|
response: {
|
|
249
249
|
200: {
|
|
250
|
-
...
|
|
250
|
+
...S,
|
|
251
251
|
nullable: !0
|
|
252
252
|
},
|
|
253
253
|
401: {
|
|
@@ -259,53 +259,53 @@ const z = async (e, r) => {
|
|
|
259
259
|
}
|
|
260
260
|
},
|
|
261
261
|
tags: ["user-devices"]
|
|
262
|
-
},
|
|
262
|
+
}, H = async (e) => {
|
|
263
263
|
const r = e.config.firebase.handlers?.userDevice;
|
|
264
264
|
e.post(
|
|
265
|
-
|
|
265
|
+
_,
|
|
266
266
|
{
|
|
267
|
-
preHandler: [e.verifySession(),
|
|
268
|
-
schema:
|
|
267
|
+
preHandler: [e.verifySession(), b(e)],
|
|
268
|
+
schema: q
|
|
269
269
|
},
|
|
270
|
-
r?.addUserDevice ||
|
|
270
|
+
r?.addUserDevice || E.addUserDevice
|
|
271
271
|
), e.delete(
|
|
272
|
-
|
|
272
|
+
P,
|
|
273
273
|
{
|
|
274
|
-
preHandler: [e.verifySession(),
|
|
275
|
-
schema:
|
|
274
|
+
preHandler: [e.verifySession(), b(e)],
|
|
275
|
+
schema: V
|
|
276
276
|
},
|
|
277
|
-
r?.removeUserDevice ||
|
|
277
|
+
r?.removeUserDevice || E.removeUserDevice
|
|
278
278
|
);
|
|
279
|
-
},
|
|
280
|
-
const { config: r, slonik: t, log:
|
|
281
|
-
r.firebase.enabled === !1 ?
|
|
279
|
+
}, Q = async (e) => {
|
|
280
|
+
const { config: r, slonik: t, log: s } = e;
|
|
281
|
+
r.firebase.enabled === !1 ? s.info("fastify-firebase plugin is not enabled") : (s.info("Registering fastify-firebase plugin"), await M(t, r), A(r, e));
|
|
282
282
|
const { routePrefix: i, routes: o } = r.firebase;
|
|
283
|
-
o?.notifications?.disabled || await e.register(
|
|
284
|
-
},
|
|
283
|
+
o?.notifications?.disabled || await e.register(z, { prefix: i }), o?.userDevices?.disabled || await e.register(H, { prefix: i });
|
|
284
|
+
}, ae = I(Q), Y = {
|
|
285
285
|
sendNotification: async (e, r, t) => {
|
|
286
|
-
const { app:
|
|
286
|
+
const { app: s, config: i, dbSchema: o, database: d, user: c } = t;
|
|
287
287
|
if (!c)
|
|
288
288
|
return new a.ErrorWithProps("unauthorized", {}, 401);
|
|
289
289
|
if (i.firebase.enabled === !1)
|
|
290
290
|
return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
|
|
291
291
|
try {
|
|
292
|
-
const { userId:
|
|
293
|
-
if (!
|
|
292
|
+
const { userId: n, title: u, body: p, data: f } = r.data;
|
|
293
|
+
if (!n)
|
|
294
294
|
return new a.ErrorWithProps("Receiver id is required", {}, 400);
|
|
295
|
-
const
|
|
295
|
+
const g = await new v(
|
|
296
296
|
i,
|
|
297
297
|
d,
|
|
298
298
|
o
|
|
299
|
-
).getByUserId(
|
|
300
|
-
if (!
|
|
299
|
+
).getByUserId(n);
|
|
300
|
+
if (!g || g.length === 0)
|
|
301
301
|
return new a.ErrorWithProps(
|
|
302
302
|
"Receiver device not found",
|
|
303
303
|
{},
|
|
304
304
|
404
|
|
305
305
|
);
|
|
306
|
-
const
|
|
307
|
-
tokens:
|
|
308
|
-
(
|
|
306
|
+
const U = {
|
|
307
|
+
tokens: g.map(
|
|
308
|
+
(w) => w.deviceToken
|
|
309
309
|
),
|
|
310
310
|
notification: {
|
|
311
311
|
title: u,
|
|
@@ -313,27 +313,27 @@ const z = async (e, r) => {
|
|
|
313
313
|
},
|
|
314
314
|
data: f
|
|
315
315
|
};
|
|
316
|
-
return await
|
|
317
|
-
} catch (
|
|
318
|
-
return
|
|
316
|
+
return await D(U), { message: "Notification sent successfully" };
|
|
317
|
+
} catch (n) {
|
|
318
|
+
return s.log.error(n), new a.ErrorWithProps(
|
|
319
319
|
"Oops, Something went wrong",
|
|
320
320
|
{},
|
|
321
321
|
500
|
|
322
322
|
);
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
|
-
},
|
|
325
|
+
}, G = {}, ce = { Mutation: Y, Query: G }, K = {
|
|
326
326
|
addUserDevice: async (e, r, t) => {
|
|
327
|
-
const { app:
|
|
327
|
+
const { app: s, config: i, dbSchema: o, database: d, user: c } = t;
|
|
328
328
|
if (i.firebase.enabled === !1)
|
|
329
329
|
return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
|
|
330
330
|
if (!c)
|
|
331
331
|
return new a.ErrorWithProps("unauthorized", {}, 401);
|
|
332
332
|
try {
|
|
333
|
-
const { deviceToken:
|
|
334
|
-
return await new v(i, d, o).create({ userId: c.id, deviceToken:
|
|
335
|
-
} catch (
|
|
336
|
-
return
|
|
333
|
+
const { deviceToken: n } = r.data;
|
|
334
|
+
return await new v(i, d, o).create({ userId: c.id, deviceToken: n });
|
|
335
|
+
} catch (n) {
|
|
336
|
+
return s.log.error(n), new a.ErrorWithProps(
|
|
337
337
|
"Oops, Something went wrong",
|
|
338
338
|
{},
|
|
339
339
|
500
|
|
@@ -341,33 +341,33 @@ const z = async (e, r) => {
|
|
|
341
341
|
}
|
|
342
342
|
},
|
|
343
343
|
removeUserDevice: async (e, r, t) => {
|
|
344
|
-
const { app:
|
|
344
|
+
const { app: s, config: i, dbSchema: o, database: d, user: c } = t;
|
|
345
345
|
if (i.firebase.enabled === !1)
|
|
346
346
|
return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
|
|
347
347
|
if (!c)
|
|
348
348
|
return new a.ErrorWithProps("unauthorized", {}, 401);
|
|
349
349
|
try {
|
|
350
|
-
const { deviceToken:
|
|
350
|
+
const { deviceToken: n } = r.data, u = new v(i, d, o), p = await u.getByUserId(c.id);
|
|
351
351
|
return !p || p.length === 0 ? new a.ErrorWithProps(
|
|
352
352
|
"No devices found for user",
|
|
353
353
|
{},
|
|
354
354
|
403
|
|
355
355
|
) : p.find(
|
|
356
|
-
(
|
|
357
|
-
) ? await u.removeByDeviceToken(
|
|
356
|
+
(y) => y.deviceToken === n
|
|
357
|
+
) ? await u.removeByDeviceToken(n) : new a.ErrorWithProps(
|
|
358
358
|
"Device requested to delete not owned by user",
|
|
359
359
|
{},
|
|
360
360
|
403
|
|
361
361
|
);
|
|
362
|
-
} catch (
|
|
363
|
-
return
|
|
362
|
+
} catch (n) {
|
|
363
|
+
return s.log.error(n), new a.ErrorWithProps(
|
|
364
364
|
"Oops, Something went wrong",
|
|
365
365
|
{},
|
|
366
366
|
500
|
|
367
367
|
);
|
|
368
368
|
}
|
|
369
369
|
}
|
|
370
|
-
},
|
|
370
|
+
}, X = {}, de = { Mutation: K, Query: X }, J = m`
|
|
371
371
|
type SendNotificationResponse {
|
|
372
372
|
message: String!
|
|
373
373
|
}
|
|
@@ -382,7 +382,7 @@ const z = async (e, r) => {
|
|
|
382
382
|
sendNotification(data: SendNotificationInput): SendNotificationResponse
|
|
383
383
|
@auth
|
|
384
384
|
}
|
|
385
|
-
`,
|
|
385
|
+
`, Z = m`
|
|
386
386
|
type UserDevice {
|
|
387
387
|
id: Int!
|
|
388
388
|
userId: String!
|
|
@@ -411,24 +411,24 @@ const z = async (e, r) => {
|
|
|
411
411
|
addUserDevice(data: UserDeviceCreateInput): UserDevice @auth
|
|
412
412
|
removeUserDevice(data: UserDeviceRemoveInput): UserDevice @auth
|
|
413
413
|
}
|
|
414
|
-
`,
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
414
|
+
`, ue = k([
|
|
415
|
+
F,
|
|
416
|
+
J,
|
|
417
|
+
Z
|
|
418
418
|
]);
|
|
419
419
|
export {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
420
|
+
$ as ROUTE_SEND_NOTIFICATION,
|
|
421
|
+
_ as ROUTE_USER_DEVICE_ADD,
|
|
422
|
+
P as ROUTE_USER_DEVICE_REMOVE,
|
|
423
|
+
T as TABLE_USER_DEVICES,
|
|
424
424
|
v as UserDeviceService,
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
425
|
+
O as createUserDevicesTableQuery,
|
|
426
|
+
ae as default,
|
|
427
|
+
ue as firebaseSchema,
|
|
428
|
+
A as initializeFirebase,
|
|
429
|
+
ce as notificationResolver,
|
|
430
|
+
z as notificationRoutes,
|
|
431
|
+
D as sendPushNotification,
|
|
432
|
+
de as userDeviceResolver,
|
|
433
|
+
H as userDeviceRoutes
|
|
434
434
|
};
|