Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

14.2 KB
Raw Blame
import { ApiKeyAccess } from '../api-key/api-key-access.decorator'
import {
  Controller,
  Get,
  Post,
  Delete,
  Query,
  Param,
  Body,
  NotFoundException,
  BadRequestException,
  ConflictException,
  Ip,
  Headers,
  HttpCode,
} from '@nestjs/common'
import { ApiTags, ApiResponse, ApiBearerAuth } from '@nestjs/swagger'
import _isEmpty from 'lodash/isEmpty'
import * as crypto from 'crypto'

import { ProjectService } from '../project/project.service'
import { AppLoggerService } from '../logger/logger.service'
import {
  AnalyticsService,
  getLowestPossibleTimeBucket,
} from '../analytics/analytics.service'
import { Auth } from '../auth/decorators'
import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'
import { RevenueService } from './revenue.service'
import { CurrencyService } from './currency.service'
import { PaddleAdapter } from './adapters/paddle.adapter'
import { StripeAdapter } from './adapters/stripe.adapter'
import { UpdateRevenueCurrencyDto } from './dto/update-revenue-currency.dto'
import { ConnectRevenueDto } from './dto/connect-revenue.dto'
import { GetRevenueDto, GetRevenueTransactionsDto } from './dto/get-revenue.dto'
import { LogRevenueDto } from './dto/log-revenue.dto'
import {
  RevenueStatsDto,
  RevenueChartDto,
  RevenueTransactionDto,
  RevenueStatusDto,
} from './dto/revenue-stats.dto'
import {
  RevenueProvider,
  RevenueStatus,
  RevenueType,
  STRIPE_REQUIRED_PERMISSIONS,
} from './interfaces/revenue.interface'
import { trackCustom } from '../common/analytics'
import { getIPFromHeaders } from '../common/utils'

@ApiTags('Revenue')
@Controller('project')
export class RevenueController {
  constructor(
    private readonly revenueService: RevenueService,
    private readonly paddleAdapter: PaddleAdapter,
    private readonly stripeAdapter: StripeAdapter,
    private readonly projectService: ProjectService,
    private readonly logger: AppLoggerService,
  ) {}

