Skip to content

Commit

Permalink
Merge pull request #456 from ITPNYU/revert-455-revert-454-riho/declin…
Browse files Browse the repository at this point in the history
…e_reason

Revert "Revert "Record who declined a request and why""
  • Loading branch information
rlho authored Oct 8, 2024
2 parents 7f3fb11 + 4d72059 commit 10c72dc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
5 changes: 2 additions & 3 deletions booking-app/app/api/approve/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { NextRequest, NextResponse } from "next/server";
import { serverApproveBooking } from "@/components/src/server/admin";

export async function POST(req: NextRequest) {
const { id } = await req.json();
const { id, email } = await req.json();

try {
console.log("id", id);
await serverApproveBooking(id);
await serverApproveBooking(id, email);
return NextResponse.json(
{ message: "Approved successfully" },
{ status: 200 },
Expand Down
42 changes: 31 additions & 11 deletions booking-app/app/decline/page.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
"use client";

import React, { Suspense, useState } from "react";

import { Button } from "@mui/material";
import { useSearchParams } from "next/navigation";
import {
DatabaseContext,
DatabaseProvider,
} from "@/components/src/client/routes/components/Provider";
import { decline } from "@/components/src/server/db";
import { Button, TextField } from "@mui/material";
import { useSearchParams } from "next/navigation";
import React, { Suspense, useContext, useState } from "react";

const DeclinePageContent: React.FC = () => {
const searchParams = useSearchParams();
const paramCalendarEventId = searchParams.get("calendarEventId");
const [loading, setLoading] = useState(false);
const [declined, setDeclined] = useState(false);
const [error, setError] = useState<string | null>(null);
const [reason, setReason] = useState("");
const { userEmail } = useContext(DatabaseContext);

const handleDecline = async () => {
if (paramCalendarEventId) {
if (paramCalendarEventId && reason.trim()) {
setLoading(true);
setError(null);
try {
await decline(paramCalendarEventId);
await decline(paramCalendarEventId, userEmail, reason);
setDeclined(true);
} catch (err) {
setError("Failed to decline booking.");
console.log(err);
} finally {
setLoading(false);
}
} else {
setError("Please provide a reason for declining.");
}
};

Expand All @@ -34,9 +41,20 @@ const DeclinePageContent: React.FC = () => {
{paramCalendarEventId ? (
<div>
<p>Event ID: {paramCalendarEventId}</p>
<TextField
label="Reason for Declining"
variant="outlined"
fullWidth
multiline
rows={4}
value={reason}
onChange={e => setReason(e.target.value)}
style={{ marginBottom: 16 }}
required
/>
<Button
onClick={() => handleDecline()}
disabled={loading || declined}
disabled={loading || declined || !reason.trim()}
variant="contained"
>
{loading
Expand All @@ -55,9 +73,11 @@ const DeclinePageContent: React.FC = () => {
};

const DeclinePage: React.FC = () => (
<Suspense fallback={<div>Loading...</div>}>
<DeclinePageContent />
</Suspense>
<DatabaseProvider>
<Suspense fallback={<div>Loading...</div>}>
<DeclinePageContent />
</Suspense>
</DatabaseProvider>
);

export default DeclinePage;
50 changes: 31 additions & 19 deletions booking-app/components/src/server/db.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
BookingFormDetails,
BookingStatusLabel,
PolicySettings,
} from "../types";
clientFetchAllDataFromCollection,
clientGetDataByCalendarEventId,
clientUpdateDataInFirestore,
} from "@/lib/firebase/firebase";
import { Timestamp, where } from "@firebase/firestore";
import {
TableNames,
clientGetFinalApproverEmail,
getCancelCcEmail,
} from "../policy";
import { Timestamp, where } from "@firebase/firestore";
import { approvalUrl, declineUrl, getBookingToolDeployUrl } from "./ui";
import {
clientFetchAllDataFromCollection,
clientGetDataByCalendarEventId,
clientUpdateDataInFirestore,
} from "@/lib/firebase/firebase";
BookingFormDetails,
BookingStatusLabel,
PolicySettings,
} from "../types";
import { approvalUrl, declineUrl, getBookingToolDeployUrl } from "./ui";

import { clientUpdateDataByCalendarEventId } from "@/lib/firebase/client/clientDb";
import { roundTimeUp } from "../client/utils/date";
Expand Down Expand Up @@ -69,9 +69,10 @@ export const getOldSafetyTrainingEmails = () => {
//return combinedValues;
};

export const decline = async (id: string) => {
export const decline = async (id: string, email: string, reason?: string) => {
clientUpdateDataByCalendarEventId(TableNames.BOOKING_STATUS, id, {
declinedAt: Timestamp.now(),
declinedBy: email,
});

const doc = await clientGetDataByCalendarEventId(
Expand All @@ -80,8 +81,15 @@ export const decline = async (id: string) => {
);
//@ts-ignore
const guestEmail = doc ? doc.email : null;
const headerMessage =
"Your reservation request for Media Commons has been declined. For detailed reasons regarding this decision, please contact us at [email protected].";
let headerMessage =
"Your reservation request for Media Commons has been declined.";

if (reason) {
headerMessage += ` Reason: ${reason}`;
} else {
headerMessage +=
" For detailed reasons regarding this decision, please contact us at [email protected].";
}
clientSendBookingDetailEmail(
id,
guestEmail,
Expand All @@ -102,9 +110,10 @@ export const decline = async (id: string) => {
}
);
};
export const cancel = async (id: string) => {
export const cancel = async (id: string, email: string) => {
clientUpdateDataByCalendarEventId(TableNames.BOOKING_STATUS, id, {
canceledAt: Timestamp.now(),
canceledBy: email,
});
const doc = await clientGetDataByCalendarEventId(
TableNames.BOOKING_STATUS,
Expand Down Expand Up @@ -156,9 +165,10 @@ export const updatePolicySettingData = async (updatedData: object) => {
console.log("No policy settings docs found");
}
};
export const checkin = async (id: string) => {
export const checkin = async (id: string, email: string) => {
clientUpdateDataByCalendarEventId(TableNames.BOOKING_STATUS, id, {
checkedInAt: Timestamp.now(),
checkedInBy: email,
});
const doc = await clientGetDataByCalendarEventId(
TableNames.BOOKING_STATUS,
Expand Down Expand Up @@ -190,10 +200,11 @@ export const checkin = async (id: string) => {
);
};

export const checkOut = async (id: string) => {
export const checkOut = async (id: string, email: string) => {
const checkoutDate = roundTimeUp();
clientUpdateDataByCalendarEventId(TableNames.BOOKING_STATUS, id, {
checkedOutAt: Timestamp.now(),
checkedOutBy: email,
});
clientUpdateDataByCalendarEventId(TableNames.BOOKING, id, {
endDate: Timestamp.fromDate(checkoutDate),
Expand Down Expand Up @@ -234,9 +245,10 @@ export const checkOut = async (id: string) => {
);
};

export const noShow = async (id: string) => {
export const noShow = async (id: string, email: string) => {
clientUpdateDataByCalendarEventId(TableNames.BOOKING_STATUS, id, {
noShowedAt: Timestamp.now(),
noShowedBy: email,
});
const doc = await clientGetDataByCalendarEventId(
TableNames.BOOKING_STATUS,
Expand Down Expand Up @@ -324,12 +336,12 @@ export const clientSendConfirmationEmail = async (
clientSendBookingDetailEmail(calendarEventId, email, headerMessage, status);
};

export const clientApproveBooking = async (id: string) => {
export const clientApproveBooking = async (id: string, email: string) => {
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/approve`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: id }),
body: JSON.stringify({ id: id, email: email }),
});
};

0 comments on commit 10c72dc

Please sign in to comment.