@amohamud23/notihub 1.1.51 → 1.1.53

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.cjs CHANGED
@@ -545,43 +545,87 @@ var Notifications = class _Notifications {
545
545
  throw new Error("Could not create notification");
546
546
  }
547
547
  }
548
- // Additional methods can be added here as needed
548
+ /**
549
+ * Deletes a notification by its ID.
550
+ * @param id - The ID of the notification to delete.
551
+ * @returns A promise that resolves when the notification is deleted.
552
+ */
553
+ static async deleteNotificationById(id) {
554
+ const command = new import_lib_dynamodb4.DeleteCommand({
555
+ TableName: _Notifications.TABLE_NAME,
556
+ Key: { id }
557
+ });
558
+ try {
559
+ await ddbDocClient.send(command);
560
+ } catch (error) {
561
+ console.error("Error deleting notification:", error);
562
+ throw new Error("Could not delete notification");
563
+ }
564
+ }
549
565
  /**
550
566
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
551
567
  * @param notiTypeIds - Array of notiTypeIds to fetch.
552
- * @param limitPerType - Max items to fetch per notiTypeId.
553
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
554
- * @returns An object with merged, sorted notifications and nextPageKeys map.
555
- */
556
- static async getUnifiedFeedByNotiTypeIds(notiTypeIds, limitPerType = 20, exclusiveStartKeys) {
568
+ * @param options - Pagination options
569
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
570
+ */
571
+ static async getUnifiedFeedByNotiTypeIds(notiTypeIds, options = {}) {
572
+ const limit = options.limit || 20;
573
+ const limitPerType = options.limitPerType || limit;
574
+ let exclusiveStartKeys = {};
575
+ if (options.cursor) {
576
+ try {
577
+ exclusiveStartKeys = JSON.parse(
578
+ Buffer.from(options.cursor, "base64").toString("utf-8")
579
+ );
580
+ } catch (error) {
581
+ console.error("Invalid cursor:", error);
582
+ throw new Error("Invalid pagination cursor");
583
+ }
584
+ }
557
585
  const results = await Promise.all(
558
586
  notiTypeIds.map(async (ntid) => {
559
587
  const params = {
560
588
  TableName: this.TABLE_NAME,
561
589
  IndexName: "NotiTypeIdCreatedAtIndex",
562
- // Assuming you have a GSI for notiTypeId
563
590
  KeyConditionExpression: "notiTypeId = :ntid",
564
591
  ExpressionAttributeValues: { ":ntid": ntid },
565
592
  ScanIndexForward: false,
566
593
  Limit: limitPerType,
567
- ExclusiveStartKey: exclusiveStartKeys?.[ntid]
568
- };
569
- const result = await ddbDocClient.send(new import_lib_dynamodb4.QueryCommand(params));
570
- return {
571
- notiTypeId: ntid,
572
- notifications: result.Items || [],
573
- nextPageKey: result.LastEvaluatedKey
594
+ ExclusiveStartKey: exclusiveStartKeys[ntid]
574
595
  };
596
+ try {
597
+ const result = await ddbDocClient.send(new import_lib_dynamodb4.QueryCommand(params));
598
+ return {
599
+ notiTypeId: ntid,
600
+ notifications: result.Items || [],
601
+ nextPageKey: result.LastEvaluatedKey
602
+ };
603
+ } catch (error) {
604
+ console.error(
605
+ `Error fetching notifications for notiTypeId: ${ntid}`,
606
+ error
607
+ );
608
+ return {
609
+ notiTypeId: ntid,
610
+ notifications: [],
611
+ nextPageKey: void 0
612
+ };
613
+ }
575
614
  })
576
615
  );
577
- const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
616
+ const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
578
617
  const nextPageKeys = results.reduce((acc, curr) => {
579
- acc[curr.notiTypeId] = curr.nextPageKey;
618
+ if (curr.nextPageKey) {
619
+ acc[curr.notiTypeId] = curr.nextPageKey;
620
+ }
580
621
  return acc;
581
622
  }, {});
623
+ const hasMore = Object.keys(nextPageKeys).length > 0;
624
+ const cursor = hasMore ? Buffer.from(JSON.stringify(nextPageKeys)).toString("base64") : void 0;
582
625
  return {
583
626
  notifications: mergedNotifications,
584
- nextPageKeys
627
+ cursor,
628
+ hasMore
585
629
  };
586
630
  }
587
631
  };
