From d7879a26190abc2462ed7b225541a0fc4be079bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Ta=C3=AFeb?= Date: Wed, 22 Jan 2025 10:59:38 +0100 Subject: [PATCH] feat: add creation info for estimation aides table --- .../migration.sql | 10 +++ prisma/schema.prisma | 4 ++ .../add-aide-in-estimation-action.ts | 5 +- .../delete-aide-in-estimation-action.ts | 3 +- src/lib/aidesTerritoires/helpers.ts | 2 +- .../prisma/prisma-aide-estimation-queries.ts | 63 +++++++++++++++++++ src/lib/prisma/prismaEstimationQueries.ts | 61 +----------------- 7 files changed, 84 insertions(+), 64 deletions(-) create mode 100644 prisma/migrations/20250122094553_add_created_date_and_creator_for_aide_estimation_table/migration.sql create mode 100644 src/lib/prisma/prisma-aide-estimation-queries.ts diff --git a/prisma/migrations/20250122094553_add_created_date_and_creator_for_aide_estimation_table/migration.sql b/prisma/migrations/20250122094553_add_created_date_and_creator_for_aide_estimation_table/migration.sql new file mode 100644 index 00000000..2062d2af --- /dev/null +++ b/prisma/migrations/20250122094553_add_created_date_and_creator_for_aide_estimation_table/migration.sql @@ -0,0 +1,10 @@ +-- AlterTable +ALTER TABLE "estimations_aides" + ADD COLUMN "created_at" TIMESTAMP(3), + ADD COLUMN "user_id" TEXT; +ALTER TABLE "estimations_aides" + ALTER "created_at" SET DEFAULT CURRENT_TIMESTAMP; + +-- AddForeignKey +ALTER TABLE "estimations_aides" + ADD CONSTRAINT "estimations_aides_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index facc1fe0..02d2fb2d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -58,6 +58,7 @@ model User { sourcing_contacts_created projet_sourcing_contact[] accept_communication_produit Boolean @default(true) emails email[] + estimations_aides estimations_aides[] } model VerificationToken { @@ -260,6 +261,9 @@ model estimations_aides { estimationId Int aide aide @relation(fields: [aideId], references: [id]) aideId Int + created_at DateTime? @default(now()) + user_id String? + created_by User? @relation(fields: [user_id], references: [id]) @@id([estimationId, aideId]) } diff --git a/src/actions/estimation/add-aide-in-estimation-action.ts b/src/actions/estimation/add-aide-in-estimation-action.ts index e9bfd3f9..9ae87388 100644 --- a/src/actions/estimation/add-aide-in-estimation-action.ts +++ b/src/actions/estimation/add-aide-in-estimation-action.ts @@ -3,13 +3,14 @@ import { fetchAideFromAidesTerritoiresById } from "@/src/lib/aidesTerritoires/fe import { auth } from "@/src/lib/next-auth/auth"; import { ResponseAction } from "../actions-types"; -import { addAideInEstimation, getEstimationById } from "@/src/lib/prisma/prismaEstimationQueries"; +import { getEstimationById } from "@/src/lib/prisma/prismaEstimationQueries"; import { customCaptureException } from "@/src/lib/sentry/sentryCustomMessage"; import { upsertAide } from "@/src/lib/prisma/prismaAideQueries"; import { resolveAidType } from "@/src/components/financement/helpers"; import { EstimationAide } from "@/src/lib/prisma/prismaCustomTypes"; import { PermissionManager } from "@/src/helpers/permission-manager"; +import { addAideInEstimation } from "@/src/lib/prisma/prisma-aide-estimation-queries"; export const addAideInEstimationAction = async ( estimationId: number, @@ -48,7 +49,7 @@ export const addAideInEstimationAction = async ( return { type: "error", message: "PROJET_UPDATE_UNAUTHORIZED" }; } - const estimationAide = await addAideInEstimation(estimationId, upsertedAide.id); + const estimationAide = await addAideInEstimation(estimationId, upsertedAide.id, session.user.id); return { type: "success", message: "ESTIMATION_AIDE_ADDED", estimationAide }; } catch (e) { customCaptureException("Error in updateAideInEstimation DB call", e); diff --git a/src/actions/estimation/delete-aide-in-estimation-action.ts b/src/actions/estimation/delete-aide-in-estimation-action.ts index ef9fcd1d..9aa57187 100644 --- a/src/actions/estimation/delete-aide-in-estimation-action.ts +++ b/src/actions/estimation/delete-aide-in-estimation-action.ts @@ -2,11 +2,12 @@ import { auth } from "@/src/lib/next-auth/auth"; import { ResponseAction } from "../actions-types"; -import { deleteAideInEstimation, getEstimationById } from "@/src/lib/prisma/prismaEstimationQueries"; +import { getEstimationById } from "@/src/lib/prisma/prismaEstimationQueries"; import { customCaptureException } from "@/src/lib/sentry/sentryCustomMessage"; import { EstimationAide } from "@/src/lib/prisma/prismaCustomTypes"; import { Prisma } from "@prisma/client"; import { PermissionManager } from "@/src/helpers/permission-manager"; +import { deleteAideInEstimation } from "@/src/lib/prisma/prisma-aide-estimation-queries"; export const deleteAideInEstimationAction = async ( estimationId: number, diff --git a/src/lib/aidesTerritoires/helpers.ts b/src/lib/aidesTerritoires/helpers.ts index 2f5de746..06b37c82 100644 --- a/src/lib/aidesTerritoires/helpers.ts +++ b/src/lib/aidesTerritoires/helpers.ts @@ -61,7 +61,7 @@ export const callAidesTerritoiresApi = async = 400) { - captureError("Error when calling Aides territoires API", result.message); + captureError(`Error when calling Aides territoires API status ${response.status}`, result.message); return null; } return result; diff --git a/src/lib/prisma/prisma-aide-estimation-queries.ts b/src/lib/prisma/prisma-aide-estimation-queries.ts new file mode 100644 index 00000000..167bcd0b --- /dev/null +++ b/src/lib/prisma/prisma-aide-estimation-queries.ts @@ -0,0 +1,63 @@ +import { prismaClient } from "@/src/lib/prisma/prismaClient"; +import { EstimationAide } from "@/src/lib/prisma/prismaCustomTypes"; +import { projetUpdated } from "./prismaProjetQueries"; + +export const addAideInEstimation = async ( + estimationId: number, + aideId: number, + userId: string, +): Promise => { + const response = await prismaClient.estimations_aides.upsert({ + where: { + estimationId_aideId: { + estimationId, + aideId, + }, + }, + update: {}, + create: { + estimationId, + aideId, + user_id: userId, + }, + include: { + aide: true, + }, + }); + + const estimation = await prismaClient.estimation.findUnique({ + where: { id: estimationId }, + select: { projet_id: true }, + }); + + if (estimation) { + await projetUpdated(estimation.projet_id); + } + + return response; +}; + +export const deleteAideInEstimation = async (estimationId: number, aideId: number): Promise => { + const response = await prismaClient.estimations_aides.delete({ + where: { + estimationId_aideId: { + estimationId, + aideId, + }, + }, + include: { + aide: true, + }, + }); + + const estimation = await prismaClient.estimation.findUnique({ + where: { id: estimationId }, + select: { projet_id: true }, + }); + + if (estimation) { + await projetUpdated(estimation.projet_id); + } + + return response; +}; diff --git a/src/lib/prisma/prismaEstimationQueries.ts b/src/lib/prisma/prismaEstimationQueries.ts index fbf2b560..c46145a3 100644 --- a/src/lib/prisma/prismaEstimationQueries.ts +++ b/src/lib/prisma/prismaEstimationQueries.ts @@ -1,11 +1,7 @@ import { generateRandomId } from "@/src/helpers/common"; import { prismaClient } from "@/src/lib/prisma/prismaClient"; import { estimation, Prisma } from "@prisma/client"; -import { - EstimationAide, - EstimationMateriauxFicheSolution, - EstimationWithAides, -} from "@/src/lib/prisma/prismaCustomTypes"; +import { EstimationMateriauxFicheSolution, EstimationWithAides } from "@/src/lib/prisma/prismaCustomTypes"; import { projetUpdated } from "./prismaProjetQueries"; export const getEstimationById = async (estimationId: number): Promise => { @@ -78,58 +74,3 @@ export const updateEstimationMateriaux = async ( return response; }; - -export const addAideInEstimation = async (estimationId: number, aideId: number): Promise => { - const response = await prismaClient.estimations_aides.upsert({ - where: { - estimationId_aideId: { - estimationId, - aideId, - }, - }, - update: {}, - create: { - estimationId, - aideId, - }, - include: { - aide: true, - }, - }); - - const estimation = await prismaClient.estimation.findUnique({ - where: { id: estimationId }, - select: { projet_id: true }, - }); - - if (estimation) { - await projetUpdated(estimation.projet_id); - } - - return response; -}; - -export const deleteAideInEstimation = async (estimationId: number, aideId: number): Promise => { - const response = await prismaClient.estimations_aides.delete({ - where: { - estimationId_aideId: { - estimationId, - aideId, - }, - }, - include: { - aide: true, - }, - }); - - const estimation = await prismaClient.estimation.findUnique({ - where: { id: estimationId }, - select: { projet_id: true }, - }); - - if (estimation) { - await projetUpdated(estimation.projet_id); - } - - return response; -};