From b2df27cfc5f5fc99878a186da7660b8d564207e4 Mon Sep 17 00:00:00 2001 From: jason Date: Fri, 13 Mar 2026 11:13:45 -0500 Subject: [PATCH] multi-file update --- .claude/settings.local.json | 8 +++++++ dev.db | Bin 81920 -> 81920 bytes prisma/schema.prisma | 1 + src/app/api/reports/[id]/export/route.ts | 26 ++++++++++++++--------- src/components/ReportForm.tsx | 6 +++--- src/lib/google-drive.ts | 24 +++++++++++++++++++++ 6 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 .claude/settings.local.json diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..6444053 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "Bash(npx prisma migrate dev --name add-drive-file-id)", + "Bash(npx prisma db push)" + ] + } +} diff --git a/dev.db b/dev.db index 8125432e5d83fb299015be8934d8e9bf1acf8115..d502be4173fe03bad376aff500f03d042c810e89 100644 GIT binary patch delta 121 zcmZo@U~On%oggj9$H2fK0>m)DH&Mq}mXATNs(_bcKZ7u100Zw8{*yd~yi2*ucs#g{ zG6qbnxx&?)#lbEvDaqKbzxgCb2&1rqQc6)~S*lxRPO4{$l0t}UM95?ruA0rZ%uTE; S{683PHXr8Lewc&N)*Jvq{T}-O delta 97 zcmZo@U~On%oggj9%fP@O0>m)DJ5k42mX|@Vs(_dO2ZJ!PEd%cr{*yd~yi2*ucs#g{ ua_pa2b7ixFz+1-6w#-eeT+Lw|?BbG=jP2~3qZmUNn`ODS%W^S3HU$9R_ZHg# diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f56fe04..db74fb9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -66,6 +66,7 @@ model Report { date DateTime @default(now()) managerName String status ReportStatus @default(IN_PROGRESS) + driveFileId String? userId String user User @relation(fields: [userId], references: [id]) tasks Task[] diff --git a/src/app/api/reports/[id]/export/route.ts b/src/app/api/reports/[id]/export/route.ts index 227f6d4..390799e 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, generateReportMarkdown } from "@/lib/google-drive"; +import { uploadToDrive, updateDriveFile, generateReportMarkdown } from "@/lib/google-drive"; export async function POST( req: Request, @@ -46,17 +46,23 @@ export async function POST( }); try { - const driveFile = await uploadToDrive( - account.access_token, - fileName, - markdown, - folderSetting?.value - ); - - // Update report status to SUBMITTED + let driveFile; + + if (report.driveFileId) { + // Update the existing Drive file in place + driveFile = await updateDriveFile(account.access_token, report.driveFileId, markdown); + } else { + // First export — create a new Drive file and store its ID + driveFile = await uploadToDrive(account.access_token, fileName, markdown, folderSetting?.value); + await prisma.report.update({ + where: { id }, + data: { driveFileId: driveFile.id }, + }); + } + await prisma.report.update({ where: { id }, - data: { status: 'SUBMITTED' } + data: { status: 'SUBMITTED' }, }); return NextResponse.json({ success: true, link: driveFile.webViewLink }); diff --git a/src/components/ReportForm.tsx b/src/components/ReportForm.tsx index c435af5..4106715 100644 --- a/src/components/ReportForm.tsx +++ b/src/components/ReportForm.tsx @@ -314,12 +314,12 @@ export default function ReportForm() { diff --git a/src/lib/google-drive.ts b/src/lib/google-drive.ts index 0bc7252..a0b9175 100644 --- a/src/lib/google-drive.ts +++ b/src/lib/google-drive.ts @@ -34,6 +34,30 @@ export async function uploadToDrive(accessToken: string, fileName: string, conte } } +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; + } +} + export function generateReportMarkdown(report: any) { let md = `# WFH Daily Report - ${new Date(report.date).toLocaleDateString()}\n`; md += `**Employee:** ${report.user.name}\n`;