multi-file update

This commit is contained in:
jason
2026-03-13 11:13:45 -05:00
parent 5e3ca19c83
commit b2df27cfc5
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())
managerName String
status ReportStatus @default(IN_PROGRESS)
driveFileId String?
userId String
user User @relation(fields: [userId], references: [id])
tasks Task[]

View File

@@ -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
);
let driveFile;
// Update report status to SUBMITTED
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: { status: 'SUBMITTED' }
data: { driveFileId: driveFile.id },
});
}
await prisma.report.update({
where: { id },
data: { status: 'SUBMITTED' },
});
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">
<button
onClick={exportToDrive}
disabled={saving || report.status === 'SUBMITTED'}
disabled={saving}
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} />
</button>
</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) {
let md = `# WFH Daily Report - ${new Date(report.date).toLocaleDateString()}\n`;
md += `**Employee:** ${report.user.name}\n`;