@prefabs.tech/fastify-firebase 0.92.1 → 0.93.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,18 +1,18 @@
1
- import w from "fastify-plugin";
2
- import { apps as I, initializeApp as N, credential as R, messaging as k } from "firebase-admin";
1
+ import I from "fastify-plugin";
2
+ import h from "firebase-admin";
3
3
  import { sql as l } from "slonik";
4
- import { DefaultSqlFactory as F, BaseService as A } from "@prefabs.tech/fastify-slonik";
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 E, mergeTypeDefs as $, baseSchema as _ } from "@prefabs.tech/fastify-graphql";
7
- const P = (e, r) => {
8
- if (!(I.length > 0)) {
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
- N({
15
- credential: R.cert({
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
- }, m = async (e) => {
30
- await k().sendEachForMulticast(e);
31
- }, O = "/send-notification", M = "/user-device", C = "/user-device", D = "user_devices", B = (e) => {
32
- const r = e.firebase.table?.userDevices?.name || D;
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
- }, L = async (e, r) => {
49
+ }, M = async (e, r) => {
50
50
  await e.connect(async (t) => {
51
- await t.query(B(r));
51
+ await t.query(O(r));
52
52
  });
53
53
  };
54
- class W extends F {
55
- static TABLE = D;
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 A {
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 W;
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 z = async (e, r) => {
95
+ const B = async (e, r) => {
96
96
  if (!e.user)
97
97
  throw e.server.httpErrors.unauthorized("Unauthorised");
98
98
  const {
99
- body: n,
99
+ body: s,
100
100
  title: i,
101
101
  data: o,
102
102
  userId: d
103
- } = e.body, s = await new v(
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 (!s || s.length === 0)
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 = s.map((f) => f.deviceToken), p = {
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: n
129
+ body: s
130
130
  },
131
131
  data: {
132
132
  ...o,
133
133
  title: i,
134
- body: n
134
+ body: s
135
135
  }
136
136
  };
137
- await m(p), r.send({ message: "Notification sent successfully" });
138
- }, x = {
139
- sendNotification: z
140
- }, j = {
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
- }, g = (e) => async () => {
169
+ }, b = (e) => async () => {
170
170
  if (e.config.firebase.enabled === !1)
171
171
  throw e.httpErrors.notFound("Firebase is disabled");
172
- }, V = async (e) => {
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 || O,
175
+ t.test.path || $,
176
176
  {
177
- preHandler: [e.verifySession(), g(e)],
178
- schema: j
177
+ preHandler: [e.verifySession(), b(e)],
178
+ schema: W
179
179
  },
180
- r?.addUserDevice || x.sendNotification
180
+ r?.addUserDevice || L.sendNotification
181
181
  );
182
- }, q = async (e, r) => {
183
- const { body: t, config: n, dbSchema: i, slonik: o, user: d } = e;
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, s = new v(n, o, i);
187
- r.send(await s.create({ userId: d.id, deviceToken: c }));
188
- }, H = async (e, r) => {
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: n } = e.body, i = new v(e.config, e.slonik, e.dbSchema), o = await i.getByUserId(t.id);
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 === n
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(n));
202
- }, y = {
203
- addUserDevice: q,
204
- removeUserDevice: H
205
- }, T = {
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
- }, Q = {
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
- ...T,
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
- }, Y = {
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
- ...T,
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
- }, G = async (e) => {
262
+ }, H = async (e) => {
263
263
  const r = e.config.firebase.handlers?.userDevice;
264
264
  e.post(
265
- M,
265
+ _,
266
266
  {
267
- preHandler: [e.verifySession(), g(e)],
268
- schema: Y
267
+ preHandler: [e.verifySession(), b(e)],
268
+ schema: q
269
269
  },
270
- r?.addUserDevice || y.addUserDevice
270
+ r?.addUserDevice || E.addUserDevice
271
271
  ), e.delete(
272
- C,
272
+ P,
273
273
  {
274
- preHandler: [e.verifySession(), g(e)],
275
- schema: Q
274
+ preHandler: [e.verifySession(), b(e)],
275
+ schema: V
276
276
  },
277
- r?.removeUserDevice || y.removeUserDevice
277
+ r?.removeUserDevice || E.removeUserDevice
278
278
  );
279
- }, K = async (e) => {
280
- const { config: r, slonik: t, log: n } = e;
281
- r.firebase.enabled === !1 ? n.info("fastify-firebase plugin is not enabled") : (n.info("Registering fastify-firebase plugin"), await L(t, r), P(r, e));
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(V, { prefix: i }), o?.userDevices?.disabled || await e.register(G, { prefix: i });
284
- }, ue = w(K), X = {
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: n, config: i, dbSchema: o, database: d, user: c } = t;
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: s, title: u, body: p, data: f } = r.data;
293
- if (!s)
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 h = await new v(
295
+ const g = await new v(
296
296
  i,
297
297
  d,
298
298
  o
299
- ).getByUserId(s);
300
- if (!h || h.length === 0)
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 S = {
307
- tokens: h.map(
308
- (U) => U.deviceToken
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 m(S), { message: "Notification sent successfully" };
317
- } catch (s) {
318
- return n.log.error(s), new a.ErrorWithProps(
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
- }, J = {}, pe = { Mutation: X, Query: J }, Z = {
325
+ }, G = {}, ce = { Mutation: Y, Query: G }, K = {
326
326
  addUserDevice: async (e, r, t) => {
327
- const { app: n, config: i, dbSchema: o, database: d, user: c } = t;
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: s } = r.data;
334
- return await new v(i, d, o).create({ userId: c.id, deviceToken: s });
335
- } catch (s) {
336
- return n.log.error(s), new a.ErrorWithProps(
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: n, config: i, dbSchema: o, database: d, user: c } = t;
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: s } = r.data, u = new v(i, d, o), p = await u.getByUserId(c.id);
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
- (b) => b.deviceToken === s
357
- ) ? await u.removeByDeviceToken(s) : new a.ErrorWithProps(
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 (s) {
363
- return n.log.error(s), new a.ErrorWithProps(
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
- }, ee = {}, le = { Mutation: Z, Query: ee }, re = E`
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
- `, te = E`
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
- `, ve = $([
415
- _,
416
- re,
417
- te
414
+ `, ue = k([
415
+ F,
416
+ J,
417
+ Z
418
418
  ]);
419
419
  export {
420
- O as ROUTE_SEND_NOTIFICATION,
421
- M as ROUTE_USER_DEVICE_ADD,
422
- C as ROUTE_USER_DEVICE_REMOVE,
423
- D as TABLE_USER_DEVICES,
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
- B as createUserDevicesTableQuery,
426
- ue as default,
427
- ve as firebaseSchema,
428
- P as initializeFirebase,
429
- pe as notificationResolver,
430
- V as notificationRoutes,
431
- m as sendPushNotification,
432
- le as userDeviceResolver,
433
- G as userDeviceRoutes
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
  };