@prefabs.tech/fastify-firebase 0.94.0 → 0.95.1
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/README.md +4 -5
- package/dist/prefabs-tech-fastify-firebase.cjs +11 -11
- package/dist/prefabs-tech-fastify-firebase.js +291 -372
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +9 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/index.d.ts +1 -1
- package/dist/types/lib/index.d.ts.map +1 -1
- package/dist/types/lib/initializeFirebase.d.ts +1 -0
- package/dist/types/lib/initializeFirebase.d.ts.map +1 -1
- package/dist/types/middlewares/verifyFirebaseAppCheck.d.ts +4 -0
- package/dist/types/middlewares/verifyFirebaseAppCheck.d.ts.map +1 -0
- package/dist/types/model/notification/handlers/index.d.ts +1 -1
- package/dist/types/model/notification/handlers/sendNotification.d.ts +2 -3
- package/dist/types/model/notification/handlers/sendNotification.d.ts.map +1 -1
- package/dist/types/model/notification/schema.d.ts +1 -1
- package/dist/types/model/userDevice/handlers/addUserDevice.d.ts +2 -3
- package/dist/types/model/userDevice/handlers/addUserDevice.d.ts.map +1 -1
- package/dist/types/model/userDevice/handlers/index.d.ts +2 -2
- package/dist/types/model/userDevice/handlers/removeUserDevice.d.ts +2 -3
- package/dist/types/model/userDevice/handlers/removeUserDevice.d.ts.map +1 -1
- package/package.json +36 -40
- package/LICENSE +0 -21
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
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
|
-
`,
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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 ${
|
|
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 ${
|
|
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
|
-
},
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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:
|
|
119
|
+
${this.getWhereFragment({ filterFragment: s.fragment`device_token = ${e}` })}
|
|
111
120
|
RETURNING *;
|
|
112
121
|
`;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
}
|
|
123
|
+
getFindByUserIdSql(e) {
|
|
124
|
+
return s.type(this.validationSchema)`
|
|
116
125
|
SELECT *
|
|
117
126
|
FROM ${this.tableFragment}
|
|
118
|
-
${this.getWhereFragment({ filterFragment:
|
|
127
|
+
${this.getWhereFragment({ filterFragment: s.fragment`user_id = ${e}` })};
|
|
119
128
|
`;
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
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 };
|