@arkstack/notifications 0.16.11 → 0.16.12

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/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import { Model } from "@arkstack/database";
4
4
  import { User } from "@app/models/User";
5
5
  import { User as User$1 } from "@arkstack/auth";
6
6
  import { PhoneNumber } from "@kanun-hq/plugin-phone";
7
+ import { ArkormCollection, LengthAwarePaginator } from "arkormx";
7
8
 
8
9
  //#region src/Contracts/UserNotification.d.ts
9
10
  declare abstract class UserNotification extends Model {
@@ -357,7 +358,7 @@ declare class RealtimeNotification extends NotificationContract<RealtimeBroadcas
357
358
  * @returns
358
359
  */
359
360
  store(store?: boolean): this;
360
- type(type: DbNotificationType | null): this;
361
+ type(type?: DbNotificationType | null): this;
361
362
  action(text?: string | null, link?: string | null): this;
362
363
  meta(meta?: NotificationData | null): this;
363
364
  private resolveChannel;
@@ -421,12 +422,52 @@ declare class Notification<D extends keyof DriverMap = keyof DriverMap> {
421
422
  //#region src/UserNotificationCenter.d.ts
422
423
  declare class UserNotificationCenter {
423
424
  private static getModel;
425
+ /**
426
+ * Create a database notification then broadcast it using the configured realtime driver
427
+ *
428
+ * @param user
429
+ * @param payload
430
+ */
431
+ static send(user: User, payload: DbNotificationPayload, channel?: string[]): Promise<RealtimeBroadcastResult>;
432
+ /**
433
+ * Create a database notification
434
+ *
435
+ * @param user
436
+ * @param payload
437
+ */
424
438
  static create(user: User, payload: DbNotificationPayload): Promise<UserNotification>;
425
- static forUser(user: User): Promise<import("arkormx").ArkormCollection<UserNotification, UserNotification[]>>;
426
- static unreadForUser(user: User): Promise<import("arkormx").ArkormCollection<UserNotification, UserNotification[]>>;
427
- static markRead(notification: UserNotification | UserNotification['id']): Promise<void>;
428
- static markAllRead(user: User): Promise<void>;
429
- static delete(notification: UserNotification | UserNotification['id']): Promise<void>;
439
+ static forUser(user: User): Promise<ArkormCollection<UserNotification, UserNotification[]>>;
440
+ /**
441
+ * Fetch all the users unread messages
442
+ *
443
+ * @param user
444
+ */
445
+ static unreadForUser(user: User): Promise<ArkormCollection<UserNotification, UserNotification[]>>;
446
+ /**
447
+ * Fetch all the users unread messages with a lenght aware paginator instance
448
+ *
449
+ * @param user
450
+ * @param perPage
451
+ */
452
+ static unreadForUser(user: User, perPage: number): Promise<LengthAwarePaginator<UserNotification>>;
453
+ /**
454
+ * Mark the notification as read
455
+ *
456
+ * @param notification
457
+ */
458
+ static markRead(notification: UserNotification | string | number): Promise<void>;
459
+ /**
460
+ * Mark all unread notifications as read
461
+ *
462
+ * @param user
463
+ */
464
+ static markAllRead(user: User, ids?: string[] | number[]): Promise<void>;
465
+ /**
466
+ * Delete the indicated notifications
467
+ *
468
+ * @param notification
469
+ */
470
+ static delete(notification: UserNotification | string | number | UserNotification[] | string[] | number[]): Promise<void>;
430
471
  }
431
472
  //#endregion
432
473
  //#region src/config.d.ts
package/dist/index.js CHANGED
@@ -29,6 +29,21 @@ var UserNotificationCenter = class {
29
29
  static async getModel() {
30
30
  return await getModel("UserNotification");
31
31
  }
32
+ /**
33
+ * Create a database notification then broadcast it using the configured realtime driver
34
+ *
35
+ * @param user
36
+ * @param payload
37
+ */
38
+ static async send(user, payload, channel = []) {
39
+ return await Notification.realtime().store().channel(user.pushTokens ?? channel).meta(payload.meta).type(payload.type).subject(payload.title).action(payload.actionText, payload.actionLink).send(payload.description, payload.title, void 0, payload);
40
+ }
41
+ /**
42
+ * Create a database notification
43
+ *
44
+ * @param user
45
+ * @param payload
46
+ */
32
47
  static async create(user, payload) {
33
48
  return await (await this.getModel()).query().create({
34
49
  userId: user.id,
@@ -43,12 +58,19 @@ var UserNotificationCenter = class {
43
58
  static async forUser(user) {
44
59
  return await (await this.getModel()).query().where({ userId: user.id }).get();
45
60
  }
46
- static async unreadForUser(user) {
47
- return await (await this.getModel()).query().where({
61
+ static async unreadForUser(user, perPage) {
62
+ const query = (await this.getModel()).query().where({
48
63
  userId: user.id,
49
64
  readAt: null
50
- }).get();
65
+ });
66
+ if (perPage) return await query.paginate();
67
+ return await query.get();
51
68
  }
69
+ /**
70
+ * Mark the notification as read
71
+ *
72
+ * @param notification
73
+ */
52
74
  static async markRead(notification) {
53
75
  const Model = await this.getModel();
54
76
  const id = typeof notification === "object" ? notification.id : notification;
@@ -56,16 +78,33 @@ var UserNotificationCenter = class {
56
78
  await Model.query().where({ id }).update({ readAt });
57
79
  if (typeof notification === "object") notification.readAt = readAt;
58
80
  }
59
- static async markAllRead(user) {
60
- await (await this.getModel()).query().where({
81
+ /**
82
+ * Mark all unread notifications as read
83
+ *
84
+ * @param user
85
+ */
86
+ static async markAllRead(user, ids) {
87
+ const query = (await this.getModel()).query().where({
61
88
  userId: user.id,
62
89
  readAt: null
63
- }).update({ readAt: /* @__PURE__ */ new Date() });
90
+ });
91
+ if (ids) query.whereIn("id", ids);
92
+ await query.update({ readAt: /* @__PURE__ */ new Date() });
64
93
  }
94
+ /**
95
+ * Delete the indicated notifications
96
+ *
97
+ * @param notification
98
+ */
65
99
  static async delete(notification) {
66
100
  const Model = await this.getModel();
67
- const id = typeof notification === "object" ? notification.id : notification;
68
- await Model.query().where({ id }).delete();
101
+ if (Array.isArray(notification)) {
102
+ const ids = notification.map((e) => typeof e === "object" ? e.id : e);
103
+ await Model.query().whereIn("id", ids).delete();
104
+ } else {
105
+ const id = typeof notification === "object" ? notification.id : notification;
106
+ await Model.query().where({ id }).delete();
107
+ }
69
108
  }
70
109
  };
71
110
  //#endregion
@@ -568,7 +607,7 @@ var RealtimeNotification = class extends NotificationContract {
568
607
  read_at: stored?.readAt ? new Date(stored.readAt).toISOString() : null,
569
608
  created_at: stored?.createdAt ? new Date(stored.createdAt).toISOString() : (/* @__PURE__ */ new Date()).toISOString()
570
609
  };
571
- await this.driver.broadcast(channel, this.eventName, payload);
610
+ if (channel && channel.length > 0) await this.driver.broadcast(channel, this.eventName, payload);
572
611
  return {
573
612
  channel,
574
613
  event: this.eventName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkstack/notifications",
3
- "version": "0.16.11",
3
+ "version": "0.16.12",
4
4
  "type": "module",
5
5
  "description": "Framework-agnostic notification module for Arkstack and Nodejs, providing support for multi-channel notification delivery.",
6
6
  "homepage": "https://arkstack.toneflix.net/guide/notifications",
@@ -34,14 +34,14 @@
34
34
  "africastalking": "^0.8.0",
35
35
  "nodemailer": "^8.0.7",
36
36
  "twilio": "^6.0.0",
37
- "@arkstack/common": "^0.16.11"
37
+ "@arkstack/common": "^0.16.12"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@kanun-hq/plugin-phone": "^0.1.8",
41
41
  "firebase-admin": "^13.0.0",
42
42
  "pusher": "^5.2.0",
43
- "@arkstack/contract": "^0.16.11",
44
- "@arkstack/database": "^0.16.11"
43
+ "@arkstack/contract": "^0.16.12",
44
+ "@arkstack/database": "^0.16.12"
45
45
  },
46
46
  "peerDependenciesMeta": {
47
47
  "pusher": {