Skip to content

Commit

Permalink
Merge pull request #421 from ITPNYU/main
Browse files Browse the repository at this point in the history
Staging release 09.25
  • Loading branch information
rlho authored Sep 25, 2024
2 parents 7a04bb7 + 33a057d commit 4e9da83
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy_development_app_engine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
echo "NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID }}" >> .env.production
echo "NEXT_PUBLIC_DATABASE_NAME=${{ secrets.NEXT_PUBLIC_DATABASE_NAME }}" >> .env.production
echo "NEXT_PUBLIC_BRANCH_NAME=${{ secrets.NEXT_PUBLIC_BRANCH_NAME }}" >> .env.production
echo "NEXT_PUBLIC_GCP_LOG_NAME=${{ secrets.NEXT_PUBLIC_GCP_LOG_NAME }}" >> .env.production
- name: Install dependencies
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy_production_app_engine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
echo "NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID }}" >> .env.production
echo "NEXT_PUBLIC_DATABASE_NAME=${{ secrets.NEXT_PUBLIC_DATABASE_NAME }}" >> .env.production
echo "NEXT_PUBLIC_BRANCH_NAME=${{ secrets.NEXT_PUBLIC_BRANCH_NAME }}" >> .env.production
echo "NEXT_PUBLIC_GCP_LOG_NAME=${{ secrets.NEXT_PUBLIC_GCP_LOG_NAME }}" >> .env.production
- name: Install dependencies
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy_staging_app_engine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
echo "NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID }}" >> .env.production
echo "NEXT_PUBLIC_DATABASE_NAME=${{ secrets.NEXT_PUBLIC_DATABASE_NAME }}" >> .env.production
echo "NEXT_PUBLIC_BRANCH_NAME=${{ secrets.NEXT_PUBLIC_BRANCH_NAME }}" >> .env.production
echo "NEXT_PUBLIC_GCP_LOG_NAME=${{ secrets.NEXT_PUBLIC_GCP_LOG_NAME }}" >> .env.production
- name: Install dependencies
run: |
Expand Down
1 change: 1 addition & 0 deletions booking-app/app.development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ automatic_scaling:
env_variables:
NEXT_PUBLIC_BRANCH_NAME: "development"
NODE_OPTIONS: "--max-old-space-size=4096"
TZ: "America/New_York"

build_env_variables:
NODE_OPTIONS: "--max-old-space-size=4096"
Expand Down
1 change: 1 addition & 0 deletions booking-app/app.production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ automatic_scaling:
env_variables:
NEXT_PUBLIC_BRANCH_NAME: "production"
NODE_OPTIONS: "--max-old-space-size=4096"
TZ: "America/New_York"

build_env_variables:
NODE_OPTIONS: "--max-old-space-size=4096"
Expand Down
1 change: 1 addition & 0 deletions booking-app/app.staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ automatic_scaling:
env_variables:
NEXT_PUBLIC_BRANCH_NAME: "staging"
NODE_OPTIONS: "--max-old-space-size=4096"
TZ: "America/New_York"

build_env_variables:
NODE_OPTIONS: "--max-old-space-size=4096"
Expand Down
2 changes: 0 additions & 2 deletions booking-app/app/api/bookings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,11 @@ export async function POST(request: NextRequest) {
equipmentCheckedOut: false,
...data,
});
console.log(" Done serverSaveDataToFirestore booking");
await serverSaveDataToFirestore(TableNames.BOOKING_STATUS, {
calendarEventId,
email,
requestedAt: Timestamp.now(),
});
console.log(" Done serverSaveDataToFirestore booking status");

await handleBookingApprovalEmails(
isAutoApproval,
Expand Down
10 changes: 8 additions & 2 deletions booking-app/app/api/calendarEvents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
updateEventPrefix,
} from "@/components/src/server/calendars";

import { serverBookingContents } from "@/components/src/server/admin";
import { getCalendarClient } from "@/lib/googleClient";
import { serverBookingContents } from "@/components/src/server/admin";

