const express = require('express'); const router = express.Router(); const sql = require('mssql'); const moment = require('moment'); const { htmlPage } = require('../util.js'); router.get('/', async function(req, res, next) { res.setHeader('Content-Type', 'text/html'); // TO/DO: Get order id const orderId = req.query.orderId; let pool; let validateIdStatement; let fetchItemsStatement; let createShipmentStatement; let checkInventoryStatement; let updateInventoryStatement; let orderSummaryHTML; let tx; let failed = false; try { pool = await sql.connect(dbConfig); // TODO: Check if valid order id validateIdStatement = new sql.PreparedStatement(pool); validateIdStatement.input('orderId', sql.Int) await validateIdStatement.prepare(`select * from ordersummary where orderId = @orderId`); const results = await validateIdStatement.execute({orderId}); if (results.recordset.length === 0) { throw new Error("The order you're looking for doesn't exist!"); } const order = results.recordset[0]; // TO/DO: Start a transaction tx = new sql.Transaction(pool); await tx.begin(); // TO/DO: Retrieve all items in order with given id let items; fetchItemsStatement = new sql.PreparedStatement(tx); fetchItemsStatement.input('orderId', sql.Int); await fetchItemsStatement.prepare(` select * from orderproduct where orderId = @orderId `); items = (await fetchItemsStatement.execute({orderId: order.orderId})).recordset; await fetchItemsStatement.unprepare(); fetchItemsStatement = false; orderSummaryHTML = `
Tracking number: ${shipmentResults.recordset[0].shipmentId}
Shipment unsuccessful. Insufficient inventory for product ID: ${err.productId}