Skip to content

Commit

Permalink
airhorn store crud methods for subscription and notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Aug 26, 2024
1 parent 572974c commit e22c8de
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/store/airhorn-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type CreateAirhornNotification = {
export type AirhornStoreProvider = {
name: string;
uri: string;
createSubscription(subscription: CreateAirhornNotification): Promise<AirhornSubscription>;
createSubscription(subscription: CreateAirhornSubscription): Promise<AirhornSubscription>;
updateSubscription(notification: AirhornSubscription): Promise<AirhornSubscription>;
deleteSubscription(notification: AirhornSubscription): Promise<void>;
deleteSubscriptionById(id: string): Promise<void>;
Expand Down Expand Up @@ -91,4 +91,25 @@ export class AirhornStore {
public set provider(provider: AirhornStoreProvider) {
this._provider = provider;
}

public async createSubscription(subscription: CreateAirhornSubscription): Promise<AirhornSubscription> {
return this._provider.createSubscription(subscription);
}

public async updateSubscription(subscription: AirhornSubscription): Promise<AirhornSubscription> {
return this._provider.updateSubscription(subscription);
}

public async deleteSubscription(subscription: AirhornSubscription): Promise<void> {
return this._provider.deleteSubscription(subscription);
}

public async deleteSubscriptionById(id: string): Promise<void> {
return this._provider.deleteSubscriptionById(id);
}

public async getSubscriptionById(id: string): Promise<AirhornSubscription> {
return this._provider.getSubscriptionById(id);
}
}

54 changes: 54 additions & 0 deletions test/store/airhorn-store.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});

0 comments on commit e22c8de

Please sign in to comment.