diff --git a/src/store/airhorn-store.ts b/src/store/airhorn-store.ts index 478507a..11dd7bc 100644 --- a/src/store/airhorn-store.ts +++ b/src/store/airhorn-store.ts @@ -54,9 +54,8 @@ export type CreateAirhornNotification = { export type AirhornStoreProvider = { name: string; uri: string; - createSubscription(subscription: CreateAirhornNotification): Promise; + createSubscription(subscription: CreateAirhornSubscription): Promise; updateSubscription(notification: AirhornSubscription): Promise; - deleteSubscription(notification: AirhornSubscription): Promise; deleteSubscriptionById(id: string): Promise; getSubscriptions(): Promise; getSubscriptionById(id: string): Promise; @@ -66,7 +65,6 @@ export type AirhornStoreProvider = { getSubscriptionsByProviderType(providerType: AirhornProviderType): Promise; createNotification(notification: CreateAirhornNotification): Promise; updateNotification(status: AirhornNotification): Promise; - deleteNotification(status: AirhornNotification): Promise; deleteNotificationById(id: string): Promise; getNotifications(): Promise; getNotificationById(id: string): Promise; @@ -91,4 +89,45 @@ export class AirhornStore { public set provider(provider: AirhornStoreProvider) { this._provider = provider; } + + public async createSubscription(subscription: CreateAirhornSubscription): Promise { + return this._provider.createSubscription(subscription); + } + + public async updateSubscription(subscription: AirhornSubscription): Promise { + return this._provider.updateSubscription(subscription); + } + + public async deleteSubscription(subscription: AirhornSubscription): Promise { + return this.deleteSubscriptionById(subscription.id); + } + + public async deleteSubscriptionById(id: string): Promise { + return this._provider.deleteSubscriptionById(id); + } + + public async getSubscriptionById(id: string): Promise { + return this._provider.getSubscriptionById(id); + } + + public async createNotification(notification: CreateAirhornNotification): Promise { + return this._provider.createNotification(notification); + } + + public async updateNotification(notification: AirhornNotification): Promise { + return this._provider.updateNotification(notification); + } + + public async deleteNotification(notification: AirhornNotification): Promise { + return this.deleteNotificationById(notification.id); + } + + public async deleteNotificationById(id: string): Promise { + return this._provider.deleteNotificationById(id); + } + + public async getNotificationById(id: string): Promise { + return this._provider.getNotificationById(id); + } } + diff --git a/src/store/mongo-store-provider.ts b/src/store/mongo-store-provider.ts index 0acbbfb..60a7e7b 100644 --- a/src/store/mongo-store-provider.ts +++ b/src/store/mongo-store-provider.ts @@ -75,10 +75,6 @@ export class MongoStoreProvider implements AirhornStoreProvider { return this.mapDocumentToSubscription(updatedSubscription); } - async deleteSubscription(subscription: AirhornSubscription): Promise { - await this.deleteSubscriptionById(subscription.id); - } - async deleteSubscriptionById(id: string): Promise { await this.subscriptionsCollection.deleteOne({_id: new ObjectId(id)}); } @@ -164,10 +160,6 @@ export class MongoStoreProvider implements AirhornStoreProvider { return this.mapDocumentToNotification(updatedNotification); } - async deleteNotification(notification: AirhornNotification): Promise { - await this.deleteNotificationById(notification.id); - } - async deleteNotificationById(id: string): Promise { await this.notificationsCollection.deleteOne({_id: new ObjectId(id)}); } diff --git a/test/store/airhorn-store.test.ts b/test/store/airhorn-store.test.ts index 5bc7799..416d111 100644 --- a/test/store/airhorn-store.test.ts +++ b/test/store/airhorn-store.test.ts @@ -1,6 +1,7 @@ import {test, describe, expect} from 'vitest'; -import {AirhornStore} from '../../src/store/airhorn-store.js'; +import {type CreateAirhornNotification, AirhornStore, AirhornNotificationStatus} from '../../src/store/airhorn-store.js'; import { MongoStoreProvider } from '../../src/store/mongo-store-provider.js'; +import { AirhornProviderType } from '../../src/provider-type.js'; const mongoUri = 'mongodb://localhost:27017/airhorn'; @@ -22,4 +23,115 @@ describe('AirhornStore', async () => { expect(store.provider).toBeDefined(); expect(store.provider?.uri).toBe('mongodb://localhost:27017/airhornnew'); }); + + test('Create Subscription', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createSubscription = { + to: 'foo@bar.com', + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + }; + + const subscription = await store.createSubscription(createSubscription); + expect(subscription).toBeDefined(); + expect(subscription.id).toBeDefined(); + expect(subscription.to).toBe(createSubscription.to); + + await store.deleteSubscription(subscription); + }); + + test('Update Subscription', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createSubscription = { + to: 'foo@bar.com', + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + }; + + const subscription = await store.createSubscription(createSubscription); + expect(subscription).toBeDefined(); + subscription.templateName = 'updated-template'; + const updatedSubscription = await store.updateSubscription(subscription); + expect(updatedSubscription).toBeDefined(); + expect(updatedSubscription.id).toStrictEqual(subscription.id); + expect(updatedSubscription.templateName).toBe('updated-template'); + await store.deleteSubscriptionById(updatedSubscription.id); + }); + + test('Get Subscription by Id', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createSubscription = { + to: 'foo@bar.com', + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + }; + + const subscription = await store.createSubscription(createSubscription); + expect(subscription).toBeDefined(); + const subscriptionById = await store.getSubscriptionById(subscription.id); + expect(subscriptionById).toBeDefined(); + expect(subscriptionById.id).toStrictEqual(subscription.id); + await store.deleteSubscriptionById(subscription.id); + }); + + test('Create Notification', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createNotification: CreateAirhornNotification = { + to: 'foo@foo.com', + subscriptionId: '123', + status: AirhornNotificationStatus.QUEUED, + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + providerName: 'smtp-provider', + }; + + const notification = await store.createNotification(createNotification); + expect(notification).toBeDefined(); + expect(notification.id).toBeDefined(); + expect(notification.to).toBe(createNotification.to); + await store.deleteNotification(notification); + }); + + test('Update Notification', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createNotification: CreateAirhornNotification = { + to: 'foo@foo.com', + subscriptionId: '123', + status: AirhornNotificationStatus.QUEUED, + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + providerName: 'smtp-provider', + }; + const notification = await store.createNotification(createNotification); + notification.status = AirhornNotificationStatus.SENT; + const updatedNotification = await store.updateNotification(notification); + expect(updatedNotification).toBeDefined(); + expect(updatedNotification.id).toStrictEqual(notification.id); + expect(updatedNotification.status).toBe(AirhornNotificationStatus.SENT); + await store.deleteNotificationById(updatedNotification.id); + }); + + test('Get Notification by Id', async () => { + const provider = new MongoStoreProvider({uri: mongoUri}); + const store = new AirhornStore(provider); + const createNotification: CreateAirhornNotification = { + to: 'foo@foo.com', + subscriptionId: '123', + status: AirhornNotificationStatus.QUEUED, + templateName: 'test-template', + providerType: AirhornProviderType.SMTP, + providerName: 'smtp-provider', + }; + const notification = await store.createNotification(createNotification); + expect(notification).toBeDefined(); + const notificationById = await store.getNotificationById(notification.id); + expect(notificationById).toBeDefined(); + expect(notificationById.id).toStrictEqual(notification.id); + await store.deleteNotificationById(notification.id); + }); }); diff --git a/test/store/mongo-store-provider.test.ts b/test/store/mongo-store-provider.test.ts index dab9ce3..87ac670 100644 --- a/test/store/mongo-store-provider.test.ts +++ b/test/store/mongo-store-provider.test.ts @@ -43,7 +43,7 @@ describe('MongoStoreProvider Subscriptions', () => { expect(subscription.templateName).toBe('foo.template'); expect(subscription.providerType).toBe(AirhornProviderType.SMTP); expect(subscription.externalId).toBe('externalId'); - await provider.deleteSubscription(subscription); + await provider.deleteSubscriptionById(subscription.id); }); test('updateSubscription', async () => { @@ -53,7 +53,7 @@ describe('MongoStoreProvider Subscriptions', () => { subscription = await provider.updateSubscription(subscription); expect(subscription.to).toBe('meow@bar.com'); expect(subscription.modifiedAt.getTime()).toBeGreaterThan(subscription.createdAt.getTime()); - await provider.deleteSubscription(subscription); + await provider.deleteSubscriptionById(subscription.id); }); test('getSubscriptions', async () => { @@ -190,7 +190,7 @@ describe('MongoStoreProvider Notifications', () => { providerName: 'foo.provider', }; const notification = await provider.createNotification(createNotification); - await provider.deleteNotification(notification); + await provider.deleteNotificationById(notification.id); const result = await provider.notificationsCollection.findOne({_id: new ObjectId(notification.id)}); expect(result).toBeNull(); });