-
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
16e54f8
commit 33fdd7e
Showing
8 changed files
with
172 additions
and
45 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,28 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { | ||
serverFetchAllDataFromCollection, | ||
serverUpdateInFirestore, | ||
} from "@/lib/firebase/server/adminDb"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const { collection } = await request.json(); | ||
|
||
try { | ||
const rows = await serverFetchAllDataFromCollection(collection); | ||
|
||
// await serverUpdateInFirestore(collection, rows[0].id, { level: 1 }); | ||
await Promise.all( | ||
rows.map(row => | ||
serverUpdateInFirestore(collection, row.id, { level: 1 }), | ||
), | ||
); | ||
|
||
return NextResponse.json({ modifiedRows: rows.length }, { status: 200 }); | ||
} catch (err) { | ||
console.error(err); | ||
return NextResponse.json( | ||
{ error: "Failed to add fields to documents" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
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,65 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { | ||
serverDeleteData, | ||
serverFetchAllDataFromCollection, | ||
serverSaveDataToFirestore, | ||
} from "@/lib/firebase/server/adminDb"; | ||
|
||
import { TableNames } from "@/components/src/policy"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const { newCollection } = await request.json(); | ||
|
||
try { | ||
const rows = await serverFetchAllDataFromCollection( | ||
TableNames.SAFETY_TRAINING, | ||
); | ||
const rowsWithoutIds = rows.map(row => { | ||
const { id, ...other } = row; | ||
return other; | ||
}); | ||
await Promise.all( | ||
rowsWithoutIds.map(row => serverSaveDataToFirestore(newCollection, row)), | ||
); | ||
return NextResponse.json( | ||
{ duplicatedRows: rowsWithoutIds.length }, | ||
{ status: 200 }, | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
return NextResponse.json( | ||
{ error: "Failed to duplicate events to new collection" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} | ||
|
||
export async function DELETE(request: NextRequest) { | ||
const rows = await serverFetchAllDataFromCollection(TableNames.BOOKING); | ||
|
||
const ids = {}; | ||
|
||
for (let row of rows) { | ||
if (!ids[row.calendarEventId]) { | ||
ids[row.calendarEventId] = []; | ||
} | ||
ids[row.calendarEventId].push(row); | ||
} | ||
|
||
const deleteDocIds = []; | ||
|
||
for (let key in ids) { | ||
const dups = ids[key]; | ||
if (dups[0].requestedAt) { | ||
deleteDocIds.push(dups[1].id); | ||
} else { | ||
deleteDocIds.push(dups[0].id); | ||
} | ||
} | ||
|
||
await Promise.all( | ||
deleteDocIds.map(id => serverDeleteData(TableNames.BOOKING, id)), | ||
); | ||
|
||
return NextResponse.json({ status: 200 }); | ||
} |
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,54 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { | ||
serverFetchAllDataFromCollection, | ||
serverSaveDataToFirestore, | ||
} from "@/lib/firebase/server/adminDb"; | ||
|
||
import { TableNames } from "@/components/src/policy"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const { source, destination } = await request.json(); | ||
const sourceTable = source as TableNames; | ||
const destinationTable = destination as TableNames; | ||
|
||
try { | ||
const sourceRows = await serverFetchAllDataFromCollection(sourceTable); | ||
const destinationRows = | ||
await serverFetchAllDataFromCollection(destinationTable); | ||
|
||
// filter out ids | ||
const sourceRowsFiltered = sourceRows.map(row => { | ||
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 }; | ||
}); | ||
|
||
await Promise.all( | ||
merged.map(row => serverSaveDataToFirestore(destinationTable, row)), | ||
); | ||
|
||
return NextResponse.json( | ||
{ duplicatedRows: merged.length }, | ||
{ status: 200 }, | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
return NextResponse.json( | ||
{ error: "Failed to merge collections" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
This file was deleted.
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
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
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
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