@adatechnology/notification-contracts 0.4.0 → 0.6.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.
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ __export(index_exports, {
31
31
  EMAIL_ATTACHMENT_MAX_COUNT: () => EMAIL_ATTACHMENT_MAX_COUNT,
32
32
  EMAIL_DRIVER: () => EMAIL_DRIVER,
33
33
  INTRUSIVE_CHANNELS: () => INTRUSIVE_CHANNELS,
34
+ InvalidTemplateDefinitionError: () => InvalidTemplateDefinitionError,
34
35
  InvalidVapidKeysError: () => InvalidVapidKeysError,
35
36
  InvalidWebhookSignatureError: () => InvalidWebhookSignatureError,
36
37
  NOTIFICATION_CATEGORY_HINT: () => NOTIFICATION_CATEGORY_HINT,
@@ -63,11 +64,13 @@ __export(index_exports, {
63
64
  ThrottledError: () => ThrottledError,
64
65
  UnknownTemplateVariablesError: () => UnknownTemplateVariablesError,
65
66
  WebPushEncryptionError: () => WebPushEncryptionError,
67
+ assertTemplateDefinitions: () => assertTemplateDefinitions,
66
68
  assessStandardEmailTemplate: () => assessStandardEmailTemplate,
67
69
  broadcastPushFilterSchema: () => broadcastPushFilterSchema,
68
70
  broadcastPushSchema: () => broadcastPushSchema,
69
71
  buildInitials: () => buildInitials,
70
72
  buildPreviewPayload: () => buildPreviewPayload,
73
+ buildTemplateVariableCatalog: () => buildTemplateVariableCatalog,
71
74
  categoryPolicyEntrySchema: () => categoryPolicyEntrySchema,
72
75
  checkEmailAttachment: () => checkEmailAttachment,
73
76
  createWhatsAppDriverFromChannel: () => createWhatsAppDriverFromChannel,
@@ -76,8 +79,17 @@ __export(index_exports, {
76
79
  diffTemplateVariables: () => diffTemplateVariables,
77
80
  escapeEmailHtml: () => escapeEmailHtml,
78
81
  escapeTemplateHtml: () => escapeTemplateHtml,
82
+ extractCalendarEvent: () => extractCalendarEvent,
79
83
  extractTemplatePlaceholders: () => extractTemplatePlaceholders,
84
+ findTemplateDefinition: () => findTemplateDefinition,
80
85
  findUnsafeEmailHtml: () => findUnsafeEmailHtml,
86
+ generateCalendarLinks: () => generateCalendarLinks,
87
+ generateGoogleCalendarUrl: () => generateGoogleCalendarUrl,
88
+ generateIcsContent: () => generateIcsContent,
89
+ generateIcsDataUri: () => generateIcsDataUri,
90
+ generateOffice365CalendarUrl: () => generateOffice365CalendarUrl,
91
+ generateOutlookCalendarUrl: () => generateOutlookCalendarUrl,
92
+ generateYahooCalendarUrl: () => generateYahooCalendarUrl,
81
93
  htmlToPlainText: () => htmlToPlainText,
82
94
  interpolateEmailHtml: () => interpolateEmailHtml,
83
95
  interpolateTemplate: () => interpolateTemplate,
@@ -808,6 +820,53 @@ function buildPreviewPayload(variables) {
808
820
  }
809
821
  __name(buildPreviewPayload, "buildPreviewPayload");
810
822
 
823
+ // src/templateDefinition.ts
824
+ var InvalidTemplateDefinitionError = class extends Error {
825
+ static {
826
+ __name(this, "InvalidTemplateDefinitionError");
827
+ }
828
+ definitionKey;
829
+ constructor(definitionKey, reason) {
830
+ super(`Defini\xE7\xE3o de template inv\xE1lida (${definitionKey}): ${reason}`), this.definitionKey = definitionKey;
831
+ this.name = "InvalidTemplateDefinitionError";
832
+ }
833
+ };
834
+ function buildTemplateVariableCatalog(definitions) {
835
+ const catalog = {};
836
+ for (const definition of definitions) {
837
+ catalog[definition.key] ??= definition.variables;
838
+ }
839
+ return catalog;
840
+ }
841
+ __name(buildTemplateVariableCatalog, "buildTemplateVariableCatalog");
842
+ function findTemplateDefinition(definitions, identity) {
843
+ return definitions.find((definition) => definition.key === identity.key && definition.channel === identity.channel && definition.locale === identity.locale);
844
+ }
845
+ __name(findTemplateDefinition, "findTemplateDefinition");
846
+ function assertTemplateDefinitions(definitions) {
847
+ const seen = /* @__PURE__ */ new Set();
848
+ for (const definition of definitions) {
849
+ const identity = `${definition.key}|${definition.channel}|${definition.locale}`;
850
+ if (seen.has(identity)) throw new InvalidTemplateDefinitionError(definition.key, "identidade duplicada");
851
+ seen.add(identity);
852
+ if (definition.label.trim() === "") throw new InvalidTemplateDefinitionError(definition.key, "label vazio");
853
+ if (definition.body.trim() === "") throw new InvalidTemplateDefinitionError(definition.key, "body vazio");
854
+ const names = definition.variables.map((variable) => variable.name);
855
+ if (new Set(names).size !== names.length) {
856
+ throw new InvalidTemplateDefinitionError(definition.key, "vari\xE1vel declarada duas vezes");
857
+ }
858
+ const { unknown } = diffTemplateVariables({
859
+ body: definition.body,
860
+ subject: definition.subject,
861
+ variables: definition.variables
862
+ });
863
+ if (unknown.length > 0) {
864
+ throw new InvalidTemplateDefinitionError(definition.key, `o texto padr\xE3o usa vari\xE1veis n\xE3o declaradas: ${unknown.join(", ")}`);
865
+ }
866
+ }
867
+ }
868
+ __name(assertTemplateDefinitions, "assertTemplateDefinitions");
869
+
811
870
  // src/emailHtml.ts
812
871
  var STANDARD_EMAIL_THEME = {
813
872
  colors: {
@@ -1109,6 +1168,156 @@ function renderStandardEmail(params) {
1109
1168
  };
1110
1169
  }
1111
1170
  __name(renderStandardEmail, "renderStandardEmail");
1171
+
1172
+ // src/calendarEvent.ts
1173
+ function toIsoDate(value) {
1174
+ return value instanceof Date ? value : new Date(value);
1175
+ }
1176
+ __name(toIsoDate, "toIsoDate");
1177
+ function formatUtcCompact(date) {
1178
+ return date.toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");
1179
+ }
1180
+ __name(formatUtcCompact, "formatUtcCompact");
1181
+ function escapeIcsText(text) {
1182
+ return text.replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\r\n/g, "\\n").replace(/\n/g, "\\n");
1183
+ }
1184
+ __name(escapeIcsText, "escapeIcsText");
1185
+ function generateGoogleCalendarUrl(event) {
1186
+ const start = toIsoDate(event.startsAt);
1187
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1188
+ const params = new URLSearchParams({
1189
+ action: "TEMPLATE",
1190
+ text: event.title,
1191
+ dates: `${formatUtcCompact(start)}/${formatUtcCompact(end)}`
1192
+ });
1193
+ if (event.description) params.set("details", event.description);
1194
+ if (event.location) params.set("location", event.location);
1195
+ return `https://calendar.google.com/calendar/render?${params.toString()}`;
1196
+ }
1197
+ __name(generateGoogleCalendarUrl, "generateGoogleCalendarUrl");
1198
+ function generateOutlookCalendarUrl(event) {
1199
+ const start = toIsoDate(event.startsAt);
1200
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1201
+ const params = new URLSearchParams({
1202
+ path: "/calendar/action/compose",
1203
+ rru: "addevent",
1204
+ subject: event.title,
1205
+ startdt: start.toISOString(),
1206
+ enddt: end.toISOString()
1207
+ });
1208
+ if (event.description) params.set("body", event.description);
1209
+ if (event.location) params.set("location", event.location);
1210
+ return `https://outlook.live.com/calendar/0/deeplink/compose?${params.toString()}`;
1211
+ }
1212
+ __name(generateOutlookCalendarUrl, "generateOutlookCalendarUrl");
1213
+ function generateOffice365CalendarUrl(event) {
1214
+ const start = toIsoDate(event.startsAt);
1215
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1216
+ const params = new URLSearchParams({
1217
+ path: "/calendar/action/compose",
1218
+ rru: "addevent",
1219
+ subject: event.title,
1220
+ startdt: start.toISOString(),
1221
+ enddt: end.toISOString()
1222
+ });
1223
+ if (event.description) params.set("body", event.description);
1224
+ if (event.location) params.set("location", event.location);
1225
+ return `https://outlook.office.com/calendar/0/deeplink/compose?${params.toString()}`;
1226
+ }
1227
+ __name(generateOffice365CalendarUrl, "generateOffice365CalendarUrl");
1228
+ function generateYahooCalendarUrl(event) {
1229
+ const start = toIsoDate(event.startsAt);
1230
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1231
+ const durationMs = Math.max(0, end.getTime() - start.getTime());
1232
+ const durationHours = Math.floor(durationMs / (60 * 60 * 1e3));
1233
+ const durationMinutes = Math.floor(durationMs % (60 * 60 * 1e3) / (60 * 1e3));
1234
+ const durStr = `${String(durationHours).padStart(2, "0")}${String(durationMinutes).padStart(2, "0")}`;
1235
+ const params = new URLSearchParams({
1236
+ v: "60",
1237
+ view: "d",
1238
+ type: "20",
1239
+ title: event.title,
1240
+ st: formatUtcCompact(start),
1241
+ dur: durStr
1242
+ });
1243
+ if (event.description) params.set("desc", event.description);
1244
+ if (event.location) params.set("in_loc", event.location);
1245
+ return `https://calendar.yahoo.com/?${params.toString()}`;
1246
+ }
1247
+ __name(generateYahooCalendarUrl, "generateYahooCalendarUrl");
1248
+ function generateIcsDataUri(event, now = /* @__PURE__ */ new Date()) {
1249
+ const ics = generateIcsContent(event, now);
1250
+ return `data:text/calendar;charset=utf-8,${encodeURIComponent(ics)}`;
1251
+ }
1252
+ __name(generateIcsDataUri, "generateIcsDataUri");
1253
+ function generateCalendarLinks(event) {
1254
+ return {
1255
+ google: generateGoogleCalendarUrl(event),
1256
+ outlook: generateOutlookCalendarUrl(event),
1257
+ office365: generateOffice365CalendarUrl(event),
1258
+ yahoo: generateYahooCalendarUrl(event),
1259
+ icsDataUri: generateIcsDataUri(event)
1260
+ };
1261
+ }
1262
+ __name(generateCalendarLinks, "generateCalendarLinks");
1263
+ function generateIcsContent(event, now = /* @__PURE__ */ new Date()) {
1264
+ const start = toIsoDate(event.startsAt);
1265
+ const end = event.endsAt ? toIsoDate(event.endsAt) : new Date(start.getTime() + 60 * 60 * 1e3);
1266
+ const uid = `${now.getTime()}-${Math.random().toString(36).substring(2, 9)}@adatechnology.com.br`;
1267
+ const lines = [
1268
+ "BEGIN:VCALENDAR",
1269
+ "VERSION:2.0",
1270
+ "PRODID:-//Ada Technology//Notification Calendar//PT",
1271
+ "CALSCALE:GREGORIAN",
1272
+ "METHOD:PUBLISH",
1273
+ "BEGIN:VEVENT",
1274
+ `UID:${uid}`,
1275
+ `DTSTAMP:${formatUtcCompact(now)}`,
1276
+ `DTSTART:${formatUtcCompact(start)}`,
1277
+ `DTEND:${formatUtcCompact(end)}`,
1278
+ `SUMMARY:${escapeIcsText(event.title)}`
1279
+ ];
1280
+ if (event.description) {
1281
+ lines.push(`DESCRIPTION:${escapeIcsText(event.description)}`);
1282
+ }
1283
+ if (event.location) {
1284
+ lines.push(`LOCATION:${escapeIcsText(event.location)}`);
1285
+ }
1286
+ if (event.url) {
1287
+ lines.push(`URL:${event.url}`);
1288
+ }
1289
+ lines.push("END:VEVENT", "END:VCALENDAR");
1290
+ return lines.join("\r\n");
1291
+ }
1292
+ __name(generateIcsContent, "generateIcsContent");
1293
+ function extractCalendarEvent(payload) {
1294
+ if (!payload || typeof payload !== "object") return void 0;
1295
+ const raw = payload.calendarEvent && typeof payload.calendarEvent === "object" ? payload.calendarEvent : payload;
1296
+ const title = typeof raw.calendarTitle === "string" ? raw.calendarTitle : typeof raw.title === "string" ? raw.title : void 0;
1297
+ const startsAt = typeof raw.calendarStartsAt === "string" ? raw.calendarStartsAt : raw.startsAt instanceof Date || typeof raw.startsAt === "string" ? raw.startsAt : typeof raw.activityDate === "string" ? typeof raw.activityTime === "string" ? `${raw.activityDate}T${raw.activityTime}:00` : `${raw.activityDate}T09:00:00` : void 0;
1298
+ if (!title || !startsAt) return void 0;
1299
+ const description = typeof raw.calendarDescription === "string" ? raw.calendarDescription : typeof raw.description === "string" ? raw.description : void 0;
1300
+ const endsAt = typeof raw.calendarEndsAt === "string" ? raw.calendarEndsAt : raw.endsAt instanceof Date || typeof raw.endsAt === "string" ? raw.endsAt : void 0;
1301
+ const location = typeof raw.location === "string" ? raw.location : void 0;
1302
+ const url = typeof raw.url === "string" ? raw.url : void 0;
1303
+ return {
1304
+ title,
1305
+ startsAt,
1306
+ ...description ? {
1307
+ description
1308
+ } : {},
1309
+ ...endsAt ? {
1310
+ endsAt
1311
+ } : {},
1312
+ ...location ? {
1313
+ location
1314
+ } : {},
1315
+ ...url ? {
1316
+ url
1317
+ } : {}
1318
+ };
1319
+ }
1320
+ __name(extractCalendarEvent, "extractCalendarEvent");
1112
1321
  // Annotate the CommonJS export names for ESM import in node:
1113
1322
  0 && (module.exports = {
1114
1323
  ChannelNotConfiguredError,
@@ -1121,6 +1330,7 @@ __name(renderStandardEmail, "renderStandardEmail");
1121
1330
  EMAIL_ATTACHMENT_MAX_COUNT,
1122
1331
  EMAIL_DRIVER,
1123
1332
  INTRUSIVE_CHANNELS,
1333
+ InvalidTemplateDefinitionError,
1124
1334
  InvalidVapidKeysError,
1125
1335
  InvalidWebhookSignatureError,
1126
1336
  NOTIFICATION_CATEGORY_HINT,
@@ -1153,11 +1363,13 @@ __name(renderStandardEmail, "renderStandardEmail");
1153
1363
  ThrottledError,
1154
1364
  UnknownTemplateVariablesError,
1155
1365
  WebPushEncryptionError,
1366
+ assertTemplateDefinitions,
1156
1367
  assessStandardEmailTemplate,
1157
1368
  broadcastPushFilterSchema,
1158
1369
  broadcastPushSchema,
1159
1370
  buildInitials,
1160
1371
  buildPreviewPayload,
1372
+ buildTemplateVariableCatalog,
1161
1373
  categoryPolicyEntrySchema,
1162
1374
  checkEmailAttachment,
1163
1375
  createWhatsAppDriverFromChannel,
@@ -1166,8 +1378,17 @@ __name(renderStandardEmail, "renderStandardEmail");
1166
1378
  diffTemplateVariables,
1167
1379
  escapeEmailHtml,
1168
1380
  escapeTemplateHtml,
1381
+ extractCalendarEvent,
1169
1382
  extractTemplatePlaceholders,
1383
+ findTemplateDefinition,
1170
1384
  findUnsafeEmailHtml,
1385
+ generateCalendarLinks,
1386
+ generateGoogleCalendarUrl,
1387
+ generateIcsContent,
1388
+ generateIcsDataUri,
1389
+ generateOffice365CalendarUrl,
1390
+ generateOutlookCalendarUrl,
1391
+ generateYahooCalendarUrl,
1171
1392
  htmlToPlainText,
1172
1393
  interpolateEmailHtml,
1173
1394
  interpolateTemplate,