const express = require('express'); const router = express.Router(); const sql = require('mssql'); const moment = require('moment'); const { getUser } = require('../util.js'); router.get('/', async function(req, res, next) { res.setHeader('Content-Type', 'text/html'); /** Make connection and validate **/ let pool = false; let customerRequest = false; let orderStatement = false; let orderProductStatement = false; let content; loadedResources: try { let productList = false; if (req.session.productList && req.session.productList.length > 0) { productList = req.session.productList.filter(p => p != null); } /** Determine if valid customer id was entered Determine if there are products in the shopping cart If either are not true, display an error message **/ if (!productList) { content = `
You do not currently have any items in your cart.
`; break loadedResources; } if (!/^-?\d+$/.test(req.query.customerId)) { content = `The given ID does not match any known customer.
`; break loadedResources; } pool = await sql.connect(dbConfig); customerRequest = new sql.PreparedStatement(pool); customerRequest.input('id', sql.Int); await customerRequest.prepare(` select customerId, password from customer where customerId = @id `); const customerResults = await customerRequest.execute({id: req.query.customerId}); if (customerResults.recordset.length == 0) { content = `The given ID does not match any known customer.
`; break loadedResources; } const customer = customerResults.recordset[0]; if (customer.password != req.query.password) { content = `Id or password did not match!
`; break loadedResources; } /** Save order information to database**/ /** // Use retrieval of auto-generated keys. sqlQuery = "INSERT INTOProduct Name | Price | Quantity |
---|---|---|
${row.name} | $${Number(row.price).toFixed(2)} | ${row.quantity} |
Order Total: $${Number(totalAmount).toFixed(2)}
Tracking Number: ${orderId}
`; } catch (err) { console.error(err); content = `${err.toString()}
`; } finally { if (customerRequest) customerRequest.unprepare(); if (orderStatement) orderStatement.unprepare(); if (orderProductStatement) orderProductStatement.unprepare(); if (pool) pool.close(); } /** Print out order summary **/ /** Clear session/cart **/ req.session.productList = []; res.render('layouts/main', { title: 'Order Complete', loggedIn: true, user: getUser(req), spacer: true, content: `