forked from novuhq/novu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
5,849 additions
and
179 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# communicat | ||
# Notifire | ||
|
||
Notification Management Framework |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.