From 6545c72a64a11f9f3dc8f4577516a7c988a95802 Mon Sep 17 00:00:00 2001 From: lucia <51058748+lucia-gomez@users.noreply.github.com> Date: Fri, 20 Sep 2024 19:50:51 -0400 Subject: [PATCH] add safety training logger --- .../app/api/safety_training_users/route.ts | 24 ++++++++++++++++++- booking-app/lib/googleClient.ts | 12 +++++++++- .../scripts/getDevelopmentRefreshToken.js | 1 + booking-app/scripts/getLocalRefreshToken.js | 1 + .../scripts/getProductionRefreshToken.js | 1 + booking-app/scripts/getStagingRefreshToken.js | 1 + 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/booking-app/app/api/safety_training_users/route.ts b/booking-app/app/api/safety_training_users/route.ts index 18c5eb3d..fcc62919 100644 --- a/booking-app/app/api/safety_training_users/route.ts +++ b/booking-app/app/api/safety_training_users/route.ts @@ -1,5 +1,5 @@ -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; @@ -9,6 +9,7 @@ const MAX_ROWS = 1000; 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, @@ -33,6 +34,27 @@ 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: [] }); diff --git a/booking-app/lib/googleClient.ts b/booking-app/lib/googleClient.ts index 9fd7bbab..80a3be08 100644 --- a/booking-app/lib/googleClient.ts +++ b/booking-app/lib/googleClient.ts @@ -62,6 +62,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, +}; diff --git a/booking-app/scripts/getDevelopmentRefreshToken.js b/booking-app/scripts/getDevelopmentRefreshToken.js index 87c8a1bb..8c20ed90 100644 --- a/booking-app/scripts/getDevelopmentRefreshToken.js +++ b/booking-app/scripts/getDevelopmentRefreshToken.js @@ -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( diff --git a/booking-app/scripts/getLocalRefreshToken.js b/booking-app/scripts/getLocalRefreshToken.js index ac65929a..2747504d 100644 --- a/booking-app/scripts/getLocalRefreshToken.js +++ b/booking-app/scripts/getLocalRefreshToken.js @@ -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( diff --git a/booking-app/scripts/getProductionRefreshToken.js b/booking-app/scripts/getProductionRefreshToken.js index 191fc22e..1ab3150d 100644 --- a/booking-app/scripts/getProductionRefreshToken.js +++ b/booking-app/scripts/getProductionRefreshToken.js @@ -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( diff --git a/booking-app/scripts/getStagingRefreshToken.js b/booking-app/scripts/getStagingRefreshToken.js index 2e94c2d4..f9872ab1 100644 --- a/booking-app/scripts/getStagingRefreshToken.js +++ b/booking-app/scripts/getStagingRefreshToken.js @@ -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(