diff options
Diffstat (limited to 'lib/vendors/service.ts')
| -rw-r--r-- | lib/vendors/service.ts | 91 |
1 files changed, 46 insertions, 45 deletions
diff --git a/lib/vendors/service.ts b/lib/vendors/service.ts index 5d790a6e..0c8254f2 100644 --- a/lib/vendors/service.ts +++ b/lib/vendors/service.ts @@ -2,7 +2,7 @@ import { revalidatePath, revalidateTag, unstable_noStore } from "next/cache"; import db from "@/db/db"; -import { vendorAttachments, VendorContact, vendorContacts, vendorDetailView, vendorItemsView, vendorMaterialsView, vendorPossibleItems, vendorPossibleMateirals, vendors, vendorsWithTypesView, vendorTypes, type Vendor } from "@/db/schema"; +import { vendorAttachments, VendorContact, vendorContacts, vendorDetailView, vendorItemsView, vendorMaterialsView, vendorPossibleItems, vendorPossibleMaterials, vendors, vendorsWithTypesView, vendorsWithTypesAndMaterialsView, vendorTypes, type Vendor } from "@/db/schema"; import logger from '@/lib/logger'; import * as z from "zod" import crypto from 'crypto'; @@ -17,8 +17,6 @@ import { unstable_cache } from "@/lib/unstable-cache"; import { getErrorMessage } from "@/lib/handle-error"; import { - selectVendors, - countVendors, insertVendor, updateVendor, updateVendors, groupByStatus, @@ -32,8 +30,8 @@ import { insertVendorItem, countRfqHistory, selectRfqHistory, - selectVendorsWithTypes, - countVendorsWithTypes, + selectVendorsWithTypesAndMaterials, + countVendorsWithTypesAndMaterials, countVendorMaterials, selectVendorMaterials, insertVendorMaterial, @@ -41,7 +39,6 @@ import { } from "./repository"; import type { - CreateVendorSchema, UpdateVendorSchema, GetVendorsSchema, GetVendorContactsSchema, @@ -52,7 +49,7 @@ import type { GetVendorMaterialsSchema, } from "./validations"; -import { asc, desc, ilike, inArray, and, or, gte, lte, eq, isNull, count, sql } from "drizzle-orm"; +import { asc, desc, ilike, inArray, and, or, eq, isNull, sql } from "drizzle-orm"; import { rfqItems, rfqs, vendorRfqView } from "@/db/schema/rfq"; import { sendEmail } from "../mail/sendEmail"; import { PgTransaction } from "drizzle-orm/pg-core"; @@ -60,12 +57,11 @@ import { items, materials } from "@/db/schema/items"; import { mfaTokens, roles, userRoles, users } from "@/db/schema/users"; import { getServerSession } from "next-auth"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; -import { contracts, contractsDetailView, projects, vendorPQSubmissions, vendorsLogs } from "@/db/schema"; -import { deleteFile, saveFile, saveBuffer } from "../file-stroage"; +import { contractsDetailView, projects, vendorPQSubmissions, vendorsLogs } from "@/db/schema"; +import { deleteFile, saveFile } from "../file-stroage"; import { basicContractTemplates } from "@/db/schema/basicContractDocumnet"; import { basicContract } from "@/db/schema/basicContractDocumnet"; import { headers } from 'next/headers'; -import { Router } from "lucide-react"; /* ----------------------------------------------------- 1) 조회 관련 ----------------------------------------------------- */ @@ -81,9 +77,9 @@ export async function getVendors(input: GetVendorsSchema) { try { const offset = (input.page - 1) * input.perPage; - // 1) 고급 필터 - vendors 대신 vendorsWithTypesView 사용 + // 1) 고급 필터 - 새로운 확장 뷰 사용 const advancedWhere = filterColumns({ - table: vendorsWithTypesView, + table: vendorsWithTypesAndMaterialsView, filters: input.filters, joinOperator: input.joinOperator, }); @@ -93,28 +89,32 @@ export async function getVendors(input: GetVendorsSchema) { if (input.search) { const s = `%${input.search}%`; globalWhere = or( - ilike(vendorsWithTypesView.vendorName, s), - ilike(vendorsWithTypesView.vendorCode, s), - ilike(vendorsWithTypesView.email, s), - ilike(vendorsWithTypesView.status, s), + ilike(vendorsWithTypesAndMaterialsView.vendorName, s), + ilike(vendorsWithTypesAndMaterialsView.vendorCode, s), + ilike(vendorsWithTypesAndMaterialsView.email, s), + ilike(vendorsWithTypesAndMaterialsView.status, s), // 추가: 업체 유형 검색 - ilike(vendorsWithTypesView.vendorTypeName, s) + ilike(vendorsWithTypesAndMaterialsView.vendorTypeName, s), + // 대표품목 검색 추가 + ilike(vendorsWithTypesAndMaterialsView.primaryMaterial1, s), + ilike(vendorsWithTypesAndMaterialsView.primaryMaterial2, s), + ilike(vendorsWithTypesAndMaterialsView.primaryMaterial3, s) ); } // 최종 where 결합 const finalWhere = and(advancedWhere, globalWhere); - // 간단 검색 (advancedTable=false) 시 예시 - const simpleWhere = and( - input.vendorName - ? ilike(vendorsWithTypesView.vendorName, `%${input.vendorName}%`) - : undefined, - input.status ? ilike(vendorsWithTypesView.status, input.status) : undefined, - input.country - ? ilike(vendorsWithTypesView.country, `%${input.country}%`) - : undefined - ); + // 간단 검색 (advancedTable=false) 시 예시 - 현재 미사용 + // const simpleWhere = and( + // input.vendorName + // ? ilike(vendorsWithTypesAndMaterialsView.vendorName, `%${input.vendorName}%`) + // : undefined, + // input.status ? ilike(vendorsWithTypesAndMaterialsView.status, input.status) : undefined, + // input.country + // ? ilike(vendorsWithTypesAndMaterialsView.country, `%${input.country}%`) + // : undefined + // ); // 실제 사용될 where const where = finalWhere; @@ -123,14 +123,14 @@ export async function getVendors(input: GetVendorsSchema) { const orderBy = input.sort.length > 0 ? input.sort.map((item) => - item.desc ? desc(vendorsWithTypesView[item.id]) : asc(vendorsWithTypesView[item.id]) + item.desc ? desc(vendorsWithTypesAndMaterialsView[item.id]) : asc(vendorsWithTypesAndMaterialsView[item.id]) ) - : [asc(vendorsWithTypesView.createdAt)]; + : [asc(vendorsWithTypesAndMaterialsView.createdAt)]; // 트랜잭션 내에서 데이터 조회 const { data, total } = await db.transaction(async (tx) => { - // 1) vendor 목록 조회 (view 사용) - const vendorsData = await selectVendorsWithTypes(tx, { + // 1) vendor 목록 조회 (새로운 확장 뷰 사용) + const vendorsData = await selectVendorsWithTypesAndMaterials(tx, { where, orderBy, offset, @@ -158,7 +158,7 @@ export async function getVendors(input: GetVendorsSchema) { ); // 3) 전체 개수 - const total = await countVendorsWithTypes(tx, where); + const total = await countVendorsWithTypesAndMaterials(tx, where); return { data: vendorsWithAttachments, total }; }); @@ -452,6 +452,7 @@ export async function modifyVendor( creditRating: input.creditRating, cashFlowRating: input.cashFlowRating, status: input.status, + isAssociationMember: input.isAssociationMember, }); // 3. 상태가 변경되었다면 로그 기록 @@ -1035,12 +1036,12 @@ export async function getMaterialsForVendor(vendorId: number) { }) .from(materials) .leftJoin( - vendorPossibleMateirals, - eq(materials.itemCode, vendorPossibleMateirals.itemCode) + vendorPossibleMaterials, + eq(materials.itemCode, vendorPossibleMaterials.itemCode) ) // vendorPossibleItems.vendorId가 이 vendorId인 행이 없는(즉 아직 등록되지 않은) 아이템만 .where( - isNull(vendorPossibleMateirals.id) // 또는 isNull(vendorPossibleItems.itemCode) + isNull(vendorPossibleMaterials.id) // 또는 isNull(vendorPossibleItems.itemCode) ) .orderBy(asc(materials.itemName)) @@ -1109,11 +1110,11 @@ export async function deleteVendorMaterial( }) await db - .delete(vendorPossibleMateirals) + .delete(vendorPossibleMaterials) .where( and( - eq(vendorPossibleMateirals.itemCode, validatedData.itemCode), - eq(vendorPossibleMateirals.vendorId, validatedData.vendorId) + eq(vendorPossibleMaterials.itemCode, validatedData.itemCode), + eq(vendorPossibleMaterials.vendorId, validatedData.vendorId) ) ) @@ -1148,16 +1149,16 @@ export async function updateVendorMaterial( await db.transaction(async (tx) => { // 기존 아이템 삭제 await tx - .delete(vendorPossibleMateirals) + .delete(vendorPossibleMaterials) .where( and( - eq(vendorPossibleMateirals.itemCode, validatedData.oldItemCode), - eq(vendorPossibleMateirals.vendorId, validatedData.vendorId) + eq(vendorPossibleMaterials.itemCode, validatedData.oldItemCode), + eq(vendorPossibleMaterials.vendorId, validatedData.vendorId) ) ) // 새 아이템 추가 - await tx.insert(vendorPossibleMateirals).values({ + await tx.insert(vendorPossibleMaterials).values({ vendorId: validatedData.vendorId, itemCode: validatedData.newItemCode, }) @@ -1186,11 +1187,11 @@ export async function removeVendorMaterials(input: { const validatedData = removeVendormaterialsSchema.parse(input) await db - .delete(vendorPossibleMateirals) + .delete(vendorPossibleMaterials) .where( and( - inArray(vendorPossibleMateirals.itemCode, validatedData.itemCodes), - eq(vendorPossibleMateirals.vendorId, validatedData.vendorId) + inArray(vendorPossibleMaterials.itemCode, validatedData.itemCodes), + eq(vendorPossibleMaterials.vendorId, validatedData.vendorId) ) ) |
