feat(google-calendar) module google calendar
parent
99a82c164a
commit
08dea7965d
|
@ -0,0 +1,11 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { IndexHolidayCalendarManager } from './managers/index-holiday-google-calendar.manager';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleCalendarOrchestrator {
|
||||
constructor(private indexHoliday: IndexHolidayCalendarManager) {}
|
||||
|
||||
async holiday() {
|
||||
return await this.indexHoliday.execute();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
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.holiday_name
|
||||
.toLowerCase()
|
||||
.includes(eventName.toLowerCase());
|
||||
});
|
||||
|
||||
if (exist) {
|
||||
Object.assign(exist, {
|
||||
end_date: item.start.date,
|
||||
total_day: exist.total_day + 1,
|
||||
});
|
||||
} else {
|
||||
events.push({
|
||||
holiday_name: eventName,
|
||||
start_date: item.start?.date,
|
||||
end_date: item.start?.date,
|
||||
total_day: 1,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return events;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { IndexHolidayCalendarManager } from '../../configuration/google-calendar/domain/usecases/managers/index-holiday-google-calendar.manager';
|
||||
import { GoogleCalendarController } from './infrastructure/google-calendar.controller';
|
||||
import { GoogleCalendarOrchestrator } from './domain/usecases/google-calendar.orchestrator';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule.forRoot(), CqrsModule],
|
||||
controllers: [GoogleCalendarController],
|
||||
providers: [IndexHolidayCalendarManager, GoogleCalendarOrchestrator],
|
||||
})
|
||||
export class GoogleCalendarModule {}
|
|
@ -0,0 +1,16 @@
|
|||
import { GoogleCalendarOrchestrator } from './../domain/usecases/google-calendar.orchestrator';
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { Public } from 'src/core/guards';
|
||||
|
||||
@ApiTags(`google calendar - read`)
|
||||
@Controller('google-calendar')
|
||||
@Public(true)
|
||||
export class GoogleCalendarController {
|
||||
constructor(private orchestrator: GoogleCalendarOrchestrator) {}
|
||||
|
||||
@Get('/holiday')
|
||||
async calendar() {
|
||||
return await this.orchestrator.holiday();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue