Skip to content

Commit

Permalink
feat: add creation info for estimation aides table
Browse files Browse the repository at this point in the history
  • Loading branch information
rtaieb committed Jan 22, 2025
1 parent 3710fe4 commit d7879a2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}
Expand Down
5 changes: 3 additions & 2 deletions src/actions/estimation/add-aide-in-estimation-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/actions/estimation/delete-aide-in-estimation-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/aidesTerritoires/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const callAidesTerritoiresApi = async <T extends IApiAidesTerritoiresResp
return callAidesTerritoiresApi(url, true);
}
if (response.status >= 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;
Expand Down
63 changes: 63 additions & 0 deletions src/lib/prisma/prisma-aide-estimation-queries.ts
Original file line number Diff line number Diff line change
@@ -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<EstimationAide> => {
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<EstimationAide | null> => {
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;
};
61 changes: 1 addition & 60 deletions src/lib/prisma/prismaEstimationQueries.ts
Original file line number Diff line number Diff line change
@@ -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<estimation | null> => {
Expand Down Expand Up @@ -78,58 +74,3 @@ export const updateEstimationMateriaux = async (

return response;
};

export const addAideInEstimation = async (estimationId: number, aideId: number): Promise<EstimationAide> => {
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<EstimationAide | null> => {
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;
};

0 comments on commit d7879a2

Please sign in to comment.