-
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.
- Loading branch information
Showing
11 changed files
with
216 additions
and
0 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
7 changes: 7 additions & 0 deletions
7
back/src/adapters/primary/routers/createEstablishment/deleteEstablishment.e2e.test.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,7 @@ | ||
import { establishmentTargets } from "shared"; | ||
|
||
describe.skip(`${establishmentTargets.deleteEstablishment.method} ${establishmentTargets.deleteEstablishment.url} route`, () => { | ||
it("toto", () => { | ||
expect(true).toBe(false); | ||
}); | ||
}); |
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
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
1 change: 1 addition & 0 deletions
1
back/src/domain/immersionOffer/ports/FormEstablishmentRepository.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
60 changes: 60 additions & 0 deletions
60
back/src/domain/immersionOffer/useCases/DeleteEstablishment.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,60 @@ | ||
import { z } from "zod"; | ||
import { BackOfficeJwtPayload, SiretDto, siretSchema } from "shared"; | ||
import { | ||
ForbiddenError, | ||
NotFoundError, | ||
} from "../../../adapters/primary/helpers/httpErrors"; | ||
import { CreateNewEvent } from "../../core/eventBus/EventBus"; | ||
import { UnitOfWork, UnitOfWorkPerformer } from "../../core/ports/UnitOfWork"; | ||
import { TransactionalUseCase } from "../../core/UseCase"; | ||
|
||
type DeleteEstablishmentPayload = { | ||
siret: SiretDto; | ||
}; | ||
|
||
const deleteEstablishmentPayloadSchema: z.Schema<DeleteEstablishmentPayload> = | ||
z.object({ | ||
siret: siretSchema, | ||
}); | ||
|
||
export class DeleteEstablishment extends TransactionalUseCase< | ||
DeleteEstablishmentPayload, | ||
void, | ||
BackOfficeJwtPayload | ||
> { | ||
constructor( | ||
uowPerformer: UnitOfWorkPerformer, | ||
private createNewEvent: CreateNewEvent, | ||
) { | ||
super(uowPerformer); | ||
} | ||
|
||
inputSchema = deleteEstablishmentPayloadSchema; | ||
|
||
public async _execute( | ||
{ siret }: DeleteEstablishmentPayload, | ||
uow: UnitOfWork, | ||
jwtPayload?: BackOfficeJwtPayload, | ||
): Promise<void> { | ||
if (!jwtPayload) throw new ForbiddenError(); | ||
const establishmentInRepo = | ||
await uow.establishmentAggregateRepository.getEstablishmentAggregateBySiret( | ||
siret, | ||
); | ||
if (!establishmentInRepo) | ||
throw new NotFoundError(`Establishment with siret ${siret} not found`); | ||
|
||
await uow.establishmentAggregateRepository.delete( | ||
establishmentInRepo.establishment.siret, | ||
); | ||
|
||
const formEstablishmentInRepo = | ||
await uow.formEstablishmentRepository.getBySiret(siret); | ||
|
||
if (!formEstablishmentInRepo) | ||
throw new NotFoundError( | ||
`Establishment form with siret ${siret} not found`, | ||
); | ||
await uow.formEstablishmentRepository.delete(formEstablishmentInRepo.siret); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
back/src/domain/immersionOffer/useCases/DeleteEstablishment.unit.test.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,111 @@ | ||
import { | ||
BackOfficeJwtPayload, | ||
expectPromiseToFailWithError, | ||
expectToEqual, | ||
FormEstablishmentDtoBuilder, | ||
} from "shared"; | ||
import { EstablishmentAggregateBuilder } from "../../../_testBuilders/establishmentAggregate.test.helpers"; | ||
import { | ||
createInMemoryUow, | ||
InMemoryUnitOfWork, | ||
} from "../../../adapters/primary/config/uowConfig"; | ||
import { | ||
ForbiddenError, | ||
NotFoundError, | ||
} from "../../../adapters/primary/helpers/httpErrors"; | ||
import { CustomTimeGateway } from "../../../adapters/secondary/core/TimeGateway/CustomTimeGateway"; | ||
import { TestUuidGenerator } from "../../../adapters/secondary/core/UuidGeneratorImplementations"; | ||
import { InMemoryUowPerformer } from "../../../adapters/secondary/InMemoryUowPerformer"; | ||
import { makeCreateNewEvent } from "../../core/eventBus/EventBus"; | ||
import { DeleteEstablishment } from "./DeleteEstablishment"; | ||
|
||
describe("Delete Establishment", () => { | ||
const establishmentAggregate = new EstablishmentAggregateBuilder().build(); | ||
const formEstablishment = FormEstablishmentDtoBuilder.valid() | ||
.withSiret(establishmentAggregate.establishment.siret) | ||
.build(); | ||
const backofficeJwtPayload: BackOfficeJwtPayload = { | ||
role: "backOffice", | ||
sub: "admin", | ||
exp: 2, | ||
iat: 1, | ||
version: 1, | ||
}; | ||
let deleteEstablishment: DeleteEstablishment; | ||
let uow: InMemoryUnitOfWork; | ||
|
||
beforeEach(() => { | ||
uow = createInMemoryUow(); | ||
deleteEstablishment = new DeleteEstablishment( | ||
new InMemoryUowPerformer(uow), | ||
makeCreateNewEvent({ | ||
timeGateway: new CustomTimeGateway(), | ||
uuidGenerator: new TestUuidGenerator(), | ||
}), | ||
); | ||
}); | ||
|
||
describe("Wrong paths", () => { | ||
it("Throws forbidden error on missing backoffice jwt", async () => { | ||
await expectPromiseToFailWithError( | ||
deleteEstablishment.execute({ | ||
siret: establishmentAggregate.establishment.siret, | ||
}), | ||
new ForbiddenError("Accès refusé"), | ||
); | ||
}); | ||
it("Throws not found error on missing establishment aggregate", async () => { | ||
await expectPromiseToFailWithError( | ||
deleteEstablishment.execute( | ||
{ | ||
siret: establishmentAggregate.establishment.siret, | ||
}, | ||
backofficeJwtPayload, | ||
), | ||
new NotFoundError( | ||
`Establishment with siret ${establishmentAggregate.establishment.siret} not found`, | ||
), | ||
); | ||
}); | ||
it("Throws not found error on missing form establishment", async () => { | ||
uow.establishmentAggregateRepository.establishmentAggregates = [ | ||
establishmentAggregate, | ||
]; | ||
await expectPromiseToFailWithError( | ||
deleteEstablishment.execute( | ||
{ | ||
siret: formEstablishment.siret, | ||
}, | ||
backofficeJwtPayload, | ||
), | ||
new NotFoundError( | ||
`Establishment form with siret ${formEstablishment.siret} not found`, | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
describe("Right paths", () => { | ||
it("Establishment aggregate is deleted", async () => { | ||
uow.establishmentAggregateRepository.establishmentAggregates = [ | ||
establishmentAggregate, | ||
]; | ||
uow.formEstablishmentRepository.setFormEstablishments([ | ||
formEstablishment, | ||
]); | ||
|
||
await deleteEstablishment.execute( | ||
{ | ||
siret: establishmentAggregate.establishment.siret, | ||
}, | ||
backofficeJwtPayload, | ||
); | ||
|
||
expectToEqual( | ||
uow.establishmentAggregateRepository.establishmentAggregates, | ||
[], | ||
); | ||
expectToEqual(await uow.formEstablishmentRepository.getAll(), []); | ||
}); | ||
}); | ||
}); |