Skip to content

Commit

Permalink
fix db refactor duplicated bookings
Browse files Browse the repository at this point in the history
  • Loading branch information
lucia-gomez committed Oct 16, 2024
1 parent cacd5f5 commit 010bb44
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions booking-app/app/api/db/merge/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import {
serverFetchAllDataFromCollection,
serverSaveDataToFirestore,
serverUpdateInFirestore,
} from "@/lib/firebase/server/adminDb";

import { TableNames } from "@/components/src/policy";
Expand All @@ -21,29 +21,25 @@ export async function POST(request: NextRequest) {
const { id, email, ...other } = row;
return other;
});
const destinationRowsFiltered = destinationRows.map(row => {
const { id, ...other } = row;
return other;
});

const calIdToBookingStatus = {};
for (let row of sourceRowsFiltered) {
calIdToBookingStatus[row.calendarEventId] = row;
}

const merged = destinationRowsFiltered.map(booking => {
const matchingStatus = calIdToBookingStatus[booking.calendarEventId];
return { ...booking, ...matchingStatus };
});
const docIdToCalId: { [key: string]: string } = {};
for (let row of destinationRows) {
docIdToCalId[row.id] = row.calendarEventId;
}

await Promise.all(
merged.map(row => serverSaveDataToFirestore(destinationTable, row)),
Object.entries(docIdToCalId).map(([docId, calId]) => {
const bookingStatus = calIdToBookingStatus[calId];
return serverUpdateInFirestore(destinationTable, docId, bookingStatus);
}),
);

return NextResponse.json(
{ duplicatedRows: merged.length },
{ status: 200 },
);
return NextResponse.json({ status: 200 });
} catch (err) {
console.error(err);
return NextResponse.json(
Expand Down

0 comments on commit 010bb44

Please sign in to comment.