Files
wfh/src/lib/google-drive.ts
T

81 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-03-12 17:09:22 -05:00
import { google } from 'googleapis';
import { Readable } from 'stream';
export async function uploadToDrive(accessToken: string, fileName: string, content: string, folderId?: string) {
const auth = new google.auth.OAuth2();
auth.setCredentials({ access_token: accessToken });
const drive = google.drive({ version: 'v3', auth });
const fileMetadata: any = {
name: fileName,
mimeType: 'application/vnd.google-apps.document', // Automatically convert to Google Doc
};
if (folderId) {
fileMetadata.parents = [folderId];
}
const media = {
mimeType: 'text/markdown',
body: Readable.from([content]),
};
try {
const response = await drive.files.create({
requestBody: fileMetadata,
media: media,
fields: 'id, webViewLink'
});
return response.data;
} catch (error) {
console.error('Error uploading to Google Drive:', error);
throw error;
}
}
2026-03-13 11:13:45 -05:00
export async function updateDriveFile(accessToken: string, fileId: string, content: string) {
const auth = new google.auth.OAuth2();
auth.setCredentials({ access_token: accessToken });
const drive = google.drive({ version: 'v3', auth });
const media = {
mimeType: 'text/markdown',
body: Readable.from([content]),
};
try {
const response = await drive.files.update({
fileId,
media,
fields: 'id, webViewLink',
});
return response.data;
} catch (error) {
console.error('Error updating Google Drive file:', error);
throw error;
}
}
2026-03-12 17:09:22 -05:00
export function generateReportMarkdown(report: any) {
let md = `# WFH Daily Report - ${new Date(report.date).toLocaleDateString()}\n`;
md += `**Employee:** ${report.user.name}\n`;
md += `**Manager:** ${report.managerName}\n\n`;
md += `## Planned Tasks\n`;
report.tasks.filter((t: any) => t.type === 'PLANNED').forEach((t: any) => {
md += `- [ ] ${t.description} (Est: ${t.timeEstimate})\n`;
if (t.notes) md += ` - Notes: ${t.notes}\n`;
});
md += `\n## Completed Tasks\n`;
report.tasks.filter((t: any) => t.type === 'COMPLETED').forEach((t: any) => {
md += `- [x] ${t.description}\n`;
md += ` - Status: ${t.status}\n`;
if (t.link) md += ` - Work Link: ${t.link}\n`;
});
return md;
}