Skip to content

Commit

Permalink
Merge pull request #391 from jaredwray/moving-to-createAirhorn-with-t…
Browse files Browse the repository at this point in the history
…emplates-being-loaded

moving to createAirhorn with templates being loaded
  • Loading branch information
jaredwray authored Nov 28, 2024
2 parents 9d27ef3 + ee2d51b commit d56bc3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 56 deletions.
22 changes: 12 additions & 10 deletions src/airhorn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
} from './store.js';
import { AirhornTemplateSync } from './template-sync.js';

export type CreateAirhornOptions = {
TEMPLATE_PATH: string;
} & AirhornOptions;

export type AirhornOptions = {
TEMPLATE_PATH?: string;
DEFAULT_TEMPLATE_LANGUAGE?: string;
TWILIO_SMS_ACCOUNT_SID?: string;
TWILIO_SMS_AUTH_TOKEN?: string;
Expand All @@ -22,7 +25,6 @@ export type AirhornOptions = {

export class Airhorn {
options: AirhornOptions = {
TEMPLATE_PATH: './templates',
DEFAULT_TEMPLATE_LANGUAGE: 'en',
};

Expand Down Expand Up @@ -100,17 +102,17 @@ export class Airhorn {
public async sendMobilePush(to: string, from: string, templateName: string, data?: any, languageCode?: string): Promise<boolean> {
return this.send(to, from, templateName, AirhornProviderType.MOBILE_PUSH, data, languageCode);
}
}

public async syncTemplates(templatesPath?: string): Promise<void> {
const fullTemplatesPath = templatesPath ?? this.options.TEMPLATE_PATH;
if (!fullTemplatesPath) {
throw new Error('No template path provided');
}

const templateSync = new AirhornTemplateSync(fullTemplatesPath, this._store, this.options.DEFAULT_TEMPLATE_LANGUAGE);
export const createAirhorn = async (options?: CreateAirhornOptions) => {
const airhorn = new Airhorn(options);
if (options && options.TEMPLATE_PATH) {
const templateSync = new AirhornTemplateSync(options.TEMPLATE_PATH, airhorn.store, airhorn.options.DEFAULT_TEMPLATE_LANGUAGE);
await templateSync.sync();
}
}

return airhorn;
};

export { AirhornProviderType } from './provider-type.js';
export {
Expand Down
60 changes: 14 additions & 46 deletions test/airhorn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from 'vitest';
import type * as admin from 'firebase-admin';
import {AirhornProviderType} from '../src/provider-type.js';
import {Airhorn} from '../src/airhorn.js';
import {Airhorn, createAirhorn} from '../src/airhorn.js';
import {FirebaseMessaging} from '../src/providers/firebase-messaging.js';
import { MongoStoreProvider } from '../src/store-providers/mongo.js';
import {TestingData, TestingDataTwo} from './testing-data.js';
Expand Down Expand Up @@ -38,40 +38,18 @@ describe('Airhorn', async () => {
expect(new Airhorn()).toEqual(new Airhorn());
});

test('Airhorn - Get Providers', () => {
const options = { TEMPLATE_PATH: './foo/templates' };
const airhorn = new Airhorn(options);

expect(airhorn.providers.options.TEMPLATE_PATH).toEqual(options.TEMPLATE_PATH);
});

test('Airhorn - Get Templates', async () => {
const options = { TEMPLATE_PATH: './foo/templates' };
const airhorn = new Airhorn(options);
const options = { TEMPLATE_PATH: './test/templates' };
const airhorn = await createAirhorn(options);

expect(airhorn.templates).toBeDefined();
});

test('Airhorn - sync template throws due to invalid path', async () => {
const airhorn = new Airhorn();
airhorn.options.TEMPLATE_PATH = undefined;
await expect(airhorn.syncTemplates()).rejects.toThrow();
});

test('Airhorn - Options Validated in Config', () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);

expect(airhorn.options.TEMPLATE_PATH).toEqual(options.TEMPLATE_PATH);
});

test('Airhorn - Get Provider By Type', () => {
test('Airhorn - Get Provider By Type', async () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);
const airhorn = await createAirhorn(options);

expect(airhorn.providers.getProviderByType(AirhornProviderType.WEBHOOK).length).toEqual(1);
});
Expand All @@ -80,8 +58,7 @@ describe('Airhorn', async () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const userData = new TestingDataTwo();

Expand All @@ -92,8 +69,7 @@ describe('Airhorn', async () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const userData = new TestingDataTwo();

Expand All @@ -102,7 +78,6 @@ describe('Airhorn', async () => {

test('Airhorn - Get Loaded Providers', () => {
const airhorn = new Airhorn({
TEMPLATE_PATH: './test/templates',
TWILIO_SMS_ACCOUNT_SID: 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
TWILIO_SMS_AUTH_TOKEN: 'baz',
TWILIO_SENDGRID_API_KEY: 'foo',
Expand All @@ -116,8 +91,7 @@ describe('Airhorn', async () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const userData = new TestingData();

Expand All @@ -138,8 +112,7 @@ describe('Airhorn', async () => {
TWILIO_SENDGRID_API_KEY: 'SG.test-key',
};

const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

airhorn.send = vi.fn().mockReturnValue(true) as any;
const userData = new TestingData();
Expand All @@ -151,8 +124,7 @@ describe('Airhorn', async () => {
const options = {
TEMPLATE_PATH: './test/templates',
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const userData = new TestingData();

Expand All @@ -172,8 +144,7 @@ describe('Airhorn', async () => {
TEMPLATE_PATH: './test/templates',
FIREBASE_CERT,
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const notification = {
title: 'Airhorn',
Expand All @@ -195,8 +166,7 @@ describe('Airhorn', async () => {
TEMPLATE_PATH: './test/templates',
FIREBASE_CERT,
};
const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

const notification = {
title: 'Airhorn',
Expand All @@ -218,8 +188,7 @@ describe('Airhorn', async () => {
TEMPLATE_PATH: './test/templates',
};

const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

airhorn.providers.removeProvider('firebase-messaging');

Expand All @@ -239,8 +208,7 @@ describe('Airhorn', async () => {
TEMPLATE_PATH: './test/templates',
};

const airhorn = new Airhorn(options);
await airhorn.syncTemplates();
const airhorn = await createAirhorn(options);

airhorn.providers.removeProvider('firebase-messaging');

Expand Down

0 comments on commit d56bc3f

Please sign in to comment.