Skip to content

Commit

Permalink
fix: use global prisma client in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
achauve committed Dec 12, 2023
1 parent 1de2199 commit 7cbdf4a
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 127 deletions.
9 changes: 3 additions & 6 deletions formulaire/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -18,7 +15,7 @@ export const authOptions: NextAuthOptions = {
},
async signIn({ user }) {
if (typeof user.email !== "string") return false;
return true
return true;
},
},
logger: {
Expand Down Expand Up @@ -48,4 +45,4 @@ export const authOptions: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
};

export default NextAuth(authOptions)
export default NextAuth(authOptions);
12 changes: 4 additions & 8 deletions formulaire/pages/api/demandeur/[id].tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
},
Expand All @@ -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 },
});

Expand All @@ -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é" });
}
Expand Down
17 changes: 5 additions & 12 deletions formulaire/pages/api/dossier/[id].tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 },
});

Expand All @@ -51,25 +50,22 @@ 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é" });
}
};

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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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é" });
}
Expand Down
8 changes: 4 additions & 4 deletions formulaire/pages/api/dossier/duplicate/[id].tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions formulaire/pages/api/enfants/[id].tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 },
});

Expand All @@ -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é" });
}
Expand Down
68 changes: 33 additions & 35 deletions formulaire/pages/api/enfants/dossier/[id].tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
};
Expand Down
14 changes: 4 additions & 10 deletions formulaire/pages/api/piece/dossier/[id].tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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" });
}
};
Expand Down
Loading

0 comments on commit 7cbdf4a

Please sign in to comment.