const getCalendarEvents = async (calendarId: string) => {
const calendar = await getCalendarClient();
Expand Down Expand Up @@ -84,7 +84,13 @@ export async function GET(req: NextRequest) {

try {
const events = await getCalendarEvents(calendarId);
return NextResponse.json(events);
const res = NextResponse.json(events);
res.headers.set(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate",
);
res.headers.set("Expires", "0");
return res;
} catch (error) {
console.error("Error fetching calendar events:", error);
return NextResponse.json(
Expand Down
41 changes: 38 additions & 3 deletions booking-app/app/api/safety_training_users/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { getGoogleSheet } from "@/lib/googleClient";
import { NextRequest, NextResponse } from "next/server";
import { getGoogleSheet, getLoggingClient } from "@/lib/googleClient";

const SPREADSHEET_ID = process.env.GOOGLE_SPREADSHEET_ID;
const SHEET_GID = process.env.GOOGLE_SHEET_ID;
const COLUMN = "B";
const MAX_ROWS = 1000;

export const dynamic = "force-dynamic";
export async function GET(request: NextRequest) {
try {
const sheetsService = await getGoogleSheet(SPREADSHEET_ID);
const logger = await getLoggingClient();

const spreadsheet = await sheetsService.spreadsheets.get({
spreadsheetId: SPREADSHEET_ID,
Expand All @@ -33,16 +35,49 @@ export async function GET(request: NextRequest) {
});
console.log("emails", response.data.values);

const logEntry = {
logName: process.env.NEXT_PUBLIC_GCP_LOG_NAME + "/safety-training",
resource: { type: "global" },
entries: [
{
jsonPayload: {
message: "Fetched emails",
emails: response.data.values,
number: response.data.values.length,
branchName: process.env.NEXT_PUBLIC_BRANCH_NAME,
timestamp,
},
severity: "INFO",
},
],
};

logger.entries.write({
requestBody: logEntry,
});

const rows = response.data.values;
if (!rows || rows.length === 0) {
return NextResponse.json({ emails: [] });
const res = NextResponse.json({ emails: [] });
res.headers.set(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate",
);
res.headers.set("Expires", "0");
return res;
}

const emails = rows
.flat()
.filter(email => email && typeof email === "string");

return NextResponse.json({ emails });
const res = NextResponse.json({ emails });
res.headers.set(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate",
);
res.headers.set("Expires", "0");
return res;
} catch (error) {
console.error("Failed to fetch emails:", error);
if (
Expand Down
25 changes: 19 additions & 6 deletions booking-app/components/src/client/utils/serverDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Timestamp } from "firebase-admin/firestore";
import { format } from "date-fns";
import { toZonedTime } from "date-fns-tz";
import { parseISO } from "date-fns";
import { format } from "date-fns-tz";

type DateInput = Date | Timestamp | { [key: string]: any } | number | string;

Expand All @@ -15,15 +17,26 @@ const parseTimestamp = (value: DateInput): Timestamp => {
};

export const serverFormatDate = (
input: DateInput,
input: string,
timeZone: string = "America/New_York"
): string => {
if (!input) return "";
try {
const timestamp = parseTimestamp(input);
const date = timestamp.toDate();
const zonedDate = new Date(date.toLocaleString("en-US", { timeZone }));
return format(zonedDate, "yyyy-MM-dd hh:mm a");
const date = new Date(input);

const zonedDate = toZonedTime(date, timeZone);

const formattedResult = format(zonedDate, "yyyy-MM-dd hh:mm a", {
timeZone,
});

console.log("Input:", input);
console.log("Parsed Date:", date.toISOString());
console.log("Zoned Date:", zonedDate.toString());
console.log("Formatted Result:", formattedResult);
console.log("Timezone:", timeZone);

return formattedResult;
} catch (error) {
console.error("Error formatting date:", error, "Input:", input);
return "";
Expand Down
15 changes: 11 additions & 4 deletions booking-app/lib/googleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ const refreshAccessTokenIfNeeded = async (oauth2Client) => {
const currentTime = Date.now();
const tokenExpiryTime = oauth2Client.credentials.expiry_date;

console.log("Current time:", new Date(currentTime));
console.log("Token expiry:", new Date(tokenExpiryTime));

if (!tokenExpiryTime || currentTime >= tokenExpiryTime - 60000) {
// 1分前に更新
try {
Expand Down Expand Up @@ -62,6 +59,16 @@ const getGoogleSheet = async (spreadsheetId: string) => {
const authClient = await getAuthenticatedClient();
return google.sheets({ version: "v4", auth: authClient });
};
const getLoggingClient = async () => {
const authClient = await getAuthenticatedClient();
return google.logging({ version: "v2", auth: authClient });
};

const oauth2Client = createOAuth2Client();
export { getCalendarClient, getGmailClient, getGoogleSheet, oauth2Client };
export {
getCalendarClient,
getGmailClient,
getGoogleSheet,
getLoggingClient,
oauth2Client,
};
23 changes: 23 additions & 0 deletions booking-app/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const nextConfig = {
FIREBASE_CLIENT_X509_CERT_URL: process.env.FIREBASE_CLIENT_X509_CERT_URL,
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
NEXT_PUBLIC_BRANCH_NAME: process.env.NEXT_PUBLIC_BRANCH_NAME,
NEXT_PUBLIC_GCP_LOG_NAME: process.env.NEXT_PUBLIC_GCP_LOG_NAME,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
GOOGLE_REDIRECT_URI: process.env.GOOGLE_REDIRECT_URI,
Expand All @@ -31,6 +32,28 @@ const nextConfig = {
}
return config;
},
async headers() {
return [
{
source: "/api/safety_training_users",
headers: [
{
key: "Cache-Control",
value: "no-store, max-age=0",
},
],
},
{
source: "/api/calendarEvents",
headers: [
{
key: "Cache-Control",
value: "no-store, max-age=0",
},
],
},
];
},
};

export default nextConfig;
9 changes: 9 additions & 0 deletions booking-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions booking-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"axios": "^1.7.2",
"bootstrap": "^5.3.3",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"dayjs": "^1.11.12",
"firebase": "^10.12.4",
"firebase-admin": "^12.2.0",
Expand Down
1 change: 1 addition & 0 deletions booking-app/scripts/getDevelopmentRefreshToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SCOPES = [
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/logging.write",
];

const oauth2Client = new google.auth.OAuth2(
Expand Down
1 change: 1 addition & 0 deletions booking-app/scripts/getLocalRefreshToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const SCOPES = [
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/logging.write",
];

const oauth2Client = new google.auth.OAuth2(
Expand Down
1 change: 1 addition & 0 deletions booking-app/scripts/getProductionRefreshToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SCOPES = [
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/logging.write",
];

const oauth2Client = new google.auth.OAuth2(
Expand Down
1 change: 1 addition & 0 deletions booking-app/scripts/getStagingRefreshToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const SCOPES = [
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/logging.write",
];

const oauth2Client = new google.auth.OAuth2(
Expand Down

0 comments on commit 4e9da83

Please sign in to comment.