(request: NextRequest)
| 16 | } |
| 17 | |
| 18 | export async function POST(request: NextRequest) { |
| 19 | try { |
| 20 | const res = await request.json(); |
| 21 | const pcNumber = res.pc; |
| 22 | const partExists = await prisma.parts.findUnique({ |
| 23 | where: { |
| 24 | productCode: pcNumber, |
| 25 | |
| 26 | } |
| 27 | }); |
| 28 | if(partExists){ |
| 29 | const partUpdate = await prisma.parts.update({ |
| 30 | where: { |
| 31 | id: partExists.id, |
| 32 | }, |
| 33 | data: { |
| 34 | quantity: res.quantity + partExists.quantity, |
| 35 | } |
| 36 | }); |
| 37 | if (partUpdate) { |
| 38 | return NextResponse.json({ status: 200, body: partUpdate, message: "Part updated"}); |
| 39 | } |
| 40 | else { |
| 41 | return NextResponse.json({ status: 500, error: "Part not updated" }); |
| 42 | } |
| 43 | } else { |
| 44 | const LSCSPart = await fetch( |
| 45 | "https://wmsc.lcsc.com/wmsc/product/detail?productCode=" + pcNumber |
| 46 | ) |
| 47 | .then((response) => { |
| 48 | return response.json(); |
| 49 | }) |
| 50 | .catch((e: ErrorCallback | any) => { |
| 51 | console.error(e.message); |
| 52 | }); |
| 53 | const partInfo = extractPartInfoFromLCSCResponse(LSCSPart); |
| 54 | |
| 55 | const partCreate = await prisma.parts.create({ |
| 56 | data: { |
| 57 | title: partInfo.title, |
| 58 | quantity: res.quantity, |
| 59 | productId: partInfo.productId, |
| 60 | productCode: partInfo.productCode, |
| 61 | productModel: partInfo.productModel, |
| 62 | productDescription: partInfo.productDescription, |
| 63 | parentCatalogName: partInfo.parentCatalogName, |
| 64 | catalogName: partInfo.catalogName, |
| 65 | brandName: partInfo.brandName, |
| 66 | encapStandard: partInfo.encapStandard, |
| 67 | productImages: partInfo.productImages, |
| 68 | pdfLink: partInfo.pdfLink, |
| 69 | productLink: partInfo.productLink, |
| 70 | prices: partInfo.prices, |
| 71 | voltage: partInfo.voltage, |
| 72 | resistance: partInfo.resistance, |
| 73 | power: partInfo.power, |
| 74 | current: partInfo.current, |
| 75 | tolerance: partInfo.tolerance, |
nothing calls this directly
no test coverage detected