vintasend-prisma 0.3.0 → 0.4.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,399 +1,406 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PrismaNotificationBackendFactory = exports.PrismaNotificationBackend = exports.NotificationTypeEnum = exports.NotificationStatusEnum = void 0;
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ exports.PrismaNotificationBackendFactory =
3
+ exports.PrismaNotificationBackend =
4
+ exports.NotificationTypeEnum =
5
+ exports.NotificationStatusEnum =
6
+ void 0;
4
7
  exports.NotificationStatusEnum = {
5
- PENDING_SEND: 'PENDING_SEND',
6
- SENT: 'SENT',
7
- FAILED: 'FAILED',
8
- READ: 'READ',
9
- CANCELLED: 'CANCELLED',
8
+ PENDING_SEND: 'PENDING_SEND',
9
+ SENT: 'SENT',
10
+ FAILED: 'FAILED',
11
+ READ: 'READ',
12
+ CANCELLED: 'CANCELLED',
10
13
  };
11
14
  exports.NotificationTypeEnum = {
12
- EMAIL: 'EMAIL',
13
- PUSH: 'PUSH',
14
- SMS: 'SMS',
15
- IN_APP: 'IN_APP',
15
+ EMAIL: 'EMAIL',
16
+ PUSH: 'PUSH',
17
+ SMS: 'SMS',
18
+ IN_APP: 'IN_APP',
16
19
  };
17
20
  function convertJsonValueToRecord(jsonValue) {
18
- if (typeof jsonValue === 'object' && !Array.isArray(jsonValue)) {
19
- return jsonValue;
20
- }
21
- throw new Error('Invalid JSON value. It should be an object.');
21
+ if (typeof jsonValue === 'object' && !Array.isArray(jsonValue)) {
22
+ return jsonValue;
23
+ }
24
+ throw new Error('Invalid JSON value. It should be an object.');
22
25
  }
