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

BC-5831-rewriting-regpin-deletion #4570

Merged
merged 19 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const enum DeletionDomainModel {
FILE = 'file',
LESSONS = 'lessons',
PSEUDONYMS = 'pseudonyms',
REGISTRATIONPIN = 'registrationPin',
ROCKETCHATUSER = 'rocketChatUser',
TEAMS = 'teams',
USER = 'user',
Expand Down
20 changes: 19 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserService } from '@modules/user';
import { RocketChatService } from '@modules/rocketchat';
import { rocketChatUserFactory } from '@modules/rocketchat-user/domain/testing';
import { RocketChatUser, RocketChatUserService } from '@modules/rocketchat-user';
import { RegistrationPinService } from '@modules/registration-pin';
import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum';
import { DeletionLogService } from '../services/deletion-log.service';
import { DeletionRequestService } from '../services';
Expand All @@ -37,6 +38,7 @@ describe(DeletionRequestUc.name, () => {
let userService: DeepMocked<UserService>;
let rocketChatUserService: DeepMocked<RocketChatUserService>;
let rocketChatService: DeepMocked<RocketChatService>;
let registrationPinService: DeepMocked<RegistrationPinService>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand Down Expand Up @@ -94,6 +96,10 @@ describe(DeletionRequestUc.name, () => {
provide: RocketChatService,
useValue: createMock<RocketChatService>(),
},
{
provide: RegistrationPinService,
useValue: createMock<RegistrationPinService>(),
},
],
}).compile();

Expand All @@ -111,6 +117,7 @@ describe(DeletionRequestUc.name, () => {
userService = module.get(UserService);
rocketChatUserService = module.get(RocketChatUserService);
rocketChatService = module.get(RocketChatService);
registrationPinService = module.get(RegistrationPinService);
await setupEntities();
});

Expand Down Expand Up @@ -172,6 +179,7 @@ describe(DeletionRequestUc.name, () => {
userId: deletionRequestToExecute.targetRefId,
});

registrationPinService.deleteRegistrationPinByEmail.mockResolvedValueOnce(2);
classService.deleteUserDataFromClasses.mockResolvedValueOnce(1);
courseGroupService.deleteUserDataFromCourseGroup.mockResolvedValueOnce(2);
courseService.deleteUserDataFromCourse.mockResolvedValueOnce(2);
Expand Down Expand Up @@ -215,6 +223,16 @@ describe(DeletionRequestUc.name, () => {
expect(accountService.deleteByUserId).toHaveBeenCalled();
});

it('should call registrationPinService.deleteRegistrationPinByEmail to delete user data in registrationPin module', async () => {
const { deletionRequestToExecute } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);

await uc.executeDeletionRequests();

expect(registrationPinService.deleteRegistrationPinByEmail).toHaveBeenCalled();
});

it('should call classService.deleteUserDataFromClasses to delete user data in class module', async () => {
const { deletionRequestToExecute } = setup();

Expand Down Expand Up @@ -346,7 +364,7 @@ describe(DeletionRequestUc.name, () => {

await uc.executeDeletionRequests();

expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(9);
expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10);
});
});

Expand Down
31 changes: 30 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FilesService } from '@modules/files/service';
import { AccountService } from '@modules/account/services';
import { RocketChatUserService } from '@modules/rocketchat-user';
import { RocketChatService } from '@modules/rocketchat';
import { RegistrationPinService } from '@modules/registration-pin';
import { DeletionRequestService } from '../services/deletion-request.service';
import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum';
import { DeletionLogService } from '../services/deletion-log.service';
Expand Down Expand Up @@ -42,7 +43,8 @@ export class DeletionRequestUc {
private readonly teamService: TeamService,
private readonly userService: UserService,
private readonly rocketChatUserService: RocketChatUserService,
private readonly rocketChatService: RocketChatService
private readonly rocketChatService: RocketChatService,
private readonly registrationPinService: RegistrationPinService
) {}

