From 7cbdf4a3cd0e6f88f904d19ff991b85aa90ca9ea Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Tue, 12 Dec 2023 17:13:47 +0100 Subject: [PATCH] fix: use global prisma client in forms --- formulaire/pages/api/auth/[...nextauth].ts | 9 +-- formulaire/pages/api/demandeur/[id].tsx | 12 ++-- formulaire/pages/api/dossier/[id].tsx | 17 ++--- .../pages/api/dossier/duplicate/[id].tsx | 8 +-- formulaire/pages/api/enfants/[id].tsx | 9 +-- formulaire/pages/api/enfants/dossier/[id].tsx | 68 +++++++++---------- formulaire/pages/api/piece/dossier/[id].tsx | 14 ++-- formulaire/pages/api/piece/enfant/[id].tsx | 55 +++++++-------- formulaire/pages/api/remunerations/[id].tsx | 7 +- formulaire/pages/api/users/[email].tsx | 11 ++- formulaire/pages/api/users/index.tsx | 11 ++- 11 files changed, 94 insertions(+), 127 deletions(-) diff --git a/formulaire/pages/api/auth/[...nextauth].ts b/formulaire/pages/api/auth/[...nextauth].ts index bfd9d3884..e7a65516f 100644 --- a/formulaire/pages/api/auth/[...nextauth].ts +++ b/formulaire/pages/api/auth/[...nextauth].ts @@ -1,13 +1,10 @@ // @ts-nocheck import { PrismaAdapter } from "@next-auth/prisma-adapter"; -import { PrismaClient } from "@prisma/client" import * as Sentry from "@sentry/nextjs"; import NextAuth from "next-auth"; import EmailAuthProvider from "next-auth/providers/email"; import { sendVerificationRequest } from "../../../src/lib/emailAuth"; -import type { NextAuthOptions } from 'next-auth' - -const prisma = new PrismaClient(); +import type { NextAuthOptions } from "next-auth"; export const authOptions: NextAuthOptions = { adapter: PrismaAdapter(prisma), @@ -18,7 +15,7 @@ export const authOptions: NextAuthOptions = { }, async signIn({ user }) { if (typeof user.email !== "string") return false; - return true + return true; }, }, logger: { @@ -48,4 +45,4 @@ export const authOptions: NextAuthOptions = { secret: process.env.NEXTAUTH_SECRET, }; -export default NextAuth(authOptions) \ No newline at end of file +export default NextAuth(authOptions); diff --git a/formulaire/pages/api/demandeur/[id].tsx b/formulaire/pages/api/demandeur/[id].tsx index d314bdff5..840755225 100644 --- a/formulaire/pages/api/demandeur/[id].tsx +++ b/formulaire/pages/api/demandeur/[id].tsx @@ -1,7 +1,7 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -27,10 +27,9 @@ function getId(req: NextApiRequest): number { } const get: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const demandeurId = getId(req); try { - const demandeur = await prisma.demandeur.findFirst({ + const demandeur = await client.demandeur.findFirst({ where: { id: demandeurId, }, @@ -42,10 +41,9 @@ const get: NextApiHandler = async (req, res) => { }; const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); try { const demandeurId = getId(req); - await prisma.demandeur.delete({ + await client.demandeur.delete({ where: { id: demandeurId }, }); @@ -60,11 +58,9 @@ const remove: NextApiHandler = async (req, res) => { } return r.json(); }); - await prisma?.$disconnect() res.status(200).json({ message: "Demandeur supprimé" }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Demandeur non trouvé" }); } diff --git a/formulaire/pages/api/dossier/[id].tsx b/formulaire/pages/api/dossier/[id].tsx index 0a6c7f4b9..134383c13 100644 --- a/formulaire/pages/api/dossier/[id].tsx +++ b/formulaire/pages/api/dossier/[id].tsx @@ -1,12 +1,12 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient } from "@prisma/client"; import { generateToken } from "src/lib/utils"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); - console.log('session id', session) + console.log("session id", session); if (!session) { res.status(401).end(); return; @@ -33,10 +33,9 @@ function getId(req: NextApiRequest): number { } const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); try { const dossierId = getId(req); - const dossierDeleted = await prisma.dossier.delete({ + const dossierDeleted = await client.dossier.delete({ where: { id: dossierId }, }); @@ -51,11 +50,9 @@ const remove: NextApiHandler = async (req, res) => { } return r.json(); }); - await prisma?.$disconnect() res.status(200).json({ dossierDeleted }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Dossier non trouvé" }); } @@ -63,13 +60,12 @@ const remove: NextApiHandler = async (req, res) => { const get: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); - const prisma = new PrismaClient(); var jwt = require("jsonwebtoken"); try { const dossierId = getId(req); - const dossier = await prisma.dossier.findUnique({ + const dossier = await client.dossier.findUnique({ include: { user: true, enfants: { @@ -91,7 +87,6 @@ const get: NextApiHandler = async (req, res) => { dossier?.userId === session?.dbUser.id || dossier?.collaboratorIds.includes(session?.dbUser.id) ) { - await prisma?.$disconnect() res.status(200).json({ dossier: dossier, docs: { @@ -124,11 +119,9 @@ const get: NextApiHandler = async (req, res) => { }, }); } else { - await prisma?.$disconnect() res.status(401).json({ message: "Unauthorized" }); } } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Dossier non trouvé" }); } diff --git a/formulaire/pages/api/dossier/duplicate/[id].tsx b/formulaire/pages/api/dossier/duplicate/[id].tsx index b7096a185..0cf637cfb 100644 --- a/formulaire/pages/api/dossier/duplicate/[id].tsx +++ b/formulaire/pages/api/dossier/duplicate/[id].tsx @@ -1,7 +1,8 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { Dossier, PrismaClient, StatutDossier } from "@prisma/client"; +import { Dossier, StatutDossier } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../../auth/[...nextauth]' +import { authOptions } from "../../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -23,7 +24,6 @@ function getId(req: NextApiRequest): number { } const create: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const dossier: Dossier = JSON.parse(req.body); const session = await getServerSession(req, res, authOptions); @@ -48,7 +48,7 @@ const create: NextApiHandler = async (req, res) => { }; try { - const dossier = await prisma.dossier.create({ data }); + const dossier = await client.dossier.create({ data }); res.status(200).json(dossier); } catch (e: unknown) { console.log(e); diff --git a/formulaire/pages/api/enfants/[id].tsx b/formulaire/pages/api/enfants/[id].tsx index 2b64c2f79..0ed40eb17 100644 --- a/formulaire/pages/api/enfants/[id].tsx +++ b/formulaire/pages/api/enfants/[id].tsx @@ -1,7 +1,7 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { Enfant, PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -23,10 +23,9 @@ function getId(req: NextApiRequest): number { } const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); try { const enfantId = getId(req); - const enfantDeleted = await prisma.enfant.delete({ + const enfantDeleted = await client.enfant.delete({ where: { id: enfantId }, }); @@ -41,11 +40,9 @@ const remove: NextApiHandler = async (req, res) => { } return r.json(); }); - await prisma?.$disconnect() res.status(200).json({ message: fetching }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Enfant non trouvé" }); } diff --git a/formulaire/pages/api/enfants/dossier/[id].tsx b/formulaire/pages/api/enfants/dossier/[id].tsx index f82ae78b6..86364918b 100644 --- a/formulaire/pages/api/enfants/dossier/[id].tsx +++ b/formulaire/pages/api/enfants/dossier/[id].tsx @@ -1,7 +1,7 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { Enfant, PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../../auth/[...nextauth]' +import { authOptions } from "../../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -24,46 +24,44 @@ function getId(req: NextApiRequest): number { const get: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); - const prisma = new PrismaClient(); - console.log('order : ', req.query.order) + console.log("order : ", req.query.order); try { const dossierId = getId(req); - const dossierConcerned = await prisma.dossier.findUnique({ - where: { - id: dossierId - } - }) - + const dossierConcerned = await client.dossier.findUnique({ + where: { + id: dossierId, + }, + }); + if ( - dossierConcerned?.userId === session?.dbUser.id || - dossierConcerned?.collaboratorIds.includes(session?.dbUser.id) + dossierConcerned?.userId === session?.dbUser.id || + dossierConcerned?.collaboratorIds.includes(session?.dbUser.id) ) { - const enfantsByDossier = await prisma.enfant.findMany({ - take: req.query.numberByPage ? Number(req.query.numberByPage) : 25, - skip: 25 * Number(req.query.page), - include: { - piecesDossier: true, - remuneration: true, - Comments: true - }, - where: { - dossierId: dossierId - }, - orderBy: { - [req.query.termToOrder as string]: req.query.order as string - } - }) - const countEnfants = await prisma.enfant.count({ - where: { - dossierId: dossierId - } - } - ) - res.status(200).json({enfants: enfantsByDossier, count: countEnfants}); + const enfantsByDossier = await client.enfant.findMany({ + take: req.query.numberByPage ? Number(req.query.numberByPage) : 25, + skip: 25 * Number(req.query.page), + include: { + piecesDossier: true, + remuneration: true, + Comments: true, + }, + where: { + dossierId: dossierId, + }, + orderBy: { + [req.query.termToOrder as string]: req.query.order as string, + }, + }); + const countEnfants = await client.enfant.count({ + where: { + dossierId: dossierId, + }, + }); + res.status(200).json({ enfants: enfantsByDossier, count: countEnfants }); } else { res.status(401).json({ message: "Unauthorized" }); } - } catch(e) { + } catch (e) { console.log(e); } }; diff --git a/formulaire/pages/api/piece/dossier/[id].tsx b/formulaire/pages/api/piece/dossier/[id].tsx index d1f756dd3..d450f0ea7 100644 --- a/formulaire/pages/api/piece/dossier/[id].tsx +++ b/formulaire/pages/api/piece/dossier/[id].tsx @@ -1,7 +1,7 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../../auth/[...nextauth]' +import { authOptions } from "../../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -32,34 +32,28 @@ function getId(req: NextApiRequest): number { } const get: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const dossierId = getId(req); try { - const pieces = await prisma.pieceDossier.findMany({ + const pieces = await client.pieceDossier.findMany({ where: { dossierId: dossierId, }, }); - await prisma?.$disconnect() res.status(200).json(pieces); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); } }; const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); try { const pieceId = getId(req); - const dossierDeleted = await prisma.pieceDossier.delete({ + const dossierDeleted = await client.pieceDossier.delete({ where: { id: pieceId }, }); - await prisma?.$disconnect() res.status(200).json({ dossierDeleted }); } catch (e: unknown) { console.log(e); - await prisma?.$disconnect() res.status(200).json({ message: "Piece non trouvée" }); } }; diff --git a/formulaire/pages/api/piece/enfant/[id].tsx b/formulaire/pages/api/piece/enfant/[id].tsx index 3b6e2cd84..eb955f664 100644 --- a/formulaire/pages/api/piece/enfant/[id].tsx +++ b/formulaire/pages/api/piece/enfant/[id].tsx @@ -1,48 +1,43 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient } from '@prisma/client' import { getServerSession } from "next-auth"; -import { authOptions } from '../../auth/[...nextauth]' - - +import { authOptions } from "../../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); - if (!session) { - res.status(401).end(); - return; - } - const { id: articleIdStr } = req.query; - if (typeof articleIdStr !== "string") { - res.status(404).send(`not a valid piece id`); - return; - } - - if (req.method == "DELETE") { - await remove(req, res); - } else { - res.status(405).end(); - return; - } + if (!session) { + res.status(401).end(); + return; + } + const { id: articleIdStr } = req.query; + if (typeof articleIdStr !== "string") { + res.status(404).send(`not a valid piece id`); + return; + } + + if (req.method == "DELETE") { + await remove(req, res); + } else { + res.status(405).end(); + return; + } }; function getId(req: NextApiRequest): number { - return Number(req.query.id as string); - } - + return Number(req.query.id as string); +} + const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient() try { const pieceId = getId(req); - const pieceDeleted = await prisma.pieceDossierEnfant.delete({ - where: { id: pieceId } - }) - await prisma?.$disconnect() + const pieceDeleted = await client.pieceDossierEnfant.delete({ + where: { id: pieceId }, + }); res.status(200).json({ pieceDeleted }); } catch (e: unknown) { - await prisma?.$disconnect() console.log(e); res.status(200).json({ message: "Piece non trouvée" }); } }; - export default handler; \ No newline at end of file +export default handler; diff --git a/formulaire/pages/api/remunerations/[id].tsx b/formulaire/pages/api/remunerations/[id].tsx index 0c87bc5c7..bc572b630 100644 --- a/formulaire/pages/api/remunerations/[id].tsx +++ b/formulaire/pages/api/remunerations/[id].tsx @@ -1,7 +1,7 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -19,11 +19,10 @@ const handler: NextApiHandler = async (req, res) => { }; const remove: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const remunerationId = Number(req.body as string); try { console.log("remunerationId: ", remunerationId); - await prisma.remuneration.delete({ + await client.remuneration.delete({ where: { id: remunerationId }, }); diff --git a/formulaire/pages/api/users/[email].tsx b/formulaire/pages/api/users/[email].tsx index a77661bec..b8f695449 100644 --- a/formulaire/pages/api/users/[email].tsx +++ b/formulaire/pages/api/users/[email].tsx @@ -1,5 +1,5 @@ import type { NextApiHandler } from "next"; -import { PrismaClient, User } from "@prisma/client"; +import { User } from "@prisma/client"; import { WORDING_MAILING } from "src/lib/helpers"; import type SMTPTransport from "nodemailer/lib/smtp-transport"; import type { Transporter } from "nodemailer"; @@ -7,7 +7,8 @@ import nodemailer from "nodemailer"; import fs from "fs"; import _ from "lodash"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -24,11 +25,10 @@ const handler: NextApiHandler = async (req, res) => { }; const getUserByEmail: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); const userEmail = req.query.email as string; try { let user: User | null = null; - user = await prisma.user.findFirst({ + user = await client.user.findFirst({ where: { email: userEmail, }, @@ -38,9 +38,8 @@ const getUserByEmail: NextApiHandler = async (req, res) => { const data = { email: userEmail as string, }; - user = await prisma.user.create({ data }); + user = await client.user.create({ data }); } - await prisma?.$disconnect() //SEND EMAIL if (typeof req.body !== "string") { diff --git a/formulaire/pages/api/users/index.tsx b/formulaire/pages/api/users/index.tsx index b04894488..5f9e62cd5 100644 --- a/formulaire/pages/api/users/index.tsx +++ b/formulaire/pages/api/users/index.tsx @@ -1,8 +1,8 @@ import type { NextApiHandler, NextApiRequest } from "next"; -import { PrismaClient, User } from "@prisma/client"; import { number } from "zod"; import { getServerSession } from "next-auth"; -import { authOptions } from '../auth/[...nextauth]' +import { authOptions } from "../auth/[...nextauth]"; +import client from "src/lib/prismaClient"; const handler: NextApiHandler = async (req, res) => { const session = await getServerSession(req, res, authOptions); @@ -19,7 +19,6 @@ const handler: NextApiHandler = async (req, res) => { }; const getUserById: NextApiHandler = async (req, res) => { - const prisma = new PrismaClient(); let collaboratorIds: number[] | number = []; if (req.query.id) { @@ -32,17 +31,17 @@ const getUserById: NextApiHandler = async (req, res) => { } } try { - const users = await prisma.user.findMany({ + const users = await client.user.findMany({ where: { id: { in: collaboratorIds, }, }, }); - await prisma?.$disconnect() + await prisma?.$disconnect(); res.status(200).json(users); } catch (e: unknown) { - await prisma?.$disconnect() + await prisma?.$disconnect(); console.log(e); } };