-
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.
Merge pull request #134 from letehaha/fix/issues
App stability improvements
- Loading branch information
Showing
67 changed files
with
1,489 additions
and
626 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
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
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
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,44 @@ | ||
import { z } from 'zod'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { CustomResponse } from '@common/types'; | ||
import * as userCurrenciesService from '@services/currencies/add-user-currency'; | ||
import { errorHandler } from '../helpers'; | ||
|
||
export const addUserCurrencies = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { currencies }: AddUserCurrenciesParams = req.validated.body; | ||
|
||
const result = await userCurrenciesService.addUserCurrencies( | ||
currencies.map((item) => ({ userId, ...item })), | ||
); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: result, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
const recordId = () => z.number().int().positive().finite(); | ||
const UserCurrencySchema = z | ||
.object({ | ||
currencyId: recordId(), | ||
exchangeRate: z.number().positive().optional(), | ||
liveRateUpdate: z.boolean().optional(), | ||
}) | ||
.strict(); | ||
|
||
const bodyZodSchema = z | ||
.object({ | ||
currencies: z.array(UserCurrencySchema).nonempty(), | ||
}) | ||
.strict(); | ||
|
||
type AddUserCurrenciesParams = z.infer<typeof bodyZodSchema>; | ||
|
||
export const addUserCurrenciesSchema = 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
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
Oops, something went wrong.