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
6 changes: 6 additions & 0 deletions backend/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ export default tseslint.config(
}],
},
},
{
files: ['**/*.spec.ts', '**/*.test.ts'],
rules: {
'@typescript-eslint/unbound-method': 'off',
},
},
);
59 changes: 59 additions & 0 deletions backend/package-lock.json

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

5 changes: 4 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@
},
"dependencies": {
"@invoisio/soroban-client": "file:../soroban/client",
"@nestjs/axios": "^4.0.1",
"@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1",
"@nestjs/jwt": "^11.0.2",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.0.1",
"@nestjs/schedule": "^6.1.1",
"@nestjs/typeorm": "^11.0.0",
"@prisma/adapter-pg": "^7.4.2",
"@prisma/client": "^7.4.2",
"@stellar/stellar-sdk": "^14.6.0",
"axios": "^1.13.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.2",
"cross-env": "^10.1.0",
Expand Down Expand Up @@ -115,4 +118,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- CreateEnum
CREATE TYPE "DeliveryStatus" AS ENUM ('pending', 'success', 'failed');

-- AlterTable
ALTER TABLE "users" ADD COLUMN "webhook_url" TEXT,
ADD COLUMN "webhook_secret" TEXT;

-- CreateTable
CREATE TABLE "webhook_deliveries" (
"id" TEXT NOT NULL,
"invoice_id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"url" TEXT NOT NULL,
"payload" JSONB NOT NULL,
"status" "DeliveryStatus" NOT NULL DEFAULT 'pending',
"attempts" INTEGER NOT NULL DEFAULT 0,
"last_attempt_at" TIMESTAMP(3),
"next_attempt_at" TIMESTAMP(3),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "webhook_deliveries_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "webhook_deliveries" ADD CONSTRAINT "webhook_deliveries_invoice_id_fkey" FOREIGN KEY ("invoice_id") REFERENCES "invoices"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "webhook_deliveries" ADD CONSTRAINT "webhook_deliveries_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
30 changes: 30 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,39 @@ model User {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

webhookUrl String? @map("webhook_url")
webhookSecret String? @map("webhook_secret")

invoices Invoice[]
webhooks WebhookDelivery[]

@@map("users")
}

model WebhookDelivery {
id String @id @default(uuid())
invoiceId String @map("invoice_id")
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
userId String @map("user_id")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
url String
payload Json
status DeliveryStatus @default(pending)
attempts Int @default(0)
lastAttemptAt DateTime? @map("last_attempt_at")
nextAttemptAt DateTime? @map("next_attempt_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

@@map("webhook_deliveries")
}

enum DeliveryStatus {
pending
success
failed
}

// Invoice model matching backend/src/invoices/entities/invoice.entity.ts
model Invoice {
id String @id @default(uuid())
Expand All @@ -49,6 +77,8 @@ model Invoice {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

webhooks WebhookDelivery[]

@@map("invoices")
}

Expand Down
4 changes: 4 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { HorizonWatcherModule } from "./stellar/horizon-watcher.module";
import { AuthModule } from "./auth/auth.module";
import { UsersModule } from "./users/user.module";
import { PrismaModule } from "./prisma/prisma.module";
import { ScheduleModule } from "@nestjs/schedule";
import { WebhooksModule } from "./webhooks/webhooks.module";

/**
* Root application module
Expand Down Expand Up @@ -51,12 +53,14 @@ import { PrismaModule } from "./prisma/prisma.module";
}),
}),
PrismaModule,
ScheduleModule.forRoot(),
HealthModule,
InvoicesModule,
StellarModule,
HorizonWatcherModule,
AuthModule,
UsersModule,
WebhooksModule,
],
})
export class AppModule {}
2 changes: 2 additions & 0 deletions backend/src/invoices/invoices.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StellarModule } from "../stellar/stellar.module";
import { SorobanModule } from "../soroban/soroban.module";
import { AuthGuard } from "../auth/auth.guard";
import { PrismaModule } from "../prisma/prisma.module";
import { WebhooksModule } from "../webhooks/webhooks.module";

/**
* Invoices module
Expand All @@ -17,6 +18,7 @@ import { PrismaModule } from "../prisma/prisma.module";
StellarModule,
SorobanModule,
PrismaModule,
WebhooksModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
Expand Down
6 changes: 6 additions & 0 deletions backend/src/invoices/invoices.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CreateInvoiceDto } from "./dto/create-invoice.dto";
import { validate } from "class-validator";
import { plainToInstance } from "class-transformer";
import { PrismaService } from "../prisma/prisma.service";
import { WebhooksService } from "../webhooks/webhooks.service";

describe("InvoicesService", () => {
let service: InvoicesService;
Expand Down Expand Up @@ -195,6 +196,10 @@ describe("InvoicesService", () => {
};
};

const mockWebhooksService = {
enqueueWebhook: jest.fn().mockResolvedValue(undefined),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
Expand All @@ -203,6 +208,7 @@ describe("InvoicesService", () => {
{ provide: StellarService, useValue: mockStellarService },
{ provide: SorobanService, useValue: mockSorobanService },
{ provide: PrismaService, useFactory: mockPrisma },
{ provide: WebhooksService, useValue: mockWebhooksService },
],
}).compile();

Expand Down
10 changes: 10 additions & 0 deletions backend/src/invoices/invoices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { StellarService } from "../stellar/stellar.service";
import { SorobanService } from "../soroban/soroban.service";
import { PrismaService } from "../prisma/prisma.service";
import { Prisma, InvoiceStatus } from "@prisma/client";
import { WebhooksService } from "../webhooks/webhooks.service";

/**
* Invoices service — manages invoice lifecycle and Soroban on-chain settlement.
Expand All @@ -24,6 +25,7 @@ export class InvoicesService implements OnModuleInit {
private readonly stellarService: StellarService,
private readonly sorobanService: SorobanService,
private readonly prisma: PrismaService,
private readonly webhooksService: WebhooksService,
) {}

async onModuleInit() {
Expand Down Expand Up @@ -122,6 +124,10 @@ export class InvoicesService implements OnModuleInit {
where: { id },
data: { status },
});

// Enqueue webhook
await this.webhooksService.enqueueWebhook(id, status, updated.txHash);

return this.normalizeInvoice(updated);
}

Expand All @@ -136,6 +142,10 @@ export class InvoicesService implements OnModuleInit {
where: { id },
data: { status: "paid", txHash: txHash },
});

// Enqueue webhook
await this.webhooksService.enqueueWebhook(id, "paid", txHash);

return this.normalizeInvoice(updated);
}

Expand Down
9 changes: 2 additions & 7 deletions backend/src/stellar/soroban.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ describe("Soroban Integration", () => {

await new Promise((resolve) => setTimeout(resolve, 100));

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(invoicesService.markAsPaid).toHaveBeenCalledWith(
mockInvoice.id,
"horizon-tx-hash-abc",
);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(sorobanService.recordPayment).toHaveBeenCalledWith({
invoiceId: mockInvoice.memo,
payer: mockPaymentRecord.from,
Expand All @@ -122,7 +120,6 @@ describe("Soroban Integration", () => {
amount: "100000000",
});

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(invoicesService.updateSorobanMetadata).toHaveBeenCalledWith(
mockInvoice.id,
"soroban-tx-hash-xyz",
Expand All @@ -146,11 +143,10 @@ describe("Soroban Integration", () => {

await new Promise((resolve) => setTimeout(resolve, 100));

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(invoicesService.markAsPaid).toHaveBeenCalled();
// eslint-disable-next-line @typescript-eslint/unbound-method

expect(sorobanService.recordPayment).toHaveBeenCalled();
// eslint-disable-next-line @typescript-eslint/unbound-method

expect(invoicesService.updateSorobanMetadata).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -179,7 +175,6 @@ describe("Soroban Integration", () => {

await new Promise((resolve) => setTimeout(resolve, 100));

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(sorobanService.recordPayment).toHaveBeenCalledWith(
expect.objectContaining({
assetCode: "USDC",
Expand Down
10 changes: 10 additions & 0 deletions backend/src/webhooks/webhooks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from "@nestjs/common";
import { PrismaModule } from "../prisma/prisma.module";
import { WebhooksService } from "./webhooks.service";

@Module({
imports: [PrismaModule],
providers: [WebhooksService],
exports: [WebhooksService],
})
export class WebhooksModule {}
Loading
Loading