From 9dc1662d12ac49a74dc6422389d26b78b799aa70 Mon Sep 17 00:00:00 2001 From: "riho.takagi" Date: Tue, 5 Dec 2023 11:44:05 -0500 Subject: [PATCH] set first approver from google sheets --- .../client/booking/components/FormInput.tsx | 4 ++- .../client/booking/components/SheetEditor.tsx | 25 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/media_commons_booking_app/src/client/booking/components/FormInput.tsx b/media_commons_booking_app/src/client/booking/components/FormInput.tsx index a554561a..661df30f 100644 --- a/media_commons_booking_app/src/client/booking/components/FormInput.tsx +++ b/media_commons_booking_app/src/client/booking/components/FormInput.tsx @@ -76,7 +76,9 @@ const FormInput = ({ hasEmail, roomNumber, handleParentSubmit }) => { const onSubmit: SubmitHandler = (data) => { const dumpMediaServices = data.mediaServices || []; //@ts-ignore - data.mediaServices = dumpMediaServices?.join(', '); + data.mediaServices = Array.isArray(dumpMediaServices) + ? dumpMediaServices.join(', ') + : dumpMediaServices; handleParentSubmit(data); }; console.log('errors', errors); diff --git a/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx b/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx index 581b1d89..774f5e32 100644 --- a/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx +++ b/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx @@ -20,12 +20,13 @@ export type RoomSetting = { export type Purpose = 'multipleRoom' | 'motionCapture'; -const FIRST_APPROVER = ['rh3555@nyu.edu', 'nnp278@nyu.edu']; +const FIRST_APPROVER = ['rh3555@nyu.edu']; const ROOM_SHEET_NAME = 'rooms'; const BASE_URL = 'https://script.google.com/a/macros/itp.nyu.edu/s/AKfycbwvWl7X9w62iz0QLWOY1F1zTT-cLv9EfzPi77Adkxxwqb_ZG4vQayi3EkT7zz9jekE8/exec'; const BOOKING_SHEET_NAME = 'bookings'; +const LIAISON_SHEET_NAME = 'liaisons'; const SAFTY_TRAINING_SHEET_NAME = 'safety_training_users'; const INSTANT_APPROVAL_ROOMS = ['221', '222', '223', '224']; @@ -36,6 +37,7 @@ const SheetEditor = () => { setUserEmail(response); }); }; + const [apiKey, setApiKey] = useState(); const [showModal, setShowModal] = useState(true); const [userEmail, setUserEmail] = useState(); @@ -47,6 +49,7 @@ const SheetEditor = () => { const [mappingRoomSettings, setMappingRoomSettings] = useState([]); const [section, setSection] = useState('selectRoom'); const [loading, setLoading] = useState(true); + const [liaisonUsers, setLiaisonUsers] = useState([]); const order: (keyof Inputs)[] = [ 'firstName', 'lastName', @@ -94,6 +97,7 @@ const SheetEditor = () => { useEffect(() => { fetchRoomSettings(); + fetchLiaisonUsers(); }, []); useEffect(() => { @@ -114,6 +118,12 @@ const SheetEditor = () => { } }, [mappingRoomSettings]); + const fetchLiaisonUsers = async () => { + serverFunctions.fetchRows(LIAISON_SHEET_NAME).then((rows) => { + setLiaisonUsers(rows); + }); + }; + function findByRoomId(array, id) { return array.find((room) => room.roomId === id); } @@ -147,8 +157,19 @@ const SheetEditor = () => { }); }; + const firstApprover = (department) => { + console.log('liaisonUsers', liaisonUsers); + console.log('department', department); + const approvers = liaisonUsers.filter( + (liaison) => liaison[1] === department + ); + const emails = approvers.map((approver) => approver[0]); + console.log('approvers', emails); + approvers.length > 0 ? emails : FIRST_APPROVER; + }; const registerEvent = (data) => { const email = userEmail || data.missingEmail; + const approver = firstApprover(data.department); selectedRoom.map(async (room) => { // Add the event to the calendar. const roomCalendarId = findByRoomId( @@ -193,7 +214,7 @@ const SheetEditor = () => { rejectedUrl: values[1], ...data, }; - sendApprovalEmail(FIRST_APPROVER, userEventInputs); + sendApprovalEmail(approver, userEventInputs); }); } });