Skip to content

Commit

Permalink
Merge pull request #350 from jaredwray/airhorn-store-crud-methods-for…
Browse files Browse the repository at this point in the history
…-subscription-and-notification

airhorn store crud methods for subscription and notification
  • Loading branch information
jaredwray authored Aug 26, 2024
2 parents 572974c + 4d1d0ea commit 32cddcf
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 15 deletions.
45 changes: 42 additions & 3 deletions src/store/airhorn-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ 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>;
getSubscriptions(): Promise<AirhornSubscription[]>;
getSubscriptionById(id: string): Promise<AirhornSubscription>;
Expand All @@ -66,7 +65,6 @@ export type AirhornStoreProvider = {
getSubscriptionsByProviderType(providerType: AirhornProviderType): Promise<AirhornSubscription[]>;
createNotification(notification: CreateAirhornNotification): Promise<AirhornNotification>;
updateNotification(status: AirhornNotification): Promise<AirhornNotification>;
deleteNotification(status: AirhornNotification): Promise<void>;
deleteNotificationById(id: string): Promise<void>;
getNotifications(): Promise<AirhornNotification[]>;
getNotificationById(id: string): Promise<AirhornNotification>;
Expand All @@ -91,4 +89,45 @@ 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.deleteSubscriptionById(subscription.id);
}

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

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

public async createNotification(notification: CreateAirhornNotification): Promise<AirhornNotification> {
return this._provider.createNotification(notification);
}

public async updateNotification(notification: AirhornNotification): Promise<AirhornNotification> {
return this._provider.updateNotification(notification);
}

public async deleteNotification(notification: AirhornNotification): Promise<void> {
return this.deleteNotificationById(notification.id);
}

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

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

8 changes: 0 additions & 8 deletions src/store/mongo-store-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export class MongoStoreProvider implements AirhornStoreProvider {
return this.mapDocumentToSubscription(updatedSubscription);
}

async deleteSubscription(subscription: AirhornSubscription): Promise<void> {
await this.deleteSubscriptionById(subscription.id);
}

async deleteSubscriptionById(id: string): Promise<void> {
await this.subscriptionsCollection.deleteOne({_id: new ObjectId(id)});
}
Expand Down Expand Up @@ -164,10 +160,6 @@ export class MongoStoreProvider implements AirhornStoreProvider {
return this.mapDocumentToNotification(updatedNotification);
}

async deleteNotification(notification: AirhornNotification): Promise<void> {
await this.deleteNotificationById(notification.id);
}

async deleteNotificationById(id: string): Promise<void> {
await this.notificationsCollection.deleteOne({_id: new ObjectId(id)});
}
Expand Down
114 changes: 113 additions & 1 deletion 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 {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';

Expand All @@ -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: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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);
});
});
6 changes: 3 additions & 3 deletions test/store/mongo-store-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -53,7 +53,7 @@ describe('MongoStoreProvider Subscriptions', () => {
subscription = await provider.updateSubscription(subscription);
expect(subscription.to).toBe('[email protected]');
expect(subscription.modifiedAt.getTime()).toBeGreaterThan(subscription.createdAt.getTime());
await provider.deleteSubscription(subscription);
await provider.deleteSubscriptionById(subscription.id);
});

test('getSubscriptions', async () => {
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit 32cddcf

Please sign in to comment.