Skip to content

Commit

Permalink
Merge pull request #67 from letehaha/feat/add-custom-field-to-rates
Browse files Browse the repository at this point in the history
feat: Add `custom` field to exchange rates response
  • Loading branch information
letehaha committed Jun 27, 2023
2 parents c9c2713 + ff52ba7 commit b91d60b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/models/ExchangeRates.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { Op } from 'sequelize';
import { Transaction } from 'sequelize/types';
import Currencies from './Currencies.model';

interface ModelOptions {
transaction?: Transaction,
raw?: boolean,
}

@Table({
timestamps: true,
createdAt: 'date',
Expand Down Expand Up @@ -45,7 +50,7 @@ export async function getRatesForCurrenciesPairs(
baseCode: string;
quoteCode: string;
}[],
{ transaction }: { transaction?: Transaction } = {},
modelOptions: ModelOptions,
) {
return ExchangeRates.findAll({
where: {
Expand All @@ -56,6 +61,6 @@ export async function getRatesForCurrenciesPairs(
}
}))
},
transaction,
...modelOptions,
})
}
14 changes: 10 additions & 4 deletions src/models/UserExchangeRates.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import Currencies, { getCurrencies } from './Currencies.model';
import Users from './Users.model';
import { ValidationError } from '@js/errors';

interface ModelOptions {
transaction?: Transaction,
raw?: boolean,
}

@Table({ timestamps: true })
export default class UserExchangeRates extends Model {
@Column({
Expand Down Expand Up @@ -48,11 +53,11 @@ export interface ExchangeRatePair {
}
export async function getRates(
{ userId, pair }: { userId: number; pair: ExchangeRatePair },
{ transaction }: { transaction?: Transaction },
modelOptions: ModelOptions,
);
export async function getRates(
{ userId, pairs }: { userId: number; pairs: ExchangeRatePair[] },
{ transaction }: { transaction?: Transaction },
modelOptions: ModelOptions,
);
export async function getRates(
{
Expand All @@ -64,7 +69,7 @@ export async function getRates(
pair?: ExchangeRatePair,
pairs?: ExchangeRatePair[],
},
{ transaction }: { transaction?: Transaction } = {},
modelOptions: ModelOptions,
) {
const where: Record<string|symbol, unknown> = {
userId,
Expand Down Expand Up @@ -95,7 +100,8 @@ export async function getRates(

return UserExchangeRates.findAll({
where,
transaction,
attributes: { exclude: ['userId'] },
...modelOptions,
})
}

Expand Down
9 changes: 6 additions & 3 deletions src/services/user-exchange-rate/get-exchange-rate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export async function getExchangeRate(
const [userExchangeRate] = await UserExchangeRates.getRates({
userId,
pair,
}, { transaction });
}, { transaction, raw: true });

if (userExchangeRate) return userExchangeRate
if (userExchangeRate) {
// Add `custom` so client can understand which rate is custom
return { ...userExchangeRate, custom: true }
}

const [exchangeRate] = await ExchangeRates.getRatesForCurrenciesPairs(
[pair],
{ transaction },
{ transaction, raw: true },
);

return exchangeRate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import * as UsersCurrencies from '@models/UsersCurrencies.model';

import { getExchangeRate } from './get-exchange-rate.service';

/**
* By default we just return system exchange rates from ExchangeRates table.
* If user wants to edit exchange rate, he can add one to UserExchangeRates, so
* then we will return and use his custom rate. If user wants to use system rate
* back, we need to remove his custom record from UserExchangeRates table
*/

export async function getUserExchangeRates(
{ userId }: { userId: number },
{ transaction }: { transaction?: Transaction } = {},
Expand Down

0 comments on commit b91d60b

Please sign in to comment.