1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

Fix/add project events (#1267)

* fix: wip project events

* fix: Include deletion events for when a user is removed from a project role

Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
This commit is contained in:
sighphyre 2022-01-14 13:14:02 +02:00 committed by GitHub
parent e164e3d835
commit dc920e85ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import { nameType } from '../routes/util';
import { projectSchema } from './project-schema'; import { projectSchema } from './project-schema';
import NotFoundError from '../error/notfound-error'; import NotFoundError from '../error/notfound-error';
import { import {
ProjectUserAddedEvent,
ProjectUserRemovedEvent,
PROJECT_CREATED, PROJECT_CREATED,
PROJECT_DELETED, PROJECT_DELETED,
PROJECT_UPDATED, 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( async addUser(
projectId: string, projectId: string,
roleId: number, roleId: number,
userId: number, userId: number,
createdBy?: string,
): Promise<void> { ): Promise<void> {
const [roles, users] = await this.accessService.getProjectRoleUsers( const [roles, users] = await this.accessService.getProjectRoleUsers(
projectId, projectId,
@ -306,13 +309,22 @@ export default class ProjectService {
} }
await this.accessService.addUserToRole(userId, role.id, projectId); 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( async removeUser(
projectId: string, projectId: string,
roleId: number, roleId: number,
userId: number, userId: number,
createdBy?: string,
): Promise<void> { ): Promise<void> {
const roles = await this.accessService.getRolesForProject(projectId); const roles = await this.accessService.getRolesForProject(projectId);
const role = roles.find((r) => r.id === roleId); 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.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<number> { async getMembers(projectId: string): Promise<number> {

View File

@ -39,6 +39,8 @@ export const PROJECT_CREATED = 'project-created';
export const PROJECT_UPDATED = 'project-updated'; export const PROJECT_UPDATED = 'project-updated';
export const PROJECT_DELETED = 'project-deleted'; export const PROJECT_DELETED = 'project-deleted';
export const PROJECT_IMPORT = 'project-import'; 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 DROP_PROJECTS = 'drop-projects';
export const TAG_CREATED = 'tag-created'; export const TAG_CREATED = 'tag-created';
export const TAG_DELETED = 'tag-deleted'; export const TAG_DELETED = 'tag-deleted';
@ -378,3 +380,35 @@ export class FeatureStrategyRemoveEvent extends BaseEvent {
this.preData = preData; 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;
}
}