@prefabs.tech/fastify-firebase 0.94.1 → 0.96.0

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.
@@ -1,10 +1,14 @@
1
- import { gql as m, mergeTypeDefs as I, baseSchema as N } from "@prefabs.tech/fastify-graphql";
2
- import g from "firebase-admin";
3
- import { sql as l } from "slonik";
4
- import { DefaultSqlFactory as R, BaseService as k } from "@prefabs.tech/fastify-slonik";
5
- import { mercurius as a } from "mercurius";
6
- import F from "fastify-plugin";
7
- const A = "/send-notification", $ = "/user-device", _ = "/user-device", D = "user_devices", P = m`
1
+ import { baseSchema as e, gql as t, mergeTypeDefs as n } from "@prefabs.tech/fastify-graphql";
2
+ import r, { default as i } from "firebase-admin";
3
+ import { getAppCheck as a, getAppCheck as o } from "firebase-admin/app-check";
4
+ import { sql as s } from "slonik";
5
+ import { BaseService as c, DefaultSqlFactory as l } from "@prefabs.tech/fastify-slonik";
6
+ import { mercurius as u } from "mercurius";
7
+ import d from "fastify-plugin";
8
+ //#region src/constants.ts
9
+ var f = "/send-notification", p = "/user-device", m = "/user-device", h = "user_devices", g = n([
10
+ e,
11
+ t`
8
12
  type SendNotificationResponse {
9
13
  message: String!
10
14
  }
@@ -19,7 +23,8 @@ const A = "/send-notification", $ = "/user-device", _ = "/user-device", D = "use
19
23
  sendNotification(data: SendNotificationInput): SendNotificationResponse
20
24
  @auth
21
25
  }
22
- `, O = m`
26
+ `,
27
+ t`
23
28
  type UserDevice {
24
29
  id: Int!
25
30
  userId: String!
@@ -48,40 +53,48 @@ const A = "/send-notification", $ = "/user-device", _ = "/user-device", D = "use
48
53
  addUserDevice(data: UserDeviceCreateInput): UserDevice @auth
49
54
  removeUserDevice(data: UserDeviceRemoveInput): UserDevice @auth
50
55
  }
