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