feat(SPG-476) Google Calendar Integration - Hari Libur
parent
af6266c8d6
commit
554cda2144
|
@ -0,0 +1,53 @@
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { google } from 'googleapis';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class IndexHolidayCalendarManager {
|
||||||
|
async execute() {
|
||||||
|
const events = [];
|
||||||
|
const calendar = google.calendar({
|
||||||
|
version: 'v3',
|
||||||
|
auth: 'AIzaSyCsCt6PDd6uYLkahvtdvCoMWf-1_QaLiNM',
|
||||||
|
});
|
||||||
|
const calendarId = 'id.indonesian#holiday@group.v.calendar.google.com';
|
||||||
|
|
||||||
|
const res = await calendar.events.list({
|
||||||
|
calendarId: calendarId,
|
||||||
|
timeMin: new Date().getFullYear() + '-01-01T00:00:00Z',
|
||||||
|
timeMax: new Date().getFullYear() + '-12-31T23:59:59Z',
|
||||||
|
singleEvents: true,
|
||||||
|
orderBy: 'startTime',
|
||||||
|
});
|
||||||
|
|
||||||
|
res.data.items?.forEach((item) => {
|
||||||
|
// const eventName = item.summary.replace("Joint", "").replace("for", "").replace("after", "").replace("Holiday","").trim();
|
||||||
|
let eventName = item.summary.replace('Cuti Bersama', '').trim();
|
||||||
|
|
||||||
|
// function ini untuk menyamakan dan menggabungkan Hari Libur
|
||||||
|
if (eventName == 'Hari Idul Fitri')
|
||||||
|
eventName = eventName.replace('Hari', '').trim();
|
||||||
|
else if (eventName == 'Natal (Hari Tinju)' || eventName == 'Malam Natal')
|
||||||
|
eventName = 'Hari Raya Natal';
|
||||||
|
|
||||||
|
const exist = events.find((event) => {
|
||||||
|
return event.name.toLowerCase().includes(eventName.toLowerCase());
|
||||||
|
});
|
||||||
|
|
||||||
|
if (exist) {
|
||||||
|
Object.assign(exist, {
|
||||||
|
end_date: item.start.date,
|
||||||
|
total_day: exist.total_day + 1,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
events.push({
|
||||||
|
name: eventName,
|
||||||
|
start_date: item.start?.date,
|
||||||
|
end_date: item.start?.date,
|
||||||
|
total_day: 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue