Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohec committed Jul 26, 2023
1 parent c9f8cad commit 24f526b
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 0 deletions.
5 changes: 5 additions & 0 deletions back/src/adapters/primary/config/createUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { SendNotification } from "../../../domain/generic/notifications/useCases
import { AddFormEstablishment } from "../../../domain/immersionOffer/useCases/AddFormEstablishment";
import { AddFormEstablishmentBatch } from "../../../domain/immersionOffer/useCases/AddFormEstablismentsBatch";
import { ContactEstablishment } from "../../../domain/immersionOffer/useCases/ContactEstablishment";
import { DeleteEstablishment } from "../../../domain/immersionOffer/useCases/DeleteEstablishment";
import { AddExchangeToDiscussionAndTransferEmail } from "../../../domain/immersionOffer/useCases/discussions/AddExchangeToDiscussionAndTransferEmail";
import { EditFormEstablishment } from "../../../domain/immersionOffer/useCases/EditFormEstablishment";
import { GetSearchImmersionResultBySiretAndRome } from "../../../domain/immersionOffer/useCases/GetImmersionOfferById";
Expand Down Expand Up @@ -281,6 +282,10 @@ export const createUseCases = (
gateways.timeGateway,
createNewEvent,
),
deleteEstablishment: new DeleteEstablishment(
uowPerformer,
createNewEvent,
),
contactEstablishment: new ContactEstablishment(
uowPerformer,
createNewEvent,
Expand Down
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ export const establishmentRouterWithMagicLinkJwt = (
),
),
);

router
.route(establishmentTargets.deleteEstablishment.url)
.delete(async (req, res) =>
sendHttpResponse(req, res, () =>
deps.useCases.deleteEstablishment.execute(
req.params,
getBackOfficePayload(req),
),
),
);

return router;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export class InMemoryFormEstablishmentRepository
{
private formEstablishments: FormEstablishmentDto[] = [];

public async delete(siret: SiretDto): Promise<void> {
this.formEstablishments = this.formEstablishments.filter(
(formEstablishment) => formEstablishment.siret !== siret,
);
}

public async create(dto: FormEstablishmentDto): Promise<void> {
if (await this.getBySiret(dto.siret)) {
const message = `Immersion DTO with siret ${dto.siret} is already in the list`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export class InMemoryEstablishmentAggregateRepository
constructor(
private _establishmentAggregates: EstablishmentAggregate[] = [],
) {}
public async delete(siret: SiretDto): Promise<void> {
this._establishmentAggregates = this._establishmentAggregates.filter(
(establishmentAggregate) =>
establishmentAggregate.establishment.siret !== siret,
);
}

public getSiretOfEstablishmentsToSuggestUpdate(): Promise<SiretDto[]> {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class PgEstablishmentAggregateRepository
implements EstablishmentAggregateRepository
{
constructor(private client: PoolClient) {}
public delete(_siret: SiretDto): Promise<void> {
throw new Error("NOT IMPLEMENTED");
}

public async insertEstablishmentAggregates(
aggregates: EstablishmentAggregate[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class PgFormEstablishmentRepository
{
constructor(private client: PoolClient) {}

public delete(_siret: SiretDto): Promise<void> {
throw new Error("NOT IMPLEMENTED");
}

public async getAll(): Promise<FormEstablishmentDto[]> {
const pgResult = await this.client.query(
"SELECT * FROM form_establishments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SearchImmersionParams = {
};

export interface EstablishmentAggregateRepository {
delete: (siret: SiretDto) => Promise<void>;
insertEstablishmentAggregates: (
establishmentAggregates: EstablishmentAggregate[],
) => Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormEstablishmentDto, SiretDto } from "shared";

export interface FormEstablishmentRepository {
delete: (siret: SiretDto) => Promise<void>;
create: (formEstablishmentDto: FormEstablishmentDto) => Promise<void>;
update: (formEstablishmentDto: FormEstablishmentDto) => Promise<void>;

Expand Down
60 changes: 60 additions & 0 deletions back/src/domain/immersionOffer/useCases/DeleteEstablishment.ts
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);
}
}
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(), []);
});
});
});

0 comments on commit 24f526b

Please sign in to comment.