Skip to content

Commit

Permalink
add safety training logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lucia-gomez committed Sep 20, 2024
1 parent a15a5c1 commit 6545c72
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
24 changes: 23 additions & 1 deletion booking-app/app/api/safety_training_users/route.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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: [] });
Expand Down
12 changes: 11 additions & 1 deletion booking-app/lib/googleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
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 6545c72

Please sign in to comment.