Skip to content

Commit

Permalink
feat: update delete api method
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenfares committed Oct 9, 2023
1 parent a88239a commit 3c78f8b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
12 changes: 0 additions & 12 deletions server/src/http/routes/specific.routes/effectif.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,6 @@ export default () => {
return res.json(buildEffectifResult(effectifUpdated));
});

router.delete("/:id", async ({ params }, res) => {
let { id } = await Joi.object({
id: Joi.string().required(),
})
.unknown()
.validateAsync(params, { abortEarly: false });

await effectifsDb().deleteOne({ _id: new ObjectId(id) });

return res.json({ status: "OK" });
});

router.post("/recherche-siret", async ({ body }, res) => {
// TODO organismeFormation
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
21 changes: 20 additions & 1 deletion server/src/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import { generateSifa } from "@/common/actions/sifa.actions/sifa.actions";
import { changePassword, updateUserProfile } from "@/common/actions/users.actions";
import logger from "@/common/logger";
import { Organisme } from "@/common/model/@types";
import { jobEventsDb, organisationsDb } from "@/common/model/collections";
import { effectifsDb, jobEventsDb, organisationsDb } from "@/common/model/collections";
import { apiRoles } from "@/common/roles";
import { initSentryExpress } from "@/common/services/sentry/sentry";
import { __dirname } from "@/common/utils/esmUtils";
Expand Down Expand Up @@ -446,6 +446,25 @@ function setupRoutes(app: Application) {
return await getDuplicatesEffectifsForOrganismeId(res.locals.organismeId);
})
)
.delete(
"/duplicate/:effectifId",
requireOrganismePermission("manageEffectifs"),
async (req, res, next) => {
res.locals.duplicateId = new ObjectId((req.params as any).effectifId);
console.log("res.locals.duplicateId :>> ", res.locals.duplicateId);

const isEffectifLinkedToOrganisme = (
await effectifsDb().findOne({ _id: res.locals.duplicateId })
)?.organisme_id.equals(res.locals.organismeId);
console.log("isEffectifLinkedToOrganisme:>> ", isEffectifLinkedToOrganisme);

if (!isEffectifLinkedToOrganisme) return res.status(401).json({ error: "Accès non autorisé" });
next();
},
returnResult(async (req, res) => {
await effectifsDb().deleteOne({ _id: new ObjectId(res.locals.duplicateId) });
})
)
.get(
"/sifa-export",
requireOrganismePermission("manageEffectifs"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
} from "@chakra-ui/react";
import { useQueryClient } from "@tanstack/react-query";
import React from "react";
import { useRecoilValue } from "recoil";

import { _delete } from "@/common/httpClient";
import Ribbons from "@/components/Ribbons/Ribbons";
import { organismeAtom } from "@/hooks/organismeAtoms";

import { DuplicateEffectifDetail } from "./models/DuplicateEffectifDetail";

Expand All @@ -30,6 +32,7 @@ const EffectifDoublonDeleteAlertDialog = ({
apprenantNomPrenom: string;
}) => {
const queryClient = useQueryClient();
const organisme = useRecoilValue<any>(organismeAtom);

return (
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose} size={"4xl"}>
Expand Down Expand Up @@ -59,7 +62,7 @@ const EffectifDoublonDeleteAlertDialog = ({
<Button
colorScheme="red"
onClick={async () => {
await _delete(`/api/v1/effectif/${duplicateDetail?._id}`);
await _delete(`/api/v1/organismes/${organisme?._id}/duplicate/${duplicateDetail?.id}`);
queryClient.invalidateQueries(["duplicates-effectifs"]);
onClose();
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface DuplicateEffectifDetail {
_id: string;
id: string;
created_at: string;
updated_at: string;
source: string;
Expand Down

0 comments on commit 3c78f8b

Please sign in to comment.