Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 174 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
"@prisma/adapter-pg": "^7.8.0",
"@prisma/client": "^7.4.0",
"@stellar/stellar-sdk": "^14.5.0",
"@types/pdfkit": "^0.17.6",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"helmet": "^8.1.0",
"jsonwebtoken": "^9.0.3",
"nodemailer": "^9.0.1",
"pdfkit": "^0.19.1",
"pg": "^8.14.1",
"reflect-metadata": "^0.2.2",
"resend": "^6.14.0",
Expand Down
55 changes: 55 additions & 0 deletions src/controllers/invoice.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Request, Response } from 'express';
import {
createInvoice,
getInvoice,
getInvoiceWithMerchant,
listInvoices,
voidInvoice,
} from '../services/invoice.services.js';
import { parseInvoiceListQuery, validateCreateInvoice } from '../utils/invoice.validation.js';
import { AppError } from '../utils/errors.js';
import { generateInvoicePdf } from '../services/invoice-pdf.services.js';
import { sendInvoiceEmail } from '../services/email.service.js';

export const createInvoiceController = async (req: Request, res: Response): Promise<void> => {
const merchant = req.merchant;
Expand Down Expand Up @@ -93,3 +96,55 @@ export const voidInvoiceController = async (req: Request, res: Response): Promis
res.status(500).json({ error: 'Internal Server Error' });
}
};

export const getInvoicePdfController = async (req: Request, res: Response): Promise<void> => {
const merchant = req.merchant;
if (!merchant) {
res.status(401).json({ error: 'Unauthorized' });
return;
}

try {
const invoice = await getInvoiceWithMerchant(merchant.id, req.params.id);
const pdf = await generateInvoicePdf(invoice, invoice.merchant);

res.setHeader('Content-Type', 'application/pdf');
res.setHeader(
'Content-Disposition',
`attachment; filename="invoice-${invoice.paymentSlug}.pdf"`,
);
res.status(200).send(pdf);
} catch (error) {
if (error instanceof AppError) {
res.status(error.statusCode).json({ error: error.message });
return;
}
res.status(500).json({ error: 'Internal Server Error' });
}
};

export const sendInvoiceController = async (req: Request, res: Response): Promise<void> => {
const merchant = req.merchant;
if (!merchant) {
res.status(401).json({ error: 'Unauthorized' });
return;
}

try {
const invoice = await getInvoiceWithMerchant(merchant.id, req.params.id);

if (!invoice.email) {
res.status(400).json({ error: 'Invoice has no email on file' });
return;
}

await sendInvoiceEmail(invoice, invoice.merchant);
res.status(200).json({ message: 'Invoice email sent' });
} catch (error) {
if (error instanceof AppError) {
res.status(error.statusCode).json({ error: error.message });
return;
}
res.status(500).json({ error: 'Internal Server Error' });
}
};
Loading
Loading