Skip to content
Open
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
29 changes: 29 additions & 0 deletions src/controllers/invoice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,35 @@ export function createInvoiceController(invoiceService: InvoiceService) {
}
},

async getInvoiceAnalytics(
req: Request & { params: { id: string } },
res: Response,
next: NextFunction,
): Promise<void> {
try {
const authReq = req as AuthenticatedRequest;
if (!authReq.user) {
throw new HttpError(401, "Authentication required");
}

const { id } = req.params;

const result = await invoiceService.getInvoiceAnalytics(id, authReq.user.id);

res.status(200).json({
success: true,
data: result,
});
} catch (error) {
if (error instanceof ServiceError) {
next(new HttpError(error.statusCode, error.message));
return;
}

next(error);
}
},

async calculateTerms(
req: Request,
res: Response,
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/1711000000000-AddMarketplaceIndexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddMarketplaceIndexes1711000000000
implements MigrationInterface
{
name = "AddMarketplaceIndexes1711000000000";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE INDEX "IDX_INVOICES_STATUS_CREATED"
ON "invoices" ("status", "created_at" DESC);
`);

await queryRunner.query(`
CREATE INDEX "IDX_INVOICES_STATUS_AMOUNT"
ON "invoices" ("status", "amount" DESC);
`);

await queryRunner.query(`
CREATE INDEX "IDX_INVESTMENTS_INVESTOR_STATUS"
ON "investments" ("investor_id", "status");
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_INVESTMENTS_INVESTOR_STATUS"`);
await queryRunner.query(`DROP INDEX "IDX_INVOICES_STATUS_AMOUNT"`);
await queryRunner.query(`DROP INDEX "IDX_INVOICES_STATUS_CREATED"`);
}
}
22 changes: 22 additions & 0 deletions src/migrations/1711000000001-AddOptimisticLockingVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddOptimisticLockingVersion1711000000001
implements MigrationInterface
{
name = "AddOptimisticLockingVersion1711000000001";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "invoices" ADD COLUMN "version" integer NOT NULL DEFAULT 1;
`);

await queryRunner.query(`
ALTER TABLE "investments" ADD COLUMN "version" integer NOT NULL DEFAULT 1;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "investments" DROP COLUMN "version"`);
await queryRunner.query(`ALTER TABLE "invoices" DROP COLUMN "version"`);
}
}
4 changes: 4 additions & 0 deletions src/models/Investment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OneToMany,
JoinColumn,
Index,
VersionColumn,
} from "typeorm";
import { InvestmentStatus } from "../types/enums";
import type { User } from "./User.model";
Expand Down Expand Up @@ -59,6 +60,9 @@ export class Investment {
@DeleteDateColumn({ name: "deleted_at" })
deletedAt!: Date | null;

@VersionColumn()
version!: number;

@ManyToOne("Invoice", "investments", { onDelete: "CASCADE" })
@JoinColumn({ name: "invoice_id" })
invoice!: Invoice;
Expand Down
4 changes: 4 additions & 0 deletions src/models/Invoice.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
OneToMany,
JoinColumn,
Index,
VersionColumn,
} from "typeorm";
import Decimal from "decimal.js";
import { InvoiceStatus } from "../types/enums";
Expand Down Expand Up @@ -125,6 +126,9 @@ export class Invoice {
@DeleteDateColumn({ name: "deleted_at" })
deletedAt!: Date | null;

@VersionColumn()
version!: number;

@ManyToOne("User", "invoices", { onDelete: "CASCADE", eager: false })
@JoinColumn({ name: "seller_id" })
seller!: import("./User.model").User;
Expand Down
7 changes: 7 additions & 0 deletions src/routes/invoice.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ export function createInvoiceRouter({
controller.getInvoiceEscrowStatus,
);

// GET /api/v1/invoices/:id/analytics - Get invoice analytics (owner only)
router.get(
"/:id/analytics",
authenticateJWT,
controller.getInvoiceAnalytics,
);

// POST /api/v1/invoices/calculate-terms - Calculate invoice discounting terms, fees, and APR
router.post(
"/calculate-terms",
Expand Down
Loading