Skip to content

Commit

Permalink
N21-1848 fixes matching of migration wizard (#4883)
Browse files Browse the repository at this point in the history
fix matching bug
Co-authored-by: Arne Gnisa <[email protected]>
  • Loading branch information
IgorCapCoder authored and virgilchiriac committed Apr 3, 2024
1 parent 15d2b25 commit a55e29f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe(UserImportService.name, () => {
});
});

describe('when existing users have the same names', () => {
describe('when existing users in svs have the same names', () => {
const setup = () => {
const school: SchoolEntity = schoolEntityFactory.buildWithId();
const user1: User = userFactory.buildWithId({ firstName: 'First', lastName: 'Last' });
Expand Down Expand Up @@ -300,6 +300,40 @@ describe(UserImportService.name, () => {
expect(result).toEqual([importUser1]);
});
});

describe('when import users have the same name ', () => {
const setup = () => {
const school: SchoolEntity = schoolEntityFactory.buildWithId();
const user1: User = userFactory.buildWithId({ firstName: 'First', lastName: 'Last' });
const importUser1: ImportUser = importUserFactory.buildWithId({
school,
firstName: user1.firstName,
lastName: user1.lastName,
});
const importUser2: ImportUser = importUserFactory.buildWithId({
school,
firstName: user1.firstName,
lastName: user1.lastName,
});

userService.findUserBySchoolAndName.mockResolvedValueOnce([user1]);
userService.findUserBySchoolAndName.mockResolvedValueOnce([user1]);

return {
user1,
importUser1,
importUser2,
};
};

it('should return the users without a match', async () => {
const { importUser1, importUser2 } = setup();

const result: ImportUser[] = await service.matchUsers([importUser1, importUser2]);

result.forEach((importUser) => expect(importUser.matchedBy).toBeUndefined());
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export class UserImportService {
}

public async matchUsers(importUsers: ImportUser[]): Promise<ImportUser[]> {
const importUserMap: Map<string, ImportUser> = new Map();
const importUserMap: Map<string, number> = new Map();

importUsers.forEach((importUser) => {
const key = `${importUser.school.id}_${importUser.firstName}_${importUser.lastName}`;
importUserMap.set(key, importUser);
const count = importUserMap.get(key) || 0;
importUserMap.set(key, count + 1);
});

const matchedImportUsers: ImportUser[] = await Promise.all(
Expand All @@ -57,9 +58,8 @@ export class UserImportService {
);

const key = `${importUser.school.id}_${importUser.firstName}_${importUser.lastName}`;
const nameCount = importUserMap.has(key) ? 1 : 0;

if (user.length === 1 && nameCount === 1) {
if (user.length === 1 && importUserMap.get(key) === 1) {
importUser.user = user[0];
importUser.matchedBy = MatchCreator.AUTO;
}
Expand Down

0 comments on commit a55e29f

Please sign in to comment.