@etohq/notification 1.0.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.
Files changed (59) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +6 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +13 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/loaders/providers.d.ts +6 -0
  7. package/dist/loaders/providers.d.ts.map +1 -0
  8. package/dist/loaders/providers.js +75 -0
  9. package/dist/loaders/providers.js.map +1 -0
  10. package/dist/migrations/Migration20240509083918_InitialSetupMigration.d.ts +5 -0
  11. package/dist/migrations/Migration20240509083918_InitialSetupMigration.d.ts.map +1 -0
  12. package/dist/migrations/Migration20240509083918_InitialSetupMigration.js +18 -0
  13. package/dist/migrations/Migration20240509083918_InitialSetupMigration.js.map +1 -0
  14. package/dist/migrations/Migration20240628075401.d.ts +5 -0
  15. package/dist/migrations/Migration20240628075401.d.ts.map +1 -0
  16. package/dist/migrations/Migration20240628075401.js +16 -0
  17. package/dist/migrations/Migration20240628075401.js.map +1 -0
  18. package/dist/migrations/Migration20240830094712.d.ts +6 -0
  19. package/dist/migrations/Migration20240830094712.d.ts.map +1 -0
  20. package/dist/migrations/Migration20240830094712.js +14 -0
  21. package/dist/migrations/Migration20240830094712.js.map +1 -0
  22. package/dist/models/index.d.ts +3 -0
  23. package/dist/models/index.d.ts.map +1 -0
  24. package/dist/models/index.js +8 -0
  25. package/dist/models/index.js.map +1 -0
  26. package/dist/models/notification-provider.d.ts +24 -0
  27. package/dist/models/notification-provider.d.ts.map +1 -0
  28. package/dist/models/notification-provider.js +14 -0
  29. package/dist/models/notification-provider.js.map +1 -0
  30. package/dist/models/notification.d.ts +32 -0
  31. package/dist/models/notification.d.ts.map +1 -0
  32. package/dist/models/notification.js +35 -0
  33. package/dist/models/notification.js.map +1 -0
  34. package/dist/services/index.d.ts +3 -0
  35. package/dist/services/index.d.ts.map +1 -0
  36. package/dist/services/index.js +11 -0
  37. package/dist/services/index.js.map +1 -0
  38. package/dist/services/notification-module-service.d.ts +26 -0
  39. package/dist/services/notification-module-service.d.ts.map +1 -0
  40. package/dist/services/notification-module-service.js +138 -0
  41. package/dist/services/notification-module-service.js.map +1 -0
  42. package/dist/services/notification-provider.d.ts +43 -0
  43. package/dist/services/notification-provider.d.ts.map +1 -0
  44. package/dist/services/notification-provider.js +65 -0
  45. package/dist/services/notification-provider.js.map +1 -0
  46. package/dist/tsconfig.tsbuildinfo +1 -0
  47. package/dist/types/index.d.ts +26 -0
  48. package/dist/types/index.d.ts.map +1 -0
  49. package/dist/types/index.js +6 -0
  50. package/dist/types/index.js.map +1 -0
  51. package/dist/utils/events.d.ts +11 -0
  52. package/dist/utils/events.d.ts.map +1 -0
  53. package/dist/utils/events.js +13 -0
  54. package/dist/utils/events.js.map +1 -0
  55. package/dist/utils/index.d.ts +2 -0
  56. package/dist/utils/index.d.ts.map +1 -0
  57. package/dist/utils/index.js +18 -0
  58. package/dist/utils/index.js.map +1 -0
  59. package/package.json +62 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Notification Module
