Skip to content

Commit

Permalink
add staging dbRefactor script + button
Browse files Browse the repository at this point in the history
  • Loading branch information
lucia-gomez committed Oct 24, 2024
1 parent 33fdd7e commit 612c5de
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 20 deletions.
7 changes: 3 additions & 4 deletions booking-app/app/api/db/duplicate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
import { TableNames } from "@/components/src/policy";

export async function POST(request: NextRequest) {
const { newCollection } = await request.json();
const { sourceCollection, newCollection } = await request.json();
const source = sourceCollection as TableNames;

try {
const rows = await serverFetchAllDataFromCollection(
TableNames.SAFETY_TRAINING,
);
const rows = await serverFetchAllDataFromCollection(source);
const rowsWithoutIds = rows.map(row => {
const { id, ...other } = row;
return other;
Expand Down
142 changes: 142 additions & 0 deletions booking-app/app/api/db/refactor/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { NextRequest, NextResponse } from "next/server";
import {
serverFetchAllDataFromCollection,
serverSaveDataToFirestore,
serverUpdateInFirestore,
} from "@/lib/firebase/server/adminDb";

import { TableNames } from "@/components/src/policy";
import { Timestamp } from "@firebase/firestore";
import { getLoggingClient } from "@/lib/googleClient";

export const dynamic = "force-dynamic";

// DB refactor entrypoint, will call other API endpoints to handle refactor steps
export async function POST(request: NextRequest) {
let logger;
try {
logger = await getLoggingClient();

// 1. rename usersLiaison to usersApprovers
const sourceApproverCollection = TableNames.APPROVERS;
const newApproverCollection = "usersApprovers";
let res = await fetch(
process.env.NEXT_PUBLIC_BASE_URL + "/api/db/duplicate",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sourceCollection: sourceApproverCollection,
newCollection: newApproverCollection,
}),
},
);

if (!res.ok) {
let json = await res.json();
throw new Error(`Error ${res.status}: ${json.error}`);
} else {
log(
logger,
`Duplicated ${sourceApproverCollection} to ${newApproverCollection}`,
);
}

// 2. every document in usersApprovers needs a numeric approver level
const rows = await serverFetchAllDataFromCollection(
newApproverCollection as TableNames,
);

await Promise.all(
rows.map(row =>
serverUpdateInFirestore(newApproverCollection, row.id, { level: 1 }),
),
);
log(logger, `Added level field to docs in ${newApproverCollection}`);

// 3. add finalApproverEmail to usersApprovers collection
const date = new Date();
await serverSaveDataToFirestore(newApproverCollection, {
email: "[email protected]",
level: 2,
// createdAt: new Timestamp(date.getSeconds(), 0),
});
log(logger, `Added final approver doc to ${newApproverCollection}`);

// 4. rename usersSafetyWhitelist to usersWhitelist
const sourceWhitelistCollection = TableNames.SAFETY_TRAINING;
const newWhitelistCollection = "usersWhitelist";
res = await fetch(process.env.NEXT_PUBLIC_BASE_URL + "/api/db/duplicate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sourceCollection: sourceWhitelistCollection,
newCollection: newWhitelistCollection,
}),
});

if (!res.ok) {
let json = await res.json();
throw new Error(`Error ${res.status}: ${json.error}`);
} else {
log(
logger,
`Duplicated ${sourceWhitelistCollection} to ${newWhitelistCollection}`,
);
}

// 5. combine bookings and bookingStatus documents
const sourceBookingStatus = TableNames.BOOKING_STATUS;
const destinationBooking = TableNames.BOOKING;
res = await fetch(process.env.NEXT_PUBLIC_BASE_URL + "/api/db/merge", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
source: sourceBookingStatus,
destination: destinationBooking,
}),
});

if (!res.ok) {
let json = await res.json();
throw new Error(`Error ${res.status}: ${json.error}`);
} else {
log(logger, `Merged ${sourceBookingStatus} to ${destinationBooking}`);
}

return NextResponse.json({ status: 200 });
} catch (err) {
console.error(err);
log(logger, err);
return NextResponse.json(
{ error: "Failed to merge collections" },
{ status: 500 },
);
}
}

function log(logger, msg) {
let logEntry = {
logName: process.env.NEXT_PUBLIC_GCP_LOG_NAME + "/db-refactor",
resource: { type: "global" },
entries: [
{
jsonPayload: {
message: msg,
branchName: process.env.NEXT_PUBLIC_BRANCH_NAME,
},
severity: "INFO",
},
],
};

logger.entries.write({
requestBody: logEntry,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,11 @@ const SyncCalendars = () => {
}
};

// const duplicate = async () => {
// await fetch("/api/db/duplicate", {
// method: "DELETE",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// source: TableNames.BOOKING_STATUS,
// destination: TableNames.BOOKING,
// }),
// });
// };
const refactor = async () => {
await fetch("/api/db/refactor", {
method: "POST",
});
};

return (
<Box>
Expand All @@ -62,7 +55,12 @@ const SyncCalendars = () => {
Sync Calendar Events
</Button>
</Box>
{/* <Button onClick={duplicate}>Duplicate</Button> */}
<Box sx={{ marginTop: 6 }}>
<p>Don't press this unless you know what you're doing!</p>
<Button variant="contained" onClick={refactor} color={"error"}>
!!! DB REFACTOR !!!
</Button>
</Box>
<AlertToast
message={message}
severity={alertSeverity}
Expand Down
6 changes: 3 additions & 3 deletions booking-app/components/src/policy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/********** GOOGLE SHEETS ************/

import { clientGetFinalApproverEmailFromDatabase } from "@/lib/firebase/firebase";
import { BookingStatusLabel } from "./types";
import { clientGetFinalApproverEmailFromDatabase } from "@/lib/firebase/firebase";

/** ACTIVE master Google Sheet */
export const ACTIVE_SHEET_ID = "1MnWbn6bvNyMiawddtYYx0tRW4NMgvugl0I8zBO3sy68";

export enum TableNames {
ADMINS = "usersAdmin",
APPROVERS = "usersApprovers",
APPROVERS = "usersLiaison",
BANNED = "usersBanned",
BOOKING = "bookings",
BOOKING_STATUS = "bookingStatus",
Expand All @@ -17,7 +17,7 @@ export enum TableNames {
PAS = "usersPa",
POLICY = "policy",
RESOURCES = "resources",
SAFETY_TRAINING = "usersWhitelist",
SAFETY_TRAINING = "usersSafetyWhitelist",
SETTINGS = "settings",
}

Expand Down

0 comments on commit 612c5de

Please sign in to comment.