// Generated, do not edit. sha256:db61aab12929b00d557fdd117e7a5efb89fd1b2bb3eb23c37364a1d19f493bbc import * as db from '../db.js'; import { SOURCE_CAPABILITIES, SOURCE_ENDPOINTS } from '../../shared/config.js'; import { assertEnum } from './_shared.js'; import { hasFeature } from '../license.mjs'; import { fail } from '../../shared/app-error.js'; import { refreshNotifications } from './notifications.js'; export function priceSourceChain(assetId, capability) { return db.all( `SELECT Provider.key AS key FROM PriceSource JOIN Provider ON Provider.id = PriceSource.providerId WHERE PriceSource.assetId = ? AND PriceSource.capability = ? AND PriceSource.rank IS NOT NULL ORDER BY PriceSource.rank`, [assetId, capability] ).map(r => r.key); } export function ownPriceSourceChain(assetId, capability) { return db.all( `SELECT Provider.key AS key FROM PriceSource JOIN Provider ON Provider.id = PriceSource.providerId WHERE PriceSource.assetId = ? AND PriceSource.capability = ? AND PriceSource.rank IS NOT NULL AND PriceSource.status = 'manual' ORDER BY PriceSource.rank`, [assetId, capability] ).map(r => r.key); } export function listPriceSource() { return db.all(`SELECT assetId, capability, providerId, rank, status FROM PriceSource`); } function assertMatrixAllowed() { if (hasFeature('sourceMatrix')) return; fail('integrations.matrixPaid', null, 'The source matrix belongs to the paid version. The app keeps using the standard sources.'); } function assertAnswers(providerId, capability) { const eindpunten = SOURCE_ENDPOINTS[capability]; if (!db.one(`SELECT 1 AS x FROM ProviderEndpoint WHERE providerId = ? AND capability IN (${eindpunten.map(() => '?').join(',')})`, [providerId, ...eindpunten])) throw new Error('This provider does not cover this capability.'); } export async function setPriceSourceRank(assetId, capability, providerId) { assertMatrixAllowed(); assertEnum('capability', SOURCE_CAPABILITIES, capability); assertAnswers(providerId, capability); const used = db.all(`SELECT DISTINCT PriceSource.rank FROM PriceSource JOIN Asset ON Asset.id = PriceSource.assetId JOIN ProviderCoverage ON ProviderCoverage.providerId = PriceSource.providerId AND ProviderCoverage.symbol = Asset.symbol WHERE PriceSource.assetId = ? AND PriceSource.capability = ? AND PriceSource.rank IS NOT NULL AND PriceSource.status IN ('confirmed','manual')`, [assetId, capability]).map(r => r.rank); let next = 1; while (used.includes(next)) next += 1; db.run(`DELETE FROM PriceSource WHERE assetId = ? AND capability = ? AND (providerId = ? OR rank = ?)`, [assetId, capability, providerId, next]); db.run(`INSERT INTO PriceSource (assetId, capability, providerId, rank, status, createdAt) VALUES (?,?,?,?,'manual',?)`, [assetId, capability, providerId, next, new Date().toISOString()]); await db.persist(); return next; } export async function placePriceSourceFirst(assetId, capability, providerId) { assertMatrixAllowed(); assertEnum('capability', SOURCE_CAPABILITIES, capability); assertAnswers(providerId, capability); const rest = db.all(`SELECT providerId, status, createdAt FROM PriceSource WHERE assetId = ? AND capability = ? AND rank IS NOT NULL AND providerId <> ? ORDER BY rank`, [assetId, capability, providerId]); db.run(`DELETE FROM PriceSource WHERE assetId = ? AND capability = ? AND (rank IS NOT NULL OR providerId = ?)`, [assetId, capability, providerId]); db.run(`INSERT INTO PriceSource (assetId, capability, providerId, rank, status, createdAt) VALUES (?,?,?,1,'manual',?)`, [assetId, capability, providerId, new Date().toISOString()]); rest.forEach((r, i) => db.run( `INSERT INTO PriceSource (assetId, capability, providerId, rank, status, createdAt) VALUES (?,?,?,?,?,?)`, [assetId, capability, r.providerId, i + 2, r.status, r.createdAt])); await db.persist(); } export async function clearPriceSourceCell(assetId, capability, providerId) { assertMatrixAllowed(); db.run(`DELETE FROM PriceSource WHERE assetId = ? AND capability = ? AND providerId = ?`, [assetId, capability, providerId]); await db.persist(); } export async function confirmPriceSourceCell(assetId, capability, providerId) { assertMatrixAllowed(); db.run(`UPDATE PriceSource SET status = 'confirmed' WHERE assetId = ? AND capability = ? AND providerId = ? AND status = 'proposed'`, [assetId, capability, providerId]); await db.persist(); await refreshNotifications(); }