diff --git a/src/lib/services/project-service.ts b/src/lib/services/project-service.ts index d7dfb1c200..78aa7ccffe 100644 --- a/src/lib/services/project-service.ts +++ b/src/lib/services/project-service.ts @@ -6,6 +6,8 @@ import { nameType } from '../routes/util'; import { projectSchema } from './project-schema'; import NotFoundError from '../error/notfound-error'; import { + ProjectUserAddedEvent, + ProjectUserRemovedEvent, PROJECT_CREATED, PROJECT_DELETED, PROJECT_UPDATED, @@ -283,11 +285,12 @@ export default class ProjectService { }; } - // TODO: should be an event too + // TODO: Remove the optional nature of createdBy - this in place to make sure enterprise is compatible async addUser( projectId: string, roleId: number, userId: number, + createdBy?: string, ): Promise { const [roles, users] = await this.accessService.getProjectRoleUsers( projectId, @@ -306,13 +309,22 @@ export default class ProjectService { } await this.accessService.addUserToRole(userId, role.id, projectId); + + await this.eventStore.store( + new ProjectUserAddedEvent({ + project: projectId, + createdBy, + data: { roleId, userId, roleName: role.name }, + }), + ); } - // TODO: should be an event too + // TODO: Remove the optional nature of createdBy - this in place to make sure enterprise is compatible async removeUser( projectId: string, roleId: number, userId: number, + createdBy?: string, ): Promise { const roles = await this.accessService.getRolesForProject(projectId); const role = roles.find((r) => r.id === roleId); @@ -333,6 +345,14 @@ export default class ProjectService { } await this.accessService.removeUserFromRole(userId, role.id, projectId); + + await this.eventStore.store( + new ProjectUserRemovedEvent({ + project: projectId, + createdBy, + preData: { roleId, userId, roleName: role.name }, + }), + ); } async getMembers(projectId: string): Promise { diff --git a/src/lib/types/events.ts b/src/lib/types/events.ts index 5f4e29b855..bdf8b37bc1 100644 --- a/src/lib/types/events.ts +++ b/src/lib/types/events.ts @@ -39,6 +39,8 @@ export const PROJECT_CREATED = 'project-created'; export const PROJECT_UPDATED = 'project-updated'; export const PROJECT_DELETED = 'project-deleted'; export const PROJECT_IMPORT = 'project-import'; +export const PROJECT_USER_ADDED = 'project-user-added'; +export const PROJECT_USER_REMOVED = 'project-user-removed'; export const DROP_PROJECTS = 'drop-projects'; export const TAG_CREATED = 'tag-created'; export const TAG_DELETED = 'tag-deleted'; @@ -378,3 +380,35 @@ export class FeatureStrategyRemoveEvent extends BaseEvent { this.preData = preData; } } + +export class ProjectUserAddedEvent extends BaseEvent { + readonly project: string; + + readonly data: any; + + readonly preData: any; + + constructor(p: { project: string; createdBy: string; data: any }) { + super(PROJECT_USER_ADDED, p.createdBy); + const { project, data } = p; + this.project = project; + this.data = data; + this.preData = null; + } +} + +export class ProjectUserRemovedEvent extends BaseEvent { + readonly project: string; + + readonly data: any; + + readonly preData: any; + + constructor(p: { project: string; createdBy: string; preData: any }) { + super(PROJECT_USER_REMOVED, p.createdBy); + const { project, preData } = p; + this.project = project; + this.data = null; + this.preData = preData; + } +}