-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add route to remove user from agency
- Loading branch information
Showing
10 changed files
with
464 additions
and
39 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
44 changes: 44 additions & 0 deletions
44
...s/inclusion-connected-users/helpers/throwIfAgencyWontHaveEnoughCounsellorsOrValidators.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,44 @@ | ||
import { AgencyDto, UserId, errors } from "shared"; | ||
import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork"; | ||
|
||
export const throwIfAgencyDontHaveOtherValidatorsReceivingNotifications = | ||
async (uow: UnitOfWork, agency: AgencyDto, userId: UserId) => { | ||
if (agency.refersToAgencyId !== null) return; | ||
|
||
const agencyUsers = await uow.userRepository.getWithFilter({ | ||
agencyId: agency.id, | ||
}); | ||
|
||
const agencyHasOtherValidator = agencyUsers.some( | ||
(agencyUser) => | ||
agencyUser.id !== userId && | ||
agencyUser.agencyRights.some( | ||
(right) => | ||
right.isNotifiedByEmail && right.roles.includes("validator"), | ||
), | ||
); | ||
|
||
if (!agencyHasOtherValidator) | ||
throw errors.agency.notEnoughValidators({ agencyId: agency.id }); | ||
}; | ||
|
||
export const throwIfAgencyDontHaveOtherCounsellorsReceivingNotifications = | ||
async (uow: UnitOfWork, agency: AgencyDto, userId: UserId) => { | ||
if (!agency.refersToAgencyId) return; | ||
|
||
const agencyUsers = await uow.userRepository.getWithFilter({ | ||
agencyId: agency.id, | ||
}); | ||
|
||
const agencyHasOtherCounsellor = agencyUsers.some( | ||
(agencyUser) => | ||
agencyUser.id !== userId && | ||
agencyUser.agencyRights.some( | ||
(right) => | ||
right.isNotifiedByEmail && right.roles.includes("counsellor"), | ||
), | ||
); | ||
|
||
if (!agencyHasOtherCounsellor) | ||
throw errors.agency.notEnoughCounsellors({ agencyId: agency.id }); | ||
}; |
84 changes: 84 additions & 0 deletions
84
back/src/domains/inclusion-connected-users/use-cases/RemoveUserFromAgency.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,84 @@ | ||
import { | ||
AgencyDto, | ||
AgencyId, | ||
InclusionConnectedUser, | ||
RemoveAgencyUserParams, | ||
UserId, | ||
errors, | ||
removeAgencyUserParamsSchema, | ||
} from "shared"; | ||
import { createTransactionalUseCase } from "../../core/UseCase"; | ||
import { UserRepository } from "../../core/authentication/inclusion-connect/port/UserRepository"; | ||
import { CreateNewEvent } from "../../core/events/ports/EventBus"; | ||
import { | ||
throwIfAgencyDontHaveOtherCounsellorsReceivingNotifications, | ||
throwIfAgencyDontHaveOtherValidatorsReceivingNotifications, | ||
} from "../helpers/throwIfAgencyWontHaveEnoughCounsellorsOrValidators"; | ||
import { throwIfNotAdmin } from "../helpers/throwIfIcUserNotBackofficeAdmin"; | ||
|
||
export type RemoveUserFromAgency = ReturnType<typeof makeRemoveUserFromAgency>; | ||
|
||
const getUserAndThrowIfNotFound = async ( | ||
userRepository: UserRepository, | ||
userId: UserId, | ||
): Promise<InclusionConnectedUser> => { | ||
const requestedUser = await userRepository.getById(userId); | ||
if (!requestedUser) throw errors.user.notFound({ userId }); | ||
return requestedUser; | ||
}; | ||
|
||
const getAgencyAndThrowIfUserHasNoAgencyRight = ( | ||
user: InclusionConnectedUser, | ||
agencyId: AgencyId, | ||
): AgencyDto => { | ||
const userRight = user.agencyRights.find( | ||
(agencyRight) => agencyRight.agency.id === agencyId, | ||
); | ||
|
||
if (!userRight) | ||
throw errors.user.expectedRightsOnAgency({ | ||
agencyId, | ||
userId: user.id, | ||
}); | ||
|
||
return userRight.agency; | ||
}; | ||
|
||
export const makeRemoveUserFromAgency = createTransactionalUseCase< | ||
RemoveAgencyUserParams, | ||
void, | ||
InclusionConnectedUser, | ||
{ createNewEvent: CreateNewEvent } | ||
>( | ||
{ name: "RemoveUserFromAgency", inputSchema: removeAgencyUserParamsSchema }, | ||
async ({ currentUser, uow, inputParams }) => { | ||
throwIfNotAdmin(currentUser); | ||
const requestedUser = await getUserAndThrowIfNotFound( | ||
uow.userRepository, | ||
inputParams.userId, | ||
); | ||
const agency = getAgencyAndThrowIfUserHasNoAgencyRight( | ||
requestedUser, | ||
inputParams.agencyId, | ||
); | ||
await throwIfAgencyDontHaveOtherValidatorsReceivingNotifications( | ||
uow, | ||
agency, | ||
inputParams.userId, | ||
); | ||
await throwIfAgencyDontHaveOtherCounsellorsReceivingNotifications( | ||
uow, | ||
agency, | ||
inputParams.userId, | ||
); | ||
|
||
const filteredAgencyRights = requestedUser.agencyRights.filter( | ||
(agencyRight) => agencyRight.agency.id !== inputParams.agencyId, | ||
); | ||
|
||
await uow.userRepository.updateAgencyRights({ | ||
userId: inputParams.userId, | ||
agencyRights: filteredAgencyRights, | ||
}); | ||
}, | ||
); |
Oops, something went wrong.