Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed Aug 26, 2021
1 parent 9dff21c commit 27be647
Show file tree
Hide file tree
Showing 22 changed files with 5,849 additions and 179 deletions.
64 changes: 0 additions & 64 deletions .circleci/config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"plugin:eslint-comments/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"plugin:functional/lite",
"prettier",
"prettier/@typescript-eslint"
],
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
"rules": {
"functional/no-class": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"eslint-comments/disable-enable-pair": [
"error",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# communicat
# Notifire

Notification Management Framework
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "communicat",
"name": "@notifire/core",
"version": "1.0.0",
"description": "Notification Management Framework",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
"module": "build/module/index.js",
"repository": "https://github.com/scopsy/communicat",
"repository": "https://github.com/notifire/core",
"license": "MIT",
"keywords": [],
"scripts": {
Expand Down Expand Up @@ -41,12 +41,11 @@
"engines": {
"node": ">=10"
},
"dependencies": {
"@bitauth/libauth": "^1.17.1"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@sendgrid/mail": "^7.4.6",
"@types/node": "^16.7.2",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1",
"ava": "^3.12.1",
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './lib/async';
export * from './lib/number';
export * from './lib/notifire';
export * from './lib/notifire.interface';
export * from './lib/template/template.interface';
export * from './providers/sendgrid.provider';
7 changes: 0 additions & 7 deletions src/lib/async.spec.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/lib/async.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/lib/content/content.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as Handlebars from 'handlebars';

import { ITriggerPayload } from '../template/template.interface';

Handlebars.registerHelper('equals', function (arg1, arg2, options) {
return arg1 == arg2 ? options.fn(this) : options.inverse(this);
});

export function compileTemplate(content: string, data: ITriggerPayload) {
// const variables = getHandlebarsVariables(content);
const template = Handlebars.compile(content);

return template(data);
}

/*function getHandlebarsVariables(input: string): string[] {
const ast: hbs.AST.Program = Handlebars.parseWithoutProcessing(input);
return ast.body
.filter(({ type }: hbs.AST.Statement) => type === 'MustacheStatement')
.map((statement: hbs.AST.Statement) => {
const moustacheStatement: hbs.AST.MustacheStatement =
statement as hbs.AST.MustacheStatement;
const paramsExpressionList =
moustacheStatement.params as hbs.AST.PathExpression[];
const pathExpression = moustacheStatement.path as hbs.AST.PathExpression;
return paramsExpressionList[0]?.original || pathExpression.original;
});
}*/
14 changes: 14 additions & 0 deletions src/lib/handler/email.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { compileTemplate } from '../content/content.engine';
import { IEmailProvider } from '../provider/provider.interface';
import { IMessage, ITriggerPayload } from '../template/template.interface';

export class EmailHandler {
constructor(private message: IMessage, private provider: IEmailProvider) {}

async send(data: ITriggerPayload) {
const html = compileTemplate(this.message.template, data);
const subject = compileTemplate(this.message.subject || '', data);

await this.provider.sendMessage(data.$email, subject, html, data);
}
}
13 changes: 13 additions & 0 deletions src/lib/handler/sms.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { compileTemplate } from '../content/content.engine';
import { ISmsProvider } from '../provider/provider.interface';
import { IMessage, ITriggerPayload } from '../template/template.interface';

export class SmsHandler {
constructor(private message: IMessage, private provider: ISmsProvider) {}

async send(data: ITriggerPayload) {
const html = compileTemplate(this.message.template, data);

await this.provider.sendMessage(data.$phone, html);
}
}
12 changes: 12 additions & 0 deletions src/lib/notifire.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ProviderStore } from './provider/provider.store';
import { TemplateStore } from './template/template.store';

export interface INotifireConfig {
channels?: {
email?: {
from?: { name: string; email: string };
};
};
templateStore?: TemplateStore;
providerStore?: ProviderStore;
}
5 changes: 5 additions & 0 deletions src/lib/notifire.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from 'ava';

test('getABC', async () => {
// t.deepEqual(await te(), ['a', 'b', 'c']);
});
60 changes: 60 additions & 0 deletions src/lib/notifire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { EmailHandler } from './handler/email.handler';
import { SmsHandler } from './handler/sms.handler';
import { INotifireConfig } from './notifire.interface';
import { IEmailProvider, ISmsProvider } from './provider/provider.interface';
import { ProviderStore } from './provider/provider.store';
import {
ChannelTypeEnum,
ITemplate,
ITriggerPayload,
} from './template/template.interface';
import { TemplateStore } from './template/template.store';

export class Notifire {
private templateStore: TemplateStore;
private providerStore: ProviderStore;

constructor(private config?: INotifireConfig) {
this.templateStore = this.config?.templateStore || new TemplateStore();
this.providerStore = this.config?.providerStore || new ProviderStore();
}

async registerTemplate(template: ITemplate) {
await this.templateStore.addTemplate(template);

return await this.templateStore.getTemplateById(template.id);
}

async registerProvider(provider: IEmailProvider | ISmsProvider) {
await this.providerStore.addProvider(provider);
}

async trigger(eventId: string, data: ITriggerPayload) {
const template = await this.templateStore.getTemplateById(eventId);
if (!template) {
throw new Error(
`Template on event: ${eventId} was not found in the template store`
);
}

for (const message of template.messages) {
const provider = await this.providerStore.getProviderByChannel(
message.channel
);

if (!provider) {
throw new Error(
`Provider for ${message.channel} channel was not found`
);
}

if (provider.channelType === ChannelTypeEnum.EMAIL) {
const emailHandler = new EmailHandler(message, provider);
await emailHandler.send(data);
} else if (provider.channelType === ChannelTypeEnum.SMS) {
const smsHandler = new SmsHandler(message, provider);
await smsHandler.send(data);
}
}
}
}
11 changes: 0 additions & 11 deletions src/lib/number.spec.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/lib/number.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/lib/provider/provider.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
ChannelTypeEnum,
ITriggerPayload,
} from '../template/template.interface';

export interface IProvider {
id: string;
channelType: ChannelTypeEnum;
}

export interface IEmailProvider extends IProvider {
channelType: ChannelTypeEnum.EMAIL;

sendMessage(
to: string,
subject: string,
html: string,
attributes: ITriggerPayload
): Promise<any>;
}

export interface ISmsProvider extends IProvider {
sendMessage(to: string, content: string): Promise<any>;

channelType: ChannelTypeEnum.SMS;
}
Loading

0 comments on commit 27be647

Please sign in to comment.