  @ApiBearerAuth()
  @Get('/:pid/revenue/status')
  @Auth(true, true)
  @ApiKeyAccess('revenue:read', 'params', 'pid')
  @ApiResponse({ status: 200, type: RevenueStatusDto })
  async getRevenueStatus(
    @CurrentUserId() userId: string,
    @Param('pid') pid: string,
  ): Promise<RevenueStatusDto> {
    this.logger.log({ userId, pid }, 'GET /project/:pid/revenue/status')

    const project = await this.projectService.getFullProject(pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToView(project, userId)

    return this.revenueService.getRevenueStatus(project)
  }

  @ApiBearerAuth()
  @Post('/:pid/revenue/connect')
  @Auth()
  @ApiResponse({ status: 201 })
  async connectRevenue(
    @CurrentUserId() userId: string,
    @Param('pid') pid: string,
    @Body() dto: ConnectRevenueDto,
    @Headers() headers: Record<string, string>,
    @Ip() requestIp: string,
  ) {
    const ip = getIPFromHeaders(headers) || requestIp || ''

    this.logger.log({ userId, pid }, 'POST /project/:pid/revenue/connect')

    const project = await this.projectService.getFullProject(pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToManage(
      project,
      userId,
      'You are not allowed to manage revenue settings for this project',
    )

    const currency = dto.currency || 'USD'

    if (dto.provider === 'api') {
      const result = await this.revenueService.connectApi(pid, currency)

      if (!result.success) {
        throw new BadRequestException(result.message)
      }

      await trackCustom(ip, headers['user-agent'], {
        ev: 'REVENUE_SETUP',
        meta: {
          provider: 'api',
        },
      })

      return { success: true }
    }

    if (!dto.apiKey) {
      throw new BadRequestException('API key is required for this provider')
    }

    if (dto.provider === 'paddle') {
      // Validate API key with Paddle
      const isValid = await this.paddleAdapter.validateApiKey(dto.apiKey)
      if (!isValid) {
        throw new BadRequestException(
          'Invalid Paddle API key. Please check your key and try again.',
        )
      }

      const result = await this.revenueService.connectPaddle(
        pid,
        dto.apiKey,
        currency,
      )

      if (!result.success) {
        throw new BadRequestException(result.message)
      }

      await trackCustom(ip, headers['user-agent'], {
        ev: 'REVENUE_SETUP',
        meta: {
          provider: 'paddle',
        },
      })

      return { success: true }
    }

    // Stripe
    const isValid = await this.stripeAdapter.validateApiKey(dto.apiKey)
    if (!isValid) {
      throw new BadRequestException(
        'Invalid Stripe restricted API key. Please check your key and try again.',
      )
    }

    const result = await this.revenueService.connectStripe(
      pid,
      dto.apiKey,
      currency,
      STRIPE_REQUIRED_PERMISSIONS,
    )

    if (!result.success) {
      throw new BadRequestException(result.message)
    }

    await trackCustom(ip, headers['user-agent'], {
      ev: 'REVENUE_SETUP',
      meta: {
        provider: 'stripe',
      },
    })

    return { success: true }
  }

  @ApiBearerAuth()
  @Delete('/:pid/revenue/disconnect')
  @Auth()
  @ApiResponse({ status: 204 })
  async disconnectRevenue(
    @CurrentUserId() userId: string,
    @Param('pid') pid: string,
    @Headers() headers: Record<string, string>,
    @Ip() requestIp: string,
  ) {
    this.logger.log({ userId, pid }, 'DELETE /project/:pid/revenue/disconnect')

    const ip = getIPFromHeaders(headers) || requestIp || ''

    const project = await this.projectService.getFullProject(pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToManage(
      project,
      userId,
      'You are not allowed to manage revenue settings for this project',
    )

    await this.revenueService.disconnectRevenue(pid)

    await trackCustom(ip, headers['user-agent'], {
      ev: 'REVENUE_DISCONNECTED',
    })
  }

  @ApiBearerAuth()
  @Post('/:pid/revenue/currency')
  @Auth()
  async updateCurrency(
    @CurrentUserId() userId: string,
    @Param('pid') pid: string,
    @Body() dto: UpdateRevenueCurrencyDto,
  ) {
    this.logger.log(
      { userId, pid, currency: dto.currency },
      'POST /project/:pid/revenue/currency',
    )

    const project = await this.projectService.getFullProject(pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToManage(
      project,
      userId,
      'You are not allowed to manage revenue settings for this project',
    )

    await this.revenueService.updateCurrency(pid, dto.currency)

    // Trigger immediate re-sync
    try {
      if (project.paddleApiKeyEnc) {
        const apiKey = this.revenueService.getPaddleApiKey(project)
        if (apiKey) {
          await this.paddleAdapter.syncTransactions(
            pid,
            apiKey,
            dto.currency,
            undefined, // Full re-sync
          )
          await this.revenueService.updateLastSyncAt(pid)
        }
      } else if (project.stripeApiKeyEnc) {
        const apiKey = this.revenueService.getStripeApiKey(project)
        if (apiKey) {
          await this.stripeAdapter.syncTransactions(
            pid,
            apiKey,
            dto.currency,
            undefined, // Full re-sync
          )
          await this.revenueService.updateLastSyncAt(pid)
        }
      }
    } catch (error) {
      this.logger.error(
        { error, pid },
        'Failed to trigger re-sync after currency update',
      )
      // Don't throw here, as currency was updated successfully
    }

    return { success: true }
  }
}

@ApiTags('Revenue Analytics')
@Controller(['log/revenue', 'v1/log/revenue'])
export class RevenueAnalyticsController {
  constructor(
    private readonly revenueService: RevenueService,
    private readonly currencyService: CurrencyService,
    private readonly projectService: ProjectService,
    private readonly analyticsService: AnalyticsService,
    private readonly logger: AppLoggerService,
  ) {}

  @ApiBearerAuth()
  @Get('/')
  @Auth(true, true)
  @ApiKeyAccess('revenue:read', 'query', 'pid')
  @ApiResponse({ status: 200 })
  async getRevenueStats(
    @CurrentUserId() userId: string,
    @Query() dto: GetRevenueDto,
  ): Promise<{
    stats: RevenueStatsDto
    chart: RevenueChartDto
  }> {
    this.logger.log({ userId, ...dto }, 'GET /log/revenue')

    const project = await this.projectService.getFullProject(dto.pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToView(project, userId)

    if (
      !project.paddleApiKeyEnc &&
      !project.stripeApiKeyEnc &&
      !project.revenueApiEnabled
    ) {
      throw new BadRequestException(
        'Revenue tracking is not configured for this project',
      )
    }

    const safeTimezone = this.analyticsService.getSafeTimezone(dto.timezone)
    const timeBucket =
      dto.timeBucket ||
      getLowestPossibleTimeBucket(dto.period, dto.from, dto.to)

    const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
      dto.from,
      dto.to,
      timeBucket,
      dto.period,
      safeTimezone,
    )

    const { xShifted } = this.analyticsService.generateXAxis(
      timeBucket,
      groupFromUTC,
      groupToUTC,
      safeTimezone,
    )

    const currency = project.revenueCurrency || 'USD'

    const [stats, chart] = await Promise.all([
      this.revenueService.getRevenueStats(
        dto.pid,
        groupFromUTC,
        groupToUTC,
        currency,
      ),
      this.revenueService.getRevenueChart(
        dto.pid,
        groupFromUTC,
        groupToUTC,
        timeBucket,
        safeTimezone,
        xShifted,
      ),
    ])

    return { stats, chart }
  }

  @ApiBearerAuth()
  @Get('/transactions')
  @Auth()
  @ApiResponse({ status: 200 })
  async getRevenueTransactions(
    @CurrentUserId() userId: string,
    @Query() dto: GetRevenueTransactionsDto,
  ): Promise<{ transactions: RevenueTransactionDto[]; total: number }> {
    this.logger.log({ userId, ...dto }, 'GET /log/revenue/transactions')

    const project = await this.projectService.getFullProject(dto.pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToView(project, userId)

    if (
      !project.paddleApiKeyEnc &&
      !project.stripeApiKeyEnc &&
      !project.revenueApiEnabled
    ) {
      throw new BadRequestException(
        'Revenue tracking is not configured for this project',
      )
    }

    const safeTimezone = this.analyticsService.getSafeTimezone(dto.timezone)
    const timeBucket = getLowestPossibleTimeBucket(dto.period, dto.from, dto.to)

    const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
      dto.from,
      dto.to,
      timeBucket,
      dto.period,
      safeTimezone,
    )

    return this.revenueService.getRevenueTransactions(
      dto.pid,
      groupFromUTC,
      groupToUTC,
      dto.take || 20,
      dto.skip || 0,
      dto.type,
      dto.status,
    )
  }

  @ApiBearerAuth()
  @Post()
  @Auth(true, false)
  @ApiKeyAccess('revenue:write', 'body', 'pid')
  @HttpCode(201)
  @ApiResponse({ status: 201 })
  async logRevenue(
    @CurrentUserId() userId: string,
    @Body() dto: LogRevenueDto,
    @Headers() headers: Record<string, string>,
    @Ip() requestIp: string,
  ): Promise<{ success: true; transactionId: string }> {
    this.logger.log(
      { userId, pid: dto.pid, type: dto.type },
      'POST /log/revenue',
    )

    const ip = getIPFromHeaders(headers) || requestIp || ''

    const project = await this.projectService.getFullProject(dto.pid)

    if (_isEmpty(project)) {
      throw new NotFoundException('Project not found')
    }

    this.projectService.allowedToManage(
      project,
      userId,
      'You are not allowed to ingest revenue for this project',
    )

    if (project.paddleApiKeyEnc || project.stripeApiKeyEnc) {
      throw new ConflictException(
        'This project already has a payment provider connected (Stripe or Paddle). Disconnect it from Project Settings > Revenue before sending revenue via the API, otherwise transactions would be double-counted.',
      )
    }

    const isFirstCall = !project.revenueApiEnabled

    if (isFirstCall) {
      const result = await this.revenueService.connectApi(dto.pid, dto.currency)

      if (!result.success) {
        throw new BadRequestException(result.message)
      }

      await trackCustom(ip, headers['user-agent'], {
        ev: 'REVENUE_SETUP',
        meta: {
          provider: 'api',
          autoEnabled: 'true',
        },
      })
    }

    const targetCurrency = isFirstCall
      ? dto.currency
      : project.revenueCurrency || 'USD'

    const originalCurrency = dto.currency.toUpperCase()
    const originalAmount = dto.amount

    const convertedAmount = await this.currencyService.convert(
      originalAmount,
      originalCurrency,
      targetCurrency,
    )

    const isRefund = dto.type === 'refund'

    const type =
      dto.type === 'refund'
        ? RevenueType.REFUND
        : dto.type === 'subscription'
          ? RevenueType.SUBSCRIPTION
          : RevenueType.SALE

    const status = isRefund ? RevenueStatus.REFUNDED : RevenueStatus.COMPLETED

    const transactionId = dto.transactionId || crypto.randomUUID()
    const created = dto.created ? new Date(dto.created) : new Date()

    await this.revenueService.insertTransaction({
      pid: dto.pid,
      transactionId,
      provider: RevenueProvider.API,
      type,
      status,
      amount: isRefund ? -Math.abs(convertedAmount) : convertedAmount,
      originalAmount: isRefund ? -Math.abs(originalAmount) : originalAmount,
      originalCurrency,
      currency: targetCurrency,
      profileId: dto.profileId || null,
      sessionId: dto.sessionId || null,
      productId: dto.productId || null,
      productName: dto.productName || null,
      metadata: dto.metadata || {},
      created,
      syncedAt: new Date(),
    })

    await this.revenueService.updateLastSyncAt(dto.pid)

    await trackCustom(ip, headers['user-agent'], {
      ev: 'REVENUE_API_INGEST',
      meta: {
        type: dto.type,
      },
    })

    return { success: true, transactionId }
  }
}