mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 16:28:12 +00:00
Implemened verifayda and currency modules
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CurrencyService } from './currency.service';
|
||||
import { PrismaModule } from '../../common/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
providers: [CurrencyService],
|
||||
exports: [CurrencyService],
|
||||
})
|
||||
export class CurrencyModule {}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { PrismaService } from '../../common/prisma.service';
|
||||
import { Currency } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class CurrencyService {
|
||||
private readonly logger = new Logger(CurrencyService.name);
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async convertAmount(
|
||||
amountMinor: number,
|
||||
fromCurrency: Currency,
|
||||
toCurrency: Currency,
|
||||
): Promise<number> {
|
||||
if (fromCurrency === toCurrency) {
|
||||
return amountMinor;
|
||||
}
|
||||
|
||||
const rate = await this.getExchangeRate(fromCurrency, toCurrency);
|
||||
return Math.round(amountMinor * rate);
|
||||
}
|
||||
|
||||
async getExchangeRate(
|
||||
fromCurrency: Currency,
|
||||
toCurrency: Currency,
|
||||
): Promise<number> {
|
||||
const exchangeRate = await this.prisma.currencyExchangeRate.findFirst({
|
||||
where: {
|
||||
fromCurrency,
|
||||
toCurrency,
|
||||
},
|
||||
orderBy: {
|
||||
effectiveDate: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
if (!exchangeRate) {
|
||||
this.logger.warn(
|
||||
`No exchange rate found for ${fromCurrency} to ${toCurrency}, using 1.0`,
|
||||
);
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
return Number(exchangeRate.rate);
|
||||
}
|
||||
|
||||
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 rates = [
|
||||
{ from: 'ETB', to: 'ETB', rate: 1.0 },
|
||||
{ from: 'ETB', to: 'DJF', rate: 3.25 },
|
||||
{ from: 'ETB', to: 'USD', rate: 0.018 },
|
||||
{ from: 'DJF', to: 'ETB', rate: 0.3077 },
|
||||
{ from: 'USD', to: 'ETB', rate: 55.56 },
|
||||
];
|
||||
|
||||
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',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
this.logger.log('Exchange rates synced successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user