Skip to content

Commit

Permalink
fix: prisma formulaire
Browse files Browse the repository at this point in the history
  • Loading branch information
achauve committed Dec 12, 2023
1 parent 114b5f0 commit b65f169
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 147 deletions.
5 changes: 0 additions & 5 deletions formulaire/pages/api/comments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ const get: NextApiHandler = async (req, res) => {
dossierId: parseInt(dossierId),
},
});
await prisma?.$disconnect();
res.status(200).json(comments);
} catch (e: unknown) {
await prisma?.$disconnect();
console.log(e);
}
};
Expand Down Expand Up @@ -69,7 +67,6 @@ const updateCommentairesNotifications: NextApiHandler = async (req, res) => {
},
},
});
await prisma?.$disconnect();
res.status(200).json(updateComments);
};

Expand All @@ -81,10 +78,8 @@ const post: NextApiHandler = async (req, res) => {
console.log("comment to create : ", data);
try {
const comment = await prisma.comments.create({ data });
await prisma?.$disconnect();
res.status(200).json(comment);
} catch (e: unknown) {
await prisma?.$disconnect();
console.log(e);
}
};
Expand Down
3 changes: 1 addition & 2 deletions formulaire/pages/api/demandeur/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { NextApiHandler } from "next";
import { DemandeurModel } from "prisma/zod";
import prisma from "../../../src/lib/prismaClient";
import { getServerSession } from "next-auth";
import { authOptions } from '../auth/[...nextauth]'
import { authOptions } from "../auth/[...nextauth]";

const handler: NextApiHandler = async (req, res) => {
const session = await getServerSession(req, res, authOptions);
Expand Down Expand Up @@ -49,7 +49,6 @@ const update: NextApiHandler = async (req, res) => {
data: { ...demandeurData.parse(parsed) },
where: { id: parsed.id },
});
await prisma?.$disconnect()

res.status(200).json(dempandeurUpdated);
};
Expand Down
5 changes: 1 addition & 4 deletions formulaire/pages/api/dossier/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { STATUS_EN_COURS, STATUS_TERMINES, CATEGORIES } from "src/lib/helpers";
import { statusGroup } from "src/lib/types";
import prisma from "../../../src/lib/prismaClient";
import { getServerSession } from "next-auth";
import { authOptions } from '../auth/[...nextauth]'
import { authOptions } from "../auth/[...nextauth]";

const handler: NextApiHandler = async (req, res) => {
const session = await getServerSession(req, res, authOptions);
Expand Down Expand Up @@ -167,15 +167,13 @@ const get: NextApiHandler = async (req, res) => {
],
},
});
await prisma?.$disconnect()
res.status(200).json({
dossiers: dossiers,
countCurrent: countCurrent,
countEnCours: countEnCours,
countTermines: countTermines,
});
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
};
Expand Down Expand Up @@ -220,7 +218,6 @@ const update: NextApiHandler = async (req, res) => {
data: parsed,
where: { id: parsed.id },
});
await prisma?.$disconnect()
res.status(200).json(produitupdated);
};

Expand Down
5 changes: 0 additions & 5 deletions formulaire/pages/api/enfants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ const get: NextApiHandler = async (req, res) => {
],
},
});
await prisma?.$disconnect();
res.status(200).json(enfants);
} catch (e: unknown) {
await prisma?.$disconnect();
console.log(e);
}
};
Expand All @@ -68,10 +66,8 @@ const post: NextApiHandler = async (req, res) => {
data.userId = session?.dbUser.id;
try {
const enfant = await prisma.enfant.create({ data });
await prisma?.$disconnect();
res.status(200).json(enfant);
} catch (e: unknown) {
await prisma?.$disconnect();
console.log(e);
}
};
Expand Down Expand Up @@ -121,7 +117,6 @@ const update: NextApiHandler = async (req, res) => {
data: parsed as Enfant,
where: { id: parsed.id },
});
await prisma?.$disconnect();

