Skip to content

Commit

Permalink
Invite user to calendar event after all aproval
Browse files Browse the repository at this point in the history
  • Loading branch information
rlho committed Feb 14, 2024
1 parent d13b92d commit 33fbbec
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const RoomCalendar = ({

useEffect(() => {
//PROD
fetchCalendarEvents(room.calendarId);
fetchCalendarEvents(room.calendarIdProd);
//DEV
//fetchCalendarEvents(room.calendarId);
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down
46 changes: 33 additions & 13 deletions media_commons_booking_app/src/server/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 33fbbec

Please sign in to comment.