diff --git a/src/app/api/reports/[id]/export/route.ts b/src/app/api/reports/[id]/export/route.ts index 59ef02e..e374e47 100644 --- a/src/app/api/reports/[id]/export/route.ts +++ b/src/app/api/reports/[id]/export/route.ts @@ -5,7 +5,7 @@ export const runtime = "nodejs"; import { getServerSession } from "next-auth/next"; import { authOptions } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; -import { uploadToDrive, updateDriveFile, generateReportMarkdown, getGoogleAuth } from "@/lib/google-drive"; +import { uploadToDrive, updateDriveFile, generateReportHTML, getGoogleAuth } from "@/lib/google-drive"; export async function POST( req: Request, @@ -35,7 +35,7 @@ export async function POST( return NextResponse.json({ error: "Report not found" }, { status: 404 }); } - const markdown = generateReportMarkdown(report); + const htmlContent = generateReportHTML(report); const fileName = `WFH_Report_${new Date(report.date).toISOString().split('T')[0]}_${report.user.name}`; // Fetch designated folder ID from settings @@ -48,10 +48,10 @@ export async function POST( if (report.driveFileId) { // Update the existing Drive file in place - driveFile = await updateDriveFile(auth, report.driveFileId, markdown); + driveFile = await updateDriveFile(auth, report.driveFileId, htmlContent); } else { // First export — create a new Drive file and store its ID - driveFile = await uploadToDrive(auth, fileName, markdown, folderSetting?.value); + driveFile = await uploadToDrive(auth, fileName, htmlContent, folderSetting?.value); await prisma.report.update({ where: { id }, data: { driveFileId: driveFile.id }, diff --git a/src/lib/google-drive.ts b/src/lib/google-drive.ts index 1809992..c66466b 100644 --- a/src/lib/google-drive.ts +++ b/src/lib/google-drive.ts @@ -63,7 +63,7 @@ export async function uploadToDrive(auth: any, fileName: string, content: string } const media = { - mimeType: 'text/markdown', + mimeType: 'text/html', body: Readable.from([content]), }; @@ -84,7 +84,7 @@ export async function updateDriveFile(auth: any, fileId: string, content: string const drive = google.drive({ version: 'v3', auth }); const media = { - mimeType: 'text/markdown', + mimeType: 'text/html', body: Readable.from([content]), }; @@ -101,23 +101,66 @@ export async function updateDriveFile(auth: any, fileId: string, content: string } } -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`; +export function generateReportHTML(report: any) { + const dateObj = new Date(report.date); + const dateStr = dateObj.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); + const plannedTasks = report.tasks.filter((t: any) => t.type === 'PLANNED'); + const completedTasks = report.tasks.filter((t: any) => t.type === 'COMPLETED'); - 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`; - }); + const cellStyle = "padding: 10px; border-bottom: 1px solid #e2e8f0; font-family: Arial, sans-serif; font-size: 11pt;"; + const headerStyle = "padding: 12px 10px; background-color: #f1f5f9; border-bottom: 2px solid #cbd5e1; font-family: Arial, sans-serif; font-size: 11pt; font-weight: bold; text-align: left; color: #334155;"; - 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 ` + +
+Date: ${dateStr}
+Employee: ${report.user.name}
+Manager: ${report.managerName || 'N/A'}
+| Description | +Estimate | +Notes | +
|---|---|---|
| ${t.description} | +${t.timeEstimate || '-'} | +${t.notes || '-'} | +
No planned tasks for today.
`} + +| Description | +Status | +Work Link | +
|---|---|---|
| ${t.description} | +${t.status || 'Done'} | +${t.link ? `${t.link}` : '-'} | +
No completed tasks reported today.
`} + +