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 events after user has been created, updated and deleted #499

Merged
merged 8 commits into from
May 2, 2022
3 changes: 3 additions & 0 deletions src/definition/metadata/AppInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ export enum AppInterface {
IPreFileUpload = 'IPreFileUpload',
// Email
IPreEmailSent = 'IPreEmailSent',
IPostUserCreated = 'IPostUserCreated',
IPostUserUpdated = 'IPostUserUpdated',
IPostUserDeleted = 'IPostUserDeleted',
}
4 changes: 4 additions & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ export enum AppMethod {
EXECUTE_PRE_FILE_UPLOAD = 'executePreFileUpload',
// Email
EXECUTE_PRE_EMAIL_SENT = 'executePreEmailSent',
// User
EXECUTE_POST_USER_CREATED = 'executePostUserCreated',
EXECUTE_POST_USER_UPDATED = 'executePostUserUpdated',
EXECUTE_POST_USER_DELETED = 'executePostUserDeleted',
}
21 changes: 21 additions & 0 deletions src/definition/users/IPostUserCreated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IHttp, IModify, IPersistence, IRead } from '../accessors';
import { AppMethod } from '../metadata';
import { IUserContext } from './IUserContext';

/**
* Event interface that allows an app to
* register as a handler of the `IPostUserCreated`
* event
*
* This event is triggered *after* the
* user has been saved to the database.
*/
export interface IPostUserCreated {
[AppMethod.EXECUTE_POST_USER_CREATED](
context: IUserContext,
read: IRead,
http: IHttp,
persis: IPersistence,
modify: IModify,
): Promise<void>;
}
21 changes: 21 additions & 0 deletions src/definition/users/IPostUserDeleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IHttp, IModify, IPersistence, IRead } from '../accessors';
import { AppMethod } from '../metadata';
import { IUserContext } from './IUserContext';

/**
* Event interface that allows an app to
* register as a handler of the `IPostUserDeleted`
* event
*
* This event is triggered *after* the
* user has been removed from the database.
*/
export interface IPostUserDeleted {
[AppMethod.EXECUTE_POST_USER_DELETED](
context: IUserContext,
read: IRead,
http: IHttp,
persis: IPersistence,
modify: IModify,
): Promise<void>;
}
21 changes: 21 additions & 0 deletions src/definition/users/IPostUserUpdated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IHttp, IModify, IPersistence, IRead } from '../accessors';
import { AppMethod } from '../metadata';
import { IUserUpdateContext } from './IUserUpdateContext';

/**
* Event interface that allows an app to
* register as a handler of the `IPostUserUpdated`
* event
*
* This event is triggered *after* the
* user has been saved to the database.
*/
export interface IPostUserUpdated {
[AppMethod.EXECUTE_POST_USER_UPDATED](
context: IUserUpdateContext,
read: IRead,
http: IHttp,
persis: IPersistence,
modify: IModify,
): Promise<void>;
}
19 changes: 19 additions & 0 deletions src/definition/users/IUserContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IUser } from './IUser';

/**
* The context of execution for the following events:
* - IPostUserCreated
* - IPostUserDeleted
*/
export interface IUserContext {
/**
* The user who has been affected
* by the action
*/
user: IUser;
/**
* The user who performed the
* action. Null if it's the user himself
*/
performedBy?: IUser;
}
24 changes: 24 additions & 0 deletions src/definition/users/IUserUpdateContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IUser } from './IUser';

/**
* The context of execution for the
* IPostUserUpdated event
*/
export interface IUserUpdateContext {
/**
* The user who has been affected
* by the action
*/
user: IUser;

/**
* The user who performed the
* update. Null if it's the user himself
*/
performedBy?: IUser;

/**
* The user data before the update
*/
previousData?: IUser;
}
18 changes: 17 additions & 1 deletion src/definition/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { IPostUserCreated } from './IPostUserCreated';
import { IPostUserDeleted } from './IPostUserDeleted';
import { IPostUserUpdated } from './IPostUserUpdated';
import { IUser } from './IUser';
import { IUserContext } from './IUserContext';
import { IUserCreationOptions } from './IUserCreationOptions';
import { IUserEmail } from './IUserEmail';
import { IUserUpdateContext } from './IUserUpdateContext';
import { UserStatusConnection } from './UserStatusConnection';
import { UserType } from './UserType';

export { IUser, IUserEmail, IUserCreationOptions, UserStatusConnection, UserType };
export {
IUser,
IUserEmail,
IUserCreationOptions,
UserStatusConnection,
UserType,
IPostUserCreated,
IPostUserUpdated,
IPostUserDeleted,
IUserContext,
IUserUpdateContext,
};
63 changes: 62 additions & 1 deletion src/server/managers/AppListenerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
UIKitViewSubmitInteractionContext,
} from '../../definition/uikit/UIKitInteractionContext';
import { IFileUploadContext } from '../../definition/uploads/IFileUploadContext';
import { IUser } from '../../definition/users';
import { IUser, IUserContext, IUserUpdateContext } from '../../definition/users';
import { MessageBuilder, MessageExtender, RoomBuilder, RoomExtender } from '../accessors';
import { AppManager } from '../AppManager';
import { Message } from '../messages/Message';
Expand Down Expand Up @@ -234,6 +234,13 @@ export class AppListenerManager {
// Email
case AppInterface.IPreEmailSent:
return this.executePreEmailSent(data as IPreEmailSentContext);
// User
case AppInterface.IPostUserCreated:
return this.executePostUserCreated(data as IUserContext);
case AppInterface.IPostUserUpdated:
return this.executePostUserUpdated(data as IUserContext);
case AppInterface.IPostUserDeleted:
return this.executePostUserDeleted(data as IUserContext);
default:
console.warn('An invalid listener was called');
return;
Expand Down Expand Up @@ -1196,4 +1203,58 @@ export class AppListenerManager {

return descriptor;
}

private async executePostUserCreated(data: IUserContext): Promise<void> {
const context = Utilities.deepFreeze(data);

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

if (app.hasMethod(AppMethod.EXECUTE_POST_USER_CREATED)) {
await app.call(AppMethod.EXECUTE_POST_USER_CREATED,
context,
this.am.getReader(appId),
this.am.getHttp(appId),
this.am.getPersistence(appId),
this.am.getModifier(appId),
);
}
}
}

private async executePostUserUpdated(data: IUserUpdateContext): Promise<void> {
const context = Utilities.deepFreeze(data);

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

if (app.hasMethod(AppMethod.EXECUTE_POST_USER_UPDATED)) {
await app.call(AppMethod.EXECUTE_POST_USER_UPDATED,
context,
this.am.getReader(appId),
this.am.getHttp(appId),
this.am.getPersistence(appId),
this.am.getModifier(appId),
);
}
}
}

private async executePostUserDeleted(data: IUserContext): Promise<void> {
const context = Utilities.deepFreeze(data);

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

if (app.hasMethod(AppMethod.EXECUTE_POST_USER_DELETED)) {
await app.call(AppMethod.EXECUTE_POST_USER_DELETED,
context,
this.am.getReader(appId),
this.am.getHttp(appId),
this.am.getPersistence(appId),
this.am.getModifier(appId),
);
}
}
}
}