diff --git a/.kube-workflow/env/prod/values.yaml b/.kube-workflow/env/prod/values.yaml index 0a397c563..2dfa01a2c 100644 --- a/.kube-workflow/env/prod/values.yaml +++ b/.kube-workflow/env/prod/values.yaml @@ -1,11 +1,9 @@ app: addVolumes: - docs - replicas: 1 # temporary, need to delete when swr fixed app-form: addVolumes: - docs-form - replicas: 1 # temporary, need to delete when swr fixed metabase: enabled: true diff --git a/package-lock.json b/package-lock.json index c24a70405..866bd2603 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "enfants-du-spectacle", - "version": "1.81.0", + "version": "1.83.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "enfants-du-spectacle", - "version": "1.81.0", + "version": "1.83.2", "dependencies": { "@dataesr/react-dsfr": "^0.9.2", - "@next-auth/prisma-adapter": "^1.0.0", - "@prisma/client": "^3.6.0", + "@next-auth/prisma-adapter": "^1.0.5", + "@prisma/client": "^4.7.0", "@sentry/nextjs": "^6.15.0", "@socialgouv/matomo-next": "^1.6.1", "b64-to-blob": "^1.2.19", @@ -68,7 +68,7 @@ "maildev": "^1.1.0", "mjml": "^4.11.0", "prettier": "^2.5.1", - "prisma": "^3.6.0", + "prisma": "^4.7.0", "slugify": "^1.6.3", "ts-node": "^10.4.0", "typescript": "4.5.2" @@ -1758,15 +1758,15 @@ } }, "node_modules/@prisma/client": { - "version": "3.15.2", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-3.15.2.tgz", - "integrity": "sha512-ErqtwhX12ubPhU4d++30uFY/rPcyvjk+mdifaZO5SeM21zS3t4jQrscy8+6IyB0GIYshl5ldTq6JSBo1d63i8w==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.15.0.tgz", + "integrity": "sha512-xnROvyABcGiwqRNdrObHVZkD9EjkJYHOmVdlKy1yGgI+XOzvMzJ4tRg3dz1pUlsyhKxXGCnjIQjWW+2ur+YXuw==", "hasInstallScript": true, "dependencies": { - "@prisma/engines-version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e" + "@prisma/engines-version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944" }, "engines": { - "node": ">=12.6" + "node": ">=14.17" }, "peerDependencies": { "prisma": "*" @@ -1778,16 +1778,16 @@ } }, "node_modules/@prisma/engines": { - "version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e.tgz", - "integrity": "sha512-NHlojO1DFTsSi3FtEleL9QWXeSF/UjhCW0fgpi7bumnNZ4wj/eQ+BJJ5n2pgoOliTOGv9nX2qXvmHap7rJMNmg==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.15.0.tgz", + "integrity": "sha512-FTaOCGs0LL0OW68juZlGxFtYviZa4xdQj/rQEdat2txw0s3Vu/saAPKjNVXfIgUsGXmQ72HPgNr6935/P8FNAA==", "devOptional": true, "hasInstallScript": true }, "node_modules/@prisma/engines-version": { - "version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e.tgz", - "integrity": "sha512-e3k2Vd606efd1ZYy2NQKkT4C/pn31nehyLhVug6To/q8JT8FpiMrDy7zmm3KLF0L98NOQQcutaVtAPhzKhzn9w==" + "version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz", + "integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg==" }, "node_modules/@rollup/plugin-image": { "version": "2.1.1", @@ -12182,20 +12182,20 @@ "dev": true }, "node_modules/prisma": { - "version": "3.15.2", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-3.15.2.tgz", - "integrity": "sha512-nMNSMZvtwrvoEQ/mui8L/aiCLZRCj5t6L3yujKpcDhIPk7garp8tL4nMx2+oYsN0FWBacevJhazfXAbV1kfBzA==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.15.0.tgz", + "integrity": "sha512-iKZZpobPl48gTcSZVawLMQ3lEy6BnXwtoMj7hluoGFYu2kQ6F9LBuBrUyF95zRVnNo8/3KzLXJXJ5TEnLSJFiA==", "devOptional": true, "hasInstallScript": true, "dependencies": { - "@prisma/engines": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e" + "@prisma/engines": "4.15.0" }, "bin": { "prisma": "build/index.js", "prisma2": "build/index.js" }, "engines": { - "node": ">=12.6" + "node": ">=14.17" } }, "node_modules/process": { @@ -16526,23 +16526,23 @@ "integrity": "sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==" }, "@prisma/client": { - "version": "3.15.2", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-3.15.2.tgz", - "integrity": "sha512-ErqtwhX12ubPhU4d++30uFY/rPcyvjk+mdifaZO5SeM21zS3t4jQrscy8+6IyB0GIYshl5ldTq6JSBo1d63i8w==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.15.0.tgz", + "integrity": "sha512-xnROvyABcGiwqRNdrObHVZkD9EjkJYHOmVdlKy1yGgI+XOzvMzJ4tRg3dz1pUlsyhKxXGCnjIQjWW+2ur+YXuw==", "requires": { - "@prisma/engines-version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e" + "@prisma/engines-version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944" } }, "@prisma/engines": { - "version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e.tgz", - "integrity": "sha512-NHlojO1DFTsSi3FtEleL9QWXeSF/UjhCW0fgpi7bumnNZ4wj/eQ+BJJ5n2pgoOliTOGv9nX2qXvmHap7rJMNmg==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.15.0.tgz", + "integrity": "sha512-FTaOCGs0LL0OW68juZlGxFtYviZa4xdQj/rQEdat2txw0s3Vu/saAPKjNVXfIgUsGXmQ72HPgNr6935/P8FNAA==", "devOptional": true }, "@prisma/engines-version": { - "version": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e.tgz", - "integrity": "sha512-e3k2Vd606efd1ZYy2NQKkT4C/pn31nehyLhVug6To/q8JT8FpiMrDy7zmm3KLF0L98NOQQcutaVtAPhzKhzn9w==" + "version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz", + "integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg==" }, "@rollup/plugin-image": { "version": "2.1.1", @@ -24591,12 +24591,12 @@ } }, "prisma": { - "version": "3.15.2", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-3.15.2.tgz", - "integrity": "sha512-nMNSMZvtwrvoEQ/mui8L/aiCLZRCj5t6L3yujKpcDhIPk7garp8tL4nMx2+oYsN0FWBacevJhazfXAbV1kfBzA==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.15.0.tgz", + "integrity": "sha512-iKZZpobPl48gTcSZVawLMQ3lEy6BnXwtoMj7hluoGFYu2kQ6F9LBuBrUyF95zRVnNo8/3KzLXJXJ5TEnLSJFiA==", "devOptional": true, "requires": { - "@prisma/engines": "3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e" + "@prisma/engines": "4.15.0" } }, "process": { diff --git a/package.json b/package.json index 96a90813c..bb0a36e86 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ }, "dependencies": { "@dataesr/react-dsfr": "^0.9.2", - "@next-auth/prisma-adapter": "^1.0.0", - "@prisma/client": "^3.6.0", + "@next-auth/prisma-adapter": "^1.0.5", + "@prisma/client": "^4.7.0", "@sentry/nextjs": "^6.15.0", "@socialgouv/matomo-next": "^1.6.1", "b64-to-blob": "^1.2.19", @@ -89,7 +89,7 @@ "maildev": "^1.1.0", "mjml": "^4.11.0", "prettier": "^2.5.1", - "prisma": "^3.6.0", + "prisma": "^4.7.0", "slugify": "^1.6.3", "ts-node": "^10.4.0", "typescript": "4.5.2" diff --git a/src/pages/api/commentaires/index.ts b/src/pages/api/commentaires/index.ts index 0cce702c8..17766f35f 100644 --- a/src/pages/api/commentaires/index.ts +++ b/src/pages/api/commentaires/index.ts @@ -1,10 +1,11 @@ -import type { Commentaire } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -31,13 +32,11 @@ function getId(req: NextApiRequest): number { const remove: NextApiHandler = async (req, res) => { const commentId = Number(req.body as string); try { - await prisma.commentaire.delete({ + await client.commentaire.delete({ where: { id: commentId }, }); - await prisma?.$disconnect() res.status(200).json({ message: "Commentaire supprimé" }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Commentaire non trouve" }); } @@ -45,7 +44,7 @@ const remove: NextApiHandler = async (req, res) => { const get: NextApiHandler = async (req, res) => { const dossierId = getId(req); - const allComments = await prisma.commentaire.findMany({ + const allComments = await client.commentaire.findMany({ include: { user: true, }, @@ -54,14 +53,13 @@ const get: NextApiHandler = async (req, res) => { dossierId: dossierId, }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(allComments)); }; const post: NextApiHandler = async (req, res) => { const data = JSON.parse(req.body as string); try { - await prisma.commentaire.create({ data }); + await client.commentaire.create({ data }); } catch (e: unknown) { console.log(e); } diff --git a/src/pages/api/commissions/[id].ts b/src/pages/api/commissions/[id].ts index c2051655b..c4cf39af3 100644 --- a/src/pages/api/commissions/[id].ts +++ b/src/pages/api/commissions/[id].ts @@ -1,15 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; -//import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { - /*const session = await getSession({ req }); - if (!session) { - res.status(401).end(); - return; - }*/ const { id: commissionIdStr } = req.query; if (typeof commissionIdStr !== "string") { res.status(404).send(`not a valid commission id`); @@ -32,7 +28,7 @@ function getId(req: NextApiRequest): number { const get: NextApiHandler = async (req, res) => { const id = getId(req); - const commission = await prisma.commission.findUnique({ + const commission = await client.commission.findUnique({ include: { dossiers: { include: { @@ -47,7 +43,6 @@ const get: NextApiHandler = async (req, res) => { }, where: { id }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(commission)); }; @@ -78,11 +73,10 @@ const update: NextApiHandler = async (req, res) => { updates.dateLimiteDepot = parsed.dateLimiteDepot; } - const updateCommission = await prisma.commission.update({ + const updateCommission = await client.commission.update({ data: updates, where: { id: parsed.id }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(updateCommission)); }; diff --git a/src/pages/api/commissions/date/index.ts b/src/pages/api/commissions/date/index.ts index 834329336..d4e7746ee 100644 --- a/src/pages/api/commissions/date/index.ts +++ b/src/pages/api/commissions/date/index.ts @@ -1,8 +1,10 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { if (req.method == "GET") { await get(req, res); @@ -13,12 +15,11 @@ const handler: NextApiHandler = async (req, res) => { }; const get: NextApiHandler = async (req, res) => { const commissions = await getUpcomingCommissions(); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(commissions)); }; const getUpcomingCommissions = async () => { - return prisma.commission.findMany({ + return client.commission.findMany({ orderBy: { date: "asc" }, where: { date: { gte: new Date() }, diff --git a/src/pages/api/commissions/index.ts b/src/pages/api/commissions/index.ts index eb5f7cf07..a95ffcfab 100644 --- a/src/pages/api/commissions/index.ts +++ b/src/pages/api/commissions/index.ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if ( @@ -40,14 +42,14 @@ const get: NextApiHandler = async (req, res) => { ? await getUpcomingCommissionsNotEmpty(req) : await getUpcomingCommissions() : await getUpcomingCommissionsByDepartement(departements as string); - await prisma?.$disconnect() + await client?.$disconnect() res.status(200).json(superjson.stringify(commissions)); }; const post: NextApiHandler = async (req, res) => { const data = JSON.parse(req.body as string); try { - await prisma?.commission.create({ data }); + await client?.commission.create({ data }); } catch (e: unknown) { console.log(e); } @@ -57,7 +59,7 @@ const post: NextApiHandler = async (req, res) => { const remove: NextApiHandler = async (req, res) => { const commissionId = Number(req.body as string); try { - await prisma?.commission.delete({ + await client?.commission.delete({ where: { id: commissionId }, }); res.status(200).json({ message: "Commission supprimée" }); @@ -69,7 +71,7 @@ const remove: NextApiHandler = async (req, res) => { const getUpcomingCommissions = async () => { console.log('upcoming') - return prisma?.commission.findMany({ + return client?.commission.findMany({ include: { dossiers: { include: { @@ -88,7 +90,7 @@ const getUpcomingCommissions = async () => { const getUpcomingCommissionsNotEmpty = async (req: NextApiRequest) => { const session = await getSession({ req }); console.log('upcoming not empty !!!') - return await prisma?.commission.findMany({ + return await client?.commission.findMany({ include: { dossiers: { where: session?.dbUser.role !== "MEDECIN" ? @@ -152,7 +154,7 @@ const getUpcomingCommissionsNotEmpty = async (req: NextApiRequest) => { const getUpcomingCommissionsByDepartement = async (departements: string) => { console.log('upcoming by departement') console.log("departements : ", departements.split(",")); - return prisma.commission.findMany({ + return client.commission.findMany({ include: { dossiers: { include: { @@ -177,7 +179,7 @@ const getUpcomingCommissionsByDepartement = async (departements: string) => { const getPastCommissions = async () => { console.log('past commissions') - return prisma.commission.findMany({ + return client.commission.findMany({ include: { dossiers: { include: { diff --git a/src/pages/api/commissions/upcoming/index.tsx b/src/pages/api/commissions/upcoming/index.tsx index 9b36b1cf2..a7214f83c 100644 --- a/src/pages/api/commissions/upcoming/index.tsx +++ b/src/pages/api/commissions/upcoming/index.tsx @@ -1,8 +1,9 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; -import superjson from "superjson"; + +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); @@ -21,14 +22,13 @@ const handler: NextApiHandler = async (req, res) => { const get: NextApiHandler = async (req, res) => { const departement = req.query.departement; - const commissions = await prisma?.commission.findMany({ + const commissions = await client.commission.findMany({ orderBy: { date: "asc" }, where: { date: { gte: new Date() }, - departement: departement, + departement: departement as string, }, }); - await prisma?.$disconnect() res.status(200).json(commissions); }; diff --git a/src/pages/api/dossiers/[id].ts b/src/pages/api/dossiers/[id].ts index a4e9cbca4..bbc4fa1ea 100644 --- a/src/pages/api/dossiers/[id].ts +++ b/src/pages/api/dossiers/[id].ts @@ -2,11 +2,13 @@ import type { Dossier, StatutDossier } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import type { TransitionEvent } from "src/lib/statutDossierStateMachine"; import { factory as statutDossierStateMachineFactory } from "src/lib/statutDossierStateMachine"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -39,7 +41,7 @@ const get: NextApiHandler = async (req, res) => { const session = await getSession({ req }); const dossierId = getId(req); - const dossier = await prisma?.dossier.findUnique({ + const dossier = await client.dossier.findUnique({ include: { commentaires: { include: { @@ -68,7 +70,6 @@ const get: NextApiHandler = async (req, res) => { }, where: { id: dossierId }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(dossier)); }; @@ -76,7 +77,7 @@ const get: NextApiHandler = async (req, res) => { const remove: NextApiHandler = async (req, res) => { const dossierId = getId(req); try { - await prisma.dossier.delete({ + await client.dossier.delete({ where: { id: dossierId }, }); res.status(200).json({ message: "Dossier supprimé" }); @@ -111,7 +112,7 @@ const update: NextApiHandler = async (req, res) => { if (typeof parsed.transitionEvent === "string") { const transition = parsed.transitionEvent; - const dossier: Dossier = await prisma.dossier.findUnique({ + const dossier: Dossier | null = await client.dossier.findUnique({ where: { id: dossierId }, }); if (!dossier) { @@ -285,7 +286,7 @@ const update: NextApiHandler = async (req, res) => { console.log("updates : ", updates); - const updatedDossier = await prisma.dossier.update({ + const updatedDossier = await client.dossier.update({ data: updates, include: { commission: true, diff --git a/src/pages/api/enfant/[id].ts b/src/pages/api/enfant/[id].ts index 6b2643c44..d56bc1af0 100644 --- a/src/pages/api/enfant/[id].ts +++ b/src/pages/api/enfant/[id].ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -41,7 +43,7 @@ const update: NextApiHandler = async (req, res) => { const enfantId = getId(req); - const updatedEnfant = await prisma.enfant.update({ + const updatedEnfant = await client.enfant.update({ data: { adresseEnfant: parsed.adresseEnfant, adresseRepresentant1: parsed.adresseRepresentant1, @@ -57,7 +59,6 @@ const update: NextApiHandler = async (req, res) => { }, where: { id: enfantId }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(updatedEnfant)); }; diff --git a/src/pages/api/enfants/index.ts b/src/pages/api/enfants/index.ts index d2286116f..2f00ea90a 100644 --- a/src/pages/api/enfants/index.ts +++ b/src/pages/api/enfants/index.ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -33,13 +35,12 @@ const update: NextApiHandler = async (req, res) => { const dossierId = parsed.dossierId; - const updatedEnfant = await prisma.enfant.updateMany({ + const updatedEnfant = await client.enfant.updateMany({ data: { cdc: parsed.cdc, }, where: { dossierId: dossierId }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(updatedEnfant)); }; diff --git a/src/pages/api/search.json.ts b/src/pages/api/search.json.ts index 890078241..db3631adb 100644 --- a/src/pages/api/search.json.ts +++ b/src/pages/api/search.json.ts @@ -1,10 +1,12 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import { searchDossiers, searchEnfants } from "src/lib/queries"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { if (req.method !== "GET") { res.status(405).end(); @@ -22,8 +24,8 @@ const handler: NextApiHandler = async (req, res) => { const tsquery = req.query.search.trim().replace(/ +/g, " & "); - const enfants = await searchEnfants(prisma, tsquery); - const dossiers = await searchDossiers(prisma, tsquery); + const enfants = await searchEnfants(client, tsquery); + const dossiers = await searchDossiers(client, tsquery); res.status(200).json(superjson.stringify({ dossiers, enfants })); }; diff --git a/src/pages/api/sendlist/index.ts b/src/pages/api/sendlist/index.ts index 1451f3a4d..1c55c80a8 100644 --- a/src/pages/api/sendlist/index.ts +++ b/src/pages/api/sendlist/index.ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -30,7 +32,7 @@ function getId(req: NextApiRequest): number { const post: NextApiHandler = async (req, res) => { const data = JSON.parse(req.body as string); try { - const sendList = await prisma.sendList.create({ data }); + const sendList = await client.sendList.create({ data }); res.status(200).json(superjson.stringify(sendList)); } catch (e: unknown) { console.log(e); @@ -39,7 +41,7 @@ const post: NextApiHandler = async (req, res) => { const get: NextApiHandler = async (req, res) => { const commissionId = getId(req); - const sendList = await prisma.sendList.findMany({ + const sendList = await client.sendList.findMany({ include: { user: true, }, @@ -72,7 +74,7 @@ const update: NextApiHandler = async (req, res) => { updates.send = false; } - const updateSendList = await prisma.sendList.update({ + const updateSendList = await client.sendList.update({ data: updates, where: { id: parsed.id }, }); diff --git a/src/pages/api/sync/inc/dossiers/[id].tsx b/src/pages/api/sync/inc/dossiers/[id].tsx index 499144ad3..47e9705bd 100644 --- a/src/pages/api/sync/inc/dossiers/[id].tsx +++ b/src/pages/api/sync/inc/dossiers/[id].tsx @@ -1,7 +1,9 @@ -import { PrismaClient } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { if (req.method === "GET") { await get(req, res); @@ -19,7 +21,6 @@ function getId(req: NextApiRequest): number { } const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const parsed = JSON.parse(req.body as string); const dossierId = getId(req); @@ -27,7 +28,7 @@ const remove: NextApiHandler = async (req, res) => { res.status(401).json({ error: `Unauthorized` }); } else { try { - await prisma.dossier.delete({ + await client.dossier.delete({ where: { externalId: dossierId.toString() }, }); } catch (e: unknown) { @@ -42,24 +43,22 @@ const get: NextApiHandler = async (req, res) => { const id = match ? match[1] : ""; const token = match ? match[2] : ""; const data = { id, token }; - const prisma = new PrismaClient(); if (data.token !== process.env.API_KEY_INSTRUCTEUR) { res.status(401).json({ error: "Unauthorized" }); } else { try { if (data.id) { - const dossier = await prisma.dossier.findFirst({ + const dossier = await client.dossier.findFirst({ where: { externalId: data.id.toString() }, }); - const commission = await prisma.commission.findFirst({ + const commission = await client.commission.findFirst({ where: { id: dossier?.commissionId }, }); if (dossier?.userId) { - const instructeur = await prisma.user.findFirst({ + const instructeur = await client.user.findFirst({ where: { id: dossier.userId }, }); - await prisma?.$disconnect() res.status(200).json({ commissionDate: commission?.date, @@ -70,7 +69,6 @@ const get: NextApiHandler = async (req, res) => { } } } catch (e: unknown) { - await prisma?.$disconnect() res.status(500).json({ message: "Internal server error" }); console.log(e); } diff --git a/src/pages/api/sync/inc/dossiers/index.tsx b/src/pages/api/sync/inc/dossiers/index.tsx index 06cf2dc38..f89458eeb 100644 --- a/src/pages/api/sync/inc/dossiers/index.tsx +++ b/src/pages/api/sync/inc/dossiers/index.tsx @@ -9,9 +9,11 @@ import { SocieteProductionModel, } from "prisma/zod"; import { frenchDateText, frenchDepartementName } from "src/lib/helpers"; -import prisma from "src/lib/prismaClient"; import type { z } from "zod"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { if (req.method == "POST") { await post(req, res); @@ -42,7 +44,7 @@ const post: NextApiHandler = async (req, res) => { otherConventionCollective: true, id: true, }); - const createSociete = await prisma?.societeProduction.create({ + const createSociete = await client.societeProduction.create({ data: { ...SocieteData.parse(data.societeProduction), ["conventionCollectiveCode"]: data.demandeur.conventionCollectiveCode, @@ -52,7 +54,7 @@ const post: NextApiHandler = async (req, res) => { //console.log('societe created') //HANDLE FINDING DEMANDEUR - const demandeurFound = await prisma?.demandeur.findUnique({ + const demandeurFound = await client.demandeur.findUnique({ where: { email: data.demandeur.email, }, @@ -68,7 +70,7 @@ const post: NextApiHandler = async (req, res) => { id: true, societeProductionId: true, }); - createDemandeur = await prisma?.demandeur.create({ + createDemandeur = await client.demandeur.create({ data: { ...DemandeurData.parse(data.demandeur), fonction: "", @@ -82,7 +84,7 @@ const post: NextApiHandler = async (req, res) => { } //SEARCHING FOR COMMISSION - const commissions: Commission[] = await prisma?.commission.findMany({ + const commissions: Commission[] = await client.commission.findMany({ orderBy: { date: "asc" }, where: { dateLimiteDepot: { gte: new Date() }, @@ -101,7 +103,7 @@ const post: NextApiHandler = async (req, res) => { statut: true, userId: true, }); - const createDossier = await prisma?.dossier.create({ + const createDossier = await client.dossier.create({ data: { ...DossierData.parse(data.dossier), commission: { @@ -135,7 +137,7 @@ const post: NextApiHandler = async (req, res) => { //HANDLE ENFANTS if (data.enfants.length > 0) { const EnfantsData = EnfantModel.omit({ dossierId: true, id: true }); - const CreateEnfants = await prisma?.enfant.createMany({ + const CreateEnfants = await client.enfant.createMany({ data: data.enfants.map((enfant) => { enfant.nombreJours = typeof enfant.nombreJours === "string" @@ -169,14 +171,12 @@ const post: NextApiHandler = async (req, res) => { }); } - await prisma?.$disconnect() res.status(200).json({ message: `${frenchDepartementName( commissions[0].departement )}, ${frenchDateText(commissions[0].date)}`, }); } catch (e) { - await prisma?.$disconnect() console.log("error : ", e); res.status(500).json({ error: e }); } @@ -191,7 +191,7 @@ const update: NextApiHandler = async (req, res) => { }; try { - const commissions = await prisma?.commission.findMany({ + const commissions = await client.commission.findMany({ orderBy: { date: "asc" }, where: { dateLimiteDepot: { gte: new Date() }, @@ -210,7 +210,7 @@ const update: NextApiHandler = async (req, res) => { statut: true, userId: true, }); - const updateDossier = await prisma?.dossier.update({ + const updateDossier = await client.dossier.update({ data: { ...DossierData.parse(data.dossier), conventionCollectiveCode: data.demandeur.conventionCollectiveCode, @@ -227,7 +227,7 @@ const update: NextApiHandler = async (req, res) => { }); //HANDLE ENFANTS - const listEnfant: Enfant[] = await prisma?.enfant.findMany({ + const listEnfant: Enfant[] = await client.enfant.findMany({ where: { dossierId: updateDossier.id, }, @@ -266,7 +266,7 @@ const update: NextApiHandler = async (req, res) => { ) ) { console.log("has to update enfant :", enfant); - const updateEnfant = await prisma?.enfant.update({ + const updateEnfant = await client.enfant.update({ data: { ...EnfantsData.parse(enfant), justificatifs: enfant.piecesDossier @@ -279,7 +279,7 @@ const update: NextApiHandler = async (req, res) => { }); } else { console.log("has to create enfant :", enfant); - const CreateEnfants = await prisma?.enfant.create({ + const CreateEnfants = await client.enfant.create({ data: { ...EnfantsData.parse(enfant), dossierId: updateDossier.id, @@ -291,14 +291,12 @@ const update: NextApiHandler = async (req, res) => { }); } }); - await prisma?.$disconnect() res.status(200).json({ message: `${frenchDepartementName( commissions[0].departement )}, ${frenchDateText(commissions[0].date)}`, }); } catch (e) { - await prisma?.$disconnect() console.log("error : ", e); res.status(500).json({ error: e }); } diff --git a/src/pages/api/sync/inc/enfant/[id].tsx b/src/pages/api/sync/inc/enfant/[id].tsx index d58fca1b4..cd03a1c97 100644 --- a/src/pages/api/sync/inc/enfant/[id].tsx +++ b/src/pages/api/sync/inc/enfant/[id].tsx @@ -1,7 +1,9 @@ -import { PrismaClient } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler, NextApiRequest } from "next"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { if (req.method == "DELETE") { await remove(req, res); @@ -16,14 +18,13 @@ function getId(req: NextApiRequest): number { } const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const parsed = JSON.parse(req.body as string); const enfantId = getId(req); if (parsed.api_key !== process.env.API_KEY_SDP) { res.status(401).json({ error: `Unauthorized` }); } else { try { - await prisma.enfant.delete({ + await client.enfant.delete({ where: { externalId: enfantId.toString() }, }); } catch (e: unknown) { diff --git a/src/pages/api/sync/out/commentaires/index.tsx b/src/pages/api/sync/out/commentaires/index.tsx index bb2045591..97898f320 100644 --- a/src/pages/api/sync/out/commentaires/index.tsx +++ b/src/pages/api/sync/out/commentaires/index.tsx @@ -1,4 +1,3 @@ -import { STATUT_PIECE } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; diff --git a/src/pages/api/sync/out/enfants/index.tsx b/src/pages/api/sync/out/enfants/index.tsx index 0ee8f8aad..20cbb8d89 100644 --- a/src/pages/api/sync/out/enfants/index.tsx +++ b/src/pages/api/sync/out/enfants/index.tsx @@ -1,4 +1,4 @@ -import { Enfant, STATUT_PIECE } from "@prisma/client"; +import { Enfant } from "@prisma/client"; import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; diff --git a/src/pages/api/users/index.ts b/src/pages/api/users/index.ts index 57083c473..e39b6ad37 100644 --- a/src/pages/api/users/index.ts +++ b/src/pages/api/users/index.ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma, Role } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -28,7 +30,7 @@ const handler: NextApiHandler = async (req, res) => { const post: NextApiHandler = async (req, res) => { const data = JSON.parse(req.body as string); try { - await prisma.user.create({ data }); + await client.user.create({ data }); } catch (e: unknown) { console.log(e); } @@ -38,13 +40,11 @@ const post: NextApiHandler = async (req, res) => { const remove: NextApiHandler = async (req, res) => { const userId = Number(req.body as string); try { - await prisma.user.delete({ + await client.user.delete({ where: { id: userId }, }); - await prisma?.$disconnect() res.status(200).json({ message: "Utilisateur supprimé" }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Utilisateur non trouvé" }); } @@ -64,13 +64,12 @@ const update: NextApiHandler = async (req, res) => { const userId = parsed.id; - const updatedUser = await prisma.user.update({ + const updatedUser = await client.user.update({ data: { departements: parsed.departements, }, where: { id: userId }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(updatedUser)); }; @@ -79,14 +78,13 @@ const get: NextApiHandler = async (req, res) => { const role: string = req.query.role as string; const allUsers = role == "all" - ? await prisma.user.findMany({ + ? await client.user.findMany({ orderBy: { name: "asc" }, }) - : await prisma.user.findMany({ + : await client.user.findMany({ orderBy: { name: "asc" }, - where: { role: role.toUpperCase() }, + where: { role: role.toUpperCase() as Role }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(allUsers)); }; diff --git a/src/pages/api/users/search/index.ts b/src/pages/api/users/search/index.ts index bd99c366c..7a80b9775 100644 --- a/src/pages/api/users/search/index.ts +++ b/src/pages/api/users/search/index.ts @@ -1,9 +1,11 @@ import { withSentry } from "@sentry/nextjs"; import type { NextApiHandler } from "next"; import { getSession } from "next-auth/react"; -import prisma from "src/lib/prismaClient"; import superjson from "superjson"; +import { PrismaClient, Prisma } from '@prisma/client' +const client = new PrismaClient() + const handler: NextApiHandler = async (req, res) => { const session = await getSession({ req }); if (!session) { @@ -22,7 +24,7 @@ const handler: NextApiHandler = async (req, res) => { const get: NextApiHandler = async (req, res) => { const departement = req.query.departement; if (typeof departement === "string") { - const allUsers = await prisma.user.findMany({ + const allUsers = await client.user.findMany({ orderBy: { name: "asc" }, where: { departements: { @@ -30,10 +32,8 @@ const get: NextApiHandler = async (req, res) => { }, }, }); - await prisma?.$disconnect() res.status(200).json(superjson.stringify(allUsers)); } else { - await prisma?.$disconnect() res .status(401) .json(superjson.stringify({ error: "No departement provided" }));