diff options
Diffstat (limited to 'lib/forms')
| -rw-r--r-- | lib/forms/services.ts | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/lib/forms/services.ts b/lib/forms/services.ts index a1bbf003..22f10466 100644 --- a/lib/forms/services.ts +++ b/lib/forms/services.ts @@ -807,7 +807,7 @@ export async function uploadReportTemp( } } -export const getOrigin = async ():Promise<string> => { +export const getOrigin = async (): Promise<string> => { const headersList = await headers(); const host = headersList.get("host"); const proto = headersList.get("x-forwarded-proto") || "http"; // 기본값은 http @@ -815,3 +815,54 @@ export const getOrigin = async ():Promise<string> => { 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 }; + } +}; |
