Merge pull request 'multi-file update' (#14) from claude/suspicious-wilson into master

Reviewed-on: #14
This commit was merged in pull request #14.
This commit is contained in:
2026-03-13 11:15:35 -05:00
6 changed files with 52 additions and 13 deletions

View File

@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(npx prisma migrate dev --name add-drive-file-id)",
"Bash(npx prisma db push)"
]
}
}

BIN
dev.db

Binary file not shown.

View File

@@ -66,6 +66,7 @@ model Report {
date DateTime @default(now()) date DateTime @default(now())
managerName String managerName String
status ReportStatus @default(IN_PROGRESS) status ReportStatus @default(IN_PROGRESS)
driveFileId String?
userId String userId String
user User @relation(fields: [userId], references: [id]) user User @relation(fields: [userId], references: [id])
tasks Task[] tasks Task[]

View File

@@ -5,7 +5,7 @@ export const runtime = "nodejs";
import { getServerSession } from "next-auth/next"; import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth"; import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { uploadToDrive, generateReportMarkdown } from "@/lib/google-drive"; import { uploadToDrive, updateDriveFile, generateReportMarkdown } from "@/lib/google-drive";
export async function POST( export async function POST(
req: Request, req: Request,
@@ -46,17 +46,23 @@ export async function POST(
}); });
try { try {
const driveFile = await uploadToDrive( let driveFile;
account.access_token,
fileName, if (report.driveFileId) {
markdown, // Update the existing Drive file in place
folderSetting?.value 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 },
});
}
// Update report status to SUBMITTED
await prisma.report.update({ await prisma.report.update({
where: { id }, where: { id },
data: { status: 'SUBMITTED' } data: { status: 'SUBMITTED' },
}); });
return NextResponse.json({ success: true, link: driveFile.webViewLink }); return NextResponse.json({ success: true, link: driveFile.webViewLink });

View File

@@ -316,10 +316,10 @@ export default function ReportForm() {
<footer className="pt-8 border-t border-white/10 flex justify-end gap-4"> <footer className="pt-8 border-t border-white/10 flex justify-end gap-4">
<button <button
onClick={exportToDrive} onClick={exportToDrive}
disabled={saving || report.status === 'SUBMITTED'} disabled={saving}
className="btn-primary flex items-center gap-2 px-8" className="btn-primary flex items-center gap-2 px-8"
> >
{saving ? "Processing..." : (report.status === 'SUBMITTED' ? "Already Submitted" : "Finalize & Export to Drive")} {saving ? "Processing..." : (report.status === 'SUBMITTED' ? "Re-export to Drive" : "Finalize & Export to Drive")}
<Send size={18} /> <Send size={18} />
</button> </button>
</footer> </footer>

View File

@@ -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) { export function generateReportMarkdown(report: any) {
let md = `# WFH Daily Report - ${new Date(report.date).toLocaleDateString()}\n`; let md = `# WFH Daily Report - ${new Date(report.date).toLocaleDateString()}\n`;
md += `**Employee:** ${report.user.name}\n`; md += `**Employee:** ${report.user.name}\n`;