/** * Anvil Transfer Pricing Repository * Handles CRUD operations for data transfer pricing per region (USD only) */ import { BaseRepository } from './base'; import { AnvilTransferPricing, AnvilTransferPricingInput, RepositoryError, ErrorCodes } from '../types'; import { createLogger } from '../utils/logger'; export class AnvilTransferPricingRepository extends BaseRepository { protected tableName = 'anvil_transfer_pricing'; protected logger = createLogger('[AnvilTransferPricingRepository]'); protected allowedColumns = [ 'anvil_region_id', 'price_per_gb', ]; constructor(db: D1Database) { super(db); } /** * Find transfer pricing for a specific region */ async findByRegion(anvilRegionId: number): Promise { try { const result = await this.db .prepare('SELECT * FROM anvil_transfer_pricing WHERE anvil_region_id = ?') .bind(anvilRegionId) .first(); return result || null; } catch (error) { this.logger.error('findByRegion failed', { anvilRegionId, error: error instanceof Error ? error.message : 'Unknown error' }); throw new RepositoryError( `Failed to find transfer pricing for Anvil region: ${anvilRegionId}`, ErrorCodes.DATABASE_ERROR, error ); } } /** * Bulk upsert transfer pricing records * Uses batch operations for efficiency */ async upsertMany(pricing: AnvilTransferPricingInput[]): Promise { if (pricing.length === 0) { return 0; } try { const statements = pricing.map((price) => { return this.db.prepare( `INSERT INTO anvil_transfer_pricing ( anvil_region_id, price_per_gb ) VALUES (?, ?) ON CONFLICT(anvil_region_id) DO UPDATE SET price_per_gb = excluded.price_per_gb` ).bind( price.anvil_region_id, price.price_per_gb ); }); const results = await this.executeBatch(statements); const successCount = results.reduce( (sum, result) => sum + (result.meta.changes ?? 0), 0 ); this.logger.info('Upserted Anvil transfer pricing records', { count: successCount }); return successCount; } catch (error) { this.logger.error('upsertMany failed', { count: pricing.length, error: error instanceof Error ? error.message : 'Unknown error' }); throw new RepositoryError( 'Failed to upsert Anvil transfer pricing records', ErrorCodes.TRANSACTION_FAILED, error ); } } }