vintasend 0.1.21 → 0.2.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.
@@ -3,20 +3,23 @@ import type { DatabaseNotification, Notification } from '../../types/notificatio
3
3
  import type { BaseNotificationTypeConfig } from '../../types/notification-type-config';
4
4
  export interface BaseNotificationBackend<Config extends BaseNotificationTypeConfig> {
5
5
  getAllPendingNotifications(): Promise<DatabaseNotification<Config>[]>;
6
- getPendingNotifications(): Promise<DatabaseNotification<Config>[]>;
6
+ getPendingNotifications(page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
7
7
  getAllFutureNotifications(): Promise<DatabaseNotification<Config>[]>;
8
- getFutureNotifications(): Promise<DatabaseNotification<Config>[]>;
9
- getAllFutureNotificationsFromUser(userId: Config["UserIdType"]): Promise<DatabaseNotification<Config>[]>;
10
- getFutureNotificationsFromUser(userId: Config["UserIdType"]): Promise<DatabaseNotification<Config>[]>;
8
+ getFutureNotifications(page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
9
+ getAllFutureNotificationsFromUser(userId: Config['UserIdType']): Promise<DatabaseNotification<Config>[]>;
10
+ getFutureNotificationsFromUser(userId: Config['UserIdType'], page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
11
11
  persistNotification(notification: Omit<Notification<Config>, 'id'>): Promise<DatabaseNotification<Config>>;
12
- persistNotificationUpdate(notificationId: Config["NotificationIdType"], notification: Partial<Omit<Notification<Config>, 'id'>>): Promise<DatabaseNotification<Config>>;
13
- markAsSent(notificationId: Config["NotificationIdType"], checkIsPending: boolean): Promise<DatabaseNotification<Config>>;
14
- markAsFailed(notificationId: Config["NotificationIdType"], checkIsPending: boolean): Promise<DatabaseNotification<Config>>;
15
- markAsRead(notificationId: Config["NotificationIdType"], checkIsSent: boolean): Promise<DatabaseNotification<Config>>;
16
- cancelNotification(notificationId: Config["NotificationIdType"]): Promise<void>;
17
- getNotification(notificationId: Config["NotificationIdType"], forUpdate: boolean): Promise<DatabaseNotification<Config> | null>;
18
- filterAllInAppUnreadNotifications(userId: Config["UserIdType"]): Promise<DatabaseNotification<Config>[]>;
19
- filterInAppUnreadNotifications(userId: Config["UserIdType"], page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
20
- getUserEmailFromNotification(notificationId: Config["NotificationIdType"]): Promise<string | undefined>;
21
- storeContextUsed(notificationId: Config["NotificationIdType"], context: InputJsonValue): Promise<void>;
12
+ getAllNotifications(): Promise<DatabaseNotification<Config>[]>;
13
+ getNotifications(page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
14
+ bulkPersistNotifications(notifications: Omit<Notification<Config>, 'id'>[]): Promise<Config['NotificationIdType'][]>;
15
+ persistNotificationUpdate(notificationId: Config['NotificationIdType'], notification: Partial<Omit<Notification<Config>, 'id'>>): Promise<DatabaseNotification<Config>>;
16
+ markAsSent(notificationId: Config['NotificationIdType'], checkIsPending: boolean): Promise<DatabaseNotification<Config>>;
17
+ markAsFailed(notificationId: Config['NotificationIdType'], checkIsPending: boolean): Promise<DatabaseNotification<Config>>;
18
+ markAsRead(notificationId: Config['NotificationIdType'], checkIsSent: boolean): Promise<DatabaseNotification<Config>>;
19
+ cancelNotification(notificationId: Config['NotificationIdType']): Promise<void>;
20
+ getNotification(notificationId: Config['NotificationIdType'], forUpdate: boolean): Promise<DatabaseNotification<Config> | null>;
21
+ filterAllInAppUnreadNotifications(userId: Config['UserIdType']): Promise<DatabaseNotification<Config>[]>;
22
+ filterInAppUnreadNotifications(userId: Config['UserIdType'], page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
23
+ getUserEmailFromNotification(notificationId: Config['NotificationIdType']): Promise<string | undefined>;
24
+ storeContextUsed(notificationId: Config['NotificationIdType'], context: InputJsonValue): Promise<void>;
22
25
  }
@@ -26,16 +26,18 @@ export declare class VintaSend<Config extends BaseNotificationTypeConfig, Adapte
26
26
  updateNotification(notificationId: Config['NotificationIdType'], notification: Partial<Omit<Notification<Config>, 'id'>>): Promise<DatabaseNotification<Config>>;
27
27
  getAllFutureNotifications(): Promise<DatabaseNotification<Config>[]>;
28
28
  getAllFutureNotificationsFromUser(userId: Config['NotificationIdType']): Promise<DatabaseNotification<Config>[]>;
29
- getFutureNotificationsFromUser(userId: Config['NotificationIdType']): Promise<DatabaseNotification<Config>[]>;
30
- getFutureNotifications(): Promise<DatabaseNotification<Config>[]>;
29
+ getFutureNotificationsFromUser(userId: Config['NotificationIdType'], page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
30
+ getFutureNotifications(page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
31
31
  getNotificationContext<ContextName extends string & keyof Config['ContextMap']>(contextName: ContextName, parameters: Parameters<ReturnType<typeof this.contextGeneratorsMap.getContextGenerator<ContextName>>['generate']>[0]): Promise<JsonObject>;
32
32
  sendPendingNotifications(): Promise<void>;
33
- getPendingNotifications(): Promise<DatabaseNotification<Config>[]>;
33
+ getPendingNotifications(page: number, pageSize: number): Promise<DatabaseNotification<Config>[]>;
34
34
  getNotification(notificationId: Config['NotificationIdType'], forUpdate?: boolean): Promise<DatabaseNotification<Config> | null>;
35
35
  markRead(notificationId: Config['NotificationIdType'], checkIsSent?: boolean): Promise<DatabaseNotification<Config>>;
36
36
  getInAppUnread(userId: Config['NotificationIdType']): Promise<DatabaseNotification<Config>[]>;
37
37
  cancelNotification(notificationId: Config['NotificationIdType']): Promise<void>;
38
38
  resendNotification(notificationId: Config['NotificationIdType'], useStoredContextIfAvailable?: boolean): Promise<DatabaseNotification<Config> | undefined>;
39
39
  delayedSend(notificationId: Config['NotificationIdType']): Promise<void>;
40
+ bulkPersistNotifications(notifications: Omit<Notification<Config>, 'id'>[]): Promise<Config['NotificationIdType'][]>;
41
+ migrateToBackend<DestinationBackend extends BaseNotificationBackend<Config>>(destinationBackend: DestinationBackend, batchSize?: number): Promise<void>;
40
42
  }
41
43
  export {};
@@ -105,9 +105,9 @@ class VintaSend {
105
105
  async createNotification(notification) {
106
106
  const createdNotification = await this.backend.persistNotification(notification);
107
107
  this.logger.error(`Notification ${createdNotification.id} created`);
108
- if (notification.sendAfter && notification.sendAfter <= new Date()) {
109
- this.logger.info(`Notification ${createdNotification.id} sent immediately because sendAfter is in the past`);
110
- this.send(createdNotification);
108
+ if (!notification.sendAfter || notification.sendAfter <= new Date()) {
109
+ this.logger.info(`Notification ${createdNotification.id} sent immediately because sendAfter is null or in the past`);
110
+ await this.send(createdNotification);
111
111
  }
112
112
  else {
113
113
  this.logger.info(`Notification ${createdNotification.id} scheduled for ${notification.sendAfter}`);
@@ -125,11 +125,11 @@ class VintaSend {
125
125
  async getAllFutureNotificationsFromUser(userId) {
126
126
  return this.backend.getAllFutureNotificationsFromUser(userId);
127
127
  }
128
- async getFutureNotificationsFromUser(userId) {
129
- return this.backend.getFutureNotificationsFromUser(userId);
128
+ async getFutureNotificationsFromUser(userId, page, pageSize) {
129
+ return this.backend.getFutureNotificationsFromUser(userId, page, pageSize);
130
130
  }
131
- async getFutureNotifications() {
132
- return this.backend.getFutureNotifications();
131
+ async getFutureNotifications(page, pageSize) {
132
+ return this.backend.getFutureNotifications(page, pageSize);
133
133
  }
134
134
  async getNotificationContext(contextName, parameters) {
135
135
  const context = this.contextGeneratorsMap.getContextGenerator(contextName).generate(parameters);
@@ -142,8 +142,8 @@ class VintaSend {
142
142
  const pendingNotifications = await this.backend.getAllPendingNotifications();
143
143
  await Promise.all(pendingNotifications.map((notification) => this.send(notification)));
144
144
  }
145
- async getPendingNotifications() {
146
- return this.backend.getPendingNotifications();
145
+ async getPendingNotifications(page, pageSize) {
146
+ return this.backend.getPendingNotifications(page, pageSize);
147
147
  }
148
148
  async getNotification(notificationId, forUpdate = false) {
149
149
  return this.backend.getNotification(notificationId, forUpdate);
@@ -258,5 +258,21 @@ class VintaSend {
258
258
  this.logger.error(`Error storing context for notification ${notification.id}: ${storeContextError}`);
259
259
  }
260
260
  }
261
+ async bulkPersistNotifications(notifications) {
262
+ return this.backend.bulkPersistNotifications(notifications);
263
+ }
264
+ async migrateToBackend(destinationBackend, batchSize = 5000) {
265
+ let pageNumber = 0;
266
+ let notifications = await this.backend.getNotifications(pageNumber, batchSize);
267
+ while (notifications.length > 0) {
268
+ pageNumber += 1;
269
+ const notificationsWitoutId = notifications.map((notification) => {
270
+ const { id, ...notificationWithoutId } = notification;
271
+ return notificationWithoutId;
272
+ });
273
+ await destinationBackend.bulkPersistNotifications(notificationsWitoutId);
274
+ notifications = await this.backend.getNotifications(pageNumber, batchSize);
275
+ }
276
+ }
261
277
  }
262
278
  exports.VintaSend = VintaSend;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vintasend",
3
- "version": "0.1.21",
3
+ "version": "0.2.1",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist"