Files
InvoiceShelf/resources/scripts/api/services/exchange-rate.service.ts
Darko Gjorgjijoski 71388ec6a5 Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.

Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.

Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
2026-04-07 12:50:16 +02:00

148 lines
4.5 KiB
TypeScript

import { client } from '../client'
import { API } from '../endpoints'
import type { ExchangeRateProvider, Currency } from '@/scripts/types/domain/currency'
import type { ApiResponse, ListParams } from '@/scripts/types/api'
export interface CreateExchangeRateProviderPayload {
driver: string
key: string
active?: boolean
currencies?: string[]
driver_config?: Record<string, string>
}
// Normalized response types (what callers receive)
export interface ExchangeRateResponse {
exchangeRate: number | null
}
export interface ActiveProviderResponse {
hasActiveProvider: boolean
}
export interface SupportedCurrenciesResponse {
supportedCurrencies: string[]
}
export interface UsedCurrenciesResponse {
activeUsedCurrencies: string[]
allUsedCurrencies: string[]
}
export interface BulkCurrenciesResponse {
currencies: Array<Currency & { exchange_rate: number | null }>
}
export interface BulkUpdatePayload {
currencies: Array<{
id: number
exchange_rate: number
}>
}
export interface ConfigOption {
key: string
value: string
}
export interface ConfigDriversResponse {
exchange_rate_drivers: ConfigOption[]
}
export interface ConfigServersResponse {
currency_converter_servers: ConfigOption[]
}
export interface SupportedCurrenciesParams {
driver: string
key: string
driver_config?: Record<string, string>
}
export const exchangeRateService = {
// Providers CRUD
async listProviders(params?: ListParams): Promise<ApiResponse<ExchangeRateProvider[]>> {
const { data } = await client.get(API.EXCHANGE_RATE_PROVIDERS, { params })
return data
},
async getProvider(id: number): Promise<ApiResponse<ExchangeRateProvider>> {
const { data } = await client.get(`${API.EXCHANGE_RATE_PROVIDERS}/${id}`)
return data
},
async createProvider(payload: CreateExchangeRateProviderPayload): Promise<ApiResponse<ExchangeRateProvider>> {
const { data } = await client.post(API.EXCHANGE_RATE_PROVIDERS, payload)
return data
},
async updateProvider(
id: number,
payload: Partial<CreateExchangeRateProviderPayload>,
): Promise<ApiResponse<ExchangeRateProvider>> {
const { data } = await client.put(`${API.EXCHANGE_RATE_PROVIDERS}/${id}`, payload)
return data
},
async deleteProvider(id: number): Promise<{ success: boolean }> {
const { data } = await client.delete(`${API.EXCHANGE_RATE_PROVIDERS}/${id}`)
return data
},
// Exchange Rates
// Backend returns { exchangeRate: [number] } or { error: string }
async getRate(currencyId: number): Promise<ExchangeRateResponse> {
const { data } = await client.get(`${API.CURRENCIES}/${currencyId}/exchange-rate`)
const raw = data as Record<string, unknown>
if (raw.exchangeRate && Array.isArray(raw.exchangeRate)) {
return { exchangeRate: Number(raw.exchangeRate[0]) ?? null }
}
return { exchangeRate: null }
},
// Backend returns { success: true, message: "provider_active" } or { error: "no_active_provider" }
async getActiveProvider(currencyId: number): Promise<ActiveProviderResponse> {
const { data } = await client.get(`${API.CURRENCIES}/${currencyId}/active-provider`)
const raw = data as Record<string, unknown>
return { hasActiveProvider: raw.success === true }
},
// Currency lists
async getSupportedCurrencies(params: SupportedCurrenciesParams): Promise<SupportedCurrenciesResponse> {
const { data } = await client.get(API.SUPPORTED_CURRENCIES, { params })
return data
},
// Backend returns { activeUsedCurrencies: string[], allUsedCurrencies: string[] }
async getUsedCurrencies(params?: { provider_id?: number }): Promise<UsedCurrenciesResponse> {
const { data } = await client.get(API.USED_CURRENCIES, { params })
return data
},
async getBulkCurrencies(): Promise<BulkCurrenciesResponse> {
const { data } = await client.get(API.CURRENCIES_USED)
return data
},
async bulkUpdateExchangeRate(payload: BulkUpdatePayload): Promise<{ success: boolean }> {
const { data } = await client.post(API.CURRENCIES_BULK_UPDATE, payload)
return data
},
// Config
// Backend returns { exchange_rate_drivers: Array<{ key, value }> }
async getDrivers(): Promise<ConfigDriversResponse> {
const { data } = await client.get(API.CONFIG, { params: { key: 'exchange_rate_drivers' } })
return data
},
// Backend returns { currency_converter_servers: Array<{ key, value }> }
async getCurrencyConverterServers(): Promise<ConfigServersResponse> {
const { data } = await client.get(API.CONFIG, { params: { key: 'currency_converter_servers' } })
return data
},
}