feat: setup the invoice backend

This commit is contained in:
Nathnael
2026-06-29 13:53:32 +00:00
parent f7f0f6aef3
commit 69955bc0e6
5 changed files with 168 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
import {
Body,
Controller,
Get,
Param,
ParseUUIDPipe,
Post,
} from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";
import { CurrentUser } from "@edr/api-common";
import {
type AuthUserPayload,
resolveAuthUserId,
} from "../../common/resolve-auth-user-id";
import { BillingService } from "./billing.service";
import { PayInvoiceDto } from "./dto/pay-invoice.dto";
/**
* Customer-facing billing endpoints. Unlike {@link BillingController} (admin,
* org-wide), every route here is force-scoped to the signed-in customer's
* company — they only ever see and pay their own invoices.
*/
@ApiTags("billing")
@ApiBearerAuth()
@Controller("billing")
export class PortalBillingController {
constructor(private readonly billingService: BillingService) {}
@Get("my-invoices")
@ApiOperation({ summary: "List the signed-in customer's invoices" })
findMine(@CurrentUser() user: AuthUserPayload) {
return this.billingService.findForUser(resolveAuthUserId(user));
}
@Get("my-invoices/:id")
@ApiOperation({ summary: "Get one of the customer's invoices (+ line items)" })
findMineById(
@Param("id", ParseUUIDPipe) id: string,
@CurrentUser() user: AuthUserPayload,
) {
return this.billingService.findByIdForUser(id, resolveAuthUserId(user));
}
@Post("my-invoices/:id/pay")
@ApiOperation({ summary: "Initiate payment for one of the customer's invoices" })
pay(
@Param("id", ParseUUIDPipe) id: string,
@CurrentUser() user: AuthUserPayload,
@Body() dto: PayInvoiceDto,
) {
return this.billingService.payInvoiceForUser(id, resolveAuthUserId(user), {
method: dto.method,
platform: dto.platform ?? "web",
payerAccount: dto.payerAccount,
returnUrl: dto.returnUrl,
failureUrl: dto.failureUrl,
});
}
}