From e22c8de753ac8d39f480e6707542e10c5043ab8c Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Mon, 26 Aug 2024 09:02:09 -0700 Subject: [PATCH] airhorn store crud methods for subscription and notification --- src/store/airhorn-store.ts | 23 +++++++++++++- test/store/airhorn-store.test.ts | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/store/airhorn-store.ts b/src/store/airhorn-store.ts index 478507a..78047f5 100644 --- a/src/store/airhorn-store.ts +++ b/src/store/airhorn-store.ts @@ -54,7 +54,7 @@ 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; @@ -91,4 +91,25 @@ 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._provider.deleteSubscription(subscription); + } + + public async deleteSubscriptionById(id: string): Promise { + return this._provider.deleteSubscriptionById(id); + } + + public async getSubscriptionById(id: string): Promise { + return this._provider.getSubscriptionById(id); + } } + diff --git a/test/store/airhorn-store.test.ts b/test/store/airhorn-store.test.ts index 5bc7799..6273b36 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 { MongoStoreProvider } from '../../src/store/mongo-store-provider.js'; +import { AirhornProviderType } from '../../src/provider-type.js'; const mongoUri = 'mongodb://localhost:27017/airhorn'; @@ -22,4 +23,57 @@ 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); + }); });