@@ -0,0 +1,6 @@
1
+ import { NotificationModuleService } from "./services";
2
+ declare const _default: import("@etohq/types").ModuleExports<typeof NotificationModuleService> & {
3
+ linkable: Record<string, any>;
4
+ };
5
+ export default _default;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAA;;;;AAIrD,wBAGE"}
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const _services_1 = require("./services");
7
+ const providers_1 = __importDefault(require("./loaders/providers"));
8
+ const utils_1 = require("@etohq/framework/utils");
9
+ exports.default = (0, utils_1.Module)(utils_1.Modules.NOTIFICATION, {
10
+ service: _services_1.NotificationModuleService,
11
+ loaders: [providers_1.default],
12
+ });
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,yCAAqD;AACrD,oEAA+C;AAC/C,kDAAwD;AAExD,kBAAe,IAAA,cAAM,EAAC,eAAO,CAAC,YAAY,EAAE;IAC1C,OAAO,EAAE,qCAAyB;IAClC,OAAO,EAAE,CAAC,mBAAa,CAAC;CACzB,CAAC,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { LoaderOptions, ModuleProvider, ModulesSdkTypes } from "@etohq/framework/types";
2
+ declare const _default: ({ container, options, }: LoaderOptions<(ModulesSdkTypes.ModuleServiceInitializeOptions | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions) & {
3
+ providers: ModuleProvider[];
4
+ }>) => Promise<void>;
5
+ export default _default;
6
+ //# sourceMappingURL=providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../../src/loaders/providers.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EACb,cAAc,EACd,eAAe,EAChB,MAAM,wBAAwB,CAAA;kDAiC5B,aAAa,CACd,CACI,eAAe,CAAC,8BAA8B,GAC9C,eAAe,CAAC,6CAA6C,CAChE,GAAG;IAAE,SAAS,EAAE,cAAc,EAAE,CAAA;CAAE,CACpC,KAAG,OAAO,CAAC,IAAI,CAAC;AARjB,wBAmBC"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const modules_sdk_1 = require("@etohq/framework/modules-sdk");
4
+ const utils_1 = require("@etohq/framework/utils");
5
+ const _services_1 = require("../services");
6
+ const _types_1 = require("../types");
7
+ const awilix_1 = require("awilix");
8
+ const registrationFn = async (klass, container, pluginOptions) => {
9
+ container.register({
10
+ [_types_1.NotificationProviderRegistrationPrefix + pluginOptions.id]: (0, awilix_1.asFunction)((cradle) => new klass(cradle, pluginOptions.options ?? {}), {
11
+ lifetime: klass.LIFE_TIME || awilix_1.Lifetime.SINGLETON,
12
+ }),
13
+ });
14
+ container.registerAdd(_types_1.NotificationIdentifiersRegistrationName, (0, awilix_1.asValue)(pluginOptions.id));
15
+ };
16
+ exports.default = async ({ container, options, }) => {
17
+ await (0, modules_sdk_1.moduleProviderLoader)({
18
+ container,
19
+ providers: options?.providers || [],
20
+ registerServiceFn: registrationFn,
21
+ });
22
+ await syncDatabaseProviders({
23
+ container,
24
+ providers: options?.providers || [],
25
+ });
26
+ };
27
+ async function syncDatabaseProviders({ container, providers, }) {
28
+ const providerServiceRegistrationKey = (0, utils_1.lowerCaseFirst)(_services_1.NotificationProviderService.name);
29
+ const providerService = container.resolve(providerServiceRegistrationKey);
30
+ const logger = container.resolve(utils_1.ContainerRegistrationKeys.LOGGER) ?? console;
31
+ const normalizedProviders = providers.map((provider) => {
32
+ if (!provider.id) {
33
+ throw new Error("An entry in the provider config is required to initialize notification providers");
34
+ }
35
+ const config = provider.options;
36
+ return {
37
+ id: provider.id,
38
+ handle: provider.id,
39
+ name: provider.id,
40
+ is_enabled: true,
41
+ channels: config?.channels ?? [],
42
+ };
43
+ });
44
+ validateProviders(normalizedProviders);
45
+ try {
46
+ const providersInDb = await providerService.list({});
47
+ const providersToDisable = providersInDb.filter((dbProvider) => !normalizedProviders.some((normalizedProvider) => normalizedProvider.id === dbProvider.id));
48
+ const promises = [];
49
+ if (normalizedProviders.length) {
50
+ promises.push(providerService.upsert(normalizedProviders));
51
+ }
52
+ if (providersToDisable.length) {
53
+ promises.push(providerService.update(providersToDisable.map((p) => ({
54
+ id: p.id,
55
+ is_enabled: false,
56
+ }))));
57
+ }
58
+ await (0, utils_1.promiseAll)(promises);
59
+ }
60
+ catch (error) {
61
+ logger.error(`Error syncing the notification providers: ${error.message}`);
62
+ }
63
+ }
64
+ function validateProviders(providers) {
65
+ const hasForChannel = {};
66
+ providers.forEach((provider) => {
67
+ provider.channels.forEach((channel) => {
68
+ if (hasForChannel[channel]) {
69
+ throw new Error(`Multiple providers are configured for the same channel: ${channel}`);
70
+ }
71
+ hasForChannel[channel] = true;
72
+ });
73
+ });
74
+ }
75
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../../src/loaders/providers.ts"],"names":[],"mappings":";;AAAA,8DAAmE;AAMnE,kDAI+B;AAE/B,yCAAuD;AACvD,mCAGe;AACf,mCAAsD;AAEtD,MAAM,cAAc,GAAG,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,EAAE;IAC/D,SAAS,CAAC,QAAQ,CAAC;QACjB,CAAC,+CAAsC,GAAG,aAAa,CAAC,EAAE,CAAC,EAAE,IAAA,mBAAU,EACrE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,IAAI,EAAE,CAAC,EAC1D;YACE,QAAQ,EAAE,KAAK,CAAC,SAAS,IAAI,iBAAQ,CAAC,SAAS;SAChD,CACF;KACF,CAAC,CAAA;IAEF,SAAS,CAAC,WAAW,CACnB,gDAAuC,EACvC,IAAA,gBAAO,EAAC,aAAa,CAAC,EAAE,CAAC,CAC1B,CAAA;AACH,CAAC,CAAA;AAED,kBAAe,KAAK,EAAE,EACpB,SAAS,EACT,OAAO,GAMR,EAAiB,EAAE;IAClB,MAAM,IAAA,kCAAoB,EAAC;QACzB,SAAS;QACT,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,EAAE;QACnC,iBAAiB,EAAE,cAAc;KAClC,CAAC,CAAA;IAEF,MAAM,qBAAqB,CAAC;QAC1B,SAAS;QACT,SAAS,EAAE,OAAO,EAAE,SAAS,IAAI,EAAE;KACpC,CAAC,CAAA;AACJ,C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AAe,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAA;QAC5D,CAAC;QAED,IAAI,kBAAkB,CAAC,MAAM,EAAE,CAAC;YAC9B,QAAQ,CAAC,IAAI,CACX,eAAe,CAAC,MAAM,CACpB,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC7B,EAAE,EAAE,CAAC,CAAC,EAAE;gBACR,UAAU,EAAE,KAAK;aAClB,CAAC,CAAC,CACJ,CACF,CAAA;QACH,CAAC;QAED,MAAM,IAAA,kBAAU,EAAC,QAAQ,CAAC,CAAA;IAC5B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,6CAA6C,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;IAC5E,CAAC;AACH,CAAC;AAED,SAAS,iBAAiB,CAAC,SAAmC;IAC5D,MAAM,aAAa,GAAG,EAAE,CAAA;IACxB,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC7B,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACpC,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC3B,MAAM,IAAI,KAAK,CACb,2DAA2D,OAAO,EAAE,CACrE,CAAA;YACH,CAAC;YACD,aAAa,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;QAC/B,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { Migration } from "@mikro-orm/migrations";
2
+ export declare class Migration20240509083918_InitialSetupMigration extends Migration {
3
+ up(): Promise<void>;
4
+ }
5
+ //# sourceMappingURL=Migration20240509083918_InitialSetupMigration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240509083918_InitialSetupMigration.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240509083918_InitialSetupMigration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,6CAA8C,SAAQ,SAAS;IACpE,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;CAyB1B"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Migration20240509083918_InitialSetupMigration = void 0;
4
+ const migrations_1 = require("@mikro-orm/migrations");
5
+ class Migration20240509083918_InitialSetupMigration extends migrations_1.Migration {
6
+ async up() {
7
+ this.addSql("drop table if exists notification");
8
+ this.addSql("drop table if exists notification_provider");
9
+ this.addSql('create table if not exists "notification_provider" ("id" text not null, "handle" text not null, "name" text not null, "is_enabled" boolean not null default true, "channels" text[] not null, constraint "notification_provider_pkey" primary key ("id"));');
10
+ this.addSql('create table if not exists "notification" ("id" text not null, "to" text not null, "channel" text not null, "template" text not null, "data" jsonb null, "trigger_type" text null, "resource_id" text null, "resource_type" text null, "receiver_id" text null, "original_notification_id" text null, "idempotency_key" text null, "external_id" text null, "provider_id" text null, "created_at" timestamptz not null default now(), constraint "notification_pkey" primary key ("id"));');
11
+ this.addSql('CREATE INDEX IF NOT EXISTS "IDX_notification_provider_id" ON "notification" (provider_id);');
12
+ this.addSql('CREATE INDEX IF NOT EXISTS "IDX_notification_idempotency_key" ON "notification" (idempotency_key);');
13
+ this.addSql('CREATE INDEX IF NOT EXISTS "IDX_notification_receiver_id" ON "notification" (receiver_id);');
14
+ this.addSql('alter table if exists "notification" add constraint "notification_provider_id_foreign" foreign key ("provider_id") references "notification_provider" ("id") on update cascade on delete set null;');
15
+ }
16
+ }
17
+ exports.Migration20240509083918_InitialSetupMigration = Migration20240509083918_InitialSetupMigration;
18
+ //# sourceMappingURL=Migration20240509083918_InitialSetupMigration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240509083918_InitialSetupMigration.js","sourceRoot":"","sources":["../../src/migrations/Migration20240509083918_InitialSetupMigration.ts"],"names":[],"mappings":";;;AAAA,sDAAiD;AAEjD,MAAa,6CAA8C,SAAQ,sBAAS;IAC1E,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,MAAM,CAAC,mCAAmC,CAAC,CAAA;QAChD,IAAI,CAAC,MAAM,CAAC,4CAA4C,CAAC,CAAA;QAEzD,IAAI,CAAC,MAAM,CACT,4PAA4P,CAC7P,CAAA;QAED,IAAI,CAAC,MAAM,CACT,2dAA2d,CAC5d,CAAA;QACD,IAAI,CAAC,MAAM,CACT,4FAA4F,CAC7F,CAAA;QACD,IAAI,CAAC,MAAM,CACT,oGAAoG,CACrG,CAAA;QACD,IAAI,CAAC,MAAM,CACT,4FAA4F,CAC7F,CAAA;QAED,IAAI,CAAC,MAAM,CACT,oMAAoM,CACrM,CAAA;IACH,CAAC;CACF;AA1BD,sGA0BC"}
@@ -0,0 +1,5 @@
1
+ import { Migration } from "@mikro-orm/migrations";
2
+ export declare class Migration20240628075401 extends Migration {
3
+ up(): Promise<void>;
4
+ }
5
+ //# sourceMappingURL=Migration20240628075401.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240628075401.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240628075401.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,uBAAwB,SAAQ,SAAS;IAC9C,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;CAmB1B"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Migration20240628075401 = void 0;
4
+ const migrations_1 = require("@mikro-orm/migrations");
5
+ class Migration20240628075401 extends migrations_1.Migration {
6
+ async up() {
7
+ this.addSql('alter table if exists "notification_provider" add column if not exists "created_at" timestamptz not null default now(), add column "updated_at" timestamptz not null default now(), add column "deleted_at" timestamptz null;');
8
+ this.addSql('alter table if exists "notification_provider" alter column "channels" type text[] using ("channels"::text[]);');
9
+ this.addSql('alter table if exists "notification_provider" alter column "channels" set default \'{}\';');
10
+ this.addSql('alter table if exists "notification" add column if not exists "updated_at" timestamptz not null default now(), add column "deleted_at" timestamptz null;');
11
+ this.addSql('drop index if exists "IDX_notification_idempotency_key";');
12
+ this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_notification_idempotency_key_unique" ON "notification" (idempotency_key) WHERE deleted_at IS NULL;');
13
+ }
14
+ }
15
+ exports.Migration20240628075401 = Migration20240628075401;
16
+ //# sourceMappingURL=Migration20240628075401.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240628075401.js","sourceRoot":"","sources":["../../src/migrations/Migration20240628075401.ts"],"names":[],"mappings":";;;AAAA,sDAAiD;AAEjD,MAAa,uBAAwB,SAAQ,sBAAS;IACpD,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,MAAM,CACT,+NAA+N,CAChO,CAAA;QACD,IAAI,CAAC,MAAM,CACT,+GAA+G,CAChH,CAAA;QACD,IAAI,CAAC,MAAM,CACT,2FAA2F,CAC5F,CAAA;QAED,IAAI,CAAC,MAAM,CACT,0JAA0J,CAC3J,CAAA;QACD,IAAI,CAAC,MAAM,CAAC,0DAA0D,CAAC,CAAA;QACvE,IAAI,CAAC,MAAM,CACT,2IAA2I,CAC5I,CAAA;IACH,CAAC;CACF;AApBD,0DAoBC"}
@@ -0,0 +1,6 @@
1
+ import { Migration } from "@mikro-orm/migrations";
2
+ export declare class Migration20240830094712 extends Migration {
3
+ up(): Promise<void>;
4
+ down(): Promise<void>;
5
+ }
6
+ //# sourceMappingURL=Migration20240830094712.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240830094712.d.ts","sourceRoot":"","sources":["../../src/migrations/Migration20240830094712.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,qBAAa,uBAAwB,SAAQ,SAAS;IAC9C,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC;IAMnB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAK5B"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Migration20240830094712 = void 0;
4
+ const migrations_1 = require("@mikro-orm/migrations");
5
+ class Migration20240830094712 extends migrations_1.Migration {
6
+ async up() {
7
+ this.addSql("alter table if exists \"notification\" add column if not exists \"status\" text check (\"status\" in ('pending', 'success', 'failure')) not null default 'pending';");
8
+ }
9
+ async down() {
10
+ this.addSql('alter table if exists "notification" drop column if exists "status";');
11
+ }
12
+ }
13
+ exports.Migration20240830094712 = Migration20240830094712;
14
+ //# sourceMappingURL=Migration20240830094712.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Migration20240830094712.js","sourceRoot":"","sources":["../../src/migrations/Migration20240830094712.ts"],"names":[],"mappings":";;;AAAA,sDAAiD;AAEjD,MAAa,uBAAwB,SAAQ,sBAAS;IACpD,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,MAAM,CACT,qKAAqK,CACtK,CAAA;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,MAAM,CACT,sEAAsE,CACvE,CAAA;IACH,CAAC;CACF;AAZD,0DAYC"}
@@ -0,0 +1,3 @@
1
+ export { Notification } from "./notification";
2
+ export { NotificationProvider } from "./notification-provider";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotificationProvider = exports.Notification = void 0;
4
+ var notification_1 = require("./notification");
5
+ Object.defineProperty(exports, "Notification", { enumerable: true, get: function () { return notification_1.Notification; } });
6
+ var notification_provider_1 = require("./notification-provider");
7
+ Object.defineProperty(exports, "NotificationProvider", { enumerable: true, get: function () { return notification_provider_1.NotificationProvider; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/models/index.ts"],"names":[],"mappings":";;;AAAA,+CAA6C;AAApC,4GAAA,YAAY,OAAA;AACrB,iEAA8D;AAArD,6HAAA,oBAAoB,OAAA"}
@@ -0,0 +1,24 @@
1
+ export declare const NotificationProvider: import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
2
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
3
+ handle: import("@etohq/framework/utils").TextProperty;
4
+ name: import("@etohq/framework/utils").TextProperty;
5
+ is_enabled: import("@etohq/framework/utils").BooleanProperty;
6
+ channels: import("@etohq/framework/utils").ArrayProperty;
7
+ notifications: import("@etohq/framework/utils").HasMany<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
8
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
9
+ to: import("@etohq/framework/utils").TextProperty;
10
+ channel: import("@etohq/framework/utils").TextProperty;
11
+ template: import("@etohq/framework/utils").TextProperty;
12
+ data: import("@etohq/framework/utils").NullableModifier<Record<string, unknown>, import("@etohq/framework/utils").JSONProperty>;
13
+ trigger_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
14
+ resource_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
15
+ resource_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
16
+ receiver_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
17
+ original_notification_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
18
+ idempotency_key: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
19
+ external_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
20
+ status: import("@etohq/framework/utils").EnumProperty<typeof import("@etohq/framework/utils").NotificationStatus>;
21
+ provider: import("@etohq/framework/utils").RelationNullableModifier<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notificationProvider">, import("@etohq/framework/utils").BelongsTo<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notificationProvider">, undefined>, true>;
22
+ }>, "notification">>;
23
+ }>, "notificationProvider">;
24
+ //# sourceMappingURL=notification-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-provider.d.ts","sourceRoot":"","sources":["../../src/models/notification-provider.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;2BAO/B,CAAA"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotificationProvider = void 0;
4
+ const utils_1 = require("@etohq/framework/utils");
5
+ const notification_1 = require("./notification");
6
+ exports.NotificationProvider = utils_1.model.define("notificationProvider", {
7
+ id: utils_1.model.id({ prefix: "notpro" }).primaryKey(),
8
+ handle: utils_1.model.text(),
9
+ name: utils_1.model.text(),
10
+ is_enabled: utils_1.model.boolean().default(true),
11
+ channels: utils_1.model.array().default([]),
12
+ notifications: utils_1.model.hasMany(() => notification_1.Notification, { mappedBy: "provider" }),
13
+ });
14
+ //# sourceMappingURL=notification-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-provider.js","sourceRoot":"","sources":["../../src/models/notification-provider.ts"],"names":[],"mappings":";;;AAAA,kDAA8C;AAC9C,iDAA6C;AAEhC,QAAA,oBAAoB,GAAG,aAAK,CAAC,MAAM,CAAC,sBAAsB,EAAE;IACvE,EAAE,EAAE,aAAK,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,UAAU,EAAE;IAC/C,MAAM,EAAE,aAAK,CAAC,IAAI,EAAE;IACpB,IAAI,EAAE,aAAK,CAAC,IAAI,EAAE;IAClB,UAAU,EAAE,aAAK,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IACzC,QAAQ,EAAE,aAAK,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;IACnC,aAAa,EAAE,aAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,2BAAY,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;CAC3E,CAAC,CAAA"}
@@ -0,0 +1,32 @@
1
+ import { NotificationStatus } from "@etohq/framework/utils";
2
+ export declare const Notification: import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
3
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
4
+ to: import("@etohq/framework/utils").TextProperty;
5
+ channel: import("@etohq/framework/utils").TextProperty;
6
+ template: import("@etohq/framework/utils").TextProperty;
7
+ data: import("@etohq/framework/utils").NullableModifier<Record<string, unknown>, import("@etohq/framework/utils").JSONProperty>;
8
+ trigger_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
9
+ resource_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
10
+ resource_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
11
+ receiver_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
12
+ original_notification_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
13
+ idempotency_key: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
14
+ external_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
15
+ status: import("@etohq/framework/utils").EnumProperty<typeof NotificationStatus>;
16
+ provider: import("@etohq/framework/utils").RelationNullableModifier<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
17
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
18
+ handle: import("@etohq/framework/utils").TextProperty;
19
+ name: import("@etohq/framework/utils").TextProperty;
20
+ is_enabled: import("@etohq/framework/utils").BooleanProperty;
21
+ channels: import("@etohq/framework/utils").ArrayProperty;
22
+ notifications: import("@etohq/framework/utils").HasMany<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notification">>;
23
+ }>, "notificationProvider">, import("@etohq/framework/utils").BelongsTo<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
24
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
25
+ handle: import("@etohq/framework/utils").TextProperty;
26
+ name: import("@etohq/framework/utils").TextProperty;
27
+ is_enabled: import("@etohq/framework/utils").BooleanProperty;
28
+ channels: import("@etohq/framework/utils").ArrayProperty;
29
+ notifications: import("@etohq/framework/utils").HasMany<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notification">>;
30
+ }>, "notificationProvider">, undefined>, true>;
31
+ }>, "notification">;
32
+ //# sourceMappingURL=notification.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification.d.ts","sourceRoot":"","sources":["../../src/models/notification.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAIlE,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA4BvB,CAAA"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Notification = void 0;
4
+ const utils_1 = require("@etohq/framework/utils");
5
+ const notification_provider_1 = require("./notification-provider");
6
+ // We probably want to have a TTL for each entry, so we don't bloat the DB (and also for GDPR reasons if TTL < 30 days).
7
+ exports.Notification = utils_1.model.define("notification", {
8
+ id: utils_1.model.id({ prefix: "noti" }).primaryKey(),
9
+ // This can be an email, phone number, or username, depending on the channel.
10
+ to: utils_1.model.text().searchable(),
11
+ channel: utils_1.model.text(),
12
+ // The template name in the provider's system.
13
+ template: utils_1.model.text(),
14
+ // The data that gets passed over to the provider for rendering the notification.
15
+ data: utils_1.model.json().nullable(),
16
+ // This can be the event name, the workflow, or anything else that can help to identify what triggered the notification.
17
+ trigger_type: utils_1.model.text().nullable(),
18
+ // The ID of the resource this notification is for, if applicable. Useful for displaying relevant information in the UI
19
+ resource_id: utils_1.model.text().searchable().nullable(),
20
+ // The typeame of the resource this notification is for, if applicable, eg. "order"
21
+ resource_type: utils_1.model.text().nullable(),
22
+ // The ID of the receiver of the notification, if applicable. This can be a customer, user, a company, or anything else.
23
+ receiver_id: utils_1.model.text().index().nullable(),
24
+ // The original notification, in case this is a retried notification.
25
+ original_notification_id: utils_1.model.text().nullable(),
26
+ idempotency_key: utils_1.model.text().unique().nullable(),
27
+ // The ID of the notification in the external system, if applicable
28
+ external_id: utils_1.model.text().nullable(),
29
+ // The status of the notification
30
+ status: utils_1.model.enum(utils_1.NotificationStatus).default(utils_1.NotificationStatus.PENDING),
31
+ provider: utils_1.model
32
+ .belongsTo(() => notification_provider_1.NotificationProvider, { mappedBy: "notifications" })
33
+ .nullable(),
34
+ });
35
+ //# sourceMappingURL=notification.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification.js","sourceRoot":"","sources":["../../src/models/notification.ts"],"names":[],"mappings":";;;AAAA,kDAAkE;AAClE,mEAA8D;AAE9D,wHAAwH;AAC3G,QAAA,YAAY,GAA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
@@ -0,0 +1,3 @@
1
+ export { default as NotificationModuleService } from "./notification-module-service";
2
+ export { default as NotificationProviderService } from "./notification-provider";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,yBAAyB,EAAE,MAAM,+BAA+B,CAAA;AACpF,OAAO,EAAE,OAAO,IAAI,2BAA2B,EAAE,MAAM,yBAAyB,CAAA"}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.NotificationProviderService = exports.NotificationModuleService = void 0;
7
+ var notification_module_service_1 = require("./notification-module-service");
8
+ Object.defineProperty(exports, "NotificationModuleService", { enumerable: true, get: function () { return __importDefault(notification_module_service_1).default; } });
9
+ var notification_provider_1 = require("./notification-provider");
10
+ Object.defineProperty(exports, "NotificationProviderService", { enumerable: true, get: function () { return __importDefault(notification_provider_1).default; } });
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;AAAA,6EAAoF;AAA3E,yJAAA,OAAO,OAA6B;AAC7C,iEAAgF;AAAvE,qJAAA,OAAO,OAA+B"}
@@ -0,0 +1,26 @@
1
+ import { Context, DAL, InferEntityType, INotificationModuleService, InternalModuleDeclaration, Logger, ModulesSdkTypes, NotificationTypes } from "@etohq/framework/types";
2
+ import { Notification } from "../models";
3
+ import NotificationProviderService from "./notification-provider";
4
+ type InjectedDependencies = {
5
+ logger?: Logger;
6
+ baseRepository: DAL.RepositoryService;
7
+ notificationService: ModulesSdkTypes.IEtoInternalService<typeof Notification>;
8
+ notificationProviderService: NotificationProviderService;
9
+ };
10
+ declare const NotificationModuleService_base: import("@etohq/framework/utils").EtoServiceReturnType<{
11
+ Notification: {
12
+ dto: NotificationTypes.NotificationDTO;
13
+ };
14
+ }>;
15
+ export default class NotificationModuleService extends NotificationModuleService_base implements INotificationModuleService {
16
+ protected readonly moduleDeclaration: InternalModuleDeclaration;
17
+ protected baseRepository_: DAL.RepositoryService;
18
+ protected readonly notificationService_: ModulesSdkTypes.IEtoInternalService<typeof Notification>;
19
+ protected readonly notificationProviderService_: NotificationProviderService;
20
+ constructor({ baseRepository, notificationService, notificationProviderService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
21
+ createNotifications(data: NotificationTypes.CreateNotificationDTO[], sharedContext?: Context): Promise<NotificationTypes.NotificationDTO[]>;
22
+ createNotifications(data: NotificationTypes.CreateNotificationDTO, sharedContext?: Context): Promise<NotificationTypes.NotificationDTO>;
23
+ protected createNotifications_(data: NotificationTypes.CreateNotificationDTO[], sharedContext?: Context): Promise<InferEntityType<typeof Notification>[]>;
24
+ }
25
+ export {};
26
+ //# sourceMappingURL=notification-module-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-module-service.d.ts","sourceRoot":"","sources":["../../src/services/notification-module-service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,GAAG,EACH,eAAe,EACf,0BAA0B,EAC1B,yBAAyB,EACzB,MAAM,EACN,eAAe,EACf,iBAAiB,EAClB,MAAM,wBAAwB,CAAA;AAW/B,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAEtC,OAAO,2BAA2B,MAAM,yBAAyB,CAAA;AAEjE,KAAK,oBAAoB,GAAG;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,cAAc,EAAE,GAAG,CAAC,iBAAiB,CAAA;IACrC,mBAAmB,EAAE,eAAe,CAAC,mBAAmB,CACtD,OAAO,YAAY,CACpB,CAAA;IACD,2BAA2B,EAAE,2BAA2B,CAAA;CACzD,CAAA;;kBAIiB;QAAE,GAAG,EAAE,iBAAiB,CAAC,eAAe,CAAA;KAAE;;AAF5D,MAAM,CAAC,OAAO,OAAO,yBACnB,SAAQ,8BAGR,YAAW,0BAA0B;IAcnC,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,yBAAyB;IAZjE,SAAS,CAAC,eAAe,EAAE,GAAG,CAAC,iBAAiB,CAAA;IAChD,SAAS,CAAC,QAAQ,CAAC,oBAAoB,EAAE,eAAe,CAAC,mBAAmB,CAC1E,OAAO,YAAY,CACpB,CAAA;IACD,SAAS,CAAC,QAAQ,CAAC,4BAA4B,EAAE,2BAA2B,CAAA;gBAG1E,EACE,cAAc,EACd,mBAAmB,EACnB,2BAA2B,GAC5B,EAAE,oBAAoB,EACJ,iBAAiB,EAAE,yBAAyB;IAUjE,mBAAmB,CACjB,IAAI,EAAE,iBAAiB,CAAC,qBAAqB,EAAE,EAC/C,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,iBAAiB,CAAC,eAAe,EAAE,CAAC;IAC/C,mBAAmB,CACjB,IAAI,EAAE,iBAAiB,CAAC,qBAAqB,EAC7C,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,iBAAiB,CAAC,eAAe,CAAC;cAgC7B,oBAAoB,CAClC,IAAI,EAAE,iBAAiB,CAAC,qBAAqB,EAAE,EACjC,aAAa,GAAE,OAAY,GACxC,OAAO,CAAC,eAAe,CAAC,OAAO,YAAY,CAAC,EAAE,CAAC;CAuInD"}
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const utils_1 = require("@etohq/framework/utils");
16
+ const _models_1 = require("../models");
17
+ const _utils_1 = require("../utils");
18
+ class NotificationModuleService extends (0, utils_1.EtoService)({ Notification: _models_1.Notification }) {
19
+ constructor({ baseRepository, notificationService, notificationProviderService, }, moduleDeclaration) {
20
+ // @ts-ignore
21
+ super(...arguments);
22
+ this.moduleDeclaration = moduleDeclaration;
23
+ this.baseRepository_ = baseRepository;
24
+ this.notificationService_ = notificationService;
25
+ this.notificationProviderService_ = notificationProviderService;
26
+ }
27
+ async createNotifications(data, sharedContext = {}) {
28
+ const normalized = Array.isArray(data) ? data : [data];
29
+ const createdNotifications = await this.createNotifications_(normalized, sharedContext);
30
+ const serialized = await this.baseRepository_.serialize(createdNotifications);
31
+ _utils_1.eventBuilders.createdNotification({
32
+ data: serialized,
33
+ sharedContext,
34
+ });
35
+ return Array.isArray(data) ? serialized : serialized[0];
36
+ }
37
+ async createNotifications_(data, sharedContext = {}) {
38
+ if (!data.length) {
39
+ return [];
40
+ }
41
+ // TODO: At this point we should probably take a lock with the idempotency keys so we don't have race conditions.
42
+ // Also, we should probably rely on Redis for this instead of the database.
43
+ const idempotencyKeys = data
44
+ .map((entry) => entry.idempotency_key)
45
+ .filter(Boolean);
46
+ let { notificationsToProcess, createdNotifications } = await this.baseRepository_.transaction(async (txManager) => {
47
+ const context = {
48
+ ...sharedContext,
49
+ transactionManager: txManager,
50
+ };
51
+ const alreadySentNotifications = await this.notificationService_.list({
52
+ idempotency_key: idempotencyKeys,
53
+ }, {}, context);
54
+ const existsMap = new Map(alreadySentNotifications.map((n) => [n.idempotency_key, n]));
55
+ const notificationsToProcess = data.filter((entry) => !entry.idempotency_key ||
56
+ !existsMap.has(entry.idempotency_key) ||
57
+ (existsMap.has(entry.idempotency_key) &&
58
+ existsMap.get(entry.idempotency_key).status ===
59
+ utils_1.NotificationStatus.FAILURE));
60
+ const channels = notificationsToProcess.map((not) => not.channel);
61
+ const providers = await this.notificationProviderService_.getProviderForChannels(channels);
62
+ // Create the notifications to be sent to prevent concurrent actions listing the same notifications
63
+ const normalizedNotificationsToProcess = notificationsToProcess.map((entry) => {
64
+ const provider = providers.find((provider) => provider?.channels.includes(entry.channel));
65
+ return {
66
+ provider,
67
+ data: {
68
+ id: (0, utils_1.generateEntityId)(undefined, "noti"),
69
+ ...entry,
70
+ provider_id: provider?.id,
71
+ },
72
+ };
73
+ });
74
+ const toCreate = normalizedNotificationsToProcess
75
+ .filter((e) => !e.data.idempotency_key || !existsMap.has(e.data.idempotency_key))
76
+ .map((e) => e.data);
77
+ const createdNotifications = toCreate.length
78
+ ? await this.notificationService_.create(toCreate, context)
79
+ : [];
80
+ return {
81
+ notificationsToProcess: normalizedNotificationsToProcess,
82
+ createdNotifications,
83
+ };
84
+ });
85
+ const notificationToUpdate = [];
86
+ try {
87
+ await (0, utils_1.promiseAll)(notificationsToProcess.map(async (entry) => {
88
+ const provider = entry.provider;
89
+ if (!provider?.is_enabled) {
90
+ entry.data.status = utils_1.NotificationStatus.FAILURE;
91
+ notificationToUpdate.push(entry.data);
92
+ const errorMessage = !provider
93
+ ? `Could not find a notification provider for channel: ${entry.data.channel} for notification id ${entry.data.id}`
94
+ : `Notification provider ${provider.id} is not enabled. To enable it, configure it as a provider in the notification module options.`;
95
+ throw new utils_1.EtoError(utils_1.EtoError.Types.NOT_FOUND, errorMessage);
96
+ }
97
+ const res = await this.notificationProviderService_
98
+ .send(provider, entry.data)
99
+ .catch((e) => {
100
+ entry.data.status = utils_1.NotificationStatus.FAILURE;
101
+ notificationToUpdate.push(entry.data);
102
+ throw new utils_1.EtoError(utils_1.EtoError.Types.UNEXPECTED_STATE, `Failed to send notification with id ${entry.data.id}:\n${e.message}`);
103
+ });
104
+ entry.data.external_id = res.id;
105
+ entry.data.status = utils_1.NotificationStatus.SUCCESS;
106
+ notificationToUpdate.push(entry.data);
107
+ }), {
108
+ aggregateErrors: true,
109
+ });
110
+ }
111
+ finally {
112
+ const updatedNotifications = await this.notificationService_.update(notificationToUpdate, sharedContext);
113
+ const updatedNotificationsMap = new Map(updatedNotifications.map((n) => [n.id, n]));
114
+ // Maintain the order of the notifications
115
+ createdNotifications = createdNotifications.map((notification) => {
116
+ return updatedNotificationsMap.get(notification.id) || notification;
117
+ });
118
+ }
119
+ return createdNotifications;
120
+ }
121
+ }
122
+ exports.default = NotificationModuleService;
123
+ __decorate([
124
+ (0, utils_1.InjectManager)(),
125
+ (0, utils_1.EmitEvents)(),
126
+ __param(1, (0, utils_1.EtoContext)()),
127
+ __metadata("design:type", Function),
128
+ __metadata("design:paramtypes", [Object, Object]),
129
+ __metadata("design:returntype", Promise)
130
+ ], NotificationModuleService.prototype, "createNotifications", null);
131
+ __decorate([
132
+ (0, utils_1.InjectManager)(),
133
+ __param(1, (0, utils_1.EtoContext)()),
134
+ __metadata("design:type", Function),
135
+ __metadata("design:paramtypes", [Array, Object]),
136
+ __metadata("design:returntype", Promise)
137
+ ], NotificationModuleService.prototype, "createNotifications_", null);
138
+ //# sourceMappingURL=notification-module-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-module-service.js","sourceRoot":"","sources":["../../src/services/notification-module-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAUA,kDAS+B;AAC/B,qCAAsC;AACtC,mCAAsC;AAYtC,MAAqB,yBACnB,SAAQ,IAAA,kBAAU,EAEf,EAAE,YAAY,EAAZ,sBAAY,EAAE,CAAC;IASpB,YACE,EACE,cAAc,EACd,mBAAmB,EACnB,2BAA2B,GACN,EACJ,iBAA4C;QAE/D,aAAa;QACb,KAAK,CAAC,GAAG,SA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AAE,EAAE;gBAC/D,OAAO,uBAAuB,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC,IAAI,YAAY,CAAA;YACrE,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,OAAO,oBAAoB,CAAA;IAC7B,CAAC;CACF;AA7MD,4CA6MC;AAtKO;IAFL,IAAA,qBAAa,GAAE;IACf,IAAA,kBAAU,GAAE;IAKV,WAAA,IAAA,kBAAU,GAAE,CAAA;;;;oEAqBd;AAGe;IADf,IAAA,qBAAa,GAAE;IAGb,WAAA,IAAA,kBAAU,GAAE,CAAA;;;;qEAuId"}
@@ -0,0 +1,43 @@
1
+ import { DAL, InferEntityType, Logger, NotificationTypes } from "@etohq/framework/types";
2
+ import { NotificationProvider } from "../models";
3
+ import { NotificationProviderRegistrationPrefix } from "../types";
4
+ type InjectedDependencies = {
5
+ logger?: Logger;
6
+ notificationProviderRepository: DAL.RepositoryService<InferEntityType<typeof NotificationProvider>>;
7
+ [key: `${typeof NotificationProviderRegistrationPrefix}${string}`]: NotificationTypes.INotificationProvider;
8
+ };
9
+ type Provider = InferEntityType<typeof NotificationProvider>;
10
+ declare const NotificationProviderService_base: new (container: InjectedDependencies) => import("@etohq/framework/types").IEtoInternalService<import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
11
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
12
+ handle: import("@etohq/framework/utils").TextProperty;
13
+ name: import("@etohq/framework/utils").TextProperty;
14
+ is_enabled: import("@etohq/framework/utils").BooleanProperty;
15
+ channels: import("@etohq/framework/utils").ArrayProperty;
16
+ notifications: import("@etohq/framework/utils").HasMany<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<{
17
+ id: import("@etohq/framework/utils").PrimaryKeyModifier<string, import("@etohq/framework/utils").IdProperty>;
18
+ to: import("@etohq/framework/utils").TextProperty;
19
+ channel: import("@etohq/framework/utils").TextProperty;
20
+ template: import("@etohq/framework/utils").TextProperty;
21
+ data: import("@etohq/framework/utils").NullableModifier<Record<string, unknown>, import("@etohq/framework/utils").JSONProperty>;
22
+ trigger_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
23
+ resource_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
24
+ resource_type: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
25
+ receiver_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
26
+ original_notification_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
27
+ idempotency_key: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
28
+ external_id: import("@etohq/framework/utils").NullableModifier<string, import("@etohq/framework/utils").TextProperty>;
29
+ status: import("@etohq/framework/utils").EnumProperty<typeof import("@etohq/framework/utils").NotificationStatus>;
30
+ provider: import("@etohq/framework/utils").RelationNullableModifier<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notificationProvider">, import("@etohq/framework/utils").BelongsTo<() => import("@etohq/framework/utils").DmlEntity<import("@etohq/framework/utils").DMLEntitySchemaBuilder<any>, "notificationProvider">, undefined>, true>;
31
+ }>, "notification">>;
32
+ }>, "notificationProvider">, InjectedDependencies>;
33
+ export default class NotificationProviderService extends NotificationProviderService_base {
34
+ #private;
35
+ protected readonly notificationProviderRepository_: DAL.RepositoryService<InferEntityType<typeof NotificationProvider>>;
36
+ protected providersCache: Map<string, InferEntityType<typeof NotificationProvider>>;
37
+ constructor(container: InjectedDependencies);
38
+ protected retrieveProviderRegistration(providerId: string): NotificationTypes.INotificationProvider;
39
+ getProviderForChannels<TChannel = string | string[], TOutput = TChannel extends string[] ? Provider[] : Provider | undefined>(channels: TChannel): Promise<TOutput>;
40
+ send(provider: InferEntityType<typeof NotificationProvider>, notification: NotificationTypes.ProviderSendNotificationDTO): Promise<NotificationTypes.ProviderSendNotificationResultsDTO>;
41
+ }
42
+ export {};
43
+ //# sourceMappingURL=notification-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-provider.d.ts","sourceRoot":"","sources":["../../src/services/notification-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,eAAe,EACf,MAAM,EACN,iBAAiB,EAClB,MAAM,wBAAwB,CAAA;AAE/B,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAC9C,OAAO,EAAE,sCAAsC,EAAE,MAAM,QAAQ,CAAA;AAE/D,KAAK,oBAAoB,GAAG;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,8BAA8B,EAAE,GAAG,CAAC,iBAAiB,CACnD,eAAe,CAAC,OAAO,oBAAoB,CAAC,CAC7C,CAAA;IACD,CACE,GAAG,EAAE,GAAG,OAAO,sCAAsC,GAAG,MAAM,EAAE,GAC/D,iBAAiB,CAAC,qBAAqB,CAAA;CAC3C,CAAA;AAED,KAAK,QAAQ,GAAG,eAAe,CAAC,OAAO,oBAAoB,CAAC,CAAA;;;;;;;;;;;;;;;;;;;;;;;;AAE5D,MAAM,CAAC,OAAO,OAAO,2BAA4B,SAAQ,gCAGlC;;IACrB,SAAS,CAAC,QAAQ,CAAC,+BAA+B,EAAE,GAAG,CAAC,iBAAiB,CACvE,eAAe,CAAC,OAAO,oBAAoB,CAAC,CAC7C,CAAA;IAMD,SAAS,CAAC,cAAc,EAAE,GAAG,CAC3B,MAAM,EACN,eAAe,CAAC,OAAO,oBAAoB,CAAC,CAC7C,CAAA;gBAEW,SAAS,EAAE,oBAAoB;IAS3C,SAAS,CAAC,4BAA4B,CACpC,UAAU,EAAE,MAAM,GACjB,iBAAiB,CAAC,qBAAqB;IAoBpC,sBAAsB,CAC1B,QAAQ,GAAG,MAAM,GAAG,MAAM,EAAE,EAC5B,OAAO,GAAG,QAAQ,SAAS,MAAM,EAAE,GAAG,QAAQ,EAAE,GAAG,QAAQ,GAAG,SAAS,EACvE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC;IAqBjC,IAAI,CACR,QAAQ,EAAE,eAAe,CAAC,OAAO,oBAAoB,CAAC,EACtD,YAAY,EAAE,iBAAiB,CAAC,2BAA2B,GAC1D,OAAO,CAAC,iBAAiB,CAAC,kCAAkC,CAAC;CAIjE"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _NotificationProviderService_logger;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const utils_1 = require("@etohq/framework/utils");
16
+ const _models_1 = require("../models");
17
+ const _types_1 = require("../types");
18
+ class NotificationProviderService extends utils_1.ModulesSdkUtils.EtoInternalService(_models_1.NotificationProvider) {
19
+ constructor(container) {
20
+ super(container);
21
+ // We can store the providers in a memory since they can only be registered on startup and not changed during runtime
22
+ _NotificationProviderService_logger.set(this, void 0);
23
+ this.notificationProviderRepository_ =
24
+ container.notificationProviderRepository;
25
+ __classPrivateFieldSet(this, _NotificationProviderService_logger, container["logger"]
26
+ ? container.logger
27
+ : console, "f");
28
+ }
29
+ retrieveProviderRegistration(providerId) {
30
+ try {
31
+ return this.__container__[`${_types_1.NotificationProviderRegistrationPrefix}${providerId}`];
32
+ }
33
+ catch (err) {
34
+ if (err.name === "AwilixResolutionError") {
35
+ const errMessage = `
36
+ Unable to retrieve the notification provider with id: ${providerId}
37
+ Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.`;
38
+ throw new Error(errMessage);
39
+ }
40
+ const errMessage = `Unable to retrieve the notification provider with id: ${providerId}, the following error occurred: ${err.message}`;
41
+ __classPrivateFieldGet(this, _NotificationProviderService_logger, "f").error(errMessage);
42
+ throw new Error(errMessage);
43
+ }
44
+ }
45
+ async getProviderForChannels(channels) {
46
+ if (!this.providersCache) {
47
+ const providers = await this.notificationProviderRepository_.find({
48
+ where: { is_enabled: true },
49
+ });
50
+ this.providersCache = new Map(providers.flatMap((provider) => provider.channels.map((c) => [c, provider])));
51
+ }
52
+ const normalizedChannels = Array.isArray(channels) ? channels : [channels];
53
+ const results = normalizedChannels
54
+ .map((channel) => this.providersCache.get(channel))
55
+ .filter(Boolean);
56
+ return (Array.isArray(channels) ? results : results[0]);
57
+ }
58
+ async send(provider, notification) {
59
+ const providerHandler = this.retrieveProviderRegistration(provider.id);
60
+ return await providerHandler.send(notification);
61
+ }
62
+ }
63
+ _NotificationProviderService_logger = new WeakMap();
64
+ exports.default = NotificationProviderService;
65
+ //# sourceMappingURL=notification-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notification-provider.js","sourceRoot":"","sources":["../../src/services/notification-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAMA,kDAAwD;AACxD,qCAA8C;AAC9C,mCAA+D;AAc/D,MAAqB,2BAA4B,SAAQ,uBAAe,CAAC,kBAAkB,CAGzF,8BAAoB,CAAC;IAcrB,YAAY,SAA+B;QACzC,KAAK,CAAC,SAAS,CAAC,CAAA;QAVlB,qHAAqH;QAErH,sDAAe;QASb,IAAI,CAA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C,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;QAC1E,MAAM,OAAO,GAAG,kBAAkB;aAC/B,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;aAClD,MAAM,CAAC,OAAO,CAAC,CAAA;QAElB,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAY,CAAA;IACpE,CAAC;IAED,KAAK,CAAC,IAAI,CACR,QAAsD,EACtD,YAA2D;QAE3D,MAAM,eAAe,GAAG,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;QACtE,OAAO,MAAM,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;IACjD,CAAC;CACF;;kBA/EoB,2BAA2B"}
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/loaders/providers.ts","../src/migrations/Migration20240509083918_InitialSetupMigration.ts","../src/migrations/Migration20240628075401.ts","../src/migrations/Migration20240830094712.ts","../src/models/index.ts","../src/models/notification-provider.ts","../src/models/notification.ts","../src/services/index.ts","../src/services/notification-module-service.ts","../src/services/notification-provider.ts","../src/types/index.ts","../src/utils/events.ts","../src/utils/index.ts"],"version":"5.6.2"}
@@ -0,0 +1,26 @@
1
+ import { Logger, ModuleProviderExports, ModuleServiceInitializeOptions } from "@etohq/framework/types";
2
+ export type InitializeModuleInjectableDependencies = {
3
+ logger?: Logger;
4
+ };
5
+ export declare const NotificationIdentifiersRegistrationName = "notification_providers_identifier";
6
+ export declare const NotificationProviderRegistrationPrefix = "np_";
7
+ export type NotificationModuleOptions = Partial<ModuleServiceInitializeOptions> & {
8
+ /**
9
+ * Providers to be registered
10
+ */
11
+ providers?: {
12
+ /**
13
+ * The module provider to be registered
14
+ */
15
+ resolve: string | ModuleProviderExports;
16
+ /**
17
+ * The id of the provider
18
+ */
19
+ id: string;
20
+ /**
21
+ * key value pair of the configuration to be passed to the provider constructor
22
+ */
23
+ options?: Record<string, unknown>;
24
+ }[];
25
+ };
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,qBAAqB,EACrB,8BAA8B,EAC/B,MAAM,wBAAwB,CAAA;AAE/B,MAAM,MAAM,sCAAsC,GAAG;IACnD,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,eAAO,MAAM,uCAAuC,sCACf,CAAA;AAErC,eAAO,MAAM,sCAAsC,QAAQ,CAAA;AAE3D,MAAM,MAAM,yBAAyB,GACnC,OAAO,CAAC,8BAA8B,CAAC,GAAG;IACxC;;OAEG;IACH,SAAS,CAAC,EAAE;QACV;;WAEG;QACH,OAAO,EAAE,MAAM,GAAG,qBAAqB,CAAA;QACvC;;WAEG;QACH,EAAE,EAAE,MAAM,CAAA;QACV;;WAEG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAClC,EAAE,CAAA;CACJ,CAAA"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotificationProviderRegistrationPrefix = exports.NotificationIdentifiersRegistrationName = void 0;
4
+ exports.NotificationIdentifiersRegistrationName = "notification_providers_identifier";
5
+ exports.NotificationProviderRegistrationPrefix = "np_";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;AAUa,QAAA,uCAAuC,GAClD,mCAAmC,CAAA;AAExB,QAAA,sCAAsC,GAAG,KAAK,CAAA"}
@@ -0,0 +1,11 @@
1
+ export declare const eventBuilders: {
2
+ createdNotification: ({ data, sharedContext, }: {
3
+ data: {
4
+ id: string;
5
+ } | {
6
+ id: string;
7
+ }[];
8
+ sharedContext: import("@etohq/types").Context;
9
+ }) => void;
10
+ };
11
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,aAAa;;;;;;;;;CAOzB,CAAA"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.eventBuilders = void 0;
4
+ const utils_1 = require("@etohq/framework/utils");
5
+ exports.eventBuilders = {
6
+ createdNotification: (0, utils_1.moduleEventBuilderFactory)({
7
+ source: utils_1.Modules.NOTIFICATION,
8
+ action: utils_1.CommonEvents.CREATED,
9
+ object: "notification",
10
+ eventName: utils_1.NotificationEvents.NOTIFICATION_CREATED,
11
+ }),
12
+ };
13
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":";;;AAAA,kDAK+B;AAElB,QAAA,aAAa,GAAG;IAC3B,mBAAmB,EAAE,IAAA,iCAAyB,EAAC;QAC7C,MAAM,EAAE,eAAO,CAAC,YAAY;QAC5B,MAAM,EAAE,oBAAY,CAAC,OAAO;QAC5B,MAAM,EAAE,cAAc;QACtB,SAAS,EAAE,0BAAkB,CAAC,oBAAoB;KACnD,CAAC;CACH,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from "./events";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./events"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@etohq/notification",
3
+ "version": "1.0.0",
4
+ "description": "Eto Notification module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "!dist/**/__tests__",
10
+ "!dist/**/__mocks__",
11
+ "!dist/**/__fixtures__"
12
+ ],
13
+ "engines": {
14
+ "node": ">=20"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/etohq/eto",
19
+ "directory": "packages/modules/notification"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "author": "Eto",
25
+ "license": "MIT",
26
+ "scripts": {
27
+ "watch": "tsc --build --watch",
28
+ "watch:test": "tsc --build tsconfig.spec.json --watch",
29
+ "resolve:aliases": "tsc --showConfig -p tsconfig.json > tsconfig.resolved.json && tsc-alias -p tsconfig.resolved.json && rimraf tsconfig.resolved.json", "build": "rimraf dist && tsc --build && npm run resolve:aliases",
30
+ "test": "jest --runInBand --bail --forceExit --passWithNoTests -- src",
31
+ "test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.spec.ts",
32
+ "migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts eto-mikro-orm migration:create --initial -n InitialSetupMigration",
33
+ "migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts eto-mikro-orm migration:create",
34
+ "migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts eto-mikro-orm migration:up",
35
+ "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts eto-mikro-orm cache:clear"
36
+ },
37
+ "devDependencies": {
38
+ "@etohq/framework": "^1.0.0",
39
+ "@etohq/test-utils": "^1.0.0",
40
+ "@mikro-orm/cli": "5.9.7",
41
+ "@mikro-orm/core": "5.9.7",
42
+ "@mikro-orm/migrations": "5.9.7",
43
+ "@mikro-orm/postgresql": "5.9.7",
44
+ "@swc/core": "^1.7.28",
45
+ "@swc/jest": "^0.2.36",
46
+ "@types/jest": "^29.5.14",
47
+ "jest": "^29.7.0",
48
+ "rimraf": "^5.0.2",
49
+ "tsc-alias": "^1.8.6",
50
+ "typescript": "^5.6.2"
51
+ },
52
+ "dependencies": {
53
+ "awilix": "^8.0.1"
54
+ },
55
+ "peerDependencies": {
56
+ "@etohq/framework": "^1.0.0",
57
+ "@mikro-orm/core": "5.9.7",
58
+ "@mikro-orm/migrations": "5.9.7",
59
+ "@mikro-orm/postgresql": "5.9.7",
60
+ "awilix": "^8.0.1"
61
+ }
62
+ }