diff options
Diffstat (limited to 'lib/vendor-document-list')
| -rw-r--r-- | lib/vendor-document-list/enhanced-document-service.ts | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/lib/vendor-document-list/enhanced-document-service.ts b/lib/vendor-document-list/enhanced-document-service.ts index 6fe8feb7..b78d0fc3 100644 --- a/lib/vendor-document-list/enhanced-document-service.ts +++ b/lib/vendor-document-list/enhanced-document-service.ts @@ -21,6 +21,8 @@ import type { } from "@/types/enhanced-documents" import { GetVendorShipDcoumentsSchema } from "./validations" import { contracts, users, vendors } from "@/db/schema" +import { getServerSession } from "next-auth/next" +import { authOptions } from "@/app/api/auth/[...nextauth]/route" // 스키마 타입 정의 export interface GetEnhancedDocumentsSchema { @@ -1008,16 +1010,17 @@ export async function getDocumentDetails(documentId: number) { input: GetVendorShipDcoumentsSchema ) { try { - const offset = (input.page - 1) * input.perPage + + const session = await getServerSession(authOptions) + if (!session?.user?.id) { + throw new Error("인증이 필요합니다.") + } - // 1. 사용자의 벤더(회사) ID 조회 - const [user] = await db - .select({ companyId: users.companyId }) - .from(users) - .where(eq(users.id, userId)) - .limit(1) + const companyId = session?.user?.companyId; + + const offset = (input.page - 1) * input.perPage - if (!user?.companyId) { + if (!companyId) { return { data: [], pageCount: 0, total: 0, drawingKind: null, vendorInfo: null } } @@ -1025,7 +1028,7 @@ export async function getDocumentDetails(documentId: number) { const vendorContracts = await db .select({ id: contracts.id }) .from(contracts) - .where(eq(contracts.vendorId, user.companyId)) + .where(eq(contracts.vendorId, companyId)) const contractIds = vendorContracts.map(c => c.id) @@ -1123,14 +1126,16 @@ export async function getDocumentDetails(documentId: number) { */ export async function getUserVendorDocumentStats(userId: number) { try { - // 사용자의 벤더 ID 조회 - const [user] = await db - .select({ companyId: users.companyId }) - .from(users) - .where(eq(users.id, userId)) - .limit(1) - - if (!user?.companyId) { + + const session = await getServerSession(authOptions) + if (!session?.user?.id) { + throw new Error("인증이 필요합니다.") + } + + const companyId = session?.user?.companyId; + + + if (!companyId) { return { stats: {}, totalDocuments: 0, primaryDrawingKind: null } } @@ -1138,7 +1143,7 @@ export async function getDocumentDetails(documentId: number) { const vendorContracts = await db .select({ id: contracts.id }) .from(contracts) - .where(eq(contracts.vendorId, user.companyId)) + .where(eq(contracts.vendorId, companyId)) const contractIds = vendorContracts.map(c => c.id) |
