-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4c5a52
commit a2b3081
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Booking, BookingStatus } from "@/components/src/types"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { TableNames } from "@/components/src/policy"; | ||
import { parse } from "json2csv"; | ||
import { serverFetchAllDataFromCollection } from "@/lib/firebase/server/adminDb"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const bookings = await serverFetchAllDataFromCollection<Booking>( | ||
TableNames.BOOKING, | ||
); | ||
const statuses = await serverFetchAllDataFromCollection<BookingStatus>( | ||
TableNames.BOOKING_STATUS, | ||
); | ||
|
||
// need to find corresponding status row for booking row | ||
const idsToData: { | ||
[key: string]: { | ||
booking: Booking; | ||
status: BookingStatus; | ||
}; | ||
} = {}; | ||
|
||
for (let booking of bookings) { | ||
const calendarEventId = booking.calendarEventId; | ||
const statusMatch = statuses.filter( | ||
row => row.calendarEventId === calendarEventId, | ||
)[0]; | ||
idsToData[calendarEventId] = { | ||
booking, | ||
status: statusMatch, | ||
}; | ||
} | ||
|
||
const rows = Object.entries(idsToData) | ||
.map(([_, { booking, status }]) => { | ||
const { requestNumber, ...otherBooking } = booking; | ||
return { requestNumber, ...otherBooking, ...status }; | ||
}) | ||
.sort((a, b) => a.requestNumber - b.requestNumber); | ||
|
||
try { | ||
const csv = parse(rows); | ||
return new NextResponse(csv, { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "text/csv", | ||
"Content-Disposition": 'attachment; filename="data.csv"', | ||
}, | ||
}); | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ error: "Failed to fetch CSV data" }, | ||
{ status: 400 }, | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
booking-app/components/src/client/routes/admin/components/ExportDatabase.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Box, Button, Typography } from "@mui/material"; | ||
import React, { useState } from "react"; | ||
|
||
import AlertToast from "../../components/AlertToast"; | ||
|
||
export default function ExportDatabase() { | ||
const [loading, setLoading] = useState(false); | ||
const [showError, setShowError] = useState(false); | ||
|
||
const onClick = async () => { | ||
setLoading(true); | ||
try { | ||
const response = await fetch("/api/bookings/export"); | ||
|
||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
throw new Error(errorData.error); | ||
} | ||
|
||
const blob = await response.blob(); | ||
const url = window.URL.createObjectURL(new Blob([blob])); | ||
|
||
// Automatically trigger the download | ||
const link = document.createElement("a"); | ||
link.href = url; | ||
link.download = "data.csv"; | ||
link.click(); | ||
|
||
// Clean up | ||
window.URL.revokeObjectURL(url); | ||
} catch (ex) { | ||
setShowError(true); | ||
console.error("error exporting database", ex); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box> | ||
<Typography variant="h6">Export Database</Typography> | ||
<p>Export database booking contents as a downloadable CSV file</p> | ||
<Box sx={{ marginTop: 2 }}> | ||
<Button onClick={onClick} variant="contained" disabled={loading}> | ||
Export | ||
</Button> | ||
</Box> | ||
<AlertToast | ||
message="Failed to download file" | ||
severity="error" | ||
open={showError} | ||
handleClose={() => setShowError(false)} | ||
/> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters