Skip to content

Commit

Permalink
Fix/add project events (#1267)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
sighphyre and ivarconr committed Jan 14, 2022
1 parent e164e3d commit dc920e8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/lib/services/project-service.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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<void> {
const [roles, users] = await this.accessService.getProjectRoleUsers(
projectId,
Expand All @@ -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<void> {
const roles = await this.accessService.getRolesForProject(projectId);
const role = roles.find((r) => r.id === roleId);
Expand All @@ -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<number> {
Expand Down
34 changes: 34 additions & 0 deletions src/lib/types/events.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}

1 comment on commit dc920e8

@vercel
Copy link

@vercel vercel bot commented on dc920e8 Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.