Skip to content

Commit

Permalink
fixes in rocketChat, dashboard and useCases in deletion module
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Jan 19, 2024
1 parent 5879343 commit 0544706
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe(DeletionRequestUc.name, () => {
const { deletionRequestToExecute, rocketChatUser } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);
rocketChatUserService.findByUserId.mockResolvedValueOnce(rocketChatUser);
rocketChatUserService.findByUserId.mockResolvedValueOnce([rocketChatUser]);

await uc.executeDeletionRequests();

Expand All @@ -418,7 +418,7 @@ describe(DeletionRequestUc.name, () => {
const { deletionRequestToExecute, rocketChatUser } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);
rocketChatUserService.findByUserId.mockResolvedValueOnce(rocketChatUser);
rocketChatUserService.findByUserId.mockResolvedValueOnce([rocketChatUser]);

await uc.executeDeletionRequests();

Expand Down
17 changes: 11 additions & 6 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export class DeletionRequestUc {
this.removeUserFromTeams(deletionRequest),
this.removeUser(deletionRequest),
this.removeUserFromRocketChat(deletionRequest),
this.removeUserRegistrationPin(deletionRequest),
this.removeUsersDashboard(deletionRequest),
this.removeUserFromTasks(deletionRequest),
]);
Expand Down Expand Up @@ -144,7 +143,7 @@ export class DeletionRequestUc {
await this.logDeletion(deletionRequest, DomainModel.ACCOUNT, DeletionOperationModel.DELETE, 0, 1);
}

private async removeUserRegistrationPin(deletionRequest: DeletionRequest) {
private async removeUserRegistrationPin(deletionRequest: DeletionRequest): Promise<number[]> {
const userToDeletion = await this.userService.findById(deletionRequest.targetRefId);
const parentEmails = await this.userService.getParentEmailsFromUser(deletionRequest.targetRefId);
const emailsToDeletion: string[] = [userToDeletion.email, ...parentEmails];
Expand All @@ -161,6 +160,8 @@ export class DeletionRequestUc {
0,
deletedRegistrationPin
);

return result;
}

private async removeUserFromClasses(deletionRequest: DeletionRequest) {
Expand Down Expand Up @@ -255,8 +256,12 @@ export class DeletionRequestUc {
private async removeUser(deletionRequest: DeletionRequest) {
this.logger.debug({ action: 'removeUser', deletionRequest });

const userDeleted: number = await this.userService.deleteUser(deletionRequest.targetRefId);
await this.logDeletion(deletionRequest, DomainModel.USER, DeletionOperationModel.DELETE, 0, userDeleted);
const registrationPinDeleted = await this.removeUserRegistrationPin(deletionRequest);

if (registrationPinDeleted) {
const userDeleted: number = await this.userService.deleteUser(deletionRequest.targetRefId);
await this.logDeletion(deletionRequest, DomainModel.USER, DeletionOperationModel.DELETE, 0, userDeleted);
}
}

private async removeUserFromRocketChat(deletionRequest: DeletionRequest) {
Expand All @@ -265,8 +270,8 @@ export class DeletionRequestUc {
const rocketChatUser = await this.rocketChatUserService.findByUserId(deletionRequest.targetRefId);

const [, rocketChatUserDeleted] = await Promise.all([
this.rocketChatService.deleteUser(rocketChatUser.username),
this.rocketChatUserService.deleteByUserId(rocketChatUser.userId),
this.rocketChatService.deleteUser(rocketChatUser[0].username),
this.rocketChatUserService.deleteByUserId(rocketChatUser[0].userId),
]);
await this.logDeletion(
deletionRequest,
Expand Down
10 changes: 7 additions & 3 deletions apps/server/src/modules/learnroom/service/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export class DashboardService {
StatusModel.PENDING
)
);
const usersDashboard = await this.dashboardRepo.getUsersDashboard(userId);
await this.dashboardElementRepo.deleteByDashboardId(usersDashboard.id);
const result = await this.dashboardRepo.deleteDashboardByUserId(userId);
let result = 0;
const usersDashboard = await this.dashboardRepo.getUsersDashboardIfExist(userId);
if (usersDashboard !== null) {
await this.dashboardElementRepo.deleteByDashboardId(usersDashboard.id);
result = await this.dashboardRepo.deleteDashboardByUserId(userId);
}

this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully deleted user data from Dashboard',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ describe(RocketChatUserMapper.name, () => {
});
});

describe('mapToDOs', () => {
describe('When empty entities array is mapped for an empty domainObjects array', () => {
it('should return empty domain objects array for an empty entities array', () => {
const domainObjects = RocketChatUserMapper.mapToDOs([]);

expect(domainObjects).toEqual([]);
});
});

describe('When entities array is mapped for domainObjects array', () => {
const setup = () => {
const entities = [rocketChatUserEntityFactory.build()];

const expectedDomainObjects = entities.map(
(entity) =>
new RocketChatUser({
id: entity.id,
userId: entity.userId.toHexString(),
username: entity.username,
rcId: entity.rcId,
authToken: entity.authToken,
createdAt: entity.createdAt,
updatedAt: entity.updatedAt,
})
);

return { entities, expectedDomainObjects };
};
it('should properly map the entities to the domain objects', () => {
const { entities, expectedDomainObjects } = setup();

const domainObjects = RocketChatUserMapper.mapToDOs(entities);

expect(domainObjects).toEqual(expectedDomainObjects);
});
});
});

describe('mapToEntity', () => {
describe('When domainObject is mapped for entity', () => {
beforeAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class RocketChatUserMapper {
updatedAt: domainObject.updatedAt,
});
}

static mapToDOs(entities: RocketChatUserEntity[]): RocketChatUser[] {
return entities.map((entity) => this.mapToDO(entity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export class RocketChatUserRepo {
return RocketChatUserEntity;
}

async findByUserId(userId: EntityId): Promise<RocketChatUser> {
const entity: RocketChatUserEntity = await this.em.findOneOrFail(RocketChatUserEntity, {
async findByUserId(userId: EntityId): Promise<RocketChatUser[]> {
const entities: RocketChatUserEntity[] = await this.em.find(RocketChatUserEntity, {
userId: new ObjectId(userId),
});

const mapped: RocketChatUser = RocketChatUserMapper.mapToDO(entity);
const mapped: RocketChatUser[] = RocketChatUserMapper.mapToDOs(entities);

return mapped;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe(RocketChatUserService.name, () => {

const rocketChatUser: RocketChatUser = rocketChatUserFactory.build();

rocketChatUserRepo.findByUserId.mockResolvedValueOnce(rocketChatUser);
rocketChatUserRepo.findByUserId.mockResolvedValueOnce([rocketChatUser]);

return {
userId,
Expand All @@ -60,9 +60,9 @@ describe(RocketChatUserService.name, () => {
it('should return the rocketChatUser', async () => {
const { userId, rocketChatUser } = setup();

const result: RocketChatUser = await service.findByUserId(userId);
const result: RocketChatUser[] = await service.findByUserId(userId);

expect(result).toEqual(rocketChatUser);
expect(result[0]).toEqual(rocketChatUser);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class RocketChatUserService {
this.logger.setContext(RocketChatUserService.name);
}

public async findByUserId(userId: EntityId): Promise<RocketChatUser> {
const user: RocketChatUser = await this.rocketChatUserRepo.findByUserId(userId);
public async findByUserId(userId: EntityId): Promise<RocketChatUser[]> {
const user: RocketChatUser[] = await this.rocketChatUserRepo.findByUserId(userId);

return user;
}
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/shared/repo/dashboard/dashboard.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const generateEmptyDashboard = (userId: EntityId) => {

export interface IDashboardRepo {
getUsersDashboard(userId: EntityId): Promise<DashboardEntity>;
getUsersDashboardIfExist(userId: EntityId): Promise<DashboardEntity | null>;
getDashboardById(id: EntityId): Promise<DashboardEntity>;
persistAndFlush(entity: DashboardEntity): Promise<DashboardEntity>;
deleteDashboardByUserId(userId: EntityId): Promise<number>;
Expand Down Expand Up @@ -53,6 +54,15 @@ export class DashboardRepo implements IDashboardRepo {
return dashboard;
}

async getUsersDashboardIfExist(userId: EntityId): Promise<DashboardEntity | null> {
const dashboardModel = await this.em.findOne(DashboardModelEntity, { user: userId });
if (dashboardModel) {
return this.mapper.mapDashboardToEntity(dashboardModel);
}

return dashboardModel;
}

async deleteDashboardByUserId(userId: EntityId): Promise<number> {
const promise = await this.em.nativeDelete(DashboardModelEntity, { user: userId });

Expand Down

0 comments on commit 0544706

Please sign in to comment.