|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Aaron K. Clark |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +/** |
| 6 | + * PurchaseOrderVendor controller — direct compId scoping via povCompId. |
| 7 | + * Same auth shape as Worker/BillingType/InventoryItem. |
| 8 | + */ |
| 9 | + |
| 10 | +const db = require('../config/db.config.js'); |
| 11 | +const log = require('../config/logger.js'); |
| 12 | +const auth = require('../middleware/auth.js'); |
| 13 | +const PurchaseOrderVendor = db.PurchaseOrderVendor; |
| 14 | + |
| 15 | +const IsMaster = auth.isMaster; |
| 16 | +const GetCompanyId = auth.getCompanyId; |
| 17 | + |
| 18 | +// The schema whitelist already validates fields; we re-state it here so |
| 19 | +// the controller doesn't trust the request body verbatim (mass-assignment |
| 20 | +// defense — a forged povId in the body wouldn't make it through this |
| 21 | +// allowlist even if the schema were bypassed). |
| 22 | +const ALLOWED_FIELDS_CREATE = [ |
| 23 | + 'povName', 'povMailingAddress1', 'povMailingAddress2', 'povMailingCity', |
| 24 | + 'povMailingState', 'povMailingCountry', 'povMailingZip', |
| 25 | + 'povBillingAddress1', 'povBillingAddress2', 'povBillingCity', |
| 26 | + 'povBillingState', 'povBillingCountry', 'povBillingZip', |
| 27 | + 'povPhone', 'povEMail', 'povCompId', |
| 28 | +]; |
| 29 | +const ALLOWED_FIELDS_UPDATE = ALLOWED_FIELDS_CREATE.filter(f => f !== 'povCompId'); |
| 30 | + |
| 31 | +exports.create = async (req, res) => { |
| 32 | + const authKey = req.get('authKey'); |
| 33 | + if (!authKey) { |
| 34 | + return res.status(403).json({ message: "Authorization key not sent." }); |
| 35 | + } |
| 36 | + |
| 37 | + let isAuthKeyMasterKey; |
| 38 | + try { |
| 39 | + isAuthKeyMasterKey = await IsMaster(authKey); |
| 40 | + } catch (error) { |
| 41 | + log.error({ err: error }, 'IsMaster failed'); |
| 42 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 43 | + } |
| 44 | + |
| 45 | + const body = req.body || {}; |
| 46 | + const payload = {}; |
| 47 | + for (const f of ALLOWED_FIELDS_CREATE) { |
| 48 | + if (body[f] !== undefined) payload[f] = body[f]; |
| 49 | + } |
| 50 | + |
| 51 | + if (!isAuthKeyMasterKey) { |
| 52 | + let authKeyCompanyId; |
| 53 | + try { |
| 54 | + authKeyCompanyId = await GetCompanyId(authKey); |
| 55 | + } catch (error) { |
| 56 | + log.error({ err: error }, 'GetCompanyId failed'); |
| 57 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 58 | + } |
| 59 | + if (authKeyCompanyId === -1) { |
| 60 | + return res.status(403).json({ message: "Invalid Authorization Key." }); |
| 61 | + } |
| 62 | + if (payload.povCompId !== undefined && Number(payload.povCompId) !== authKeyCompanyId) { |
| 63 | + return res.status(403).json({ |
| 64 | + message: "Cannot create a PO vendor for a company you do not belong to.", |
| 65 | + }); |
| 66 | + } |
| 67 | + payload.povCompId = authKeyCompanyId; |
| 68 | + } else { |
| 69 | + if (payload.povCompId === undefined || Number(payload.povCompId) <= 0) { |
| 70 | + return res.status(400).json({ |
| 71 | + message: "Master-key requests must specify povCompId.", |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + payload.povArch = false; |
| 77 | + |
| 78 | + try { |
| 79 | + const created = await PurchaseOrderVendor.create(payload); |
| 80 | + return res.status(201).json({ message: "PO vendor created.", purchaseOrderVendor: created }); |
| 81 | + } catch (error) { |
| 82 | + log.error({ err: error }, 'PurchaseOrderVendor.create failed'); |
| 83 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +exports.getById = async (req, res) => { |
| 88 | + const authKey = req.get('authKey'); |
| 89 | + if (!authKey) { |
| 90 | + return res.status(403).json({ message: "Authorization key not sent." }); |
| 91 | + } |
| 92 | + |
| 93 | + let vendor; |
| 94 | + try { |
| 95 | + vendor = await PurchaseOrderVendor.findByPk(req.params.id); |
| 96 | + } catch (error) { |
| 97 | + log.error({ err: error }, 'PurchaseOrderVendor.findByPk failed'); |
| 98 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 99 | + } |
| 100 | + if (!vendor || vendor.povArch) { |
| 101 | + return res.status(404).json({ message: "Not found." }); |
| 102 | + } |
| 103 | + |
| 104 | + const isMaster = await IsMaster(authKey); |
| 105 | + if (!isMaster) { |
| 106 | + const companyId = await GetCompanyId(authKey); |
| 107 | + if (companyId === -1 || vendor.povCompId !== companyId) { |
| 108 | + return res.status(403).json({ message: "Invalid Authorization Key." }); |
| 109 | + } |
| 110 | + } |
| 111 | + return res.status(200).json({ message: "Found.", purchaseOrderVendor: vendor }); |
| 112 | +}; |
| 113 | + |
| 114 | +exports.listByCompany = async (req, res) => { |
| 115 | + const authKey = req.get('authKey'); |
| 116 | + if (!authKey) { |
| 117 | + return res.status(403).json({ message: "Authorization key not sent." }); |
| 118 | + } |
| 119 | + |
| 120 | + const targetCompanyId = Number(req.params.id); |
| 121 | + if (!Number.isInteger(targetCompanyId) || targetCompanyId <= 0) { |
| 122 | + return res.status(400).json({ message: "Invalid company id." }); |
| 123 | + } |
| 124 | + |
| 125 | + const isMaster = await IsMaster(authKey); |
| 126 | + if (!isMaster) { |
| 127 | + const companyId = await GetCompanyId(authKey); |
| 128 | + if (companyId === -1 || companyId !== targetCompanyId) { |
| 129 | + return res.status(403).json({ message: "Invalid Authorization Key." }); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + const requestedLimit = parseInt(req.query.limit, 10); |
| 134 | + const limit = Number.isInteger(requestedLimit) && requestedLimit > 0 |
| 135 | + ? Math.min(requestedLimit, 500) |
| 136 | + : 100; |
| 137 | + const requestedOffset = parseInt(req.query.offset, 10); |
| 138 | + const offset = Number.isInteger(requestedOffset) && requestedOffset >= 0 |
| 139 | + ? requestedOffset |
| 140 | + : 0; |
| 141 | + |
| 142 | + try { |
| 143 | + const { count, rows } = await PurchaseOrderVendor.findAndCountAll({ |
| 144 | + where: { povCompId: targetCompanyId, povArch: false }, |
| 145 | + limit, offset, |
| 146 | + order: [['povId', 'ASC']], |
| 147 | + }); |
| 148 | + return res.status(200).json({ |
| 149 | + message: "Successfully retrieved PO vendors with CompanyId " + targetCompanyId, |
| 150 | + count, limit, offset, purchaseOrderVendors: rows, |
| 151 | + }); |
| 152 | + } catch (error) { |
| 153 | + log.error({ err: error }, 'PurchaseOrderVendor.findAndCountAll failed'); |
| 154 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +exports.update = async (req, res) => { |
| 159 | + const authKey = req.get('authKey'); |
| 160 | + if (!authKey) { |
| 161 | + return res.status(403).json({ message: "Authorization key not sent." }); |
| 162 | + } |
| 163 | + |
| 164 | + let vendor; |
| 165 | + try { |
| 166 | + vendor = await PurchaseOrderVendor.findByPk(req.params.id); |
| 167 | + } catch (error) { |
| 168 | + log.error({ err: error }, 'PurchaseOrderVendor.findByPk failed'); |
| 169 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 170 | + } |
| 171 | + if (!vendor || vendor.povArch) { |
| 172 | + return res.status(404).json({ message: "Not found." }); |
| 173 | + } |
| 174 | + |
| 175 | + const isMaster = await IsMaster(authKey); |
| 176 | + if (!isMaster) { |
| 177 | + const companyId = await GetCompanyId(authKey); |
| 178 | + if (companyId === -1 || vendor.povCompId !== companyId) { |
| 179 | + return res.status(403).json({ message: "Invalid Authorization Key." }); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + const body = req.body || {}; |
| 184 | + const updates = {}; |
| 185 | + for (const f of ALLOWED_FIELDS_UPDATE) { |
| 186 | + if (body[f] !== undefined) updates[f] = body[f]; |
| 187 | + } |
| 188 | + if (Object.keys(updates).length === 0) { |
| 189 | + return res.status(400).json({ message: "No updatable fields supplied." }); |
| 190 | + } |
| 191 | + |
| 192 | + try { |
| 193 | + await vendor.update(updates); |
| 194 | + return res.status(200).json({ message: "Updated.", purchaseOrderVendor: vendor }); |
| 195 | + } catch (error) { |
| 196 | + log.error({ err: error }, 'PurchaseOrderVendor.update failed'); |
| 197 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 198 | + } |
| 199 | +}; |
| 200 | + |
| 201 | +exports.remove = async (req, res) => { |
| 202 | + const authKey = req.get('authKey'); |
| 203 | + if (!authKey) { |
| 204 | + return res.status(403).json({ message: "Authorization key not sent." }); |
| 205 | + } |
| 206 | + |
| 207 | + let vendor; |
| 208 | + try { |
| 209 | + vendor = await PurchaseOrderVendor.findByPk(req.params.id); |
| 210 | + } catch (error) { |
| 211 | + log.error({ err: error }, 'PurchaseOrderVendor.findByPk failed'); |
| 212 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 213 | + } |
| 214 | + if (!vendor || vendor.povArch) { |
| 215 | + return res.status(404).json({ message: "Not found." }); |
| 216 | + } |
| 217 | + |
| 218 | + const isMaster = await IsMaster(authKey); |
| 219 | + if (!isMaster) { |
| 220 | + const companyId = await GetCompanyId(authKey); |
| 221 | + if (companyId === -1 || vendor.povCompId !== companyId) { |
| 222 | + return res.status(403).json({ message: "Invalid Authorization Key." }); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + try { |
| 227 | + await vendor.update({ povArch: true }); |
| 228 | + return res.status(200).json({ message: "Archived.", id: vendor.povId }); |
| 229 | + } catch (error) { |
| 230 | + log.error({ err: error }, 'PurchaseOrderVendor archive failed'); |
| 231 | + return res.status(500).json({ message: "Error!", error: String(error) }); |
| 232 | + } |
| 233 | +}; |
| 234 | + |
| 235 | +exports._internals = { IsMaster, GetCompanyId }; |
0 commit comments