summaryrefslogtreecommitdiff
path: root/lib/vendor-registration-status
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-registration-status')
-rw-r--r--lib/vendor-registration-status/repository.ts165
-rw-r--r--lib/vendor-registration-status/service.ts260
2 files changed, 0 insertions, 425 deletions
diff --git a/lib/vendor-registration-status/repository.ts b/lib/vendor-registration-status/repository.ts
deleted file mode 100644
index f9c3d63f..00000000
--- a/lib/vendor-registration-status/repository.ts
+++ /dev/null
@@ -1,165 +0,0 @@
-import { db } from "@/db"
-import {
- vendorBusinessContacts,
- vendorAdditionalInfo,
- vendors
-} from "@/db/schema"
-import { eq, and, inArray } from "drizzle-orm"
-
-// 업무담당자 정보 타입
-export interface VendorBusinessContact {
- id: number
- vendorId: number
- contactType: "sales" | "design" | "delivery" | "quality" | "tax_invoice"
- contactName: string
- position: string
- department: string
- responsibility: string
- email: string
- createdAt: Date
- updatedAt: Date
-}
-
-// 추가정보 타입
-export interface VendorAdditionalInfo {
- id: number
- vendorId: number
- businessType?: string
- industryType?: string
- companySize?: string
- revenue?: string
- factoryEstablishedDate?: Date
- preferredContractTerms?: string
- createdAt: Date
- updatedAt: Date
-}
-
-// 업무담당자 정보 조회
-export async function getBusinessContactsByVendorId(vendorId: number): Promise<VendorBusinessContact[]> {
- try {
- return await db
- .select()
- .from(vendorBusinessContacts)
- .where(eq(vendorBusinessContacts.vendorId, vendorId))
- .orderBy(vendorBusinessContacts.contactType)
- } catch (error) {
- console.error("Error fetching business contacts:", error)
- throw new Error("업무담당자 정보를 가져오는 중 오류가 발생했습니다.")
- }
-}
-
-// 업무담당자 정보 저장/업데이트
-export async function upsertBusinessContacts(
- vendorId: number,
- contacts: Omit<VendorBusinessContact, "id" | "vendorId" | "createdAt" | "updatedAt">[]
-): Promise<void> {
- try {
- // 기존 데이터 삭제
- await db
- .delete(vendorBusinessContacts)
- .where(eq(vendorBusinessContacts.vendorId, vendorId))
-
- // 새 데이터 삽입
- if (contacts.length > 0) {
- await db
- .insert(vendorBusinessContacts)
- .values(contacts.map(contact => ({
- ...contact,
- vendorId,
- })))
- }
- } catch (error) {
- console.error("Error upserting business contacts:", error)
- throw new Error("업무담당자 정보 저장 중 오류가 발생했습니다.")
- }
-}
-
-// 추가정보 조회
-export async function getAdditionalInfoByVendorId(vendorId: number): Promise<VendorAdditionalInfo | null> {
- try {
- const result = await db
- .select()
- .from(vendorAdditionalInfo)
- .where(eq(vendorAdditionalInfo.vendorId, vendorId))
- .limit(1)
-
- return result[0] || null
- } catch (error) {
- console.error("Error fetching additional info:", error)
- throw new Error("추가정보를 가져오는 중 오류가 발생했습니다.")
- }
-}
-
-// 추가정보 저장/업데이트
-export async function upsertAdditionalInfo(
- vendorId: number,
- info: Omit<VendorAdditionalInfo, "id" | "vendorId" | "createdAt" | "updatedAt">
-): Promise<void> {
- try {
- const existing = await getAdditionalInfoByVendorId(vendorId)
-
- if (existing) {
- // 업데이트
- await db
- .update(vendorAdditionalInfo)
- .set({
- ...info,
- updatedAt: new Date(),
- })
- .where(eq(vendorAdditionalInfo.vendorId, vendorId))
- } else {
- // 신규 삽입
- await db
- .insert(vendorAdditionalInfo)
- .values({
- ...info,
- vendorId,
- })
- }
- } catch (error) {
- console.error("Error upserting additional info:", error)
- throw new Error("추가정보 저장 중 오류가 발생했습니다.")
- }
-}
-
-// 특정 벤더의 모든 추가정보 조회 (업무담당자 + 추가정보)
-export async function getVendorAllAdditionalData(vendorId: number) {
- try {
- const [businessContacts, additionalInfo] = await Promise.all([
- getBusinessContactsByVendorId(vendorId),
- getAdditionalInfoByVendorId(vendorId)
- ])
-
- return {
- businessContacts,
- additionalInfo
- }
- } catch (error) {
- console.error("Error fetching vendor additional data:", error)
- throw new Error("벤더 추가정보를 가져오는 중 오류가 발생했습니다.")
- }
-}
-
-// 업무담당자 정보 삭제
-export async function deleteBusinessContactsByVendorId(vendorId: number): Promise<void> {
- try {
- await db
- .delete(vendorBusinessContacts)
- .where(eq(vendorBusinessContacts.vendorId, vendorId))
- } catch (error) {
- console.error("Error deleting business contacts:", error)
- throw new Error("업무담당자 정보 삭제 중 오류가 발생했습니다.")
- }
-}
-
-// 추가정보 삭제
-export async function deleteAdditionalInfoByVendorId(vendorId: number): Promise<void> {
- try {
- await db
- .delete(vendorAdditionalInfo)
- .where(eq(vendorAdditionalInfo.vendorId, vendorId))
- } catch (error) {
- console.error("Error deleting additional info:", error)
- throw new Error("추가정보 삭제 중 오류가 발생했습니다.")
- }
-}
diff --git a/lib/vendor-registration-status/service.ts b/lib/vendor-registration-status/service.ts
deleted file mode 100644
index 97503a13..00000000
--- a/lib/vendor-registration-status/service.ts
+++ /dev/null
@@ -1,260 +0,0 @@
-import { revalidateTag, unstable_cache } from "next/cache"
-import {
- getBusinessContactsByVendorId,
- upsertBusinessContacts,
- getAdditionalInfoByVendorId,
- upsertAdditionalInfo,
- getVendorAllAdditionalData,
- deleteBusinessContactsByVendorId,
- deleteAdditionalInfoByVendorId,
- type VendorBusinessContact,
- type VendorAdditionalInfo
-} from "./repository"
-
-// 업무담당자 정보 조회
-export async function fetchBusinessContacts(vendorId: number) {
- return unstable_cache(
- async () => {
- try {
- const contacts = await getBusinessContactsByVendorId(vendorId)
- return {
- success: true,
- data: contacts,
- }
- } catch (error) {
- console.error("Error in fetchBusinessContacts:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "업무담당자 정보를 가져오는 중 오류가 발생했습니다.",
- }
- }
- },
- [`business-contacts-${vendorId}`],
- {
- revalidate: 300, // 5분 캐시
- tags: ["business-contacts", `vendor-${vendorId}`],
- }
- )()
-}
-
-// 업무담당자 정보 저장
-export async function saveBusinessContacts(
- vendorId: number,
- contacts: Omit<VendorBusinessContact, "id" | "vendorId" | "createdAt" | "updatedAt">[]
-) {
- try {
- await upsertBusinessContacts(vendorId, contacts)
-
- // 캐시 무효화
- revalidateTag("business-contacts")
- revalidateTag(`vendor-${vendorId}`)
-
- return {
- success: true,
- message: "업무담당자 정보가 저장되었습니다.",
- }
- } catch (error) {
- console.error("Error in saveBusinessContacts:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "업무담당자 정보 저장 중 오류가 발생했습니다.",
- }
- }
-}
-
-// 추가정보 조회
-export async function fetchAdditionalInfo(vendorId: number) {
- return unstable_cache(
- async () => {
- try {
- const additionalInfo = await getAdditionalInfoByVendorId(vendorId)
- return {
- success: true,
- data: additionalInfo,
- }
- } catch (error) {
- console.error("Error in fetchAdditionalInfo:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "추가정보를 가져오는 중 오류가 발생했습니다.",
- }
- }
- },
- [`additional-info-${vendorId}`],
- {
- revalidate: 300, // 5분 캐시
- tags: ["additional-info", `vendor-${vendorId}`],
- }
- )()
-}
-
-// 추가정보 저장
-export async function saveAdditionalInfo(
- vendorId: number,
- info: Omit<VendorAdditionalInfo, "id" | "vendorId" | "createdAt" | "updatedAt">
-) {
- try {
- await upsertAdditionalInfo(vendorId, info)
-
- // 캐시 무효화
- revalidateTag("additional-info")
- revalidateTag(`vendor-${vendorId}`)
-
- return {
- success: true,
- message: "추가정보가 저장되었습니다.",
- }
- } catch (error) {
- console.error("Error in saveAdditionalInfo:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "추가정보 저장 중 오류가 발생했습니다.",
- }
- }
-}
-
-// 모든 추가정보 조회 (업무담당자 + 추가정보)
-export async function fetchAllAdditionalData(vendorId: number) {
- return unstable_cache(
- async () => {
- try {
- const data = await getVendorAllAdditionalData(vendorId)
- return {
- success: true,
- data,
- }
- } catch (error) {
- console.error("Error in fetchAllAdditionalData:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "벤더 추가정보를 가져오는 중 오류가 발생했습니다.",
- }
- }
- },
- [`all-additional-data-${vendorId}`],
- {
- revalidate: 300, // 5분 캐시
- tags: ["business-contacts", "additional-info", `vendor-${vendorId}`],
- }
- )()
-}
-
-// 업무담당자 + 추가정보 한 번에 저장
-export async function saveAllAdditionalData(
- vendorId: number,
- data: {
- businessContacts: Omit<VendorBusinessContact, "id" | "vendorId" | "createdAt" | "updatedAt">[]
- additionalInfo: Omit<VendorAdditionalInfo, "id" | "vendorId" | "createdAt" | "updatedAt">
- }
-) {
- try {
- // 두 작업을 순차적으로 실행
- await Promise.all([
- upsertBusinessContacts(vendorId, data.businessContacts),
- upsertAdditionalInfo(vendorId, data.additionalInfo)
- ])
-
- // 캐시 무효화
- revalidateTag("business-contacts")
- revalidateTag("additional-info")
- revalidateTag(`vendor-${vendorId}`)
-
- return {
- success: true,
- message: "모든 추가정보가 저장되었습니다.",
- }
- } catch (error) {
- console.error("Error in saveAllAdditionalData:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "추가정보 저장 중 오류가 발생했습니다.",
- }
- }
-}
-
-// 업무담당자 정보 삭제
-export async function removeBusinessContacts(vendorId: number) {
- try {
- await deleteBusinessContactsByVendorId(vendorId)
-
- // 캐시 무효화
- revalidateTag("business-contacts")
- revalidateTag(`vendor-${vendorId}`)
-
- return {
- success: true,
- message: "업무담당자 정보가 삭제되었습니다.",
- }
- } catch (error) {
- console.error("Error in removeBusinessContacts:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "업무담당자 정보 삭제 중 오류가 발생했습니다.",
- }
- }
-}
-
-// 추가정보 삭제
-export async function removeAdditionalInfo(vendorId: number) {
- try {
- await deleteAdditionalInfoByVendorId(vendorId)
-
- // 캐시 무효화
- revalidateTag("additional-info")
- revalidateTag(`vendor-${vendorId}`)
-
- return {
- success: true,
- message: "추가정보가 삭제되었습니다.",
- }
- } catch (error) {
- console.error("Error in removeAdditionalInfo:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "추가정보 삭제 중 오류가 발생했습니다.",
- }
- }
-}
-
-// 입력 완성도 체크
-export async function checkAdditionalDataCompletion(vendorId: number) {
- try {
- const result = await fetchAllAdditionalData(vendorId)
-
- if (!result.success || !result.data) {
- return {
- success: false,
- error: "추가정보를 확인할 수 없습니다.",
- }
- }
-
- const { businessContacts, additionalInfo } = result.data
-
- // 필수 업무담당자 5개 타입이 모두 입력되었는지 체크
- const requiredContactTypes = ["sales", "design", "delivery", "quality", "tax_invoice"]
- const existingContactTypes = businessContacts.map(contact => contact.contactType)
- const missingContactTypes = requiredContactTypes.filter(type => !existingContactTypes.includes(type))
-
- // 업무담당자 완성도
- const businessContactsComplete = missingContactTypes.length === 0
-
- // 추가정보 완성도 (선택사항이므로 존재 여부만 체크)
- const additionalInfoExists = !!additionalInfo
-
- return {
- success: true,
- data: {
- businessContactsComplete,
- missingContactTypes,
- additionalInfoExists,
- totalCompletion: businessContactsComplete && additionalInfoExists
- }
- }
- } catch (error) {
- console.error("Error in checkAdditionalDataCompletion:", error)
- return {
- success: false,
- error: error instanceof Error ? error.message : "완성도 확인 중 오류가 발생했습니다.",
- }
- }
-}