83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import { google } from 'googleapis';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { TransactionEntity } from 'src/modules/transaction/transaction/domain/entities/transaction.entity';
|
|
|
|
export async function CreateEventCalendarHelper(
|
|
transaction: TransactionEntity,
|
|
isDelete = false,
|
|
) {
|
|
let result;
|
|
|
|
const filePath = path.join(
|
|
__dirname,
|
|
'../../../../../../../../',
|
|
'google-credential.json',
|
|
);
|
|
const credential = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
|
|
const client = new google.auth.JWT({
|
|
email: credential.client_email,
|
|
key: credential.private_key,
|
|
scopes: ['https://www.googleapis.com/auth/calendar'],
|
|
});
|
|
|
|
const calendar = google.calendar({
|
|
version: 'v3',
|
|
auth: client,
|
|
});
|
|
|
|
const eventData = mappingData(transaction);
|
|
|
|
if (transaction.calendar_id) {
|
|
result = await calendar.events.update(
|
|
{
|
|
calendarId: process.env.GOOGLE_CALENDAR_ID,
|
|
eventId: transaction.calendar_id,
|
|
requestBody: eventData,
|
|
},
|
|
{},
|
|
);
|
|
} else if (!isDelete) {
|
|
result = await calendar.events.insert(
|
|
{
|
|
calendarId: process.env.GOOGLE_CALENDAR_ID,
|
|
requestBody: eventData,
|
|
},
|
|
{},
|
|
);
|
|
} else {
|
|
result = await calendar.events.delete(
|
|
{
|
|
calendarId: process.env.GOOGLE_CALENDAR_ID,
|
|
eventId: transaction.calendar_id,
|
|
},
|
|
{},
|
|
);
|
|
}
|
|
|
|
return result?.data;
|
|
}
|
|
|
|
function mappingData(transaction) {
|
|
return {
|
|
summary: transaction.customer_name ?? transaction.invoice_code,
|
|
description: `<b>Booking for invoice ${
|
|
transaction.invoice_code
|
|
}</b><p>List Items :</p><ul>${transaction.items.map(
|
|
(item) => `<li>${item.item_name}</li>`,
|
|
)}</ul>`,
|
|
start: {
|
|
dateTime: new Date(transaction.booking_date).toISOString(),
|
|
timeZone: 'Asia/Jakarta',
|
|
},
|
|
end: {
|
|
dateTime: new Date(transaction.booking_date).toISOString(),
|
|
timeZone: 'Asia/Jakarta',
|
|
},
|
|
reminders: {
|
|
useDefault: false,
|
|
},
|
|
};
|
|
}
|