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

N21-841 use user login migration #4356

Merged
merged 19 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -109,7 +109,7 @@ describe('UserLoginMigrationController (API)', () => {
{
sourceSystemId: sourceSystem.id,
targetSystemId: targetSystem.id,
startedAt: userLoginMigration.startedAt.toISOString(),
startedAt: userLoginMigration.startedAt?.toISOString(),
closedAt: userLoginMigration.closedAt?.toISOString(),
finishedAt: userLoginMigration.finishedAt?.toISOString(),
mandatorySince: userLoginMigration.mandatorySince?.toISOString(),
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('UserLoginMigrationController (API)', () => {
expect(response.body).toEqual({
sourceSystemId: sourceSystem.id,
targetSystemId: targetSystem.id,
startedAt: userLoginMigration.startedAt.toISOString(),
startedAt: userLoginMigration.startedAt?.toISOString(),
closedAt: userLoginMigration.closedAt?.toISOString(),
finishedAt: userLoginMigration.finishedAt?.toISOString(),
mandatorySince: userLoginMigration.mandatorySince?.toISOString(),
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe('UserLoginMigrationController (API)', () => {
expect(response.body).toEqual({
targetSystemId: userLoginMigration.targetSystem.id,
sourceSystemId: userLoginMigration.sourceSystem?.id,
startedAt: userLoginMigration.startedAt.toISOString(),
startedAt: userLoginMigration.startedAt?.toISOString(),
closedAt: expect.any(String),
finishedAt: expect.any(String),
});
Expand Down Expand Up @@ -1109,7 +1109,7 @@ describe('UserLoginMigrationController (API)', () => {
expect(response.body).toEqual({
targetSystemId: userLoginMigration.targetSystem.id,
sourceSystemId: userLoginMigration.sourceSystem?.id,
startedAt: userLoginMigration.startedAt.toISOString(),
startedAt: userLoginMigration.startedAt?.toISOString(),
closedAt: userLoginMigration.closedAt?.toISOString(),
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UserLoginMigrationResponse {
@ApiProperty({
description: 'Date when the migration was started',
})
startedAt: Date;
startedAt?: Date;
mrikallab marked this conversation as resolved.
Show resolved Hide resolved

@ApiPropertyOptional({
description: 'Date when the migration was completed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export class MigrationCheckService {

const user: UserDO | null = await this.userService.findByExternalId(externalUserId, systemId);

if (user?.lastLoginSystemChange && userLoginMigration && !userLoginMigration.closedAt) {
if (
user?.lastLoginSystemChange &&
userLoginMigration &&
!userLoginMigration.closedAt &&
userLoginMigration.startedAt
) {
const hasMigrated: boolean = user.lastLoginSystemChange > userLoginMigration.startedAt;
return !hasMigrated;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { SchoolFeatures } from '@shared/domain';
import { SchoolFeatures, UserLoginMigrationDO } from '@shared/domain';
import { SchoolService } from '@src/modules/school';
import { setupEntities, userLoginMigrationDOFactory } from '@shared/testing';
import { UserLoginMigrationRevertService } from './user-login-migration-revert.service';
Expand Down Expand Up @@ -71,6 +71,22 @@ describe('UserLoginMigrationRevertService', () => {

expect(userLoginMigrationService.deleteUserLoginMigration).toHaveBeenCalledWith(userLoginMigration);
});

it('should return reverted user login migration', async () => {
const { userLoginMigration } = setup();

const result: UserLoginMigrationDO = await service.revertUserLoginMigration(userLoginMigration);

expect(result).toEqual<UserLoginMigrationDO>({
schoolId: userLoginMigration.schoolId,
targetSystemId: userLoginMigration.targetSystemId,
startedAt: undefined,
finishedAt: undefined,
mandatorySince: undefined,
closedAt: undefined,
sourceSystemId: undefined,
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ export class UserLoginMigrationRevertService {
private readonly schoolService: SchoolService
) {}

async revertUserLoginMigration(userLoginMigration: UserLoginMigrationDO): Promise<void> {
async revertUserLoginMigration(userLoginMigration: UserLoginMigrationDO): Promise<UserLoginMigrationDO> {
await this.schoolService.removeFeature(userLoginMigration.schoolId, SchoolFeatures.OAUTH_PROVISIONING_ENABLED);
await this.userLoginMigrationService.deleteUserLoginMigration(userLoginMigration);

const revertedUserLoginMigration = new UserLoginMigrationDO({
schoolId: userLoginMigration.schoolId,
targetSystemId: userLoginMigration.targetSystemId,
startedAt: undefined,
finishedAt: undefined,
mandatorySince: undefined,
closedAt: undefined,
sourceSystemId: undefined,
});

return revertedUserLoginMigration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export class UserLoginMigrationService {
}

private async updateExistingMigration(userLoginMigrationDO: UserLoginMigrationDO) {
userLoginMigrationDO.startedAt = new Date();
userLoginMigrationDO.closedAt = undefined;
userLoginMigrationDO.finishedAt = undefined;

Expand Down Expand Up @@ -202,8 +201,10 @@ export class UserLoginMigrationService {
return null;
}

const hasUserMigrated: boolean =
!!userDO.lastLoginSystemChange && userDO.lastLoginSystemChange > userLoginMigration.startedAt;
let hasUserMigrated = false;
if (userLoginMigration.startedAt) {
hasUserMigrated = !!userDO.lastLoginSystemChange && userDO.lastLoginSystemChange > userLoginMigration.startedAt;
}

if (hasUserMigrated) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ describe('CloseUserLoginMigrationUc', () => {
authorizationService.getUserWithPermissions.mockResolvedValue(user);
userLoginMigrationService.closeMigration.mockResolvedValue(closedUserLoginMigration);
schoolMigrationService.hasSchoolMigratedUser.mockResolvedValue(false);
userLoginMigrationRevertService.revertUserLoginMigration.mockResolvedValue({
...closedUserLoginMigration,
startedAt: undefined,
closedAt: undefined,
finishedAt: undefined,
sourceSystemId: undefined,
mandatorySince: undefined,
});

return {
user,
Expand Down Expand Up @@ -179,6 +187,23 @@ describe('CloseUserLoginMigrationUc', () => {

expect(userLoginMigrationRevertService.revertUserLoginMigration).toHaveBeenCalledWith(closedUserLoginMigration);
});

it('should return reverted user login migration', async () => {
const { user, schoolId, closedUserLoginMigration } = setup();

const result: UserLoginMigrationDO = await uc.closeMigration(user.id, schoolId);

expect(result).toEqual<UserLoginMigrationDO>({
id: closedUserLoginMigration.id,
schoolId: closedUserLoginMigration.schoolId,
targetSystemId: closedUserLoginMigration.targetSystemId,
startedAt: undefined,
finishedAt: undefined,
mandatorySince: undefined,
closedAt: undefined,
sourceSystemId: undefined,
});
});
});

describe('when the user login migration was already closed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export class CloseUserLoginMigrationUc {
const hasSchoolMigratedUser: boolean = await this.schoolMigrationService.hasSchoolMigratedUser(schoolId);

if (!hasSchoolMigratedUser) {
await this.userLoginMigrationRevertService.revertUserLoginMigration(updatedUserLoginMigration);
} else {
await this.schoolMigrationService.markUnmigratedUsersAsOutdated(schoolId);
const revertedUserLoginMigration: UserLoginMigrationDO =
await this.userLoginMigrationRevertService.revertUserLoginMigration(updatedUserLoginMigration);

return revertedUserLoginMigration;
}
await this.schoolMigrationService.markUnmigratedUsersAsOutdated(schoolId);

return updatedUserLoginMigration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export class RestartUserLoginMigrationUc {
userLoginMigration = await this.userLoginMigrationService.restartMigration(schoolId);

this.logger.info(new UserLoginMigrationStartLoggable(userId, schoolId));
} else {
// Do nothing, if migration is already started but not stopped.
}

return userLoginMigration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export class StartUserLoginMigrationUc {
userLoginMigration.id as string,
userLoginMigration.closedAt
);
} else {
// Do nothing, if migration is already started but not stopped.
}

return userLoginMigration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class UserLoginMigrationDO extends BaseDO {

mandatorySince?: Date;

startedAt: Date;
startedAt?: Date;

closedAt?: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class UserLoginMigration extends BaseEntityWithTimestamps {
mandatorySince?: Date;

@Property()
startedAt: Date;
startedAt?: Date;

@Property({ nullable: true })
closedAt?: Date;
Expand Down