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
286 changes: 33 additions & 253 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^6.1.3",
"@nestjs/terminus": "^10.1.0",
"@nestjs/typeorm": "^10.0.0",
"@stellar/stellar-sdk": "^12.0.0",
"axios": "^1.6.0",
"class-transformer": "^0.5.1",
Expand All @@ -47,7 +46,6 @@
"prom-client": "^15.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"typeorm": "^0.3.17",
"uuid": "^9.0.0",
"zod": "^4.4.3"
},
Expand Down
30 changes: 8 additions & 22 deletions src/__tests__/app.smoke.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmHealthIndicator } from '@nestjs/terminus';
import request from 'supertest';
import { AppModule } from '../app.module';
import { CorsOriginsCacheService } from '../middleware/cors-origins-cache.service';
import { PaymentDetectorService } from '../payments/payment-detector.service';
import { RedisService } from '../redis/redis.service';

jest.mock('@nestjs/typeorm', () => {
const actual = jest.requireActual('@nestjs/typeorm');

return {
...actual,
TypeOrmModule: {
...actual.TypeOrmModule,
forRoot: jest.fn(() => ({
module: class MockTypeOrmModule {},
})),
},
};
});
jest.mock('../db/index', () => ({
db: {
execute: jest.fn().mockResolvedValue([]),
query: {},
},
client: {},
schema: {},
}));

describe('App smoke test', () => {
let app: INestApplication;
Expand All @@ -37,14 +31,6 @@ describe('App smoke test', () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(TypeOrmHealthIndicator)
.useValue({
pingCheck: jest.fn().mockResolvedValue({
database: {
status: 'up',
},
}),
})
.overrideProvider(RedisService)
.useValue({
getClient: jest.fn(() => redisClient),
Expand Down
7 changes: 0 additions & 7 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { AuthModule } from './auth/auth.module';
import { MerchantsModule } from './merchants/merchants.module';
Expand All @@ -19,12 +18,6 @@ import { SecurityHeadersMiddleware } from './middleware/security-headers.middlew
imports: [
ConfigModule,
ScheduleModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
autoLoadEntities: true,
synchronize: process.env.NODE_ENV !== 'production',
}),
RedisModule,
RateLimitModule,
AuditModule,
Expand Down
6 changes: 6 additions & 0 deletions src/checkout/checkout.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export class CheckoutController {
return this.checkout.getSession(id);
}

@UseGuards(ApiKeyGuard)
@Get('sessions/:id/payment')
getSessionPayment(@Param('id') id: string) {
return this.checkout.getSessionPayment(id);
}

@UseGuards(ApiKeyGuard, RolesGuard, ResourceOwnershipGuard)
@Roles('admin', 'merchant')
@ResourceOwner('checkout_session')
Expand Down
24 changes: 23 additions & 1 deletion src/checkout/checkout.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { db } from '../db/index';
import { checkoutSessions } from '../db/schema';
import { checkoutSessions, payments } from '../db/schema';
import { eq, and } from 'drizzle-orm';
import * as crypto from 'crypto';
import { AuditService } from '../audit/audit.service';
Expand Down Expand Up @@ -129,4 +129,26 @@ export class CheckoutService {
.returning();
return updated;
}

async getSessionPayment(sessionId: string) {
const session = await db.query.checkoutSessions.findFirst({
where: eq(checkoutSessions.id, sessionId),
});
if (!session) throw new NotFoundException('Session not found');

const payment = await db.query.payments.findFirst({
where: eq(payments.sessionId, sessionId),
});
if (!payment) throw new NotFoundException('Payment not found for session');

return {
id: payment.id,
sessionId: payment.sessionId,
txHash: payment.txHash,
amount: payment.amount,
asset: payment.assetCode,
sender: payment.senderAddress,
confirmedAt: payment.confirmedAt,
};
}
}
30 changes: 30 additions & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ export const webhookDeadLetters = pgTable('webhook_dead_letters', {
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
});

export const webhookEndpoints = pgTable('webhook_endpoints', {
id: uuid('id').defaultRandom().primaryKey(),
merchantId: uuid('merchant_id')
.notNull()
.references(() => merchants.id, { onDelete: 'cascade' }),
url: text('url').notNull(),
events: jsonb('events').notNull().default([]),
secret: text('secret').notNull(),
isActive: boolean('is_active').notNull().default(true),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
});

export const paymentLinks = pgTable('payment_links', {
id: uuid('id').defaultRandom().primaryKey(),
merchantId: uuid('merchant_id')
.notNull()
.references(() => merchants.id, { onDelete: 'cascade' }),
amount: numeric('amount', { precision: 36, scale: 7 }).notNull(),
assetCode: text('asset_code').notNull(),
assetIssuer: text('asset_issuer'),
description: text('description'),
displayCurrency: text('display_currency'),
metadata: jsonb('metadata'),
active: boolean('active').notNull().default(true),
expiresAt: timestamp('expires_at', { withTimezone: true }),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
});

export const auditLogs = pgTable('audit_logs', {
id: uuid('id').defaultRandom().primaryKey(),
merchantId: uuid('merchant_id').references(() => merchants.id),
Expand All @@ -171,5 +199,7 @@ export const schema = {
payments,
webhookDeliveries,
webhookDeadLetters,
webhookEndpoints,
paymentLinks,
auditLogs,
};
16 changes: 10 additions & 6 deletions src/monitoring/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';
import { db } from '../db/index';
import { sql } from 'drizzle-orm';

@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
) {}
constructor(private health: HealthCheckService) {}

@Get()
@HealthCheck()
check() {
return this.health.check([() => this.db.pingCheck('database')]);
return this.health.check([
async () => {
await db.execute(sql`SELECT 1`);
return { database: { status: 'up' } };
},
]);
}
}
Loading