async createDeletionRequest(deletionRequest: DeletionRequestProps): Promise<DeletionRequestCreateAnswer> {
Expand Down Expand Up @@ -101,6 +103,7 @@ export class DeletionRequestUc {
this.removeUserFromTeams(deletionRequest),
this.removeUser(deletionRequest),
this.removeUserFromRocketChat(deletionRequest),
this.removeUserRegistrationPin(deletionRequest),
]);
await this.deletionRequestService.markDeletionRequestAsExecuted(deletionRequest.id);
} catch (error) {
Expand Down Expand Up @@ -131,6 +134,32 @@ export class DeletionRequestUc {
await this.logDeletion(deletionRequest, DeletionDomainModel.ACCOUNT, DeletionOperationModel.DELETE, 0, 1);
}

private async removeUserRegistrationPin(deletionRequest: DeletionRequest) {
const userToDeletion = await this.userService.findById(deletionRequest.targetRefId);
const parentEmails = await this.userService.getParentEmailsFromUser(deletionRequest.targetRefId);
let deletedParentRegistrationPin = 0;

if (parentEmails) {
const result = await Promise.all(
parentEmails.map((email) => this.registrationPinService.deleteRegistrationPinByEmail(email))
);
deletedParentRegistrationPin = result.filter((res) => res !== 0).length;
}

const deletedUserRegistrationPins: number = await this.registrationPinService.deleteRegistrationPinByEmail(
userToDeletion.email
);

const deletedRegistrationPin = deletedUserRegistrationPins + deletedParentRegistrationPin;
await this.logDeletion(
deletionRequest,
DeletionDomainModel.REGISTRATIONPIN,
DeletionOperationModel.DELETE,
0,
deletedRegistrationPin
);
}

private async removeUserFromClasses(deletionRequest: DeletionRequest) {
const classesUpdated: number = await this.classService.deleteUserDataFromClasses(deletionRequest.targetRefId);
await this.logDeletion(
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/modules/registration-pin/entity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './registration-pin.entity';
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { setupEntities } from '@shared/testing';
import { ObjectId } from '@mikro-orm/mongodb';
import { RegistrationPinEntity } from '.';

describe(RegistrationPinEntity.name, () => {
beforeAll(async () => {
await setupEntities();
});

beforeEach(() => {
jest.clearAllMocks();
});

const setup = () => {
const props = {
id: new ObjectId().toHexString(),
email: '[email protected]',
pin: 'test123',
verified: false,
importHash: '02a00804nnQbLbCDEMVuk56pzZ3A0SC2cYnmM9cyY25IVOnf0K3YCKqW6zxC',
};

return { props };
};

describe('constructor', () => {
describe('When constructor is called', () => {
it('should throw an error by empty constructor', () => {
// @ts-expect-error: Test case
const test = () => new RegistrationPinEntity();
expect(test).toThrow();
});

it('should create a registrationPins by passing required properties', () => {
const { props } = setup();
const entity: RegistrationPinEntity = new RegistrationPinEntity(props);

expect(entity instanceof RegistrationPinEntity).toEqual(true);
});

it(`should return a valid object with fields values set from the provided complete props object`, () => {
const { props } = setup();
const entity: RegistrationPinEntity = new RegistrationPinEntity(props);

const entityProps = {
id: entity.id,
email: entity.email,
pin: entity.pin,
verified: entity.verified,
importHash: entity.importHash,
};

expect(entityProps).toEqual(props);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Entity, Index, Property } from '@mikro-orm/core';
import { EntityId } from '@shared/domain';
import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity';
WojciechGrancow marked this conversation as resolved.
Show resolved Hide resolved

export interface RegistrationPinEntityProps {
id?: EntityId;
email: string;
pin: string;
verified: boolean;
importHash: string;
}

@Entity({ tableName: 'registrationpins' })
@Index({ properties: ['email', 'pin'] })
export class RegistrationPinEntity extends BaseEntityWithTimestamps {
@Property()
@Index()
email: string;

@Property()
pin: string;

@Property({ default: false })
verified: boolean;

@Property()
@Index()
importHash: string;

constructor(props: RegistrationPinEntityProps) {
super();
if (props.id !== undefined) {
this.id = props.id;
}
this.email = props.email;
this.pin = props.pin;
this.verified = props.verified;
this.importHash = props.importHash;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './registration-pin.entity.factory';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { BaseFactory } from '@shared/testing';
import { RegistrationPinEntity, RegistrationPinEntityProps } from '../../registration-pin.entity';

export const registrationPinEntityFactory = BaseFactory.define<RegistrationPinEntity, RegistrationPinEntityProps>(
RegistrationPinEntity,
({ sequence }) => {
return {
id: new ObjectId().toHexString(),
email: `name-${sequence}@schul-cloud.org`,
pin: `123-${sequence}`,
verified: false,
importHash: `importHash-${sequence}`,
createdAt: new Date(),
updatedAt: new Date(),
};
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './factory';
2 changes: 2 additions & 0 deletions apps/server/src/modules/registration-pin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './registration-pin.module';
export { RegistrationPinService } from './service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { RegistrationPinService } from './service';
import { RegistrationPinRepo } from './repo';

@Module({
imports: [LoggerModule],
providers: [RegistrationPinService, RegistrationPinRepo],
exports: [RegistrationPinService],
})
export class RegistrationPinModule {}
1 change: 1 addition & 0 deletions apps/server/src/modules/registration-pin/repo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './registration-pin.repo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { MongoMemoryDatabaseModule } from '@infra/database';
import { cleanupCollections, userFactory } from '@shared/testing';
import { RegistrationPinRepo } from '.';
import { registrationPinEntityFactory } from '../entity/testing';

describe(RegistrationPinRepo.name, () => {
let module: TestingModule;
let repo: RegistrationPinRepo;
let em: EntityManager;

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [MongoMemoryDatabaseModule.forRoot()],
providers: [RegistrationPinRepo],
}).compile();

repo = module.get(RegistrationPinRepo);
em = module.get(EntityManager);
});

afterAll(async () => {
await module.close();
});

afterEach(async () => {
await cleanupCollections(em);
});

describe('deleteRegistrationPinByEmail', () => {
const setup = async () => {
const user = userFactory.buildWithId();
const userWithoutRegistrationPin = userFactory.buildWithId();
const registrationPinForUser = registrationPinEntityFactory.buildWithId({ email: user.email });

await em.persistAndFlush(registrationPinForUser);

return {
user,
userWithoutRegistrationPin,
};
};

describe('when registrationPin exists', () => {
it('should delete registrationPins by email', async () => {
const { user } = await setup();

const result: number = await repo.deleteRegistrationPinByEmail(user.email);

expect(result).toEqual(1);
});
});

describe('when there is no registrationPin', () => {
it('should return empty array', async () => {
const { userWithoutRegistrationPin } = await setup();

const result: number = await repo.deleteRegistrationPinByEmail(userWithoutRegistrationPin.email);
expect(result).toEqual(0);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { RegistrationPinEntity } from '../entity';

@Injectable()
export class RegistrationPinRepo {
constructor(private readonly em: EntityManager) {}

async deleteRegistrationPinByEmail(email: string): Promise<number> {
const promise: Promise<number> = this.em.nativeDelete(RegistrationPinEntity, { email });

return promise;
}
}
1 change: 1 addition & 0 deletions apps/server/src/modules/registration-pin/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './registration-pin.service';
Loading
Loading