mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add/delete entry in google calendar
This commit is contained in:
committed by
Thomas Ruoff
parent
673c2e2d00
commit
33c5a91e68
@@ -5,7 +5,7 @@ import { clearBookingData, loadBookingData } from '../helpers/storage'
|
|||||||
import { createBooking } from '../helpers/booking'
|
import { createBooking } from '../helpers/booking'
|
||||||
import { Booking } from '../db/booking'
|
import { Booking } from '../db/booking'
|
||||||
|
|
||||||
export type BookFormData = Omit<Booking, 'uuid'> & {
|
export type BookFormData = Omit<Booking, 'uuid' | 'calendarEventId'> & {
|
||||||
storeData?: boolean
|
storeData?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as mongoose from 'mongoose'
|
import * as mongoose from 'mongoose'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
|
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date'
|
||||||
|
import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar'
|
||||||
|
|
||||||
import { Bill } from './bill'
|
import { Bill } from './bill'
|
||||||
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
|
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
|
||||||
@@ -21,7 +22,8 @@ export type Booking = {
|
|||||||
purpose?: string
|
purpose?: string
|
||||||
org?: string
|
org?: string
|
||||||
destination?: string
|
destination?: string
|
||||||
days?: string[]
|
days?: string[],
|
||||||
|
calendarEventId: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BookingDocument = Booking &
|
export type BookingDocument = Booking &
|
||||||
@@ -103,6 +105,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
|
|||||||
purpose: { type: String, required: false },
|
purpose: { type: String, required: false },
|
||||||
org: { type: String, required: false },
|
org: { type: String, required: false },
|
||||||
destination: { type: String, required: false },
|
destination: { type: String, required: false },
|
||||||
|
calendarEventId: { type: String, required: false },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
@@ -120,6 +123,20 @@ BookingSchema.pre('validate', function (next: () => void): void {
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
BookingSchema.pre('save', async function (next: () => void): Promise<void> {
|
||||||
|
const booking = this as BookingDocument
|
||||||
|
|
||||||
|
if (!booking.calendarEventId) {
|
||||||
|
// create calendar event before saving to database
|
||||||
|
await createCalendarEvent(booking);
|
||||||
|
} else if ([BOOKING_STATUS.CANCELED, BOOKING_STATUS.REJECTED].includes(booking.status)) {
|
||||||
|
// event has been canceled or rejected, delete calendar event again to free up the slot
|
||||||
|
await deleteCalendarEvent(booking);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
BookingSchema.static('findBookedDays', async function (
|
BookingSchema.static('findBookedDays', async function (
|
||||||
uuidsToIngore: string[] = []
|
uuidsToIngore: string[] = []
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
|
|||||||
79
lib/googlecalendar.ts
Normal file
79
lib/googlecalendar.ts
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
import {google} from 'googleapis';
|
||||||
|
import { Booking } from '../db/booking';
|
||||||
|
|
||||||
|
const keyFile = process.env.GOOGLE_SERVICE_ACCOUNT_KEY_FILE;
|
||||||
|
const calendarId = process.env.GOOGLE_CALENDAR_ID;
|
||||||
|
|
||||||
|
const auth = new google.auth.GoogleAuth({
|
||||||
|
keyFile,
|
||||||
|
scopes: [
|
||||||
|
'https://www.googleapis.com/auth/calendar',
|
||||||
|
'https://www.googleapis.com/auth/calendar.readonly'
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const calendar = google.calendar({
|
||||||
|
version: 'v3',
|
||||||
|
auth,
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function getNewEvents() {
|
||||||
|
const { data } = await calendar.events.list({
|
||||||
|
calendarId,
|
||||||
|
timeMin: new Date().toISOString()}
|
||||||
|
);
|
||||||
|
|
||||||
|
return data.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSummary(booking: Partial<Booking>) : string {
|
||||||
|
let summary = "";
|
||||||
|
|
||||||
|
if (booking.org) {
|
||||||
|
summary += `${booking.org} - `;
|
||||||
|
}
|
||||||
|
|
||||||
|
summary += booking.name;
|
||||||
|
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createCalendarEvent(booking : Booking) : Promise<Booking> {
|
||||||
|
|
||||||
|
const response = await calendar.events.insert({
|
||||||
|
auth,
|
||||||
|
calendarId,
|
||||||
|
requestBody: {
|
||||||
|
summary: getSummary(booking),
|
||||||
|
// description,
|
||||||
|
start: { date: booking.startDate },
|
||||||
|
end: { date: booking.endDate },
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
booking.calendarEventId = response.data.id;
|
||||||
|
|
||||||
|
return booking;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteCalendarEvent(booking: Booking) {
|
||||||
|
await calendar.events.delete({
|
||||||
|
auth,
|
||||||
|
calendarId,
|
||||||
|
eventId: booking.calendarEventId,
|
||||||
|
});
|
||||||
|
|
||||||
|
booking.calendarEventId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//export async function patchCalendarEvent(booking: { calendarEventId: string } & Partial<Booking> ) : Promise<Booking> {
|
||||||
|
// const response = await calendar.events.patch({
|
||||||
|
// auth,
|
||||||
|
// calendarId,
|
||||||
|
// eventId: booking.calendarEventId,
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// return booking;
|
||||||
|
//}
|
||||||
|
|
||||||
Reference in New Issue
Block a user