Skip to content
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

Merged

Conversation

yunchipang
Copy link
Contributor

@yunchipang yunchipang commented Nov 8, 2024

Description

completes #491

image

What kind of change(s) does this PR introduce?

  • Enhancement
  • Bug fix
  • Refactor

Please check if the PR fulfils these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Issue

What is the current behavior?

What is the new behavior?

Breaking change

Does this PR introduce a breaking change?

Other useful information

@yunchipang yunchipang marked this pull request as ready for review November 13, 2024 21:24
Copy link
Collaborator

@Kpoke Kpoke left a 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);
Copy link
Collaborator

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,
Copy link
Collaborator

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) {
Copy link
Collaborator

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


return {
message: 'Batch transfer successful',
};
} catch (e) {
await fs.unlink(filePath);
if (!process.env.JEST_WORKER_ID) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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

Copy link
Collaborator

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

@yunchipang
Copy link
Contributor Author

image
image

@yunchipang yunchipang requested a review from Kpoke November 20, 2024 00:04
@yunchipang yunchipang changed the title feat: add batchCreateWallet service & controller feat: batchCreateWallet & batchTransferWallet service and controller Nov 20, 2024
token_transfer_amount_overwrite?: number;
}

export class BatchTransferWalletDto {
Copy link
Collaborator

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.

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

@yunchipang yunchipang requested a review from Kpoke November 20, 2024 22:34

import { Type } from 'class-transformer';

// Custom validator to check for unique `wallet_name` values in the csvJson array
Copy link
Contributor Author

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);
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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!!

@yunchipang
Copy link
Contributor Author

yunchipang commented Dec 20, 2024

@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!

test 1: when wallet_name is empty
Screenshot 2024-12-20 at 11 48 53 AM

test 2: when fields are ideal
Screenshot 2024-12-20 at 11 48 06 AM
image

@yunchipang
Copy link
Contributor Author

@Kpoke batch-transfer API is ready as well.
Screenshot 2024-12-22 at 8 53 57 PM

}[],
filePath: string,
): Promise<{ message: string }> {
const queryRunner = this.dataSource.createQueryRunner();
Copy link
Contributor Author

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.

@Kpoke Kpoke merged commit 9b50020 into Greenstand:typescript-branch Jan 4, 2025
1 check passed
@yunchipang yunchipang deleted the feat/batch-create-wallet-func branch January 4, 2025 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants