Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Add new event for pre email #461

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/definition/email/IEmailDescriptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export interface IEmailDescriptor {
from?: string | undefined;
to?: string | Array<string> | undefined;
graywolf336 marked this conversation as resolved.
Show resolved Hide resolved
cc?: string | Array<string> | undefined;
bcc?: string | Array<string> | undefined;
replyTo?: string | Array<string> | undefined;
subject?: string | undefined;
text?: string | undefined;
html?: string | undefined;
headers?: Record<string, string> | undefined;
}
25 changes: 25 additions & 0 deletions src/definition/email/IPreEmailSent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IEmailDescriptor, IPreEmailSentContext } from '.';
import { IHttp, IModify, IPersistence, IRead } from '../accessors';
import { AppMethod } from '../metadata';

/**
* Event interface that allows apps to
* register as a handler of of the `IPreEmailSent`
* event.
*
* This event is trigger before the mailer sends
* an email.
*
* To prevent the email from being sent, you can
* throw an error with a message specifying the
* reason for rejection.
*/
export interface IPreEmailSent {
[AppMethod.EXECUTE_PRE_EMAIL_SENT](
context: IPreEmailSentContext,
read: IRead,
http: IHttp,
persis: IPersistence,
modify: IModify,
): Promise<IEmailDescriptor>;
d-gubert marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 6 additions & 0 deletions src/definition/email/IPreEmailSentContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IEmailDescriptor } from './IEmailDescriptor';

export interface IPreEmailSentContext {
context: unknown;
email: IEmailDescriptor;
}
2 changes: 2 additions & 0 deletions src/definition/email/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './IEmailDescriptor';
export * from './IPreEmailSentContext';
2 changes: 2 additions & 0 deletions src/definition/metadata/AppInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ export enum AppInterface {
IPostLivechatRoomSaved = 'IPostLivechatRoomSaved',
// FileUpload
IPreFileUpload = 'IPreFileUpload',
// Email
IPreEmailSent = 'IPreEmailSent',
}
2 changes: 2 additions & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ export enum AppMethod {
EXECUTE_POST_LIVECHAT_ROOM_SAVED = 'executePostLivechatRoomSaved',
// FileUpload
EXECUTE_PRE_FILE_UPLOAD = 'executePreFileUpload',
// Email
EXECUTE_PRE_EMAIL_SENT = 'executePreEmailSent',
}
33 changes: 31 additions & 2 deletions src/server/managers/AppListenerManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IEmailDescriptor, IPreEmailSentContext } from '../../definition/email';
import { EssentialAppDisabledException } from '../../definition/exceptions';
import { IExternalComponent } from '../../definition/externalComponent';
import { ILivechatEventContext, ILivechatRoom, ILivechatTransferEventContext, IVisitor } from '../../definition/livechat';
Expand Down Expand Up @@ -33,7 +34,8 @@ type EventData = (
IRoomUserJoinedContext |
IRoomUserLeaveContext |
ILivechatTransferEventContext |
IFileUploadContext
IFileUploadContext |
IPreEmailSentContext
);

type EventReturn = (
Expand All @@ -43,7 +45,8 @@ type EventReturn = (
IRoom |
IUser |
IUIKitResponse |
ILivechatRoom
ILivechatRoom |
IEmailDescriptor
);

export class AppListenerManager {
Expand Down Expand Up @@ -224,6 +227,9 @@ export class AppListenerManager {
// FileUpload
case AppInterface.IPreFileUpload:
return this.executePreFileUpload(data as IFileUploadContext);
// Email
case AppInterface.IPreEmailSent:
return this.executePreEmailSent(data as IPreEmailSentContext);
default:
console.warn('An invalid listener was called');
return;
Expand Down Expand Up @@ -1147,4 +1153,27 @@ export class AppListenerManager {
}
}
}

private async executePreEmailSent(data: IPreEmailSentContext): Promise<IEmailDescriptor> {
let descriptor = data.email;

for (const appId of this.listeners.get(AppInterface.IPreEmailSent)) {
const app = this.manager.getOneById(appId);

if (app.hasMethod(AppMethod.EXECUTE_PRE_EMAIL_SENT)) {
descriptor = await app.call(AppMethod.EXECUTE_PRE_EMAIL_SENT,
{
context: data.context,
email: descriptor,
},
this.am.getReader(appId),
this.am.getHttp(appId),
this.am.getPersistence(appId),
this.am.getModifier(appId),
);
}
}

return descriptor;
}
}