summaryrefslogtreecommitdiff
path: root/pages/api
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-03-28 06:12:09 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-03-28 06:12:09 +0000
commitf2c29e0023200b58d5e5c7a4c677a3c473c9bb8e (patch)
treef6fcfb6e5dcddceab7529d7ca5fb0a4b44bd8798 /pages/api
parentf3e640b666c57f133c229d2742fb214c586d21e4 (diff)
parentdbfc752cbbae6bded6040f4727ec7564d2a3b759 (diff)
기만프로 작업내역 머지
Diffstat (limited to 'pages/api')
-rw-r--r--pages/api/pdftron/createVendorDataReports.ts78
-rw-r--r--pages/api/po/sendDocuSign.ts8
-rw-r--r--pages/api/po/webhook.ts29
3 files changed, 82 insertions, 33 deletions
diff --git a/pages/api/pdftron/createVendorDataReports.ts b/pages/api/pdftron/createVendorDataReports.ts
index 47f6055d..f461a7fb 100644
--- a/pages/api/pdftron/createVendorDataReports.ts
+++ b/pages/api/pdftron/createVendorDataReports.ts
@@ -1,5 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
+import type { File as FormidableFile } from "formidable";
import formidable from "formidable";
+import fs from "fs/promises";
+import { createReport } from "@/lib/pdftron/serverSDK/createReport";
export const config = {
api: {
@@ -15,22 +18,61 @@ export default async function handler(
return res.status(405).end();
}
- const form = formidable({ multiples: true });
-
- form.parse(req, async (err, fields, files) => {
- if (err) {
- console.error(err);
- return res.status(500).json({ error: "Error parsing form" });
- }
-
- try {
- const additionalData = JSON.parse((fields?.additionalData ?? "") as string);
- console.log("📦 additionalData:", additionalData);
- console.log("📎 files:", files.files); // files.files는 array or single file
-
- return res.status(200).json({ success: true });
- } catch (e) {
- return res.status(400).json({ error: "Invalid additionalData" });
- }
- });
+ try {
+ const form = formidable({ multiples: false });
+
+ form.parse(req, async (err, fields, files) => {
+ if (err) {
+ console.error(err);
+ return res.status(500).json({ error: "Error parsing form" });
+ }
+
+ try {
+ const fileName = fields?.customFileName?.[0] ?? "";
+ const reportDatas = JSON.parse(fields?.reportDatas?.[0] ?? "[]") as {
+ [key: string]: any;
+ }[];
+ const reportTempPath = fields?.reportTempPath?.[0] ?? "";
+ const reportCoverPage: FormidableFile | undefined = files?.file?.[0];
+
+ if (
+ !reportCoverPage ||
+ fileName.length === 0 ||
+ reportDatas.length === 0 ||
+ reportTempPath.length === 0
+ ) {
+ return res.status(400).json({ error: "Invalid Report Data" });
+ }
+
+ const buffer = await fs.readFile(reportCoverPage.filepath);
+
+ const {
+ result,
+ buffer: pdfBuffer,
+ error,
+ } = await createReport(buffer, reportTempPath, reportDatas);
+
+ if (result && pdfBuffer) {
+ res.setHeader("Content-Type", "application/pdf");
+ res.setHeader(
+ "Content-Disposition",
+ `attachment; filename="${fileName}"`
+ );
+
+ return res.send(Buffer.from(pdfBuffer));
+ }
+
+ return res.status(200).json({
+ success: false,
+ message: "Report 생성에 실패하였습니다.",
+ error,
+ });
+ } catch (e) {
+ console.log(e);
+ return res.status(400).json({ error: "Invalid additionalData" });
+ }
+ });
+ } catch (err) {
+ return res.status(401).end();
+ }
}
diff --git a/pages/api/po/sendDocuSign.ts b/pages/api/po/sendDocuSign.ts
index ccb83733..eb218c2c 100644
--- a/pages/api/po/sendDocuSign.ts
+++ b/pages/api/po/sendDocuSign.ts
@@ -5,7 +5,7 @@ export const config = {
};
import type { NextApiRequest, NextApiResponse } from "next";
-import { requestContractSign } from "@/lib/docuSign/docuSignFns";
+import { requestContractSign, getRecipients } from "@/lib/docuSign/docuSignFns";
export default async function handler(
req: NextApiRequest,
@@ -36,11 +36,13 @@ export default async function handler(
const { result, envelopeId, error } = docuSignStart;
- res.status(200).json({
+ const sendResult = {
success: result,
envelopeId,
message: error?.message,
- });
+ };
+
+ res.status(200).json(sendResult);
} catch (error: any) {
res
.status(500)
diff --git a/pages/api/po/webhook.ts b/pages/api/po/webhook.ts
index e61246a2..39a6931e 100644
--- a/pages/api/po/webhook.ts
+++ b/pages/api/po/webhook.ts
@@ -3,15 +3,11 @@ export const config = {
bodyParser: true, // ✅ 이게 false면 안 됨!
},
};
-
import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs";
import * as z from "zod";
import db from "@/db/db";
-import { GetPOSchema } from "@/lib/po/validations";
-import { unstable_cache } from "@/lib/unstable-cache";
-import { filterColumns } from "@/lib/filter-columns";
import {
asc,
desc,
@@ -25,18 +21,15 @@ import {
eq,
count,
} from "drizzle-orm";
-import { countPos, selectPos } from "@/lib/po/repository";
import {
contractEnvelopes,
- contractsDetailView,
contractSigners,
contracts,
} from "@/db/schema/contract";
-import { vendors, vendorContacts } from "@/db/schema/vendors";
-import dayjs from "dayjs";
-
-import { POContent } from "@/lib/docuSign/types";
-import { downloadContractFile, getRecipients } from "@/lib/docuSign/docuSignFns";
+import {
+ downloadContractFile,
+ getRecipients,
+} from "@/lib/docuSign/docuSignFns";
export default async function handler(
req: NextApiRequest,
@@ -108,7 +101,7 @@ export default async function handler(
await tx
.update(contracts)
- .set({ status: `$FAILED_${safeRole}_SEND_MAIL(${message})` })
+ .set({ status: `FAILED_${safeRole}_SEND_MAIL(${message})` })
.where(eq(contracts.id, contractId));
await tx
@@ -154,6 +147,10 @@ export default async function handler(
.where(eq(dbSchma.envelopeId, envelopeId))
.limit(1);
+ if (!targetContract) {
+ continue;
+ }
+
const { id, contractId } = targetContract;
if (contractId === null || contractId === undefined) {
@@ -210,6 +207,10 @@ export default async function handler(
.where(eq(dbSchma.envelopeId, envelopeId))
.limit(1);
+ if (!targetContract) {
+ continue;
+ }
+
const { id, contractId, fileName, filePath } = targetContract;
if (contractId === null || contractId === undefined) {
@@ -277,6 +278,10 @@ export default async function handler(
.where(eq(dbSchma.envelopeId, envelopeId))
.limit(1);
+ if (!targetContract) {
+ continue;
+ }
+
const { contractId, fileName, filePath } = targetContract;
if (contractId === null || contractId === undefined) {