-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: batchCreateWallet & batchTransferWallet service and controller #492
feat: batchCreateWallet & batchTransferWallet service and controller #492
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, you can look into updating the tests for the service file, maybe in another PR or something.
destination: './uploads', | ||
filename: (req, file, callback) => { | ||
const uniqueSuffix = | ||
Date.now() + '-' + Math.round(Math.random() * 1e9); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option could be to use a uuid
}), | ||
) | ||
async batchCreateWallet( | ||
@Body('sender_wallet') senderWallet: string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a dto folder and a dto file that matches the body. So we don't have to have @Body three times, we can just have one
csvJson, | ||
file.path, | ||
); | ||
} catch (error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have the try catch in the service file
src/modules/wallet/wallet.service.ts
Outdated
|
||
return { | ||
message: 'Batch transfer successful', | ||
}; | ||
} catch (e) { | ||
await fs.unlink(filePath); | ||
if (!process.env.JEST_WORKER_ID) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kpoke to skip fs.unlink(filePath)
if the wallet service was called in tests. because deleting a file seems unnecessary while in test as we were justing mocking it. please lmk if there's a better way to handle this! tysm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would also be mocking the fs module for unit tests, so there's no need to check for that. And for integration tests we would be using a mock file to ensure it all works as it should
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you can remove it
token_transfer_amount_overwrite?: number; | ||
} | ||
|
||
export class BatchTransferWalletDto { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can you crosscheck and confirm the validations? If any field is required, use IsNotEmpty; if not, use IsOptional decorators. Also, verify if there are any other types of validations applied.
treetracker-wallet-api/server/handlers/walletHandler/schemas.js
Lines 63 to 100 in bf235b0
const walletBatchCreateBodySchema = Joi.object({ | |
sender_wallet: Joi.string(), | |
token_transfer_amount_default: Joi.number().integer(), | |
}).with('token_transfer_amount_default', 'sender_wallet'); | |
const csvValidationSchema = Joi.array() | |
.items( | |
Joi.object({ | |
wallet_name: Joi.string().trim().required(), | |
token_transfer_amount_overwrite: [ | |
Joi.number().integer(), | |
Joi.string().valid(''), | |
], | |
extra_wallet_data_about: Joi.string(), | |
}), | |
) | |
.unique('wallet_name') | |
.min(1) | |
.max(2500); | |
const csvValidationSchemaTransfer = Joi.array() | |
.items( | |
Joi.object({ | |
wallet_name: Joi.string().trim().required(), | |
token_transfer_amount_overwrite: [ | |
Joi.number().integer(), | |
Joi.string().valid(''), | |
], | |
}), | |
) | |
.unique('wallet_name') | |
.min(1) | |
.max(2500); | |
const walletBatchTransferBodySchema = Joi.object({ | |
sender_wallet: Joi.string().required(), | |
token_transfer_amount_default: Joi.number().integer(), | |
}).with('token_transfer_amount_default', 'sender_wallet'); |
For instance, wallet_name should be unique in the csvJson array. And there is no corresponding csvJsonArray validation for BatchCreateWallet
|
||
import { Type } from 'class-transformer'; | ||
|
||
// Custom validator to check for unique `wallet_name` values in the csvJson array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kpoke validations are added here.
expect(walletService.getByName).toHaveBeenCalledWith(senderWallet); | ||
expect(walletService.createWallet).not.toHaveBeenCalled(); | ||
expect(transferService.transferBundle).not.toHaveBeenCalled(); | ||
expect(fs.promises.unlink).toHaveBeenCalledWith(filePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kpoke i've removed the check for jest ID from service logic, and added a test for deleting file here.
} | ||
} | ||
|
||
export class CsvItemDto { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kpoke i've added this CsvItemDto
for validation purposes for both batchCreateWallet
and batchTransferWallet
. let me know if this is what you're looking for. thanks!!
@Kpoke i made some adjustments and successfully posted to the batch-create-wallet endpoint. batch-transfer has not been tested. just leaving the checkpoint here. thanks! |
@Kpoke |
}[], | ||
filePath: string, | ||
): Promise<{ message: string }> { | ||
const queryRunner = this.dataSource.createQueryRunner(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kpoke batchCreateWallet
and batchTransferWallet
are made database transactions.
Description
completes #491
What kind of change(s) does this PR introduce?
Please check if the PR fulfils these requirements
Issue
What is the current behavior?
What is the new behavior?
Breaking change
Does this PR introduce a breaking change?
Other useful information