summaryrefslogtreecommitdiff
path: root/pages/api/po/createContractFile.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-03-26 00:37:41 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-03-26 00:37:41 +0000
commite0dfb55c5457aec489fc084c4567e791b4c65eb1 (patch)
tree68543a65d88f5afb3a0202925804103daa91bc6f /pages/api/po/createContractFile.ts
3/25 까지의 대표님 작업사항
Diffstat (limited to 'pages/api/po/createContractFile.ts')
-rw-r--r--pages/api/po/createContractFile.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/pages/api/po/createContractFile.ts b/pages/api/po/createContractFile.ts
new file mode 100644
index 00000000..b74c95f2
--- /dev/null
+++ b/pages/api/po/createContractFile.ts
@@ -0,0 +1,57 @@
+export const config = {
+ api: {
+ bodyParser: true, // ✅ 이게 false면 안 됨!
+ },
+};
+
+import type { NextApiRequest, NextApiResponse } from "next";
+import { downloadContractFile } from "@/lib/docuSign/docuSignFns";
+import path from "path";
+import fs from "fs";
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method !== "POST") {
+ return res.status(405).end();
+ }
+
+ try {
+ const { envelopeId, fileName, filePath } = req.body;
+
+ if (!envelopeId) {
+ return res
+ .status(500)
+ .json({ success: false, message: "Not use envelopeId" });
+ }
+
+ const docuSignStart = await downloadContractFile(envelopeId);
+
+ const { result, buffer, error } = docuSignStart;
+
+ if (result && buffer) {
+ const cleanedPath = filePath.replace(/^\/+/, "");
+ const dirPath = path.resolve(process.cwd(), "public", cleanedPath); // 예: 'contracts/185/signatures'
+ const fullFilePath = path.join(dirPath, fileName); // 예: 'contracts/185/signatures/xxx.pdf'
+
+ if (!fs.existsSync(dirPath)) {
+ fs.mkdirSync(dirPath, { recursive: true });
+ }
+
+ fs.writeFileSync(fullFilePath, buffer);
+ return res.status(200).json({
+ success: result,
+ });
+ }
+
+ return res.status(200).json({
+ success: result,
+ message: error?.message,
+ });
+ } catch (error: any) {
+ res
+ .status(500)
+ .json({ success: false, message: error?.message || "Unknown error" });
+ }
+}