This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { renderToStream } from "@ceereals/vue-pdf";
|
||||
import { db } from "../../../../db";
|
||||
import { invoices, invoiceItems, clients, organizations, members } from "../../../../db/schema";
|
||||
import {
|
||||
invoices,
|
||||
invoiceItems,
|
||||
clients,
|
||||
organizations,
|
||||
members,
|
||||
} from "../../../../db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { createInvoiceDocument } from "../../../../pdf/generateInvoicePDF";
|
||||
|
||||
@@ -17,11 +23,12 @@ export const GET: APIRoute = async ({ params, locals }) => {
|
||||
}
|
||||
|
||||
// Fetch invoice with related data
|
||||
const invoiceResult = await db.select({
|
||||
invoice: invoices,
|
||||
client: clients,
|
||||
organization: organizations,
|
||||
})
|
||||
const invoiceResult = await db
|
||||
.select({
|
||||
invoice: invoices,
|
||||
client: clients,
|
||||
organization: organizations,
|
||||
})
|
||||
.from(invoices)
|
||||
.leftJoin(clients, eq(invoices.clientId, clients.id))
|
||||
.innerJoin(organizations, eq(invoices.organizationId, organizations.id))
|
||||
@@ -35,12 +42,15 @@ export const GET: APIRoute = async ({ params, locals }) => {
|
||||
const { invoice, client, organization } = invoiceResult;
|
||||
|
||||
// Verify membership
|
||||
const membership = await db.select()
|
||||
const membership = await db
|
||||
.select()
|
||||
.from(members)
|
||||
.where(and(
|
||||
eq(members.userId, user.id),
|
||||
eq(members.organizationId, invoice.organizationId)
|
||||
))
|
||||
.where(
|
||||
and(
|
||||
eq(members.userId, user.id),
|
||||
eq(members.organizationId, invoice.organizationId),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
if (!membership) {
|
||||
@@ -48,7 +58,8 @@ export const GET: APIRoute = async ({ params, locals }) => {
|
||||
}
|
||||
|
||||
// Fetch items
|
||||
const items = await db.select()
|
||||
const items = await db
|
||||
.select()
|
||||
.from(invoiceItems)
|
||||
.where(eq(invoiceItems.invoiceId, invoice.id))
|
||||
.all();
|
||||
@@ -82,12 +93,18 @@ export const GET: APIRoute = async ({ params, locals }) => {
|
||||
zip: organization.zip || null,
|
||||
country: organization.country || null,
|
||||
logoUrl: organization.logoUrl || null,
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const stream = await renderToStream(document);
|
||||
const chunks: Uint8Array[] = [];
|
||||
|
||||
return new Response(stream, {
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(chunk as Uint8Array);
|
||||
}
|
||||
const buffer = Buffer.concat(chunks);
|
||||
|
||||
return new Response(buffer, {
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${invoice.number}.pdf"`,
|
||||
|
||||
Reference in New Issue
Block a user