Skip to content

Commit

Permalink
Correctifs pour la fonction de clonage et les BSFF (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelFerrand authored Nov 8, 2024
1 parent cdc75ea commit ec9e125
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import gql from "graphql-tag";
import {
bsddFinalOperationFactory,
formFactory,
userFactory
userFactory,
userWithCompanyFactory
} from "../../../../__tests__/factories";
import { Mutation } from "../../../../generated/graphql/types";
import makeClient from "../../../../__tests__/testClient";
Expand All @@ -26,10 +27,12 @@ import {
} from "../../../../bsdasris/__tests__/factories";
import {
createBsff,
createBsffPackagingFinalOperation
createBsffPackagingFinalOperation,
createFicheIntervention
} from "../../../../bsffs/__tests__/factories";
import { bsvhuFactory } from "../../../../bsvhu/__tests__/factories.vhu";
import { bspaohFactory } from "../../../../bspaoh/__tests__/factories";
import { UserRole } from "@prisma/client";

const CLONE_BSD = gql`
mutation cloneBsd($id: String!) {
Expand Down Expand Up @@ -370,18 +373,52 @@ describe("mutation cloneBsd", () => {
});

expectBsdsToMatch(initialBsff, newBsff);
});

it("should clone BSFF with fiche d'intervention", async () => {
// Given
const user = await userFactory();
const operateur = await userWithCompanyFactory(UserRole.ADMIN);
const detenteur = await userWithCompanyFactory(UserRole.ADMIN);
const ficheIntervention = await createFicheIntervention({
operateur,
detenteur
});
const bsff = await createBsff(
{},
{
data: {
ficheInterventions: { connect: { id: ficheIntervention.id } },
detenteurCompanySirets: [detenteur.company.orgId]
}
}
);

// When
const { mutate } = makeClient(user);
const { errors, data } = await mutate<Pick<Mutation, "cloneBsd">>(
CLONE_BSD,
{
variables: {
id: bsff.id
}
}
);

// Then
expect(errors).toBeUndefined();

const initialPackagings = await prisma.bsffPackaging.findMany({
where: { bsffId: initialBsff.id },
include: { finalOperations: true }
const initialBsff = await prisma.bsff.findFirstOrThrow({
where: { id: bsff.id },
include: bsffInclude
});

const newPackagings = await prisma.bsffPackaging.findMany({
where: { bsffId: newBsff.id },
include: { finalOperations: true }
const newBsff = await prisma.bsff.findFirstOrThrow({
where: { id: data.cloneBsd.id },
include: bsffInclude
});

expectBsdsToMatch(initialPackagings, newPackagings);
expectBsdsToMatch(initialBsff, newBsff);
});

it("should clone regular BSVHU", async () => {
Expand Down
13 changes: 12 additions & 1 deletion back/src/bsds/resolvers/mutations/utils/clone.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ export const cloneBsff = async (user: Express.User, id: string) => {
throw new UserInputError(`ID invalide ${id}`);
}

if (bsff.packagings?.some(packaging => packaging.nextPackagingId)) {
throw new UserInputError(
"Impossible de cloner ce type de BSD pour le moment"
);
}

const { create } = getBsffRepository(user);

const newBsffCreateInput: Omit<
Expand Down Expand Up @@ -441,7 +447,12 @@ export const cloneBsff = async (user: Express.User, id: string) => {
emitterEmissionSignatureAuthor: bsff.emitterEmissionSignatureAuthor,
emitterEmissionSignatureDate: bsff.emitterEmissionSignatureDate,
ficheInterventions: bsff.ficheInterventions.length
? { create: bsff.ficheInterventions[0] }
? {
create: {
...bsff.ficheInterventions[0],
id: undefined
}
}
: {},
isDeleted: bsff.isDeleted,
isDraft: bsff.isDraft,
Expand Down

0 comments on commit ec9e125

Please sign in to comment.