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