import { getDb } from './db'; export interface CourierOffice { id: string; name: string; city: string; address: string; courier: string; } export async function refreshEcontOffices(): Promise { const resp = await fetch('https://ee.econt.com/services/Nomenclatures/NomenclaturesService.getOffices.json', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ countryCode: 'BGR' }), }); if (!resp.ok) throw new Error(`econt api: ${resp.status}`); const data = await resp.json() as any; const offices = data.offices || []; if (offices.length === 0) throw new Error('econt returned no offices'); const db = getDb(); const upsert = db.prepare(`INSERT INTO offices (id, courier, name, city, address, updated_at) VALUES (?, 'econt', ?, ?, ?, datetime('now')) ON CONFLICT(id) DO UPDATE SET name = excluded.name, city = excluded.city, address = excluded.address, updated_at = excluded.updated_at`); const tx = db.transaction((items: any[]) => { for (const o of items) { upsert.run(String(o.id), o.name, o.address?.city?.name || '', o.address?.fullAddress || o.name); } }); tx(offices); return offices.length; } export async function refreshSpeedyOffices(): Promise { const user = process.env.SPEEDY_USERNAME; const pass = process.env.SPEEDY_PASSWORD; if (!user || !pass) return 0; const resp = await fetch('https://api.speedy.bg/v1/location/office', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userName: user, password: pass, language: 'BG' }), }); if (!resp.ok) throw new Error(`speedy api: ${resp.status}`); const data = await resp.json() as any; const offices = data.offices || []; if (offices.length === 0) throw new Error('speedy returned no offices'); const db = getDb(); const upsert = db.prepare(`INSERT INTO offices (id, courier, name, city, address, updated_at) VALUES (?, 'speedy', ?, ?, ?, datetime('now')) ON CONFLICT(id) DO UPDATE SET name = excluded.name, city = excluded.city, address = excluded.address, updated_at = excluded.updated_at`); const tx = db.transaction((items: any[]) => { for (const o of items) { upsert.run(String(o.id), o.name, o.address?.siteName || o.siteName || '', o.address?.fullAddress || o.address?.localAddress || o.name); } }); tx(offices); return offices.length; } export async function refreshAllOffices(): Promise<{ econt: number; speedy: number }> { const db = getDb(); db.run('DELETE FROM offices'); const econt = await refreshEcontOffices().catch(() => 0); const speedy = await refreshSpeedyOffices().catch(() => 0); return { econt, speedy }; } export function getOfficesByCourier(courier: string): CourierOffice[] { return getDb().query('SELECT id, courier, name, city, address FROM offices WHERE courier = ? ORDER BY city, name').all(courier) as CourierOffice[]; } export function getCities(): string[] { const rows = getDb().query('SELECT DISTINCT city FROM offices ORDER BY city').all() as { city: string }[]; if (rows.length > 0) return rows.map(r => r.city); return ['София', 'Пловдив', 'Варна', 'Бургас', 'Русе', 'Стара Загора', 'Плевен', 'Велико Търново', 'Благоевград', 'Добрич', 'Шумен', 'Перник']; } export function courierSupportsOffices(courier: string): boolean { return courier === 'econt'; }