mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 17:08:18 +00:00
Fare engine added
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../../common/prisma.service';
|
||||
import { Currency } from '@prisma/client';
|
||||
|
||||
@@ -47,9 +47,8 @@ export class CurrencyService {
|
||||
|
||||
async syncExchangeRates(): Promise<void> {
|
||||
this.logger.log('Syncing exchange rates from external provider');
|
||||
|
||||
// In production, fetch from external API
|
||||
// For now, using static rates
|
||||
|
||||
const today = this.todayUtc();
|
||||
const rates = [
|
||||
{ from: 'ETB', to: 'ETB', rate: 1.0 },
|
||||
{ from: 'ETB', to: 'DJF', rate: 3.25 },
|
||||
@@ -59,25 +58,47 @@ export class CurrencyService {
|
||||
];
|
||||
|
||||
for (const { from, to, rate } of rates) {
|
||||
await this.prisma.currencyExchangeRate.upsert({
|
||||
where: {
|
||||
fromCurrency_toCurrency_effectiveDate: {
|
||||
fromCurrency: from as Currency,
|
||||
toCurrency: to as Currency,
|
||||
effectiveDate: new Date(),
|
||||
},
|
||||
},
|
||||
update: { rate },
|
||||
create: {
|
||||
fromCurrency: from as Currency,
|
||||
toCurrency: to as Currency,
|
||||
rate,
|
||||
effectiveDate: new Date(),
|
||||
source: 'EXTERNAL_API',
|
||||
},
|
||||
});
|
||||
await this.upsertRate(from as Currency, to as Currency, rate, today, 'EXTERNAL_API');
|
||||
}
|
||||
|
||||
this.logger.log('Exchange rates synced successfully');
|
||||
}
|
||||
|
||||
async listRates() {
|
||||
return this.prisma.currencyExchangeRate.findMany({
|
||||
orderBy: [{ fromCurrency: 'asc' }, { toCurrency: 'asc' }, { effectiveDate: 'desc' }],
|
||||
});
|
||||
}
|
||||
|
||||
async upsertRate(
|
||||
fromCurrency: Currency,
|
||||
toCurrency: Currency,
|
||||
rate: number,
|
||||
effectiveDate?: Date,
|
||||
source = 'MANUAL',
|
||||
) {
|
||||
const date = effectiveDate ?? this.todayUtc();
|
||||
return this.prisma.currencyExchangeRate.upsert({
|
||||
where: { fromCurrency_toCurrency_effectiveDate: { fromCurrency, toCurrency, effectiveDate: date } },
|
||||
update: { rate, source },
|
||||
create: { fromCurrency, toCurrency, rate, effectiveDate: date, source },
|
||||
});
|
||||
}
|
||||
|
||||
async updateRateById(id: string, rate: number, source = 'MANUAL') {
|
||||
const existing = await this.prisma.currencyExchangeRate.findUnique({ where: { id } });
|
||||
if (!existing) throw new NotFoundException('Exchange rate not found');
|
||||
return this.prisma.currencyExchangeRate.update({ where: { id }, data: { rate, source } });
|
||||
}
|
||||
|
||||
async deleteRate(id: string) {
|
||||
return this.prisma.currencyExchangeRate.delete({ where: { id } });
|
||||
}
|
||||
|
||||
/** Returns midnight UTC for today — used as the date-only key for upserts. */
|
||||
private todayUtc(): Date {
|
||||
const d = new Date();
|
||||
d.setUTCHours(0, 0, 0, 0);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user