-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add exchange rates editing service + tests
- Loading branch information
Showing
7 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { z } from 'zod'; | ||
import cc from 'currency-codes'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { CustomResponse } from '@common/types'; | ||
import * as userExchangeRates from '@services/user-exchange-rate'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
|
||
export const editCurrencyExchangeRate = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { pairs }: EditCurrencyExchangeRateParams = req.validated.body; | ||
|
||
const data = await userExchangeRates.editUserExchangeRates({ | ||
userId, | ||
pairs, | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: data, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
const isValidCurrencyCode = (code: string) => cc.code(code) !== undefined; | ||
|
||
const CurrencyCodeSchema = z.string().refine( | ||
(code) => isValidCurrencyCode(code), | ||
(code) => ({ message: `Invalid currency code: ${code}. Use ISO 4217 Code. For example: USD` }), | ||
); | ||
|
||
const UpdateExchangeRatePairSchema = z | ||
.object({ | ||
baseCode: CurrencyCodeSchema, | ||
quoteCode: CurrencyCodeSchema, | ||
rate: z.number().positive(), | ||
}) | ||
.strict(); | ||
|
||
const bodyZodSchema = z | ||
.object({ | ||
pairs: z | ||
.array(UpdateExchangeRatePairSchema) | ||
.nonempty() | ||
.refine( | ||
(pairs) => pairs.every((pair) => pair.baseCode !== pair.quoteCode), | ||
'You cannot edit pair with the same base and quote currency code.', | ||
) | ||
.refine( | ||
(pairs) => pairs.every((pair) => pairs.some((item) => item.baseCode === pair.quoteCode)), | ||
"When changing base-quote pair rate, you need to also change opposite pair's rate.", | ||
), | ||
}) | ||
.strict(); | ||
|
||
type EditCurrencyExchangeRateParams = z.infer<typeof bodyZodSchema>; | ||
|
||
export const editCurrencyExchangeRateSchema = z.object({ | ||
body: bodyZodSchema, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/services/user-exchange-rate/update-exchange-rates.service.e2e.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { expect } from '@jest/globals'; | ||
import * as helpers from '@tests/helpers'; | ||
import { ERROR_CODES } from '@js/errors'; | ||
|
||
describe('Edit currency exchange rate controller', () => { | ||
it('should fail editing currency exchange rates for non-connected currencies', async () => { | ||
const pairs = [ | ||
{ baseCode: 'USD', quoteCode: 'EUR', rate: 0.85 }, | ||
{ baseCode: 'EUR', quoteCode: 'USD', rate: 1.18 }, | ||
]; | ||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
expect(res.statusCode).toEqual(ERROR_CODES.NotFoundError); | ||
}); | ||
|
||
describe('', () => { | ||
beforeEach(async () => { | ||
// Setup: Ensure the user has the necessary currencies | ||
await helpers.addUserCurrencies({ currencyCodes: ['USD', 'EUR', 'GBP'] }); | ||
}); | ||
|
||
it('should successfully edit currency exchange rates', async () => { | ||
const allCurrencies = await helpers.getAllCurrencies(); | ||
const eur = allCurrencies.find((i) => i.code === 'EUR')!; | ||
|
||
await helpers.makeRequest({ | ||
method: 'post', | ||
url: '/user/currencies', | ||
payload: { | ||
currencies: [{ currencyId: eur.id }], | ||
}, | ||
raw: false, | ||
}); | ||
|
||
const pairs = [ | ||
{ baseCode: 'USD', quoteCode: 'EUR', rate: 0.85 }, | ||
{ baseCode: 'EUR', quoteCode: 'USD', rate: 1.18 }, | ||
]; | ||
|
||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
|
||
// Verify that edition request returned edited currencies | ||
const returnedValues = helpers.extractResponse(res); | ||
expect(['USD', 'EUR'].every((code) => returnedValues.map((r) => r.baseCode === code))).toBe( | ||
true, | ||
); | ||
|
||
const usdEurRate = returnedValues.find( | ||
(rate) => rate.baseCode === 'USD' && rate.quoteCode === 'EUR', | ||
)!; | ||
const eurUsdRate = returnedValues.find( | ||
(rate) => rate.baseCode === 'EUR' && rate.quoteCode === 'USD', | ||
)!; | ||
|
||
expect(usdEurRate.rate).toBeCloseTo(0.85); | ||
expect(eurUsdRate.rate).toBeCloseTo(1.18); | ||
}); | ||
|
||
it('should return validation error if invalid currency code is provided', async () => { | ||
const pairs = [{ baseCode: 'USD', quoteCode: 'INVALID', rate: 1.5 }]; | ||
|
||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
|
||
expect(res.statusCode).toEqual(ERROR_CODES.ValidationError); | ||
}); | ||
|
||
it('should return error when trying to edit pair with same base and quote currency', async () => { | ||
const pairs = [{ baseCode: 'USD', quoteCode: 'USD', rate: 1 }]; | ||
|
||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
|
||
expect(res.statusCode).toEqual(ERROR_CODES.ValidationError); | ||
}); | ||
|
||
it('should require opposite pair rate change', async () => { | ||
const pairs = [{ baseCode: 'USD', quoteCode: 'EUR', rate: 0.85 }]; | ||
|
||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
|
||
expect(res.statusCode).toEqual(ERROR_CODES.ValidationError); | ||
}); | ||
|
||
it('should return error when trying to edit non-existent currency pair', async () => { | ||
const pairs = [ | ||
{ baseCode: 'USD', quoteCode: 'JPY', rate: 110 }, | ||
{ baseCode: 'JPY', quoteCode: 'USD', rate: 0.0091 }, | ||
]; | ||
|
||
const res = await helpers.editCurrencyExchangeRate({ pairs }); | ||
|
||
expect(res.statusCode).toEqual(ERROR_CODES.NotFoundError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CustomResponse } from '@common/types'; // Adjust the import path as needed | ||
import { extractResponse, makeRequest } from './common'; | ||
import { editUserExchangeRates } from '@root/services/user-exchange-rate'; | ||
|
||
type ExchangeRatePair = { | ||
baseCode: string; | ||
quoteCode: string; | ||
rate: number; | ||
}; | ||
|
||
type RatesReturnType<T> = T extends true | ||
? Awaited<ReturnType<typeof editUserExchangeRates>> | ||
: CustomResponse; | ||
|
||
export async function editCurrencyExchangeRate<T extends boolean = false>({ | ||
pairs, | ||
raw = false as T, | ||
}: { | ||
pairs: ExchangeRatePair[]; | ||
raw?: T; | ||
}): Promise<RatesReturnType<T>> { | ||
const result: RatesReturnType<T> = await makeRequest({ | ||
method: 'put', | ||
url: '/user/currency/rates', | ||
payload: { pairs }, | ||
raw, | ||
}); | ||
|
||
return raw ? extractResponse(result) : result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters