summaryrefslogtreecommitdiff
path: root/lib/tech-vendors/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tech-vendors/service.ts')
-rw-r--r--lib/tech-vendors/service.ts131
1 files changed, 113 insertions, 18 deletions
diff --git a/lib/tech-vendors/service.ts b/lib/tech-vendors/service.ts
index 72f8632d..940e59ce 100644
--- a/lib/tech-vendors/service.ts
+++ b/lib/tech-vendors/service.ts
@@ -40,6 +40,7 @@ import { sql } from "drizzle-orm";
import { decryptWithServerAction } from "@/components/drm/drmUtils";
import { deleteFile, saveDRMFile } from "../file-stroage";
import { techSalesContactPossibleItems } from "@/db/schema";
+import { normalizeEmailFields } from "./utils";
/* -----------------------------------------------------
1) 조회 관련
@@ -563,24 +564,6 @@ export async function updateTechVendorContact(input: UpdateTechVendorContactSche
}
}
-export async function deleteTechVendorContact(contactId: number, vendorId: number) {
- unstable_noStore();
- try {
- const [deletedContact] = await db
- .delete(techVendorContacts)
- .where(eq(techVendorContacts.id, contactId))
- .returning();
-
- // 캐시 무효화
- revalidateTag(`tech-vendor-contacts-${contactId}`);
- revalidateTag(`tech-vendor-contacts-${vendorId}`);
-
- return { data: deletedContact, error: null };
- } catch (err) {
- return { data: null, error: getErrorMessage(err) };
- }
-}
-
/* -----------------------------------------------------
5) 아이템 관리
----------------------------------------------------- */
@@ -4116,4 +4099,116 @@ export async function generateContactPossibleItemsErrorExcel(errors: Array<{
return new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
})
+}
+
+/**
+ * 기술영업 벤더 담당자 삭제 (cascade 방식)
+ * 1. tech_sales_contact_possible_items (해당 담당자의 아이템 매핑)
+ * 2. tech_vendor_contacts (해당 담당자)
+ */
+export async function deleteTechVendorContact(contactId: number, vendorId: number) {
+ unstable_noStore()
+
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 1. 해당 담당자의 아이템 매핑 삭제 (tech_sales_contact_possible_items)
+ await tx
+ .delete(techSalesContactPossibleItems)
+ .where(eq(techSalesContactPossibleItems.contactId, contactId))
+
+ // 2. 담당자 삭제 (tech_vendor_contacts)
+ const deletedContact = await tx
+ .delete(techVendorContacts)
+ .where(eq(techVendorContacts.id, contactId))
+ .returning()
+
+ return deletedContact
+ })
+
+ // 캐시 무효화
+ revalidateTag(`tech-vendor-contacts-${vendorId}`)
+ revalidateTag("tech-vendors")
+
+ return {
+ success: true,
+ data: result,
+ error: null
+ }
+ } catch (err) {
+ console.error("담당자 삭제 오류:", err)
+ return {
+ success: false,
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
+}
+
+/**
+ * 기술영업 벤더 삭제 (cascade 방식)
+ * 1. tech_sales_contact_possible_items (담당자별 아이템)
+ * 2. tech_vendor_possible_items (벤더 아이템)
+ * 3. tech_vendor_contacts (벤더 담당자)
+ * 4. tech_vendor_attachments (벤더 첨부파일)
+ * 5. tech_vendors (벤더)
+ */
+// 임시 삭제 함수
+export async function deleteTechVendor(vendorId: number) {
+ unstable_noStore()
+
+ try {
+ const result = await db.transaction(async (tx) => {
+ // 1. 해당 벤더의 담당자별 아이템 삭제 (tech_sales_contact_possible_items)
+ await tx
+ .delete(techSalesContactPossibleItems)
+ .where(
+ inArray(
+ techSalesContactPossibleItems.contactId,
+ tx
+ .select({ contactId: techVendorContacts.id })
+ .from(techVendorContacts)
+ .where(eq(techVendorContacts.vendorId, vendorId))
+ )
+ )
+
+ // 2. 해당 벤더의 아이템 삭제 (tech_vendor_possible_items)
+ await tx
+ .delete(techVendorPossibleItems)
+ .where(eq(techVendorPossibleItems.vendorId, vendorId))
+
+ // 3. 해당 벤더의 담당자 삭제 (tech_vendor_contacts)
+ await tx
+ .delete(techVendorContacts)
+ .where(eq(techVendorContacts.vendorId, vendorId))
+
+ // 4. 해당 벤더의 첨부파일 삭제 (tech_vendor_attachments)
+ await tx
+ .delete(techVendorAttachments)
+ .where(eq(techVendorAttachments.vendorId, vendorId))
+
+ // 5. 벤더 삭제 (tech_vendors)
+ const deletedVendor = await tx
+ .delete(techVendors)
+ .where(eq(techVendors.id, vendorId))
+ .returning()
+
+ return deletedVendor
+ })
+
+ // 캐시 무효화
+ revalidateTag("tech-vendors")
+
+ return {
+ success: true,
+ data: result,
+ error: null
+ }
+ } catch (err) {
+ console.error("벤더 삭제 오류:", err)
+ return {
+ success: false,
+ data: null,
+ error: getErrorMessage(err)
+ }
+ }
} \ No newline at end of file