res.status(200).json(enfantUpdated);
};
Expand Down
4 changes: 1 addition & 3 deletions formulaire/pages/api/piece/dossier/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import prisma from "../../../../src/lib/prismaClient";
import fs from "fs";
import { generateToken } from "src/lib/utils";
import { getServerSession } from "next-auth";
import { authOptions } from '../../auth/[...nextauth]'
import { authOptions } from "../../auth/[...nextauth]";

const handler: NextApiHandler = async (req, res) => {
const session = await getServerSession(req, res, authOptions);
Expand All @@ -32,10 +32,8 @@ const post: NextApiHandler = async (req, res) => {
piece.link,
piece.statut
);
await prisma?.$disconnect()
res.status(200).json({ pieceDossier: piece, tokenizedLink: tokenizedLink });
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
};
Expand Down
54 changes: 30 additions & 24 deletions formulaire/pages/api/piece/enfant/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,41 @@ import type { NextApiHandler } from "next";
import { generateToken } from "src/lib/utils";
import prisma from "../../../../src/lib/prismaClient";
import { getServerSession } from "next-auth";
import { authOptions } from '../../auth/[...nextauth]'
import { authOptions } from "../../auth/[...nextauth]";

const handler: NextApiHandler = async (req, res) => {
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end();
return;
}
if (req.method == "POST") {
await post(req, res);
} else {
res.status(405).end();
return;
}
if (!session) {
res.status(401).end();
return;
}
if (req.method == "POST") {
await post(req, res);
} else {
res.status(405).end();
return;
}
};

const post: NextApiHandler = async (req, res) => {
const data = JSON.parse(req.body) as PieceDossierEnfant & {dossierId: number}
const dossierId = data.dossierId
delete data.dossierId
try {
const piece = await prisma.pieceDossierEnfant.create({ data });
const tokenizedLink = generateToken(piece.id, dossierId, piece.type, piece.link, piece.statut)
await prisma?.$disconnect()
res.status(200).json({pieceDossier: piece, tokenizedLink: tokenizedLink});
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
const data = JSON.parse(req.body) as PieceDossierEnfant & {
dossierId: number;
};
const dossierId = data.dossierId;
delete data.dossierId;
try {
const piece = await prisma.pieceDossierEnfant.create({ data });
const tokenizedLink = generateToken(
piece.id,
dossierId,
piece.type,
piece.link,
piece.statut
);
res.status(200).json({ pieceDossier: piece, tokenizedLink: tokenizedLink });
} catch (e: unknown) {
console.log(e);
}
};

export default withSentry(handler);
export default withSentry(handler);
110 changes: 53 additions & 57 deletions formulaire/pages/api/societe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,70 @@ import type { NextApiHandler } from "next";
import { SocieteProductionModel } from "prisma/zod";
import prisma from "../../../src/lib/prismaClient";
import { getServerSession } from "next-auth";
import { authOptions } from '../auth/[...nextauth]'
import { authOptions } from "../auth/[...nextauth]";

const handler: NextApiHandler = async (req, res) => {
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end();
return;
}
if (req.method == "POST") {
await post(req, res);
} else if (req.method == "GET") {
await get(req, res);
} else if (req.method == "PUT") {
await update(req, res);
} else {
res.status(405).end();
return;
}
if (!session) {
res.status(401).end();
return;
}
if (req.method == "POST") {
await post(req, res);
} else if (req.method == "GET") {
await get(req, res);
} else if (req.method == "PUT") {
await update(req, res);
} else {
res.status(405).end();
return;
}
};

