Skip to content

Commit

Permalink
fix: instantiate once for each handler (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannNumericite authored May 31, 2023
1 parent e7c87e7 commit 42f2f80
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 139 deletions.
2 changes: 0 additions & 2 deletions .kube-workflow/env/prod/values.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
70 changes: 35 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions src/pages/api/commentaires/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -31,21 +32,19 @@ 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" });
}
};

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,
},
Expand All @@ -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);
}
Expand Down
16 changes: 5 additions & 11 deletions src/pages/api/commissions/[id].ts
Original file line number Diff line number Diff line change
@@ -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`);
Expand All @@ -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: {
Expand All @@ -47,7 +43,6 @@ const get: NextApiHandler = async (req, res) => {
},
where: { id },
});
await prisma?.$disconnect()

res.status(200).json(superjson.stringify(commission));
};
Expand Down Expand Up @@ -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));
};
Expand Down
7 changes: 4 additions & 3 deletions src/pages/api/commissions/date/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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() },
Expand Down
18 changes: 10 additions & 8 deletions src/pages/api/commissions/index.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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);
}
Expand All @@ -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" });
Expand All @@ -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: {
Expand All @@ -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" ?
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
Loading

0 comments on commit 42f2f80

Please sign in to comment.