51
- `, ae = I([
52
- N,
53
- P,
54
- O
55
- ]), M = (e, r) => {
56
- if (!(g.apps.length > 0)) {
57
- if (e.firebase?.enabled !== !1 && !e.firebase.credentials) {
58
- r.log.error("Firebase credentials are missing");
59
- return;
60
- }
61
- try {
62
- g.initializeApp({
63
- credential: g.credential.cert({
64
- clientEmail: e.firebase.credentials?.clientEmail,
65
- privateKey: e.firebase.credentials?.privateKey.replaceAll(
66
- String.raw`\n`,
67
- `
68
56
  `
69
- ),
70
- projectId: e.firebase.credentials?.projectId
71
- })
72
- });
73
- } catch (t) {
74
- r.log.error("Failed to initialize firebase"), r.log.error(t);
75
- }
76
- }
77
- }, T = async (e) => {
78
- await g.messaging().sendEachForMulticast(e);
79
- }, C = (e) => {
80
- const r = e.firebase.table?.userDevices?.name || D;
81
- return l.unsafe`
57
+ ]), _ = (e, t) => {
58
+ if (!(r.apps.length > 0)) {
59
+ if (e.firebase?.enabled !== !1 && !e.firebase.credentials) {
60
+ t.log.error("Firebase credentials are missing");
61
+ return;
62
+ }
63
+ try {
64
+ r.initializeApp({ credential: r.credential.cert({
65
+ clientEmail: e.firebase.credentials?.clientEmail,
66
+ privateKey: e.firebase.credentials?.privateKey.replaceAll(String.raw`\n`, "\n"),
67
+ projectId: e.firebase.credentials?.projectId
68
+ }) });
69
+ } catch (e) {
70
+ t.log.error("Failed to initialize firebase"), t.log.error(e);
71
+ }
72
+ }
73
+ }, v = async (e) => {
74
+ await r.messaging().sendEachForMulticast(e);
75
+ }, y = async (e, t) => {
76
+ if (!e.config.firebase.appCheck?.enabled) return;
77
+ let n = new Set(e.config.firebase.appCheck?.routes);
78
+ try {
79
+ if (!n.has(e.url.split("?", 1)[0])) return;
80
+ let r = e.headers["x-firebase-appcheck"];
81
+ if (!r || Array.isArray(r)) return t.status(403).send({
82
+ code: "FORBIDDEN",
83
+ message: "You aren't authorized to access this resource"
84
+ });
85
+ await o().verifyToken(r);
86
+ } catch (n) {
87
+ return e.log.error(`Error verifying Firebase App Check token: ${n}`), t.status(403).send({
88
+ code: "FORBIDDEN",
89
+ message: "You aren't authorized to access this resource"
90
+ });
91
+ }
92
+ }, b = (e) => {
93
+ let t = e.firebase.table?.userDevices?.name || "user_devices";
94
+ return s.unsafe`
82
95
  DO $$
83
96
  BEGIN
84
- CREATE TABLE IF NOT EXISTS ${l.identifier([r])} (
97
+ CREATE TABLE IF NOT EXISTS ${s.identifier([t])} (
85
98
  id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
86
99
  user_id VARCHAR(255) NOT NULL,
87
100
  device_token VARCHAR(255) NOT NULL,
@@ -89,346 +102,252 @@ const A = "/send-notification", $ = "/user-device", _ = "/user-device", D = "use
89
102
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
90
103
  );
91
104
 
92
- CREATE INDEX IF NOT EXISTS idx_user_id_device_token ON ${l.identifier([
93
- r
94
- ])} (user_id, device_token);
105
+ CREATE INDEX IF NOT EXISTS idx_user_id_device_token ON ${s.identifier([t])} (user_id, device_token);
95
106
  END $$;
96
107
  `;
97
- }, b = (e) => async () => {
98
- if (e.config.firebase.enabled === !1)
99
- throw e.httpErrors.notFound("Firebase is disabled");
100
- };
101
- class B extends R {
102
- static TABLE = D;
103
- get table() {
104
- return this.config.firebase.table?.userDevices?.name || super.table;
105
- }
106
- getDeleteExistingTokenSql(r) {
107
- return l.type(this.validationSchema)`
108
+ }, x = (e) => async () => {
109
+ if (e.config.firebase.enabled === !1) throw e.httpErrors.notFound("Firebase is disabled");
110
+ }, S = class extends l {
111
+ static TABLE = h;
112
+ get table() {
113
+ return this.config.firebase.table?.userDevices?.name || super.table;
114
+ }
115
+ getDeleteExistingTokenSql(e) {
116
+ return s.type(this.validationSchema)`
108
117
  DELETE
109
118
  FROM ${this.tableFragment}
110
- ${this.getWhereFragment({ filterFragment: l.fragment`device_token = ${r}` })}
119
+ ${this.getWhereFragment({ filterFragment: s.fragment`device_token = ${e}` })}
111
120
  RETURNING *;
112
121
  `;
113
- }
114
- getFindByUserIdSql(r) {
115
- return l.type(this.validationSchema)`
122
+ }
123
+ getFindByUserIdSql(e) {
124
+ return s.type(this.validationSchema)`
116
125
  SELECT *
117
126
  FROM ${this.tableFragment}
118
- ${this.getWhereFragment({ filterFragment: l.fragment`user_id = ${r}` })};
127
+ ${this.getWhereFragment({ filterFragment: s.fragment`user_id = ${e}` })};
119
128
  `;
120
- }
121
- }
122
- class v extends k {
123
- get factory() {
124
- return super.factory;
125
- }
126
- get sqlFactoryClass() {
127
- return B;
128
- }
129
- async getByUserId(r) {
130
- const t = this.factory.getFindByUserIdSql(r);
131
- return await this.database.connect((i) => i.any(t));
132
- }
133
- async removeByDeviceToken(r) {
134
- const t = this.factory.getDeleteExistingTokenSql(r);
135
- return await this.database.connect((i) => i.maybeOne(t));
136
- }
137
- async preCreate(r) {
138
- const { deviceToken: t } = r;
139
- return await this.removeByDeviceToken(t), r;
140
- }
141
- }
142
- const L = async (e, r) => {
143
- if (!e.user)
144
- throw e.server.httpErrors.unauthorized("Unauthorised");
145
- const {
146
- body: s,
147
- data: i,
148
- title: o,
149
- userId: d
150
- } = e.body, n = await new v(
151
- e.config,
152
- e.slonik,
153
- e.dbSchema
154
- ).getByUserId(d);
155
- if (!n || n.length === 0)
156
- throw e.server.httpErrors.unprocessableEntity(
157
- "No devices found for the receiver"
158
- );
159
- const u = n.map((f) => f.deviceToken), p = {
160
- android: {
161
- notification: {
162
- sound: "default"
163
- },
164
- priority: "high"
165
- },
166
- apns: {
167
- payload: {
168
- aps: {
169
- sound: "default"
170
- }
171
- }
172
- },
173
- data: {
174
- ...i,
175
- body: s,
176
- title: o
177
- },
178
- notification: {
179
- body: s,
180
- title: o
181
- },
182
- tokens: u
183
- };
184
- await T(p), r.send({ message: "Notification sent successfully" });
185
- }, W = {
186
- sendNotification: L
187
- }, z = {
188
- body: {
189
- properties: {
190
- message: { type: "string" },
191
- title: { type: "string" },
192
- userId: { type: "string" }
193
- },
194
- required: ["title", "message", "userId"],
195
- type: "object"
196
- },
197
- description: "Send a notification to a specific user",
198
- operationId: "sendNotification",
199
- response: {
200
- 200: {
201
- properties: {
202
- message: { type: "string" },
203
- success: { type: "boolean" }
204
- },
205
- type: "object"
206
- },
207
- 401: {
208
- $ref: "ErrorResponse#",
209
- description: "Unauthorized"
210
- },
211
- 500: {
212
- $ref: "ErrorResponse#"
213
- }
214
- },
215
- tags: ["notifications"]
216
- }, x = async (e) => {
217
- const r = e.config.firebase.handlers?.notification, t = e.config.firebase.notification;
218
- t?.test?.enabled && e.post(
219
- t.test.path || A,
220
- {
221
- preHandler: [e.verifySession(), b(e)],
222
- schema: z
223
- },
224
- r?.sendNotification || W.sendNotification
225
- );
129
+ }
130
+ }, C = class extends c {
131
+ get factory() {
132
+ return super.factory;
133
+ }
134
+ get sqlFactoryClass() {
135
+ return S;
136
+ }
137
+ async getByUserId(e) {
138
+ let t = this.factory.getFindByUserIdSql(e);
139
+ return await this.database.connect((e) => e.any(t));
140
+ }
141
+ async removeByDeviceToken(e) {
142
+ let t = this.factory.getDeleteExistingTokenSql(e);
143
+ return await this.database.connect((e) => e.maybeOne(t));
144
+ }
145
+ async preCreate(e) {
146
+ let { deviceToken: t } = e;
147
+ return await this.removeByDeviceToken(t), e;
148
+ }
149
+ }, w = { sendNotification: async (e, t) => {
150
+ if (!e.user) throw e.server.httpErrors.unauthorized("Unauthorised");
151
+ let { body: n, data: r, title: i, userId: a } = e.body, o = await new C(e.config, e.slonik, e.dbSchema).getByUserId(a);
152
+ if (!o || o.length === 0) throw e.server.httpErrors.unprocessableEntity("No devices found for the receiver");
153
+ let s = o.map((e) => e.deviceToken);
154
+ await v({
155
+ android: {
156
+ notification: { sound: "default" },
157
+ priority: "high"
158
+ },
159
+ apns: { payload: { aps: { sound: "default" } } },
160
+ data: {
161
+ ...r,
162
+ body: n,
163
+ title: i
164
+ },
165
+ notification: {
166
+ body: n,
167
+ title: i
168
+ },
169
+ tokens: s
170
+ }), t.send({ message: "Notification sent successfully" });
171
+ } }, T = {
172
+ body: {
173
+ properties: {
174
+ body: { type: "string" },
175
+ title: { type: "string" },
176
+ userId: { type: "string" }
177
+ },
178
+ required: [
179
+ "title",
180
+ "body",
181
+ "userId"
182
+ ],
183
+ type: "object"
184
+ },
185
+ description: "Send a notification to a specific user",
186
+ operationId: "sendNotification",
187
+ response: {
188
+ 200: {
189
+ properties: {
190
+ message: { type: "string" },
191
+ success: { type: "boolean" }
192
+ },
193
+ type: "object"
194
+ },
195
+ 401: {
196
+ $ref: "ErrorResponse#",
197
+ description: "Unauthorized"
198
+ },
199
+ 500: { $ref: "ErrorResponse#" }
200
+ },
201
+ tags: ["notifications"]
202
+ }, E = async (e) => {
203
+ let t = e.config.firebase.handlers?.notification, n = e.config.firebase.notification;
204
+ n?.test?.enabled && e.post(n.test.path || "/send-notification", {
205
+ preHandler: [e.verifySession(), x(e)],
206
+ schema: T
207
+ }, t?.sendNotification || w.sendNotification);
208
+ }, D = {
209
+ Mutation: { sendNotification: async (e, t, n) => {
210
+ let { app: r, config: i, database: a, dbSchema: o, user: s } = n;
211
+ if (!s) return new u.ErrorWithProps("unauthorized", {}, 401);
212
+ if (i.firebase.enabled === !1) return new u.ErrorWithProps("Firebase is not enabled", {}, 404);
213
+ try {
214
+ let { body: e, data: n, title: r, userId: s } = t.data;
215
+ if (!s) return new u.ErrorWithProps("Receiver id is required", {}, 400);
216
+ let c = await new C(i, a, o).getByUserId(s);
217
+ if (!c || c.length === 0) return new u.ErrorWithProps("Receiver device not found", {}, 404);
218
+ let l = c.map((e) => e.deviceToken);
219
+ return await v({
220
+ data: n,
221
+ notification: {
222
+ body: e,
223
+ title: r
224
+ },
225
+ tokens: l
226
+ }), { message: "Notification sent successfully" };
227
+ } catch (e) {
228
+ return r.log.error(e), new u.ErrorWithProps("Oops, Something went wrong", {}, 500);
229
+ }
230
+ } },
231
+ Query: {}
232
+ }, O = {
233
+ addUserDevice: async (e, t) => {
234
+ let { body: n, config: r, dbSchema: i, slonik: a, user: o } = e;
235
+ if (!o) throw e.server.httpErrors.unauthorized("Unauthorised");
236
+ let { deviceToken: s } = n, c = new C(r, a, i);
237
+ t.send(await c.create({
238
+ deviceToken: s,
239
+ userId: o.id
240
+ }));
241
+ },
242
+ removeUserDevice: async (e, t) => {
243
+ let n = e.user;
244
+ if (!n) throw e.server.httpErrors.unauthorized("Unauthorised");
245
+ let { deviceToken: r } = e.body, i = new C(e.config, e.slonik, e.dbSchema), a = await i.getByUserId(n.id);
246
+ if (!a || a.length === 0) throw e.server.httpErrors.notFound("No devices found for the user");
247
+ if (!a.find((e) => e.deviceToken === r)) throw e.server.httpErrors.unprocessableEntity("Device requested to delete not owned by user");
248
+ t.send(await i.removeByDeviceToken(r));
249
+ }
250
+ }, k = {
251
+ properties: {
252
+ createdAt: { type: "number" },
253
+ deviceToken: { type: "string" },
254
+ updatedAt: { type: "number" },
255
+ userId: { type: "string" }
256
+ },
257
+ required: [
258
+ "userId",
259
+ "deviceToken",
260
+ "createdAt",
261
+ "updatedAt"
262
+ ],
263
+ type: "object"
264
+ }, A = {
265
+ body: {
266
+ properties: { deviceToken: { type: "string" } },
267
+ required: ["deviceToken"],
268
+ type: "object"
269
+ },
270
+ description: "Delete a user device by device token",
271
+ operationId: "deleteUserDevice",
272
+ response: {
273
+ 200: {
274
+ ...k,
275
+ nullable: !0
276
+ },
277
+ 401: {
278
+ $ref: "ErrorResponse#",
279
+ description: "Unauthorized"
280
+ },
281
+ 500: { $ref: "ErrorResponse#" }
282
+ },
283
+ tags: ["user-devices"]
226
284
  }, j = {
227
- sendNotification: async (e, r, t) => {
228
- const { app: s, config: i, database: o, dbSchema: d, user: c } = t;
229
- if (!c)
230
- return new a.ErrorWithProps("unauthorized", {}, 401);
231
- if (i.firebase.enabled === !1)
232
- return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
233
- try {
234
- const { body: n, data: u, title: p, userId: f } = r.data;
235
- if (!f)
236
- return new a.ErrorWithProps("Receiver id is required", {}, 400);
237
- const h = await new v(
238
- i,
239
- o,
240
- d
241
- ).getByUserId(f);
242
- if (!h || h.length === 0)
243
- return new a.ErrorWithProps(
244
- "Receiver device not found",
245
- {},
246
- 404
247
- );
248
- const U = h.map(
249
- (w) => w.deviceToken
250
- );
251
- return await T({
252
- data: u,
253
- notification: {
254
- body: n,
255
- title: p
256
- },
257
- tokens: U
258
- }), { message: "Notification sent successfully" };
259
- } catch (n) {
260
- return s.log.error(n), new a.ErrorWithProps(
261
- "Oops, Something went wrong",
262
- {},
263
- 500
264
- );
265
- }
266
- }
267
- }, V = {}, ce = { Mutation: j, Query: V }, q = async (e, r) => {
268
- const { body: t, config: s, dbSchema: i, slonik: o, user: d } = e;
269
- if (!d)
270
- throw e.server.httpErrors.unauthorized("Unauthorised");
271
- const { deviceToken: c } = t, n = new v(s, o, i);
272
- r.send(await n.create({ deviceToken: c, userId: d.id }));
273
- }, H = async (e, r) => {
274
- const t = e.user;
275
- if (!t)
276
- throw e.server.httpErrors.unauthorized("Unauthorised");
277
- const { deviceToken: s } = e.body, i = new v(e.config, e.slonik, e.dbSchema), o = await i.getByUserId(t.id);
278
- if (!o || o.length === 0)
279
- throw e.server.httpErrors.notFound("No devices found for the user");
280
- if (!o.find(
281
- (c) => c.deviceToken === s
282
- ))
283
- throw e.server.httpErrors.unprocessableEntity(
284
- "Device requested to delete not owned by user"
285
- );
286
- r.send(await i.removeByDeviceToken(s));
287
- }, E = {
288
- addUserDevice: q,
289
- removeUserDevice: H
290
- }, S = {
291
- properties: {
292
- createdAt: { type: "number" },
293
- deviceToken: { type: "string" },
294
- updatedAt: { type: "number" },
295
- userId: { type: "string" }
296
- },
297
- required: ["userId", "deviceToken", "createdAt", "updatedAt"],
298
- type: "object"
299
- }, Q = {
300
- body: {
301
- properties: {
302
- deviceToken: { type: "string" }
303
- },
304
- required: ["deviceToken"],
305
- type: "object"
306
- },
307
- description: "Delete a user device by device token",
308
- operationId: "deleteUserDevice",
309
- response: {
310
- 200: {
311
- ...S,
312
- nullable: !0
313
- },
314
- 401: {
315
- $ref: "ErrorResponse#",
316
- description: "Unauthorized"
317
- },
318
- 500: {
319
- $ref: "ErrorResponse#"
320
- }
321
- },
322
- tags: ["user-devices"]
323
- }, Y = {
324
- body: {
325
- properties: {
326
- deviceToken: { type: "string" }
327
- },
328
- required: ["deviceToken"],
329
- type: "object"
330
- },
331
- description: "Register a new user device",
332
- operationId: "postUserDevice",
333
- response: {
334
- 200: {
335
- ...S,
336
- nullable: !0
337
- },
338
- 401: {
339
- $ref: "ErrorResponse#",
340
- description: "Unauthorized"
341
- },
342
- 500: {
343
- $ref: "ErrorResponse#"
344
- }
345
- },
346
- tags: ["user-devices"]
347
- }, G = async (e) => {
348
- const r = e.config.firebase.handlers?.userDevice;
349
- e.post(
350
- $,
351
- {
352
- preHandler: [e.verifySession(), b(e)],
353
- schema: Y
354
- },
355
- r?.addUserDevice || E.addUserDevice
356
- ), e.delete(
357
- _,
358
- {
359
- preHandler: [e.verifySession(), b(e)],
360
- schema: Q
361
- },
362
- r?.removeUserDevice || E.removeUserDevice
363
- );
364
- }, K = {
365
- addUserDevice: async (e, r, t) => {
366
- const { app: s, config: i, database: o, dbSchema: d, user: c } = t;
367
- if (i.firebase.enabled === !1)
368
- return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
369
- if (!c)
370
- return new a.ErrorWithProps("unauthorized", {}, 401);
371
- try {
372
- const { deviceToken: n } = r.data;
373
- return await new v(i, o, d).create({ deviceToken: n, userId: c.id });
374
- } catch (n) {
375
- return s.log.error(n), new a.ErrorWithProps(
376
- "Oops, Something went wrong",
377
- {},
378
- 500
379
- );
380
- }
381
- },
382
- removeUserDevice: async (e, r, t) => {
383
- const { app: s, config: i, database: o, dbSchema: d, user: c } = t;
384
- if (i.firebase.enabled === !1)
385
- return new a.ErrorWithProps("Firebase is not enabled", {}, 404);
386
- if (!c)
387
- return new a.ErrorWithProps("unauthorized", {}, 401);
388
- try {
389
- const { deviceToken: n } = r.data, u = new v(i, o, d), p = await u.getByUserId(c.id);
390
- return !p || p.length === 0 ? new a.ErrorWithProps(
391
- "No devices found for user",
392
- {},
393
- 403
394
- ) : p.find(
395
- (y) => y.deviceToken === n
396
- ) ? await u.removeByDeviceToken(n) : new a.ErrorWithProps(
397
- "Device requested to delete not owned by user",
398
- {},
399
- 403
400
- );
401
- } catch (n) {
402
- return s.log.error(n), new a.ErrorWithProps(
403
- "Oops, Something went wrong",
404
- {},
405
- 500
406
- );
407
- }
408
- }
409
- }, X = {}, de = { Mutation: K, Query: X }, J = async (e, r) => {
410
- await e.connect(async (t) => {
411
- await t.query(C(r));
412
- });
413
- }, Z = async (e) => {
414
- const { config: r, log: t, slonik: s } = e;
415
- r.firebase.enabled === !1 ? t.info("fastify-firebase plugin is not enabled") : (t.info("Registering fastify-firebase plugin"), await J(s, r), M(r, e));
416
- const { routePrefix: i, routes: o } = r.firebase;
417
- o?.notifications?.disabled || await e.register(x, { prefix: i }), o?.userDevices?.disabled || await e.register(G, { prefix: i });
418
- }, ue = F(Z);
419
- export {
420
- A as ROUTE_SEND_NOTIFICATION,
421
- $ as ROUTE_USER_DEVICE_ADD,
422
- _ as ROUTE_USER_DEVICE_REMOVE,
423
- D as TABLE_USER_DEVICES,
424
- v as UserDeviceService,
425
- C as createUserDevicesTableQuery,
426
- ue as default,
427
- ae as firebaseSchema,
428
- M as initializeFirebase,
429
- ce as notificationResolver,
430
- x as notificationRoutes,
431
- T as sendPushNotification,
432
- de as userDeviceResolver,
433
- G as userDeviceRoutes
434
- };
285
+ body: {
286
+ properties: { deviceToken: { type: "string" } },
287
+ required: ["deviceToken"],
288
+ type: "object"
289
+ },
290
+ description: "Register a new user device",
291
+ operationId: "postUserDevice",
292
+ response: {
293
+ 200: {
294
+ ...k,
295
+ nullable: !0
296
+ },
297
+ 401: {
298
+ $ref: "ErrorResponse#",
299
+ description: "Unauthorized"
300
+ },
301
+ 500: { $ref: "ErrorResponse#" }
302
+ },
303
+ tags: ["user-devices"]
304
+ }, M = async (e) => {
305
+ let t = e.config.firebase.handlers?.userDevice;
306
+ e.post(p, {
307
+ preHandler: [e.verifySession(), x(e)],
308
+ schema: j
309
+ }, t?.addUserDevice || O.addUserDevice), e.delete(m, {
310
+ preHandler: [e.verifySession(), x(e)],
311
+ schema: A
312
+ }, t?.removeUserDevice || O.removeUserDevice);
313
+ }, N = {
314
+ Mutation: {
315
+ addUserDevice: async (e, t, n) => {
316
+ let { app: r, config: i, database: a, dbSchema: o, user: s } = n;
317
+ if (i.firebase.enabled === !1) return new u.ErrorWithProps("Firebase is not enabled", {}, 404);
318
+ if (!s) return new u.ErrorWithProps("unauthorized", {}, 401);
319
+ try {
320
+ let { deviceToken: e } = t.data;
321
+ return await new C(i, a, o).create({
322
+ deviceToken: e,
323
+ userId: s.id
324
+ });
325
+ } catch (e) {
326
+ return r.log.error(e), new u.ErrorWithProps("Oops, Something went wrong", {}, 500);
327
+ }
328
+ },
329
+ removeUserDevice: async (e, t, n) => {
330
+ let { app: r, config: i, database: a, dbSchema: o, user: s } = n;
331
+ if (i.firebase.enabled === !1) return new u.ErrorWithProps("Firebase is not enabled", {}, 404);
332
+ if (!s) return new u.ErrorWithProps("unauthorized", {}, 401);
333
+ try {
334
+ let { deviceToken: e } = t.data, n = new C(i, a, o), r = await n.getByUserId(s.id);
335
+ return !r || r.length === 0 ? new u.ErrorWithProps("No devices found for user", {}, 403) : r.find((t) => t.deviceToken === e) ? await n.removeByDeviceToken(e) : new u.ErrorWithProps("Device requested to delete not owned by user", {}, 403);
336
+ } catch (e) {
337
+ return r.log.error(e), new u.ErrorWithProps("Oops, Something went wrong", {}, 500);
338
+ }
339
+ }
340
+ },
341
+ Query: {}
342
+ }, P = async (e, t) => {
343
+ await e.connect(async (e) => {
344
+ await e.query(b(t));
345
+ });
346
+ }, F = d(async (e) => {
347
+ let { config: t, log: n, slonik: r } = e;
348
+ t.firebase.enabled === !1 ? n.info("fastify-firebase plugin is not enabled") : (n.info("Registering fastify-firebase plugin"), await P(r, t), _(t, e));
349
+ let { routePrefix: i, routes: a } = t.firebase;
350
+ a?.notifications?.disabled || await e.register(E, { prefix: i }), a?.userDevices?.disabled || await e.register(M, { prefix: i });
351
+ });
352
+ //#endregion
353
+ export { f as ROUTE_SEND_NOTIFICATION, p as ROUTE_USER_DEVICE_ADD, m as ROUTE_USER_DEVICE_REMOVE, h as TABLE_USER_DEVICES, C as UserDeviceService, b as createUserDevicesTableQuery, F as default, i as firebaseAdmin, g as firebaseSchema, a as getAppCheck, _ as initializeFirebase, D as notificationResolver, E as notificationRoutes, v as sendPushNotification, N as userDeviceResolver, M as userDeviceRoutes, y as verifyFirebaseAppCheck };