-
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 investments tx controller and more tests
- Loading branch information
Showing
7 changed files
with
158 additions
and
46 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
54 changes: 54 additions & 0 deletions
54
src/controllers/investments/transactions/create-transaction.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,54 @@ | ||
import { z } from 'zod'; | ||
import { API_RESPONSE_STATUS, TRANSACTION_TYPES } from 'shared-types'; | ||
import { CustomRequest, CustomResponse } from '@common/types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
|
||
import * as investmentTransactionsService from '@services/investments/transactions'; | ||
|
||
export const createInvestmentTransaction = async ( | ||
req: CustomRequest<typeof createInvestmentTransactionSchema>, | ||
res: CustomResponse, | ||
) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { accountId, securityId, transactionType, date, name, quantity, price, fees } = | ||
req.validated.body; | ||
|
||
const data = await investmentTransactionsService.createInvestmentTransaction({ | ||
userId, | ||
params: { | ||
accountId, | ||
securityId, | ||
transactionType, | ||
date, | ||
name, | ||
quantity: String(quantity), | ||
price: String(price), | ||
fees: String(fees), | ||
}, | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: data, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
const recordId = () => z.number().int().positive().finite(); | ||
export const createInvestmentTransactionBodySchema = z.object({ | ||
accountId: recordId(), | ||
securityId: recordId(), | ||
transactionType: z.nativeEnum(TRANSACTION_TYPES), | ||
date: z.string().datetime({ message: 'Invalid ISO date string' }), | ||
name: z.string().max(1000, 'The string must not exceed 1000 characters.').nullish(), | ||
quantity: z.number().positive('Quantity must be greater than 0').finite(), | ||
price: z.number().positive('Price must be greater than 0').finite(), | ||
fees: z.number().positive('Fees must be greater than 0').finite(), | ||
}); | ||
|
||
export const createInvestmentTransactionSchema = z.object({ | ||
body: createInvestmentTransactionBodySchema, | ||
}); |
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,19 @@ | ||
import { makeRequest } from '../common'; | ||
import { z } from 'zod'; | ||
import * as investmentService from '@services/investments/transactions'; | ||
import { createInvestmentTransactionBodySchema } from '@controllers/investments/transactions/create-transaction'; | ||
|
||
export function createInvestmentTransaction<R extends boolean | undefined = undefined>({ | ||
raw, | ||
payload, | ||
}: { | ||
raw?: R; | ||
payload: z.infer<typeof createInvestmentTransactionBodySchema>; | ||
}) { | ||
return makeRequest<Awaited<ReturnType<typeof investmentService.createInvestmentTransaction>>, R>({ | ||
method: 'post', | ||
url: '/investing/transaction', | ||
payload, | ||
raw, | ||
}); | ||
} |