const get: NextApiHandler = async (req, res) => {
const siret = req.query.siret as string;
try {
const societe = await prisma.societeProduction.findFirst({
where: {
siret: siret
}
})
await prisma?.$disconnect()
res.status(200).json(societe)
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
const siret = req.query.siret as string;
try {
const societe = await prisma.societeProduction.findFirst({
where: {
siret: siret,
},
});
res.status(200).json(societe);
} catch (e: unknown) {
console.log(e);
}
};

const update: NextApiHandler = async (req, res) => {
console.log('in societe update')
const data = JSON.parse(req.body)
const societeData = SocieteProductionModel.omit({id: true})
try {
const societe = await prisma.societeProduction.update({
data: {...societeData.parse(data)},
where: {
id: data.id
}
});
await prisma?.$disconnect()
res.status(200).json(societe);
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
console.log("in societe update");
const data = JSON.parse(req.body);
const societeData = SocieteProductionModel.omit({ id: true });
try {
const societe = await prisma.societeProduction.update({
data: { ...societeData.parse(data) },
where: {
id: data.id,
},
});
res.status(200).json(societe);
} catch (e: unknown) {
console.log(e);
}
};

const post: NextApiHandler = async (req, res) => {
console.log('in societe create')
const data = JSON.parse(req.body)
const societeData = SocieteProductionModel.omit({id: true})
try {
const societe = await prisma.societeProduction.create({ data: {...societeData.parse(data)} });
console.log('societe created : ', societe)
await prisma?.$disconnect()
res.status(200).json(societe);
} catch (e: unknown) {
await prisma?.$disconnect()
console.log(e);
}
console.log("in societe create");
const data = JSON.parse(req.body);
const societeData = SocieteProductionModel.omit({ id: true });
try {
const societe = await prisma.societeProduction.create({
data: { ...societeData.parse(data) },
});
console.log("societe created : ", societe);
res.status(200).json(societe);
} catch (e: unknown) {
console.log(e);
}
};

export default withSentry(handler);
export default withSentry(handler);
2 changes: 0 additions & 2 deletions formulaire/pages/api/sync/inc/commentaires/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const post: NextApiHandler = async (req, res) => {
const comment = await prisma.comments.create({
data: data.comment,
});
await prisma?.$disconnect();
//console.log("comments created : ", comment);
res.status(200).json(comment);
}
Expand Down Expand Up @@ -137,7 +136,6 @@ const get: NextApiHandler = async (req, res) => {
};
}
);
await prisma?.$disconnect();

res.status(200).json(notificationsByDossier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Comments } from "@prisma/client";
import { withSentry } from "@sentry/nextjs";
import type { NextApiHandler } from "next";
import { CommentaireNotifications } from "src/lib/types";
// import prisma from "../../../../../src/lib/prismaClient";
import prisma from "../../../../../../src/lib/prismaClient";

const handler: NextApiHandler = async (req, res) => {
if (req.method == "PUT") {
Expand Down Expand Up @@ -48,7 +48,6 @@ const update: NextApiHandler = async (req, res) => {
},
},
});
await prisma?.$disconnect();
res.status(200).json(updateComments);
}
};
Expand Down
1 change: 0 additions & 1 deletion formulaire/pages/api/sync/inc/docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const get: NextApiHandler = async (req, res) => {
},
},
});
await prisma?.$disconnect();
res.status(200).json({
dossier: {
id: dossier?.id,
Expand Down
2 changes: 0 additions & 2 deletions formulaire/pages/api/sync/inc/dossiers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ const update: NextApiHandler = async (req, res) => {
id: parseInt(data.dossier.externalId),
},
});
await prisma?.$disconnect()
res.status(200).json({ message: "Dossier created successfully" });
} catch (e) {
console.log("error : ", e);
await prisma?.$disconnect()
res.status(500).json({ error: e });
}
};
Expand Down
2 changes: 0 additions & 2 deletions formulaire/pages/api/sync/inc/enfants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ const update: NextApiHandler = async (req, res) => {
},
});
// console.log('update : ', update)
await prisma?.$disconnect();
res.status(200).json(update);
} catch (e) {
await prisma?.$disconnect();
res.status(500).json({ message: "Something went wrong" });
console.log(e);
}
Expand Down
1 change: 0 additions & 1 deletion formulaire/pages/api/sync/inc/pieces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const update: NextApiHandler = async (req, res) => {
});
// console.log("piece trouvee : ", piece);
}
await prisma?.$disconnect();

res.status(200).json({ message: "OK" });
};
Expand Down
Loading

0 comments on commit b65f169

Please sign in to comment.