diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2026-02-15 11:18:35 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2026-02-15 11:18:35 +0700 |
| commit | 2af2659018d08cd8685a6e727994ab76f0735189 (patch) | |
| tree | 5d1327d8269c9e2f625ec809ac31beb4fb4b08e4 /src/app/api/product/import/route.tsx | |
| parent | d131b3cc79148b9f72a9bfe2fab3a0c6b3dd092d (diff) | |
<Miqdad> Locatorlocator
Diffstat (limited to 'src/app/api/product/import/route.tsx')
| -rw-r--r-- | src/app/api/product/import/route.tsx | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/app/api/product/import/route.tsx b/src/app/api/product/import/route.tsx index 37b4690..f96c2d9 100644 --- a/src/app/api/product/import/route.tsx +++ b/src/app/api/product/import/route.tsx @@ -16,18 +16,49 @@ export async function POST(request: NextRequest) { const fileData: any[] = XLSX.utils.sheet_to_json(worksheet, { header: 1 }) fileData.shift(); - const newProducts = fileData.map(row => ({ - id: undefined, - isDifferent: false, - name: row[0]?.toString() || '', // Handle undefined values - barcode: row[1]?.toString() || '', // Handle undefined values - itemCode: row[2]?.toString() || '', // Handle undefined values - onhandQty: row[3] || 0, // Handle undefined values - differenceQty: row[4] || 0, // Handle undefined values - companyId: intCompanyId, // Use the parsed company ID - externalId: row[6] ? row[6].toString() : null, // Handle undefined values - value: row[7] != null ? Number(row[7]) || 0 : null, - })); + // Fetch all locations for this company for location validation + const locations = await prisma.location.findMany({ + where: { companyId: intCompanyId } + }) + + // Create a map for quick location lookup by name (case-insensitive) and by id + const locationByName = new Map( + locations.map(loc => [loc.name.toLowerCase(), loc.id]) + ) + const locationById = new Map( + locations.map(loc => [loc.id.toString(), loc.id]) + ) + + const newProducts = fileData.map(row => { + let locationId: number | null = null + + // Parse locationId from column 8 + const locationValue = row[8]?.toString().trim() + + if (locationValue) { + // Try to match by ID first, then by name + const idAsNumber = parseInt(locationValue) + if (!isNaN(idAsNumber) && locationById.has(idAsNumber.toString())) { + locationId = idAsNumber + } else if (locationByName.has(locationValue.toLowerCase())) { + locationId = locationByName.get(locationValue.toLowerCase()) || null + } + } + + return { + id: undefined, + isDifferent: false, + name: row[0]?.toString() || '', // Handle undefined values + barcode: row[1]?.toString() || '', // Handle undefined values + itemCode: row[2]?.toString() || '', // Handle undefined values + onhandQty: row[3] || 0, // Handle undefined values + differenceQty: row[4] || 0, // Handle undefined values + companyId: intCompanyId, // Use the parsed company ID + externalId: row[6] ? row[6].toString() : null, // Handle undefined values + value: row[7] != null ? Number(row[7]) || 0 : null, + locationId: locationId, // Set locationId or null if not found/provided + } + }); const whereCompany = { companyId: intCompanyId } |