package/dist/index.d.cts CHANGED
@@ -370,16 +370,26 @@ declare class Notifications {
370
370
  * @returns A promise that resolves to the created notification object.
371
371
  */
372
372
  static createNotification(notification: INotiHubNotification): Promise<INotiHubNotification>;
373
+ /**
374
+ * Deletes a notification by its ID.
375
+ * @param id - The ID of the notification to delete.
376
+ * @returns A promise that resolves when the notification is deleted.
377
+ */
378
+ static deleteNotificationById(id: string): Promise<void>;
373
379
  /**
374
380
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
375
381
  * @param notiTypeIds - Array of notiTypeIds to fetch.
376
- * @param limitPerType - Max items to fetch per notiTypeId.
377
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
378
- * @returns An object with merged, sorted notifications and nextPageKeys map.
379
- */
380
- static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], limitPerType?: number, exclusiveStartKeys?: Record<string, Record<string, any>>): Promise<{
382
+ * @param options - Pagination options
383
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
384
+ */
385
+ static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], options?: {
386
+ limit?: number;
387
+ limitPerType?: number;
388
+ cursor?: string;
389
+ }): Promise<{
381
390
  notifications: INotiHubNotification[];
382
- nextPageKeys: Record<string, Record<string, any> | undefined>;
391
+ cursor?: string;
392
+ hasMore: boolean;
383
393
  }>;
384
394
  }
385
395
 
package/dist/index.d.ts CHANGED
@@ -370,16 +370,26 @@ declare class Notifications {
370
370
  * @returns A promise that resolves to the created notification object.
371
371
  */
372
372
  static createNotification(notification: INotiHubNotification): Promise<INotiHubNotification>;
373
+ /**
374
+ * Deletes a notification by its ID.
375
+ * @param id - The ID of the notification to delete.
376
+ * @returns A promise that resolves when the notification is deleted.
377
+ */
378
+ static deleteNotificationById(id: string): Promise<void>;
373
379
  /**
374
380
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
375
381
  * @param notiTypeIds - Array of notiTypeIds to fetch.
376
- * @param limitPerType - Max items to fetch per notiTypeId.
377
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
378
- * @returns An object with merged, sorted notifications and nextPageKeys map.
379
- */
380
- static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], limitPerType?: number, exclusiveStartKeys?: Record<string, Record<string, any>>): Promise<{
382
+ * @param options - Pagination options
383
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
384
+ */
385
+ static getUnifiedFeedByNotiTypeIds(notiTypeIds: string[], options?: {
386
+ limit?: number;
387
+ limitPerType?: number;
388
+ cursor?: string;
389
+ }): Promise<{
381
390
  notifications: INotiHubNotification[];
382
- nextPageKeys: Record<string, Record<string, any> | undefined>;
391
+ cursor?: string;
392
+ hasMore: boolean;
383
393
  }>;
384
394
  }
385
395
 
package/dist/index.js CHANGED
@@ -453,6 +453,7 @@ var Customer_default = Customer;
453
453
 
454
454
  // src/DynamoModels/Notifications.ts
455
455
  import {
456
+ DeleteCommand as DeleteCommand3,
456
457
  PutCommand as PutCommand3,
457
458
  QueryCommand as QueryCommand3
458
459
  } from "@aws-sdk/lib-dynamodb";
@@ -522,43 +523,87 @@ var Notifications = class _Notifications {
522
523
  throw new Error("Could not create notification");
523
524
  }
524
525
  }