23
26
  class PrismaNotificationBackend {
24
- constructor(prismaClient) {
25
- this.prismaClient = prismaClient;
26
- }
27
- /**
28
- * Serialize a Prisma notification model to either DatabaseNotification or DatabaseOneOffNotification
29
- * based on whether it has a userId or not
30
- */
31
- serializeNotification(notification) {
32
- const baseData = {
33
- id: notification.id,
34
- notificationType: notification.notificationType,
35
- title: notification.title,
36
- bodyTemplate: notification.bodyTemplate,
37
- contextName: notification.contextName,
38
- contextParameters: notification.contextParameters
39
- ? notification.contextParameters
40
- : {},
41
- sendAfter: notification.sendAfter,
42
- subjectTemplate: notification.subjectTemplate,
43
- status: notification.status,
44
- contextUsed: notification.contextUsed,
45
- extraParams: notification.extraParams
46
- ? convertJsonValueToRecord(notification.extraParams)
47
- : null,
48
- adapterUsed: notification.adapterUsed,
49
- sentAt: notification.sentAt,
50
- readAt: notification.readAt,
51
- createdAt: notification.createdAt,
52
- updatedAt: notification.updatedAt,
53
- };
54
- // Check if this is a one-off notification (has emailOrPhone but no userId)
55
- if (notification.emailOrPhone && !notification.userId) {
56
- return {
57
- ...baseData,
58
- emailOrPhone: notification.emailOrPhone,
59
- firstName: notification.firstName || '',
60
- lastName: notification.lastName || '',
61
- };
62
- }
63
- // Regular notification with userId
64
- return {
65
- ...baseData,
66
- userId: notification.userId,
67
- };
68
- }
69
- deserializeNotification(notification) {
70
- return {
71
- user: {
72
- connect: {
73
- id: notification.userId,
74
- },
75
- },
76
- notificationType: notification.notificationType,
77
- title: notification.title,
78
- bodyTemplate: notification.bodyTemplate,
79
- contextName: notification.contextName,
80
- contextParameters: notification.contextParameters,
81
- sendAfter: notification.sendAfter,
82
- subjectTemplate: notification.subjectTemplate,
83
- extraParams: notification.extraParams,
84
- };
85
- }
86
- deserializeNotificationForUpdate(notification) {
87
- return {
88
- ...(notification.userId ? { user: { connect: { id: notification.userId } } } : {}),
89
- ...(notification.notificationType
90
- ? {
91
- notificationType: exports.NotificationTypeEnum[notification.notificationType],
92
- }
93
- : {}),
94
- ...(notification.title ? { title: notification.title } : {}),
95
- ...(notification.bodyTemplate ? { bodyTemplate: notification.bodyTemplate } : {}),
96
- ...(notification.contextName ? { contextName: notification.contextName } : {}),
97
- ...(notification.contextParameters
98
- ? {
99
- contextParameters: notification.contextParameters ? notification.contextParameters : {},
100
- }
101
- : {}),
102
- ...(notification.sendAfter ? { sendAfter: notification.sendAfter } : {}),
103
- ...(notification.subjectTemplate ? { subjectTemplate: notification.subjectTemplate } : {}),
104
- };
105
- }
106
- async getAllPendingNotifications() {
107
- const notifications = await this.prismaClient.notification.findMany({
108
- where: { status: exports.NotificationStatusEnum.PENDING_SEND },
109
- });
110
- return notifications.map((n) => this.serializeNotification(n));
111
- }
112
- async getPendingNotifications(page = 0, pageSize = 100) {
113
- const notifications = await this.prismaClient.notification.findMany({
114
- where: {
115
- status: exports.NotificationStatusEnum.PENDING_SEND,
116
- sendAfter: null,
117
- },
118
- skip: page * pageSize,
119
- take: pageSize,
120
- });
121
- return notifications.map((n) => this.serializeNotification(n));
122
- }
123
- async getAllFutureNotifications() {
124
- const notifications = await this.prismaClient.notification.findMany({
125
- where: {
126
- status: { not: exports.NotificationStatusEnum.PENDING_SEND },
127
- sendAfter: { lte: new Date() },
128
- },
129
- });
130
- return notifications.map((n) => this.serializeNotification(n));
131
- }
132
- async getFutureNotifications(page = 0, pageSize = 100) {
133
- const notifications = await this.prismaClient.notification.findMany({
134
- where: {
135
- status: { not: exports.NotificationStatusEnum.PENDING_SEND },
136
- sendAfter: { lte: new Date() },
137
- },
138
- skip: page * pageSize,
139
- take: pageSize,
140
- });
141
- return notifications.map((n) => this.serializeNotification(n));
142
- }
143
- async getAllFutureNotificationsFromUser(userId) {
144
- const notifications = await this.prismaClient.notification.findMany({
145
- where: {
146
- userId,
147
- status: {
148
- not: exports.NotificationStatusEnum.PENDING_SEND,
149
- },
150
- sendAfter: {
151
- lte: new Date(),
152
- },
153
- },
154
- });
155
- // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
156
- return notifications.map((n) => this.serializeNotification(n));
157
- }
158
- async getFutureNotificationsFromUser(userId, page, pageSize) {
159
- const notifications = await this.prismaClient.notification.findMany({
160
- where: {
161
- userId,
162
- status: {
163
- not: exports.NotificationStatusEnum.PENDING_SEND,
164
- },
165
- sendAfter: {
166
- lte: new Date(),
167
- },
168
- },
169
- skip: page * pageSize,
170
- take: pageSize,
171
- });
172
- // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
173
- return notifications.map((n) => this.serializeNotification(n));
174
- }
175
- async getAllNotifications() {
176
- const notifications = await this.prismaClient.notification.findMany({});
177
- return notifications.map((n) => this.serializeNotification(n));
178
- }
179
- async getNotifications(page, pageSize) {
180
- const notifications = await this.prismaClient.notification.findMany({
181
- skip: page * pageSize,
182
- take: pageSize,
183
- });
184
- return notifications.map((n) => this.serializeNotification(n));
185
- }
186
- async persistNotification(notification) {
187
- const created = await this.prismaClient.notification.create({
188
- data: this.deserializeNotification(notification),
189
- });
190
- // persistNotification takes NotificationInput which always has userId, so this is always a regular notification
191
- return this.serializeNotification(created);
27
+ constructor(prismaClient) {
28
+ this.prismaClient = prismaClient;
29
+ }
30
+ /**
31
+ * Serialize a Prisma notification model to either DatabaseNotification or DatabaseOneOffNotification
32
+ * based on whether it has a userId or not
33
+ */
34
+ serializeNotification(notification) {
35
+ const baseData = {
36
+ id: notification.id,
37
+ notificationType: notification.notificationType,
38
+ title: notification.title,
39
+ bodyTemplate: notification.bodyTemplate,
40
+ contextName: notification.contextName,
41
+ contextParameters: notification.contextParameters ? notification.contextParameters : {},
42
+ sendAfter: notification.sendAfter,
43
+ subjectTemplate: notification.subjectTemplate,
44
+ status: notification.status,
45
+ contextUsed: notification.contextUsed,
46
+ extraParams: notification.extraParams
47
+ ? convertJsonValueToRecord(notification.extraParams)
48
+ : null,
49
+ adapterUsed: notification.adapterUsed,
50
+ sentAt: notification.sentAt,
51
+ readAt: notification.readAt,
52
+ createdAt: notification.createdAt,
53
+ updatedAt: notification.updatedAt,
54
+ };
55
+ // Check if this is a one-off notification (has emailOrPhone but no userId)
56
+ if (notification.emailOrPhone && !notification.userId) {
57
+ return {
58
+ ...baseData,
59
+ emailOrPhone: notification.emailOrPhone,
60
+ firstName: notification.firstName || '',
61
+ lastName: notification.lastName || '',
62
+ };
192
63
  }
193
- async persistNotificationUpdate(notificationId, notification) {
194
- const updated = await this.prismaClient.notification.update({
195
- where: {
196
- id: notificationId,
197
- },
198
- data: this.deserializeNotificationForUpdate(notification),
199
- });
200
- // persistNotificationUpdate takes Partial<DatabaseNotification> which has userId, so this is always a regular notification
201
- return this.serializeNotification(updated);
64
+ // Regular notification with userId
65
+ return {
66
+ ...baseData,
67
+ userId: notification.userId,
68
+ };
69
+ }
70
+ deserializeNotification(notification) {
71
+ return {
72
+ user: {
73
+ connect: {
74
+ id: notification.userId,
75
+ },
76
+ },
77
+ notificationType: notification.notificationType,
78
+ title: notification.title,
79
+ bodyTemplate: notification.bodyTemplate,
80
+ contextName: notification.contextName,
81
+ contextParameters: notification.contextParameters,
82
+ sendAfter: notification.sendAfter,
83
+ subjectTemplate: notification.subjectTemplate,
84
+ extraParams: notification.extraParams,
85
+ };
86
+ }
87
+ deserializeNotificationForUpdate(notification) {
88
+ return {
89
+ ...(notification.userId ? { user: { connect: { id: notification.userId } } } : {}),
90
+ ...(notification.notificationType
91
+ ? {
92
+ notificationType: exports.NotificationTypeEnum[notification.notificationType],
93
+ }
94
+ : {}),
95
+ ...(notification.title ? { title: notification.title } : {}),
96
+ ...(notification.bodyTemplate ? { bodyTemplate: notification.bodyTemplate } : {}),
97
+ ...(notification.contextName ? { contextName: notification.contextName } : {}),
98
+ ...(notification.contextParameters
99
+ ? {
100
+ contextParameters: notification.contextParameters ? notification.contextParameters : {},
101
+ }
102
+ : {}),
103
+ ...(notification.sendAfter ? { sendAfter: notification.sendAfter } : {}),
104
+ ...(notification.subjectTemplate ? { subjectTemplate: notification.subjectTemplate } : {}),
105
+ };
106
+ }
107
+ async getAllPendingNotifications() {
108
+ const notifications = await this.prismaClient.notification.findMany({
109
+ where: { status: exports.NotificationStatusEnum.PENDING_SEND },
110
+ });
111
+ return notifications.map((n) => this.serializeNotification(n));
112
+ }
113
+ async getPendingNotifications(page = 0, pageSize = 100) {
114
+ const notifications = await this.prismaClient.notification.findMany({
115
+ where: {
116
+ status: exports.NotificationStatusEnum.PENDING_SEND,
117
+ sendAfter: null,
118
+ },
119
+ skip: page * pageSize,
120
+ take: pageSize,
121
+ });
122
+ return notifications.map((n) => this.serializeNotification(n));
123
+ }
124
+ async getAllFutureNotifications() {
125
+ const notifications = await this.prismaClient.notification.findMany({
126
+ where: {
127
+ status: { not: exports.NotificationStatusEnum.PENDING_SEND },
128
+ sendAfter: { lte: new Date() },
129
+ },
130
+ });
131
+ return notifications.map((n) => this.serializeNotification(n));
132
+ }
133
+ async getFutureNotifications(page = 0, pageSize = 100) {
134
+ const notifications = await this.prismaClient.notification.findMany({
135
+ where: {
136
+ status: { not: exports.NotificationStatusEnum.PENDING_SEND },
137
+ sendAfter: { lte: new Date() },
138
+ },
139
+ skip: page * pageSize,
140
+ take: pageSize,
141
+ });
142
+ return notifications.map((n) => this.serializeNotification(n));
143
+ }
144
+ async getAllFutureNotificationsFromUser(userId) {
145
+ const notifications = await this.prismaClient.notification.findMany({
146
+ where: {
147
+ userId,
148
+ status: {
149
+ not: exports.NotificationStatusEnum.PENDING_SEND,
150
+ },
151
+ sendAfter: {
152
+ lte: new Date(),
153
+ },
154
+ },
155
+ });
156
+ // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
157
+ return notifications.map((n) => this.serializeNotification(n));
158
+ }
159
+ async getFutureNotificationsFromUser(userId, page, pageSize) {
160
+ const notifications = await this.prismaClient.notification.findMany({
161
+ where: {
162
+ userId,
163
+ status: {
164
+ not: exports.NotificationStatusEnum.PENDING_SEND,
165
+ },
166
+ sendAfter: {
167
+ lte: new Date(),
168
+ },
169
+ },
170
+ skip: page * pageSize,
171
+ take: pageSize,
172
+ });
173
+ // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
174
+ return notifications.map((n) => this.serializeNotification(n));
175
+ }
176
+ async getAllNotifications() {
177
+ const notifications = await this.prismaClient.notification.findMany({});
178
+ return notifications.map((n) => this.serializeNotification(n));
179
+ }
180
+ async getNotifications(page, pageSize) {
181
+ const notifications = await this.prismaClient.notification.findMany({
182
+ skip: page * pageSize,
183
+ take: pageSize,
184
+ });
185
+ return notifications.map((n) => this.serializeNotification(n));
186
+ }
187
+ async persistNotification(notification) {
188
+ const created = await this.prismaClient.notification.create({
189
+ data: this.deserializeNotification(notification),
190
+ });
191
+ // persistNotification takes NotificationInput which always has userId, so this is always a regular notification
192
+ return this.serializeNotification(created);
193
+ }
194
+ async persistNotificationUpdate(notificationId, notification) {
195
+ const updated = await this.prismaClient.notification.update({
196
+ where: {
197
+ id: notificationId,
198
+ },
199
+ data: this.deserializeNotificationForUpdate(notification),
200
+ });
201
+ // persistNotificationUpdate takes Partial<DatabaseNotification> which has userId, so this is always a regular notification
202
+ return this.serializeNotification(updated);
203
+ }
204
+ /* One-off notification persistence and query methods */
205
+ async persistOneOffNotification(notification) {
206
+ const created = await this.prismaClient.notification.create({
207
+ data: {
208
+ userId: null, // One-off notifications don't have a userId
209
+ emailOrPhone: notification.emailOrPhone,
210
+ firstName: notification.firstName,
211
+ lastName: notification.lastName,
212
+ notificationType: notification.notificationType,
213
+ title: notification.title,
214
+ bodyTemplate: notification.bodyTemplate,
215
+ contextName: notification.contextName,
216
+ contextParameters: notification.contextParameters,
217
+ sendAfter: notification.sendAfter,
218
+ subjectTemplate: notification.subjectTemplate,
219
+ extraParams: notification.extraParams,
220
+ status: exports.NotificationStatusEnum.PENDING_SEND,
221
+ },
222
+ });
223
+ return this.serializeNotification(created);
224
+ }
225
+ async persistOneOffNotificationUpdate(notificationId, notification) {
226
+ const data = {
227
+ ...(notification.emailOrPhone !== undefined
228
+ ? { emailOrPhone: notification.emailOrPhone }
229
+ : {}),
230
+ ...(notification.firstName !== undefined ? { firstName: notification.firstName } : {}),
231
+ ...(notification.lastName !== undefined ? { lastName: notification.lastName } : {}),
232
+ ...(notification.notificationType ? { notificationType: notification.notificationType } : {}),
233
+ ...(notification.title ? { title: notification.title } : {}),
234
+ ...(notification.bodyTemplate ? { bodyTemplate: notification.bodyTemplate } : {}),
235
+ ...(notification.contextName ? { contextName: notification.contextName } : {}),
236
+ ...(notification.contextParameters
237
+ ? { contextParameters: notification.contextParameters }
238
+ : {}),
239
+ ...(notification.sendAfter ? { sendAfter: notification.sendAfter } : {}),
240
+ ...(notification.subjectTemplate ? { subjectTemplate: notification.subjectTemplate } : {}),
241
+ };
242
+ const updated = await this.prismaClient.notification.update({
243
+ where: { id: notificationId },
244
+ data,
245
+ });
246
+ return this.serializeNotification(updated);
247
+ }
248
+ async getOneOffNotification(notificationId, _forUpdate) {
249
+ const notification = await this.prismaClient.notification.findUnique({
250
+ where: {
251
+ id: notificationId,
252
+ },
253
+ });
254
+ if (!notification || !notification.emailOrPhone || notification.userId !== null) {
255
+ return null;
202
256
  }
203
- /* One-off notification persistence and query methods */
204
- async persistOneOffNotification(notification) {
205
- const created = await this.prismaClient.notification.create({
206
- data: {
207
- userId: null, // One-off notifications don't have a userId
208
- emailOrPhone: notification.emailOrPhone,
209
- firstName: notification.firstName,
210
- lastName: notification.lastName,
211
- notificationType: notification.notificationType,
212
- title: notification.title,
213
- bodyTemplate: notification.bodyTemplate,
214
- contextName: notification.contextName,
215
- contextParameters: notification.contextParameters,
216
- sendAfter: notification.sendAfter,
217
- subjectTemplate: notification.subjectTemplate,
218
- extraParams: notification.extraParams,
219
- status: exports.NotificationStatusEnum.PENDING_SEND,
220
- },
221
- });
222
- return this.serializeNotification(created);
257
+ return this.serializeNotification(notification);
258
+ }
259
+ async getAllOneOffNotifications() {
260
+ const notifications = await this.prismaClient.notification.findMany({
261
+ where: {
262
+ userId: null,
263
+ emailOrPhone: { not: null },
264
+ },
265
+ });
266
+ return notifications
267
+ .filter((n) => n.emailOrPhone !== null)
268
+ .map((n) => this.serializeNotification(n));
269
+ }
270
+ async getOneOffNotifications(page, pageSize) {
271
+ const notifications = await this.prismaClient.notification.findMany({
272
+ where: {
273
+ userId: null,
274
+ emailOrPhone: { not: null },
275
+ },
276
+ skip: page * pageSize,
277
+ take: pageSize,
278
+ });
279
+ return notifications
280
+ .filter((n) => n.emailOrPhone !== null)
281
+ .map((n) => this.serializeNotification(n));
282
+ }
283
+ async markAsSent(notificationId, checkIsPending = true) {
284
+ const whereClause = {
285
+ id: notificationId,
286
+ };
287
+ if (checkIsPending) {
288
+ whereClause.status = exports.NotificationStatusEnum.PENDING_SEND;
223
289
  }
224
- async persistOneOffNotificationUpdate(notificationId, notification) {
225
- const data = {
226
- ...(notification.emailOrPhone !== undefined ? { emailOrPhone: notification.emailOrPhone } : {}),
227
- ...(notification.firstName !== undefined ? { firstName: notification.firstName } : {}),
228
- ...(notification.lastName !== undefined ? { lastName: notification.lastName } : {}),
229
- ...(notification.notificationType ? { notificationType: notification.notificationType } : {}),
230
- ...(notification.title ? { title: notification.title } : {}),
231
- ...(notification.bodyTemplate ? { bodyTemplate: notification.bodyTemplate } : {}),
232
- ...(notification.contextName ? { contextName: notification.contextName } : {}),
233
- ...(notification.contextParameters ? { contextParameters: notification.contextParameters } : {}),
234
- ...(notification.sendAfter ? { sendAfter: notification.sendAfter } : {}),
235
- ...(notification.subjectTemplate ? { subjectTemplate: notification.subjectTemplate } : {}),
236
- };
237
- const updated = await this.prismaClient.notification.update({
238
- where: { id: notificationId },
239
- data,
240
- });
241
- return this.serializeNotification(updated);
290
+ const updated = await this.prismaClient.notification.update({
291
+ where: whereClause,
292
+ data: {
293
+ status: exports.NotificationStatusEnum.SENT,
294
+ sentAt: new Date(),
295
+ },
296
+ });
297
+ return this.serializeNotification(updated);
298
+ }
299
+ async markAsFailed(notificationId, checkIsPending = true) {
300
+ const whereClause = {
301
+ id: notificationId,
302
+ };
303
+ if (checkIsPending) {
304
+ whereClause.status = exports.NotificationStatusEnum.PENDING_SEND;
242
305
  }
243
- async getOneOffNotification(notificationId, _forUpdate) {
244
- const notification = await this.prismaClient.notification.findUnique({
245
- where: {
246
- id: notificationId,
247
- },
248
- });
249
- if (!notification || !notification.emailOrPhone || notification.userId !== null) {
250
- return null;
251
- }
252
- return this.serializeNotification(notification);
253
- }
254
- async getAllOneOffNotifications() {
255
- const notifications = await this.prismaClient.notification.findMany({
256
- where: {
257
- userId: null,
258
- emailOrPhone: { not: null },
259
- },
260
- });
261
- return notifications
262
- .filter((n) => n.emailOrPhone !== null)
263
- .map((n) => this.serializeNotification(n));
264
- }
265
- async getOneOffNotifications(page, pageSize) {
266
- const notifications = await this.prismaClient.notification.findMany({
267
- where: {
268
- userId: null,
269
- emailOrPhone: { not: null },
270
- },
271
- skip: page * pageSize,
272
- take: pageSize,
273
- });
274
- return notifications
275
- .filter((n) => n.emailOrPhone !== null)
276
- .map((n) => this.serializeNotification(n));
277
- }
278
- async markAsSent(notificationId, checkIsPending = true) {
279
- const whereClause = {
280
- id: notificationId,
281
- };
282
- if (checkIsPending) {
283
- whereClause.status = exports.NotificationStatusEnum.PENDING_SEND;
284
- }
285
- const updated = await this.prismaClient.notification.update({
286
- where: whereClause,
287
- data: {
288
- status: exports.NotificationStatusEnum.SENT,
289
- sentAt: new Date(),
290
- },
291
- });
292
- return this.serializeNotification(updated);
293
- }
294
- async markAsFailed(notificationId, checkIsPending = true) {
295
- const whereClause = {
296
- id: notificationId,
297
- };
298
- if (checkIsPending) {
299
- whereClause.status = exports.NotificationStatusEnum.PENDING_SEND;
300
- }
301
- const updated = await this.prismaClient.notification.update({
302
- where: whereClause,
303
- data: {
304
- status: exports.NotificationStatusEnum.FAILED,
305
- sentAt: new Date(),
306
- },
307
- });
308
- return this.serializeNotification(updated);
309
- }
310
- async markAsRead(notificationId, checkIsSent = true) {
311
- const whereClause = {
312
- id: notificationId,
313
- };
314
- if (checkIsSent) {
315
- whereClause.status = exports.NotificationStatusEnum.SENT;
316
- }
317
- const updated = await this.prismaClient.notification.update({
318
- where: whereClause,
319
- data: {
320
- status: 'READ',
321
- readAt: new Date(),
322
- },
323
- });
324
- // markAsRead is only for in-app notifications (which are always regular notifications with userId)
325
- return this.serializeNotification(updated);
326
- }
327
- async cancelNotification(notificationId) {
328
- await this.prismaClient.notification.update({
329
- where: {
330
- id: notificationId,
331
- },
332
- data: {
333
- status: exports.NotificationStatusEnum.CANCELLED,
334
- },
335
- });
336
- }
337
- async getNotification(notificationId, _forUpdate) {
338
- const notification = await this.prismaClient.notification.findUnique({
339
- where: { id: notificationId },
340
- });
341
- if (!notification)
342
- return null;
343
- return this.serializeNotification(notification);
344
- }
345
- async filterAllInAppUnreadNotifications(userId) {
346
- const notifications = await this.prismaClient.notification.findMany({
347
- where: {
348
- userId,
349
- status: 'SENT',
350
- readAt: null,
351
- },
352
- });
353
- // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
354
- return notifications.map((n) => this.serializeNotification(n));
355
- }
356
- async filterInAppUnreadNotifications(userId, page, pageSize) {
357
- const notifications = await this.prismaClient.notification.findMany({
358
- where: {
359
- userId,
360
- status: 'SENT',
361
- readAt: null,
362
- },
363
- skip: page * pageSize,
364
- take: pageSize,
365
- });
366
- // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
367
- return notifications.map((n) => this.serializeNotification(n));
368
- }
369
- async getUserEmailFromNotification(notificationId) {
370
- var _a;
371
- const notification = await this.prismaClient.notification.findUnique({
372
- where: {
373
- id: notificationId,
374
- },
375
- include: {
376
- user: true,
377
- },
378
- });
379
- return (_a = notification === null || notification === void 0 ? void 0 : notification.user) === null || _a === void 0 ? void 0 : _a.email;
380
- }
381
- async storeContextUsed(notificationId, context) {
382
- await this.prismaClient.notification.update({
383
- where: { id: notificationId },
384
- data: { contextUsed: context }
385
- });
386
- }
387
- async bulkPersistNotifications(notifications) {
388
- return this.prismaClient.notification.createMany({
389
- data: notifications.map((notification) => this.deserializeNotification(notification)),
390
- });
306
+ const updated = await this.prismaClient.notification.update({
307
+ where: whereClause,
308
+ data: {
309
+ status: exports.NotificationStatusEnum.FAILED,
310
+ sentAt: new Date(),
311
+ },
312
+ });
313
+ return this.serializeNotification(updated);
314
+ }
315
+ async markAsRead(notificationId, checkIsSent = true) {
316
+ const whereClause = {
317
+ id: notificationId,
318
+ };
319
+ if (checkIsSent) {
320
+ whereClause.status = exports.NotificationStatusEnum.SENT;
391
321
  }
322
+ const updated = await this.prismaClient.notification.update({
323
+ where: whereClause,
324
+ data: {
325
+ status: 'READ',
326
+ readAt: new Date(),
327
+ },
328
+ });
329
+ // markAsRead is only for in-app notifications (which are always regular notifications with userId)
330
+ return this.serializeNotification(updated);
331
+ }
332
+ async cancelNotification(notificationId) {
333
+ await this.prismaClient.notification.update({
334
+ where: {
335
+ id: notificationId,
336
+ },
337
+ data: {
338
+ status: exports.NotificationStatusEnum.CANCELLED,
339
+ },
340
+ });
341
+ }
342
+ async getNotification(notificationId, _forUpdate) {
343
+ const notification = await this.prismaClient.notification.findUnique({
344
+ where: { id: notificationId },
345
+ });
346
+ if (!notification) return null;
347
+ return this.serializeNotification(notification);
348
+ }
349
+ async filterAllInAppUnreadNotifications(userId) {
350
+ const notifications = await this.prismaClient.notification.findMany({
351
+ where: {
352
+ userId,
353
+ status: 'SENT',
354
+ readAt: null,
355
+ },
356
+ });
357
+ // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
358
+ return notifications.map((n) => this.serializeNotification(n));
359
+ }
360
+ async filterInAppUnreadNotifications(userId, page, pageSize) {
361
+ const notifications = await this.prismaClient.notification.findMany({
362
+ where: {
363
+ userId,
364
+ status: 'SENT',
365
+ readAt: null,
366
+ },
367
+ skip: page * pageSize,
368
+ take: pageSize,
369
+ });
370
+ // These are guaranteed to be regular notifications (not one-off) since we're filtering by userId
371
+ return notifications.map((n) => this.serializeNotification(n));
372
+ }
373
+ async getUserEmailFromNotification(notificationId) {
374
+ var _a;
375
+ const notification = await this.prismaClient.notification.findUnique({
376
+ where: {
377
+ id: notificationId,
378
+ },
379
+ include: {
380
+ user: true,
381
+ },
382
+ });
383
+ return (_a = notification === null || notification === void 0 ? void 0 : notification.user) ===
384
+ null || _a === void 0
385
+ ? void 0
386
+ : _a.email;
387
+ }
388
+ async storeContextUsed(notificationId, context) {
389
+ await this.prismaClient.notification.update({
390
+ where: { id: notificationId },
391
+ data: { contextUsed: context },
392
+ });
393
+ }
394
+ async bulkPersistNotifications(notifications) {
395
+ return this.prismaClient.notification.createMany({
396
+ data: notifications.map((notification) => this.deserializeNotification(notification)),
397
+ });
398
+ }
392
399
  }
393
400
  exports.PrismaNotificationBackend = PrismaNotificationBackend;
394
401
  class PrismaNotificationBackendFactory {
395
- create(prismaClient) {
396
- return new PrismaNotificationBackend(prismaClient);
397
- }
402
+ create(prismaClient) {
403
+ return new PrismaNotificationBackend(prismaClient);
404
+ }
398
405
  }
399
406
  exports.PrismaNotificationBackendFactory = PrismaNotificationBackendFactory;