diff options
Diffstat (limited to 'lib/forms')
| -rw-r--r-- | lib/forms/services.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/forms/services.ts b/lib/forms/services.ts index ff21626c..22f10466 100644 --- a/lib/forms/services.ts +++ b/lib/forms/services.ts @@ -1,6 +1,7 @@ // lib/forms/services.ts "use server"; +import { headers } from "next/headers"; import path from "path"; import fs from "fs/promises"; import { v4 as uuidv4 } from "uuid"; @@ -805,3 +806,63 @@ export async function uploadReportTemp( }); } } + +export const getOrigin = async (): Promise<string> => { + const headersList = await headers(); + const host = headersList.get("host"); + const proto = headersList.get("x-forwarded-proto") || "http"; // 기본값은 http + const origin = `${proto}://${host}`; + + return origin; +}; + +export const getReportTempFileData = async (): Promise<{ + fileName: string; + fileType: string; + base64: string; +}> => { + const fileName = "sample_template_file.docx"; + + const tempFile = await fs.readFile( + `public/vendorFormReportSample/${fileName}` + ); + + return { + fileName, + fileType: + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + base64: tempFile.toString("base64"), + }; +}; + +type deleteReportTempFile = (id: number) => Promise<{ + result: boolean; + error?: any; +}>; + +export const deleteReportTempFile: deleteReportTempFile = async (id) => { + try { + return db.transaction(async (tx) => { + const [targetTempFile] = await tx + .select() + .from(vendorDataReportTemps) + .where(eq(vendorDataReportTemps.id, id)); + + if (!targetTempFile) { + throw new Error("해당 Template File을 찾을 수 없습니다."); + } + + await tx + .delete(vendorDataReportTemps) + .where(eq(vendorDataReportTemps.id, id)); + + const { filePath } = targetTempFile; + + await fs.unlink("public" + filePath); + + return { result: true }; + }); + } catch (err) { + return { result: false, error: (err as Error).message }; + } +}; |
