← back to Wine Finder Next

lib/affiliates.ts

35 lines

// lib/affiliates.ts
import { getDb } from './db';

type Vendor = 'winecom' | 'winebid' | 'drizly' | 'vivino' | 'reservebar';

export function recordAffiliateClick(params: {
  userId?: number;
  wineId?: number;
  vendor: Vendor;
  productId?: string;
  clickRef?: string;
}) {
  const db = getDb();
  db.prepare(`
    INSERT INTO affiliate_clicks (user_id, wine_id, vendor, product_id, click_ref)
    VALUES (@userId, @wineId, @vendor, @productId, @clickRef)
  `).run(params);
}

export function buildVendorUrl(vendor: Vendor, productId: string, clickRef?: string) {
  switch (vendor) {
    case 'winecom':
      return `https://www.wine.com/product/${productId}?ref=${encodeURIComponent(clickRef || '')}`;
    case 'winebid':
      return `https://www.winebid.com/BuyWine/Item/${productId}?ref=${encodeURIComponent(clickRef || '')}`;
    case 'drizly':
      return `https://drizly.com/product/${productId}?ref=${encodeURIComponent(clickRef || '')}`;
    case 'vivino':
      return `https://www.vivino.com/wines/${productId}?ref=${encodeURIComponent(clickRef || '')}`;
    case 'reservebar':
      return `https://www.reservebar.com/products/${productId}?ref=${encodeURIComponent(clickRef || '')}`;
    default:
      return '#';
  }
}