diff options
Diffstat (limited to 'lib/vendor-investigation/service.ts')
| -rw-r--r-- | lib/vendor-investigation/service.ts | 164 |
1 files changed, 77 insertions, 87 deletions
diff --git a/lib/vendor-investigation/service.ts b/lib/vendor-investigation/service.ts index c365a7ad..39984661 100644 --- a/lib/vendor-investigation/service.ts +++ b/lib/vendor-investigation/service.ts @@ -20,94 +20,84 @@ import { decryptWithServerAction } from "@/components/drm/drmUtils"; import { format, addDays } from "date-fns"; export async function getVendorsInvestigation(input: GetVendorsInvestigationSchema) { - return unstable_cache( - async () => { - try { - const offset = (input.page - 1) * input.perPage - - // 1) Advanced filters - const advancedWhere = filterColumns({ - table: vendorInvestigationsView, - filters: input.filters, - joinOperator: input.joinOperator, - }) - - // 2) Global search - let globalWhere - if (input.search) { - const s = `%${input.search}%` - globalWhere = or( - // 협력업체 정보 - ilike(vendorInvestigationsView.vendorName, s), - ilike(vendorInvestigationsView.vendorCode, s), - - // 담당자 정보 (새로 추가) - ilike(vendorInvestigationsView.requesterName, s), - ilike(vendorInvestigationsView.qmManagerName, s), - - // 실사 정보 - ilike(vendorInvestigationsView.investigationNotes, s), - ilike(vendorInvestigationsView.investigationStatus, s), - ilike(vendorInvestigationsView.investigationAddress, s), - ilike(vendorInvestigationsView.investigationMethod, s), - - // 평가 결과 - ilike(vendorInvestigationsView.evaluationResult, s) - ) - } - // 3) Combine finalWhere - const finalWhere = and( - advancedWhere, - globalWhere - ) - - // 4) Sorting - const orderBy = - input.sort && input.sort.length > 0 - ? input.sort.map((item) => - item.desc - ? desc(vendorInvestigationsView[item.id]) - : asc(vendorInvestigationsView[item.id]) - ) - : [desc(vendorInvestigationsView.createdAt)] - - // 5) Query & count - const { data, total } = await db.transaction(async (tx) => { - // a) Select from the view - const investigationsData = await tx - .select() - .from(vendorInvestigationsView) - .where(finalWhere) - .orderBy(...orderBy) - .offset(offset) - .limit(input.perPage) - - // b) Count total - const resCount = await tx - .select({ count: count() }) - .from(vendorInvestigationsView) - .where(finalWhere) - - return { data: investigationsData, total: resCount[0]?.count } - }) - - // 6) Calculate pageCount - const pageCount = Math.ceil(total / input.perPage) - - // Data is already in the correct format from the simplified view - return { data, pageCount } - } catch (err) { - console.error(err) - return { data: [], pageCount: 0 } - } - }, - // Cache key - [JSON.stringify(input)], - { - revalidate: 3600, - tags: ["vendor-investigations"], + try { + const offset = (input.page - 1) * input.perPage + + // 1) Advanced filters + const advancedWhere = filterColumns({ + table: vendorInvestigationsView, + filters: input.filters, + joinOperator: input.joinOperator, + }) + + // 2) Global search + let globalWhere + if (input.search) { + const s = `%${input.search}%` + globalWhere = or( + // 협력업체 정보 + ilike(vendorInvestigationsView.vendorName, s), + ilike(vendorInvestigationsView.vendorCode, s), + + // 담당자 정보 (새로 추가) + ilike(vendorInvestigationsView.requesterName, s), + ilike(vendorInvestigationsView.qmManagerName, s), + + // 실사 정보 + ilike(vendorInvestigationsView.investigationNotes, s), + ilike(vendorInvestigationsView.investigationStatus, s), + ilike(vendorInvestigationsView.investigationAddress, s), + ilike(vendorInvestigationsView.investigationMethod, s), + + // 평가 결과 + ilike(vendorInvestigationsView.evaluationResult, s) + ) } - )() + // 3) Combine finalWhere + const finalWhere = and( + advancedWhere, + globalWhere + ) + + // 4) Sorting + const orderBy = + input.sort && input.sort.length > 0 + ? input.sort.map((item) => + item.desc + ? desc(vendorInvestigationsView[item.id]) + : asc(vendorInvestigationsView[item.id]) + ) + : [desc(vendorInvestigationsView.createdAt)] + + // 5) Query & count + const { data, total } = await db.transaction(async (tx) => { + // a) Select from the view + const investigationsData = await tx + .select() + .from(vendorInvestigationsView) + .where(finalWhere) + .orderBy(...orderBy) + .offset(offset) + .limit(input.perPage) + + // b) Count total + const resCount = await tx + .select({ count: count() }) + .from(vendorInvestigationsView) + .where(finalWhere) + + return { data: investigationsData, total: resCount[0]?.count } + }) + + // 6) Calculate pageCount + const pageCount = Math.ceil(total / input.perPage) + + // Data is already in the correct format from the simplified view + return { data, pageCount } + } catch (err) { + console.error(err) + return { data: [], pageCount: 0 } + } } /** * Get existing investigations for a list of vendor IDs |
