diff --git a/media_commons_booking_app/src/client/booking/components/RoomCalendar.tsx b/media_commons_booking_app/src/client/booking/components/RoomCalendar.tsx index a79e8cbd..26930666 100644 --- a/media_commons_booking_app/src/client/booking/components/RoomCalendar.tsx +++ b/media_commons_booking_app/src/client/booking/components/RoomCalendar.tsx @@ -21,7 +21,7 @@ export const RoomCalendar = ({ useEffect(() => { //PROD - fetchCalendarEvents(room.calendarId); + fetchCalendarEvents(room.calendarIdProd); //DEV //fetchCalendarEvents(room.calendarId); }, []); 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 adb7ad32..bdde2e4d 100644 --- a/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx +++ b/media_commons_booking_app/src/client/booking/components/SheetEditor.tsx @@ -52,15 +52,15 @@ const INSTANT_APPROVAL_ROOMS = ['221', '222', '223', '224']; const SheetEditor = () => { //IN PRODUCTION - //const roomCalendarId = (room) => { - // return findByRoomId(mappingRoomSettings, room.roomId)?.calendarIdProd; - //}; - - //IN DEV const roomCalendarId = (room) => { - return findByRoomId(mappingRoomSettings, room.roomId)?.calendarId; + return findByRoomId(mappingRoomSettings, room.roomId)?.calendarIdProd; }; + //IN DEV + //const roomCalendarId = (room) => { + // return findByRoomId(mappingRoomSettings, room.roomId)?.calendarId; + //}; + const getActiveUserEmail = () => { serverFunctions.getActiveUserEmail().then((response) => { console.log('userEmail response', response); @@ -222,6 +222,7 @@ const SheetEditor = () => { const registerEvent = async (data) => { const email = userEmail || data.missingEmail; const [room, ...otherRooms] = selectedRoom; + const selectedRoomIds = selectedRoom.map((r) => r.roomId); const otherRoomIds = otherRooms.map((r) => roomCalendarId(r)); const firstApprovers = firstApproverEmailsByDepartment( @@ -232,49 +233,47 @@ const SheetEditor = () => { // Add the event to the calendar. const calendarEventId = await serverFunctions.addEventToCalendar( roomCalendarId(room), - `[REQUESTED] ${room.roomId},${otherRooms.map( - (r) => room.roomId - )} ${department} - ${data.firstName} ${data.lastName} (${data.netId})`, + `[REQUESTED] ${selectedRoomIds} ${department} - ${data.firstName} ${data.lastName} (${data.netId})`, 'Your reservation is not yet confirmed. The coordinator will review and finalize your reservation within a few days.', bookInfo.startStr, bookInfo.endStr, otherRoomIds ); - selectedRoom.map(async (room) => { - // Record the event to the spread sheet. - const contents = order.map(function (key) { - return data[key]; - }); - serverFunctions.appendRow(BOOKING_SHEET_NAME, [ - calendarEventId, - room.roomId, - email, - bookInfo.startStr, - bookInfo.endStr, - ...contents, - ]); - serverFunctions.request(calendarEventId, email).then(() => { - // Rooms 221 to 224 don't need approval - if (INSTANT_APPROVAL_ROOMS.includes(room.roomId)) { - serverFunctions.approveInstantBooking(calendarEventId); - } else { - const getApprovalUrl = serverFunctions.approvalUrl(calendarEventId); - const getRejectedUrl = serverFunctions.rejectUrl(calendarEventId); - Promise.all([getApprovalUrl, getRejectedUrl]).then((values) => { - const userEventInputs = { - calendarEventId: calendarEventId, - roomId: room.roomId, - email: email, - startDate: bookInfo?.startStr, - endDate: bookInfo?.endStr, - approvalUrl: values[0], - rejectedUrl: values[1], - ...data, - }; - sendApprovalEmail(FIRST_APPROVER, userEventInputs); - }); - } - }); + // Record the event to the spread sheet. + const contents = order.map(function (key) { + return data[key]; + }); + serverFunctions.appendRow(BOOKING_SHEET_NAME, [ + calendarEventId, + selectedRoomIds, + email, + bookInfo.startStr, + bookInfo.endStr, + ...contents, + ]); + serverFunctions.request(calendarEventId, email).then(() => { + const isAutoApproval = selectedRoomIds.every((r) => + INSTANT_APPROVAL_ROOMS.includes(r) + ); + if (isAutoApproval) { + serverFunctions.approveInstantBooking(calendarEventId); + } else { + const getApprovalUrl = serverFunctions.approvalUrl(calendarEventId); + const getRejectedUrl = serverFunctions.rejectUrl(calendarEventId); + Promise.all([getApprovalUrl, getRejectedUrl]).then((values) => { + const userEventInputs = { + calendarEventId: calendarEventId, + roomId: selectedRoomIds, + email: email, + startDate: bookInfo?.startStr, + endDate: bookInfo?.endStr, + approvalUrl: values[0], + rejectedUrl: values[1], + ...data, + }; + sendApprovalEmail(firstApprovers, userEventInputs); + }); + } }); alert('Your request has been sent.'); setLoading(false); diff --git a/media_commons_booking_app/src/server/admin.js b/media_commons_booking_app/src/server/admin.js index db07e338..0b7751d6 100644 --- a/media_commons_booking_app/src/server/admin.js +++ b/media_commons_booking_app/src/server/admin.js @@ -65,11 +65,7 @@ export const approveInstantBooking = (id) => { .getRange(rowIndex + 1, 4) .setValue(current); - SpreadsheetApp.openById(ACTIVE_SHEET_ID) - .getSheetByName(BOOKING_STATUS_SHEET_NAME) - .getRange(rowIndex + 1, 5) - .setValue(current); - updateEventPrefix(id, 'APPROVED'); + approveEvent(rowIndex, id); }; export const approveBooking = (id) => { @@ -83,15 +79,10 @@ export const approveBooking = (id) => { .getRange(rowIndex + 1, 4); console.log('firstApproveDate', firstApproveDate.getValue()); - //TODO: check email address + //COMPLETE ALL APPROVAL if (firstApproveDate.getValue() !== '') { // second approve - SpreadsheetApp.openById(ACTIVE_SHEET_ID) - .getSheetByName(BOOKING_STATUS_SHEET_NAME) - .getRange(rowIndex + 1, 5) - .setValue(current); - //TODO: send email to user - updateEventPrefix(id, 'APPROVED'); + approveEvent(rowIndex, id); } else { // first approve SpreadsheetApp.openById(ACTIVE_SHEET_ID) @@ -111,12 +102,41 @@ export const allRoomIds = () => { const rows = fetchRows_('rooms'); const ids = rows.map((row) => row[3]); ids.shift(); - console.log('ids', ids); return ids; }; +export const approveEvent = (rowIndex, id) => { + SpreadsheetApp.openById(ACTIVE_SHEET_ID) + .getSheetByName(BOOKING_STATUS_SHEET_NAME) + .getRange(rowIndex + 1, 5) + .setValue(current); + const guestEmail = SpreadsheetApp.openById(ACTIVE_SHEET_ID) + .getSheetByName(BOOKING_STATUS_SHEET_NAME) + .getRange(rowIndex + 1, 2) + .getValue(); + console.log('guestEmail', guestEmail); + + updateEventPrefix(id, 'APPROVED'); + inviteUserToCalendarEvent(id, guestEmail); +}; + +const inviteUserToCalendarEvent = (eventId, guestEmail) => { + console.log(`Invite User:${guestEmail}`); + //TODO: getting roomId from booking sheet + const roomIds = allRoomIds(); + roomIds.map((roomId) => { + const calendar = CalendarApp.getCalendarById(roomId); + const event = calendar.getEventById(eventId); + if (event) { + event.addGuest(guestEmail); + console.log(`Invited ${guestEmail} to room: ${roomId} event: ${eventId}`); + } + }); +}; + export const updateEventPrefix = (id, newPrefix) => { const roomIds = allRoomIds(); + //TODO: getting roomId from booking sheet roomIds.map((roomId) => { const calendar = CalendarApp.getCalendarById(roomId); const event = calendar.getEventById(id);