UNPKG

powr-sdk-api

Version:

Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.

135 lines (134 loc) 3.25 kB
"use strict"; const express = require('express'); const router = express.Router(); const { getDb } = require("../services/mongo"); const { ObjectId } = require("mongodb"); router.get('/', async (req, res) => { const projectId = req.projectId; try { const query = { projectId }; const db = await getDb(); const invoiceData = await db.collection("invoices").find(query).toArray(); return res.status(200).json({ success: true, invoices: invoiceData }); } catch (error) { console.error("Error retrieving invoices :", error); return res.status(500).json({ success: false, message: "Failed to retrieve invoices ." }); } }); router.get('/:invoiceId', async (req, res) => { const { invoiceId } = req.params; const projectId = req.projectId; try { const _id = new ObjectId(invoiceId); const db = await getDb(); const invoiceData = await db.collection("invoices").findOne({ _id, projectId }); return res.status(200).json({ success: true, invoice: invoiceData }); } catch (error) { console.error("Error retrieving invoice :", error); return res.status(500).json({ success: false, message: "Failed to retrieve invoice." }); } }); router.post('/', async (req, res) => { const projectId = req.projectId; const { invoiceNumber, dateOfIssue, paymentStatus, customerInfo, serviceSection, paymentSection } = req.body; const invoice = { invoiceNumber, dateOfIssue, paymentStatus, customerInfo, serviceSection, paymentSection, createdAt: new Date() }; invoice.projectId = projectId; try { const db = await getDb(); const result = await db.collection("invoices").insertOne(invoice); if (result && result.insertedId) { return res.status(201).json({ success: true, message: "Invoice created successfully.", invoiceId: result.insertedId, invoice }); } else { return res.status(500).json({ success: false, message: "Invoice could not be added." }); } } catch (error) { console.error("Error adding invoice:", error); return res.status(500).json({ success: false, message: "Failed to add invoice due to a server error." }); } }); router.put('/:invoiceId', async (req, res) => { const { invoiceId } = req.params; const projectId = req.projectId; let filter; try { filter = { _id: new ObjectId(invoiceId), projectId: projectId }; } catch (e) { return res.status(400).json({ success: false, message: "Invalid invoiceId format." }); } const updateData = req.body; try { const db = await getDb(); const result = await db.collection("invoices").updateOne(filter, { $set: updateData }); return res.status(200).json({ success: true, message: "Invoice updated successfully.", result: result }); } catch (error) { console.error("Error updating invoice:", error); return res.status(500).json({ success: false, message: "Failed to update invoice due to a server error." }); } }); module.exports = router;