generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: issue 1594 synchronize users ad app (#1596)
- Loading branch information
1 parent
e238d2e
commit 1433045
Showing
16 changed files
with
360 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
.../modules/azure/interfaces/schedules/synchronize-ad-users.cron.azure.use-case.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
|
||
export interface SynchronizeADUsersCronUseCaseInterface extends UseCase<void, void> { | ||
execute(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
backend/src/modules/azure/schedules/synchronize-ad-users.cron.azure.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AUTH_AZURE_SERVICE } from '../constants'; | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { AuthAzureServiceInterface } from '../interfaces/services/auth.azure.service.interface'; | ||
import { DeleteUserUseCase } from 'src/modules/users/applications/delete-user.use-case'; | ||
import { | ||
CREATE_USER_SERVICE, | ||
DELETE_USER_USE_CASE, | ||
GET_ALL_USERS_INCLUDE_DELETED_USE_CASE | ||
} from 'src/modules/users/constants'; | ||
import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { AzureUserFactory } from 'src/libs/test-utils/mocks/factories/azure-user-factory'; | ||
import { CreateUserServiceInterface } from 'src/modules/users/interfaces/services/create.user.service.interface'; | ||
import GetAllUsersIncludeDeletedUseCase from 'src/modules/users/applications/get-all-users-include-deleted.use-case'; | ||
import { SynchronizeADUsersCronUseCase } from './synchronize-ad-users.cron.azure.use-case'; | ||
|
||
const usersAD = AzureUserFactory.createMany(4, () => ({ | ||
deletedDateTime: null, | ||
employeeLeaveDateTime: null | ||
})); | ||
const users = UserFactory.createMany( | ||
4, | ||
usersAD.map((u) => ({ | ||
email: u.mail, | ||
firstName: u.displayName.split(' ')[0], | ||
lastName: u.displayName.split(' ')[1] | ||
})) as never | ||
); | ||
|
||
describe('SynchronizeAdUsersCronUseCase', () => { | ||
let synchronizeADUsers: UseCase<void, void>; | ||
let authAzureServiceMock: DeepMocked<AuthAzureServiceInterface>; | ||
let getAllUsersMock: DeepMocked<GetAllUsersIncludeDeletedUseCase>; | ||
let deleteUserMock: DeepMocked<DeleteUserUseCase>; | ||
let createUserServiceMock: DeepMocked<CreateUserServiceInterface>; | ||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
SynchronizeADUsersCronUseCase, | ||
{ | ||
provide: AUTH_AZURE_SERVICE, | ||
useValue: createMock<AuthAzureServiceInterface>() | ||
}, | ||
{ | ||
provide: GET_ALL_USERS_INCLUDE_DELETED_USE_CASE, | ||
useValue: createMock<GetAllUsersIncludeDeletedUseCase>() | ||
}, | ||
{ | ||
provide: DELETE_USER_USE_CASE, | ||
useValue: createMock<DeleteUserUseCase>() | ||
}, | ||
{ | ||
provide: CREATE_USER_SERVICE, | ||
useValue: createMock<CreateUserServiceInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
synchronizeADUsers = module.get(SynchronizeADUsersCronUseCase); | ||
authAzureServiceMock = module.get(AUTH_AZURE_SERVICE); | ||
getAllUsersMock = module.get(GET_ALL_USERS_INCLUDE_DELETED_USE_CASE); | ||
deleteUserMock = module.get(DELETE_USER_USE_CASE); | ||
createUserServiceMock = module.get(CREATE_USER_SERVICE); | ||
}); | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(synchronizeADUsers).toBeDefined(); | ||
}); | ||
it('execute', async () => { | ||
const userNotInApp = AzureUserFactory.create({ | ||
employeeLeaveDateTime: null, | ||
deletedDateTime: null | ||
}); | ||
const finalADUsers = [userNotInApp, ...usersAD]; | ||
authAzureServiceMock.getADUsers.mockResolvedValueOnce(finalADUsers); | ||
const userNotInAD = UserFactory.create(); | ||
const finalAppUsers = [userNotInAD, ...users]; | ||
getAllUsersMock.execute.mockResolvedValueOnce(finalAppUsers); | ||
await synchronizeADUsers.execute(); | ||
expect(deleteUserMock.execute).toBeCalledWith(userNotInAD._id); | ||
expect(deleteUserMock.execute.mock.calls).toEqual([[userNotInAD._id]]); | ||
expect(createUserServiceMock.create).toHaveBeenCalledWith({ | ||
email: userNotInApp.mail, | ||
firstName: userNotInApp.displayName.split(' ')[0], | ||
lastName: userNotInApp.displayName.split(' ')[1], | ||
providerAccountCreatedAt: userNotInApp.createdDateTime | ||
}); | ||
}); | ||
}); |
117 changes: 117 additions & 0 deletions
117
backend/src/modules/azure/schedules/synchronize-ad-users.cron.azure.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { Cron } from '@nestjs/schedule'; | ||
import { SynchronizeADUsersCronUseCaseInterface } from '../interfaces/schedules/synchronize-ad-users.cron.azure.use-case.interface'; | ||
import { AUTH_AZURE_SERVICE } from '../constants'; | ||
import { AuthAzureServiceInterface } from '../interfaces/services/auth.azure.service.interface'; | ||
import { | ||
CREATE_USER_SERVICE, | ||
DELETE_USER_USE_CASE, | ||
GET_ALL_USERS_INCLUDE_DELETED_USE_CASE | ||
} from 'src/modules/users/constants'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import User from 'src/modules/users/entities/user.schema'; | ||
import { AzureUserDTO } from '../dto/azure-user.dto'; | ||
import { CreateUserServiceInterface } from 'src/modules/users/interfaces/services/create.user.service.interface'; | ||
|
||
@Injectable() | ||
export class SynchronizeADUsersCronUseCase implements SynchronizeADUsersCronUseCaseInterface { | ||
private readonly logger: Logger = new Logger(SynchronizeADUsersCronUseCase.name); | ||
constructor( | ||
@Inject(AUTH_AZURE_SERVICE) | ||
private readonly authAzureService: AuthAzureServiceInterface, | ||
@Inject(GET_ALL_USERS_INCLUDE_DELETED_USE_CASE) | ||
private readonly getAllUsersIncludeDeletedUseCase: UseCase<void, Array<User>>, | ||
@Inject(DELETE_USER_USE_CASE) | ||
private readonly deleteUserUseCase: UseCase<string, boolean>, | ||
@Inject(CREATE_USER_SERVICE) | ||
private readonly createUserService: CreateUserServiceInterface | ||
) {} | ||
|
||
//Runs every saturday at mid-night | ||
//@Cron('0 0 * * 6') | ||
@Cron('0 14 * * *') | ||
async execute() { | ||
try { | ||
const usersADAll = await this.authAzureService.getADUsers(); | ||
|
||
if (!usersADAll.length) { | ||
throw new Error('Azure AD users list is empty.'); | ||
} | ||
|
||
const usersApp = await this.getAllUsersIncludeDeletedUseCase.execute(); | ||
|
||
if (!usersApp.length) { | ||
throw new Error('Split app users list is empty.'); | ||
} | ||
|
||
const today = new Date(); | ||
//Filter out users that don't have a '.' in the beggining of the email | ||
let usersADFiltered = usersADAll.filter((u) => | ||
/[a-z]+\.[a-zA-Z0-9]+@/.test(u.userPrincipalName) | ||
); | ||
|
||
//Filter out users that have a deletedDateTime bigger than 'today' | ||
usersADFiltered = usersADFiltered.filter((u) => | ||
'deletedDateTime' in u ? u.deletedDateTime === null || u.deletedDateTime >= today : true | ||
); | ||
|
||
//Filter out users that have a employeeLeaveDateTime bigger than 'today' | ||
usersADFiltered = usersADFiltered.filter((u) => | ||
'employeeLeaveDateTime' in u | ||
? u.employeeLeaveDateTime === null || u.employeeLeaveDateTime >= today | ||
: true | ||
); | ||
|
||
await this.removeUsersFromApp(usersADFiltered, usersApp); | ||
await this.addUsersToApp(usersADFiltered, usersApp); | ||
} catch (err) { | ||
this.logger.error( | ||
`An error occurred while synchronizing users between AD and Aplit Application. Message: ${err.message}` | ||
); | ||
} | ||
} | ||
|
||
private async removeUsersFromApp(usersADFiltered: Array<AzureUserDTO>, usersApp: Array<User>) { | ||
const notIntersectedUsers = usersApp.filter( | ||
(userApp) => | ||
userApp.isDeleted === false && | ||
usersADFiltered.findIndex( | ||
(userAd) => (userAd.mail ?? userAd.userPrincipalName) === userApp.email | ||
) === -1 | ||
); | ||
|
||
for (const user of notIntersectedUsers) { | ||
try { | ||
await this.deleteUserUseCase.execute(user._id); | ||
} catch (err) { | ||
this.logger.error( | ||
`An error occurred while deleting user with id '${user._id}' through the syncronize AD Users Cron. Message: ${err.message}` | ||
); | ||
} | ||
} | ||
} | ||
private async addUsersToApp(usersADFiltered: Array<AzureUserDTO>, usersApp: Array<User>) { | ||
const notIntersectedUsers = usersADFiltered.filter( | ||
(userAd) => | ||
usersApp.findIndex( | ||
(userApp) => userApp.email === (userAd.mail ?? userAd.userPrincipalName) | ||
) === -1 | ||
); | ||
|
||
for (const user of notIntersectedUsers) { | ||
try { | ||
const splittedName = user.displayName.split(' '); | ||
await this.createUserService.create({ | ||
email: user.mail, | ||
firstName: splittedName[0], | ||
lastName: splittedName.at(-1), | ||
providerAccountCreatedAt: user.createdDateTime | ||
}); | ||
} catch (err) { | ||
this.logger.error( | ||
`An error as occurred while creating user with email '${user.mail}' through the syncronize AD Users Cron. Message: ${err.message}` | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
backend/src/modules/users/applications/get-all-users-include-deleted.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import { UserRepositoryInterface } from '../repository/user.repository.interface'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import User from '../entities/user.schema'; | ||
import { USER_REPOSITORY } from 'src/modules/users/constants'; | ||
import GetAllUsersIncludeDeletedUseCase from './get-all-users-include-deleted.use-case'; | ||
|
||
describe('GetAllUsersIncludeDeletedUseCase', () => { | ||
let getAllUsers: UseCase<void, User[]>; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
GetAllUsersIncludeDeletedUseCase, | ||
{ | ||
provide: USER_REPOSITORY, | ||
useValue: createMock<UserRepositoryInterface>() | ||
} | ||
] | ||
}).compile(); | ||
|
||
getAllUsers = module.get(GetAllUsersIncludeDeletedUseCase); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(getAllUsers).toBeDefined(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
backend/src/modules/users/applications/get-all-users-include-deleted.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { USER_REPOSITORY } from '../constants'; | ||
import { UserRepositoryInterface } from '../repository/user.repository.interface'; | ||
import { UseCase } from 'src/libs/interfaces/use-case.interface'; | ||
import User from '../entities/user.schema'; | ||
|
||
@Injectable() | ||
export default class GetAllUsersIncludeDeletedUseCase implements UseCase<void, User[]> { | ||
constructor( | ||
@Inject(USER_REPOSITORY) | ||
private readonly userRepository: UserRepositoryInterface | ||
) {} | ||
|
||
execute() { | ||
return this.userRepository.getAllUsersIncludeDeleted(); | ||
} | ||
} |
Oops, something went wrong.