@spfn/notification 0.1.0-beta.24 → 0.1.0-beta.25

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/server.js CHANGED
@@ -621,7 +621,7 @@ function registerBuiltinTemplates() {
621
621
 
622
622
  // src/services/notification.service.ts
623
623
  import { create, createMany, findOne, findMany, updateOne, count, getDatabase } from "@spfn/core/db";
624
- import { desc, eq, and, gte, lte, count as drizzleCount } from "drizzle-orm";
624
+ import { desc, eq, and, gte, lte, inArray, sql, count as drizzleCount } from "drizzle-orm";
625
625
 
626
626
  // src/entities/schema.ts
627
627
  import { createSchema } from "@spfn/core/db";
@@ -799,6 +799,34 @@ async function markNotificationFailed(id3, errorMessage) {
799
799
  }
800
800
  );
801
801
  }
802
+ async function markManySent(items) {
803
+ if (items.length === 0) {
804
+ return;
805
+ }
806
+ const cases = sql.join(
807
+ items.map((it) => sql`when ${it.id} then ${it.providerMessageId ?? null}`),
808
+ sql` `
809
+ );
810
+ await getDatabase("write").update(notifications).set({
811
+ status: "sent",
812
+ sentAt: /* @__PURE__ */ new Date(),
813
+ // ELSE keeps the existing value and anchors the CASE result type to text.
814
+ providerMessageId: sql`case ${notifications.id} ${cases} else ${notifications.providerMessageId} end`
815
+ }).where(inArray(notifications.id, items.map((it) => it.id)));
816
+ }
817
+ async function markManyFailed(items) {
818
+ if (items.length === 0) {
819
+ return;
820
+ }
821
+ const cases = sql.join(
822
+ items.map((it) => sql`when ${it.id} then ${it.errorMessage}`),
823
+ sql` `
824
+ );
825
+ await getDatabase("write").update(notifications).set({
826
+ status: "failed",
827
+ errorMessage: sql`case ${notifications.id} ${cases} else ${notifications.errorMessage} end`
828
+ }).where(inArray(notifications.id, items.map((it) => it.id)));
829
+ }
802
830
  async function markNotificationPending(id3) {
803
831
  return await updateOne(
804
832
  notifications,
@@ -3805,7 +3833,8 @@ async function sendEmailBulk(items, options) {
3805
3833
  for (const { index: index3, result } of earlyFailures) {
3806
3834
  results[index3] = result;
3807
3835
  }
3808
- const historyUpdates = [];
3836
+ const sentItems = [];
3837
+ const failedItems = [];
3809
3838
  for (let i = 0; i < prepared.length; i++) {
3810
3839
  const { index: index3, recipients, subject } = prepared[i];
3811
3840
  const result = sendResults[i];
@@ -3819,13 +3848,17 @@ async function sendEmailBulk(items, options) {
3819
3848
  }
3820
3849
  const historyId = historyRecords[i]?.id;
3821
3850
  if (historyId && isHistoryEnabled()) {
3822
- const promise = result.success ? markNotificationSent(historyId, result.messageId) : markNotificationFailed(historyId, result.error || "Unknown error");
3823
- historyUpdates.push(
3824
- promise.catch((err) => log2.warn("Failed to update notification history", err))
3825
- );
3851
+ if (result.success) {
3852
+ sentItems.push({ id: historyId, providerMessageId: result.messageId });
3853
+ } else {
3854
+ failedItems.push({ id: historyId, errorMessage: result.error || "Unknown error" });
3855
+ }
3826
3856
  }
3827
3857
  }
3828
- await Promise.all(historyUpdates);
3858
+ await Promise.all([
3859
+ markManySent(sentItems).catch((err) => log2.warn("Failed to update notification history", err)),
3860
+ markManyFailed(failedItems).catch((err) => log2.warn("Failed to update notification history", err))
3861
+ ]);
3829
3862
  return { results, successCount, failureCount, batchId };
3830
3863
  }
3831
3864
 
@@ -4100,7 +4133,8 @@ async function sendSMSBulk(items, options) {
4100
4133
  concurrency
4101
4134
  );
4102
4135
  const resultsMap = /* @__PURE__ */ new Map();
4103
- const historyUpdates = [];
4136
+ const sentItems = [];
4137
+ const failedItems = [];
4104
4138
  for (let i = 0; i < prepared.length; i++) {
4105
4139
  const { index: index3, phone } = prepared[i];
4106
4140
  const result = sendResults[i];
@@ -4115,13 +4149,17 @@ async function sendSMSBulk(items, options) {
4115
4149
  }
4116
4150
  const historyId = historyRecords[i]?.id;
4117
4151
  if (historyId && isHistoryEnabled()) {
4118
- const promise = result.success ? markNotificationSent(historyId, result.messageId) : markNotificationFailed(historyId, result.error || "Unknown error");
4119
- historyUpdates.push(
4120
- promise.catch((err) => log4.warn("Failed to update notification history", err))
4121
- );
4152
+ if (result.success) {
4153
+ sentItems.push({ id: historyId, providerMessageId: result.messageId });
4154
+ } else {
4155
+ failedItems.push({ id: historyId, errorMessage: result.error || "Unknown error" });
4156
+ }
4122
4157
  }
4123
4158
  }
4124
- await Promise.all(historyUpdates);
4159
+ await Promise.all([
4160
+ markManySent(sentItems).catch((err) => log4.warn("Failed to update notification history", err)),
4161
+ markManyFailed(failedItems).catch((err) => log4.warn("Failed to update notification history", err))
4162
+ ]);
4125
4163
  const results = new Array(items.length);
4126
4164
  let successCount = 0;
4127
4165
  let failureCount = earlyFailures.length;
@@ -4381,7 +4419,8 @@ async function sendSlackBulk(items, options) {
4381
4419
  for (const { index: index3, result } of earlyFailures) {
4382
4420
  results[index3] = result;
4383
4421
  }
4384
- const historyUpdates = [];
4422
+ const sentItems = [];
4423
+ const failedItems = [];
4385
4424
  for (let i = 0; i < prepared.length; i++) {
4386
4425
  const { index: index3 } = prepared[i];
4387
4426
  const result = sendResults[i];
@@ -4395,13 +4434,17 @@ async function sendSlackBulk(items, options) {
4395
4434
  }
4396
4435
  const historyId = historyRecords[i]?.id;
4397
4436
  if (historyId && isHistoryEnabled()) {
4398
- const promise = result.success ? markNotificationSent(historyId, result.messageId) : markNotificationFailed(historyId, result.error || "Unknown error");
4399
- historyUpdates.push(
4400
- promise.catch((err) => log6.warn("Failed to update notification history", err))
4401
- );
4437
+ if (result.success) {
4438
+ sentItems.push({ id: historyId, providerMessageId: result.messageId });
4439
+ } else {
4440
+ failedItems.push({ id: historyId, errorMessage: result.error || "Unknown error" });
4441
+ }
4402
4442
  }
4403
4443
  }
4404
- await Promise.all(historyUpdates);
4444
+ await Promise.all([
4445
+ markManySent(sentItems).catch((err) => log6.warn("Failed to update notification history", err)),
4446
+ markManyFailed(failedItems).catch((err) => log6.warn("Failed to update notification history", err))
4447
+ ]);
4405
4448
  return { results, successCount, failureCount, batchId };
4406
4449
  }
4407
4450