525
- // Additional methods can be added here as needed
526
+ /**
527
+ * Deletes a notification by its ID.
528
+ * @param id - The ID of the notification to delete.
529
+ * @returns A promise that resolves when the notification is deleted.
530
+ */
531
+ static async deleteNotificationById(id) {
532
+ const command = new DeleteCommand3({
533
+ TableName: _Notifications.TABLE_NAME,
534
+ Key: { id }
535
+ });
536
+ try {
537
+ await ddbDocClient.send(command);
538
+ } catch (error) {
539
+ console.error("Error deleting notification:", error);
540
+ throw new Error("Could not delete notification");
541
+ }
542
+ }
526
543
  /**
527
544
  * Fetches and merges paginated notifications for multiple notiTypeIds, sorted by createdAt descending.
528
545
  * @param notiTypeIds - Array of notiTypeIds to fetch.
529
- * @param limitPerType - Max items to fetch per notiTypeId.
530
- * @param exclusiveStartKeys - Optional map of pagination keys per notiTypeId.
531
- * @returns An object with merged, sorted notifications and nextPageKeys map.
532
- */
533
- static async getUnifiedFeedByNotiTypeIds(notiTypeIds, limitPerType = 20, exclusiveStartKeys) {
546
+ * @param options - Pagination options
547
+ * @returns An object with paginated notifications, cursor for next page, and hasMore flag.
548
+ */
549
+ static async getUnifiedFeedByNotiTypeIds(notiTypeIds, options = {}) {
550
+ const limit = options.limit || 20;
551
+ const limitPerType = options.limitPerType || limit;
552
+ let exclusiveStartKeys = {};
553
+ if (options.cursor) {
554
+ try {
555
+ exclusiveStartKeys = JSON.parse(
556
+ Buffer.from(options.cursor, "base64").toString("utf-8")
557
+ );
558
+ } catch (error) {
559
+ console.error("Invalid cursor:", error);
560
+ throw new Error("Invalid pagination cursor");
561
+ }
562
+ }
534
563
  const results = await Promise.all(
535
564
  notiTypeIds.map(async (ntid) => {
536
565
  const params = {
537
566
  TableName: this.TABLE_NAME,
538
567
  IndexName: "NotiTypeIdCreatedAtIndex",
539
- // Assuming you have a GSI for notiTypeId
540
568
  KeyConditionExpression: "notiTypeId = :ntid",
541
569
  ExpressionAttributeValues: { ":ntid": ntid },
542
570
  ScanIndexForward: false,
543
571
  Limit: limitPerType,
544
- ExclusiveStartKey: exclusiveStartKeys?.[ntid]
545
- };
546
- const result = await ddbDocClient.send(new QueryCommand3(params));
547
- return {
548
- notiTypeId: ntid,
549
- notifications: result.Items || [],
550
- nextPageKey: result.LastEvaluatedKey
572
+ ExclusiveStartKey: exclusiveStartKeys[ntid]
551
573
  };
574
+ try {
575
+ const result = await ddbDocClient.send(new QueryCommand3(params));
576
+ return {
577
+ notiTypeId: ntid,
578
+ notifications: result.Items || [],
579
+ nextPageKey: result.LastEvaluatedKey
580
+ };
581
+ } catch (error) {
582
+ console.error(
583
+ `Error fetching notifications for notiTypeId: ${ntid}`,
584
+ error
585
+ );
586
+ return {
587
+ notiTypeId: ntid,
588
+ notifications: [],
589
+ nextPageKey: void 0
590
+ };
591
+ }
552
592
  })
553
593
  );
554
- const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
594
+ const mergedNotifications = results.flatMap((r) => r.notifications).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit);
555
595
  const nextPageKeys = results.reduce((acc, curr) => {
556
- acc[curr.notiTypeId] = curr.nextPageKey;
596
+ if (curr.nextPageKey) {
597
+ acc[curr.notiTypeId] = curr.nextPageKey;
598
+ }
557
599
  return acc;
558
600
  }, {});
601
+ const hasMore = Object.keys(nextPageKeys).length > 0;
602
+ const cursor = hasMore ? Buffer.from(JSON.stringify(nextPageKeys)).toString("base64") : void 0;
559
603
  return {
560
604
  notifications: mergedNotifications,
561
- nextPageKeys
605
+ cursor,
606
+ hasMore
562
607
  };
563
608
  }
564
609
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amohamud23/notihub",
3
- "version": "1.1.51",
3
+ "version": "1.1.53",
4
4
  "description": "Notihub Package",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",