Skip to content

Commit

Permalink
added delete and patch routes for individual booknowclient and inquir…
Browse files Browse the repository at this point in the history
…ingclient on the admin side
  • Loading branch information
CodeOnTheWall committed Aug 29, 2023
1 parent 50ed2a4 commit 2e2bf1a
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 297 deletions.
7 changes: 3 additions & 4 deletions app/admin/(components)/BookNowClient/CellAction.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useState } from "react";
import { useParams, useRouter } from "next/navigation";
import { useRouter } from "next/navigation";
import { toast } from "react-hot-toast";

import {
Expand All @@ -25,16 +25,15 @@ export default function CellAction({ data }: CellActionProps) {
const [isOpen, setIsOpen] = useState(false);

const router = useRouter();
const params = useParams();

const onDelete = async () => {
try {
setIsLoading(true);
await fetch(`/api/inquiringClient/${params.inquiringClientId}/`, {
await fetch(`/api/booknowclient/${data.id}/`, {
method: "DELETE",
});
router.refresh();
toast.success("Inquring Client Deleted");
toast.success("Book Now Client Deleted");
} catch (error) {
toast.error(`${error}`);
} finally {
Expand Down
135 changes: 135 additions & 0 deletions app/api/booknowclient/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import connectToDB from "@/utils/database";
import BookNowClient from "@/models/booknowclient";

import { NextResponse } from "next/server";

export async function DELETE(
req: Request,
{ params }: { params: { id: string } }
) {
try {
connectToDB();

const deletedBookNowClient = await BookNowClient.findByIdAndDelete(
params.id
);

return NextResponse.json(deletedBookNowClient);
} catch (error) {
console.log("[BOOKNOWCLIENT_DELETE]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function PATCH(
req: Request,
{ params }: { params: { id: string } }
) {
const {
firstName,
lastName,
email,
phone,
address,
sex,
gender,
dateOfBirth,
personalHealthNumber,
guardian,
emergencyContact,
emergencyContactPhone,
occupation,
employer,
familyDoctor,
referringProfessional,
howDidYouHearAboutUs,
referredTo,
healthConcerns,
whatIsMakingItBetter,
whatIsMakingItWorse,
currentMedications,
knownAllergiesOrHypersensitivities,
majorAccidentsOrSurgeries,
familyHistory,
activitiesSportsHobbies,
treatmentExpectation,
otherTherapyTreatment,
cardiovascular,
respiratory,
neurological,
headNeck,
digestive,
otherConditions,
accuracyOfInformation,
privacyAndSharingOfInformation,
cancellationPolicy,
lateArrivalPolicy,
inappropriateBehaviourPolicy,
treatmentConsentStatement,
minorConsent,
paymentPolicy,
communicationConsent,
signature,
} = await req.json();

const updatedFields = {
firstName,
lastName,
email,
phone,
address,
sex,
gender,
dateOfBirth,
personalHealthNumber,
guardian,
emergencyContact,
emergencyContactPhone,
occupation,
employer,
familyDoctor,
referringProfessional,
howDidYouHearAboutUs,
referredTo,
healthConcerns,
whatIsMakingItBetter,
whatIsMakingItWorse,
currentMedications,
knownAllergiesOrHypersensitivities,
majorAccidentsOrSurgeries,
familyHistory,
activitiesSportsHobbies,
treatmentExpectation,
otherTherapyTreatment,
cardiovascular,
respiratory,
neurological,
headNeck,
digestive,
otherConditions,
accuracyOfInformation,
privacyAndSharingOfInformation,
cancellationPolicy,
lateArrivalPolicy,
inappropriateBehaviourPolicy,
treatmentConsentStatement,
minorConsent,
paymentPolicy,
communicationConsent,
signature,
};

try {
connectToDB();

const updatedBookNowClient = await BookNowClient.findByIdAndUpdate(
params.id,
{ $set: updatedFields }
);
console.log(updatedBookNowClient);

return NextResponse.json(updatedBookNowClient);
} catch (error) {
console.log("[BOOKNOWCLIENT_PATCH]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
35 changes: 18 additions & 17 deletions app/api/booknowclient/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import BookNowClient from "@/models/booknowclient";

import { NextResponse } from "next/server";

export async function GET(req: Request) {
try {
await connectToDB();

const BookNowClients = await BookNowClient.find({});

// returning new NextResponse is only for errors
if (!BookNowClients) {
return new NextResponse("No Contact Clients yet", { status: 404 });
}

return NextResponse.json(BookNowClients);
} catch (error) {
return new NextResponse("Internal Error", { status: 500 });
}
}

export async function POST(req: Request) {
try {
// req.json parses the JSON to JS
Expand Down Expand Up @@ -127,6 +144,7 @@ export async function POST(req: Request) {
communicationConsent,
signature,
});
console.log(newBookNowClient);

await newBookNowClient.save();
// console.log(newBookNowClient);
Expand All @@ -144,20 +162,3 @@ export async function POST(req: Request) {
});
}
}

export async function GET(req: Request) {
try {
await connectToDB();

const BookNowClients = await BookNowClient.find({});

// returning new NextResponse is only for errors
if (!BookNowClients) {
return new NextResponse("No Contact Clients yet", { status: 404 });
}

return NextResponse.json(BookNowClients);
} catch (error) {
return new NextResponse("Internal Error", { status: 500 });
}
}
22 changes: 22 additions & 0 deletions app/api/inquiringclient/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import connectToDB from "@/utils/database";
import BookNowClient from "@/models/booknowclient";

import { NextResponse } from "next/server";

export async function DELETE(
req: Request,
{ params }: { params: { id: string } }
) {
try {
connectToDB();

const deletedBookNowClient = await BookNowClient.findByIdAndDelete(
params.id
);

return NextResponse.json(deletedBookNowClient);
} catch (error) {
console.log("[INQURINGCLIENT_DELETE]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
77 changes: 0 additions & 77 deletions app/api/inquiringclient/[inquiringClientId]/route.ts

This file was deleted.

Loading

0 comments on commit 2e2bf1a

Please sign in to comment.