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
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
39c80b4
feat: add batchCreateWallet service & controller
yunchipang Nov 8, 2024
1d5febd
test: batchCreateWallet test passed
yunchipang Nov 13, 2024
b1ff980
feat: use uuid as unique identifier
yunchipang Nov 19, 2024
aaa421c
feat: add BatchCreateWalletDto
yunchipang Nov 19, 2024
2ecf603
feat: move error handling from controller to service
yunchipang Nov 19, 2024
9ff3e6f
test: add batchTransferWallet tests to wallet.service.spec.ts
yunchipang Nov 19, 2024
d9e1845
test: add failure test cases to batchCreateWallet in wallet.service.s…
yunchipang Nov 19, 2024
b6d5586
test: add batchCreateWallet test suite in wallet.controller.spec.ts
yunchipang Nov 20, 2024
d9ef7e3
test: mock fs module in batchCreateWallet unit tests
yunchipang Nov 20, 2024
ebffa42
test: update wallet controller test
yunchipang Nov 20, 2024
5080e99
feat: add validations to DTOs
yunchipang Nov 20, 2024
528ae11
feat: add csv file filter util and test
yunchipang Nov 22, 2024
b6d1361
test: update batchTransferWallet test to take a csv input file
yunchipang Nov 22, 2024
44fadf4
test: update batchTransferWallet tests to match the pattern of batchC…
yunchipang Nov 22, 2024
3ac0069
feat: add CsvItemDto for validation
yunchipang Dec 4, 2024
d8bb927
feat: add validation pipe to batchWalletTransfer as well
yunchipang Dec 4, 2024
f328495
feat: batch-create-wallet endpoint protected by csv file check
yunchipang Dec 17, 2024
783f304
feat: batch-create-wallet endpoint success
yunchipang Dec 20, 2024
8a4b37c
feat: batch-transfer endpoint success
yunchipang Dec 23, 2024
5879088
test: fix npm tests
yunchipang Dec 23, 2024
250ee4a
feat: make batchCreateWallet and batchTransferWallet database transac…
yunchipang Jan 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added eslint
Empty file.
2,007 changes: 1,370 additions & 637 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"db-migrate-ci": "cd database; ../node_modules/db-migrate/bin/db-migrate --env dev up"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.637.0",
"@aws-sdk/client-s3": "^3.85.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
Expand All @@ -49,6 +49,7 @@
"axios": "^1.7.5",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"csvtojson": "^2.0.10",
"db-migrate": "^0.11.14",
"db-migrate-pg": "^1.5.2",
"dotenv": "^16.4.5",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"chai": "^4.5.0",
"commitlint": "^19.5.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
248 changes: 167 additions & 81 deletions src/modules/wallet/__tests__/wallet.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import {
import { HttpStatus, HttpException } from '@nestjs/common';
import { UpdateWalletDto } from '../dto/update-wallet.dto';

jest.mock('csvtojson', () => {
return jest.fn(() => ({
fromFile: jest.fn(() =>
Promise.resolve([
{ wallet_name: 'wallet1', token_transfer_amount_overwrite: 50 },
{ wallet_name: 'wallet2' },
]),
),
}));
});

describe('WalletController', () => {
let walletController: WalletController;
let walletService: WalletService;
Expand All @@ -29,6 +40,7 @@ describe('WalletController', () => {
useValue: {
getById: jest.fn(),
updateWallet: jest.fn(),
batchCreateWallet: jest.fn(),
batchTransferWallet: jest.fn(),
},
},
Expand Down Expand Up @@ -102,87 +114,6 @@ describe('WalletController', () => {
expect(result).toEqual(trustRelationships);
});

describe('batchTransfer', () => {
it('should return batch transfer successful', async () => {
const transferData = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'sender_wallet_id',
csvJson: [
{
wallet_name: 'recipient_wallet_1',
token_transfer_amount_overwrite: 50,
},
{ wallet_name: 'recipient_wallet_2' },
],
filePath: '/path/to/file.csv',
};

jest
.spyOn(walletService, 'batchTransferWallet')
.mockResolvedValue({ message: 'Batch transfer successful' });

const result = await walletController.batchTransfer(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
);

expect(result).toEqual({ message: 'Batch transfer successful' });
expect(walletService.batchTransferWallet).toHaveBeenCalledWith(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
);
});
it('should return batch transfer failure ', async () => {
const transferData = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'sender_wallet_id',
csvJson: [
{
wallet_name: 'recipient_wallet_1',
token_transfer_amount_overwrite: 50,
},
{ wallet_name: 'recipient_wallet_2' },
],
filePath: '/path/to/file.csv',
};

jest
.spyOn(walletService, 'batchTransferWallet')
.mockRejectedValue(
new HttpException(
'Failed to process batch transfer',
HttpStatus.INTERNAL_SERVER_ERROR,
),
);

await expect(
walletController.batchTransfer(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
),
).rejects.toThrow(HttpException);

expect(walletService.batchTransferWallet).toHaveBeenCalledWith(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
);
});
});

it('updateWallet', async () => {
const walletId = uuid.v4();
const updateWalletDto: UpdateWalletDto = {
Expand Down Expand Up @@ -254,4 +185,159 @@ describe('WalletController', () => {
walletId,
);
});

describe('batchCreateWallet', () => {
it('should return batch wallet creation successful', async () => {
const batchCreateWalletDto = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'parent_wallet_id',
};

jest.spyOn(walletService, 'batchCreateWallet').mockResolvedValue({
message: 'Batch wallet creation successful',
});

const mockUploadedFile: Express.Multer.File = {
fieldname: 'file',
originalname: 'test.csv',
encoding: '7bit',
mimetype: 'text/csv',
destination: './uploads',
filename: 'file-unique-id-test.csv',
path: './uploads/file-unique-id-test.csv',
size: 1024,
} as Express.Multer.File;

const result = await walletController.batchCreateWallet(
batchCreateWalletDto,
mockUploadedFile,
);

expect(result).toEqual({ message: 'Batch wallet creation successful' });
expect(walletService.batchCreateWallet).toHaveBeenCalledWith(
batchCreateWalletDto.sender_wallet,
batchCreateWalletDto.token_transfer_amount_default,
batchCreateWalletDto.wallet_id,
[
{ wallet_name: 'wallet1', token_transfer_amount_overwrite: 50 },
{ wallet_name: 'wallet2' },
],
mockUploadedFile.path,
);
});

it('should return batch wallet creation failure', async () => {
const batchCreateWalletDto = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'parent_wallet_id',
};

jest
.spyOn(walletService, 'batchCreateWallet')
.mockRejectedValue(
new Error('Failed to process batch wallet creation'),
);

const mockUploadedFile: Express.Multer.File = {
fieldname: 'file',
originalname: 'test.csv',
encoding: '7bit',
mimetype: 'text/csv',
destination: './uploads',
filename: 'file-unique-id-test.csv',
path: './uploads/file-unique-id-test.csv',
size: 1024,
} as Express.Multer.File;

await expect(
walletController.batchCreateWallet(
batchCreateWalletDto,
mockUploadedFile,
),
).rejects.toThrow('Failed to process batch wallet creation');

expect(walletService.batchCreateWallet).toHaveBeenCalledWith(
batchCreateWalletDto.sender_wallet,
batchCreateWalletDto.token_transfer_amount_default,
batchCreateWalletDto.wallet_id,
[
{ wallet_name: 'wallet1', token_transfer_amount_overwrite: 50 },
{ wallet_name: 'wallet2' },
],
mockUploadedFile.path,
);
});
});

describe('batchTransferWallet', () => {
it('should return batch transfer wallet successful', async () => {
const transferData = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'sender_wallet_id',
csvJson: [
{
wallet_name: 'recipient_wallet_1',
token_transfer_amount_overwrite: 50,
},
{ wallet_name: 'recipient_wallet_2' },
],
filePath: '/path/to/file.csv',
};

jest
.spyOn(walletService, 'batchTransferWallet')
.mockResolvedValue({ message: 'Batch transfer successful' });

const result = await walletController.batchTransfer(transferData);

expect(result).toEqual({ message: 'Batch transfer successful' });
expect(walletService.batchTransferWallet).toHaveBeenCalledWith(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
);
});

it('should return batch transfer wallet failure', async () => {
const transferData = {
sender_wallet: 'sender_wallet_name',
token_transfer_amount_default: 100,
wallet_id: 'sender_wallet_id',
csvJson: [
{
wallet_name: 'recipient_wallet_1',
token_transfer_amount_overwrite: 50,
},
{ wallet_name: 'recipient_wallet_2' },
],
filePath: '/path/to/file.csv',
};

jest
.spyOn(walletService, 'batchTransferWallet')
.mockRejectedValue(
new HttpException(
'Failed to process batch transfer',
HttpStatus.INTERNAL_SERVER_ERROR,
),
);

await expect(
walletController.batchTransfer(transferData),
).rejects.toThrow(HttpException);

expect(walletService.batchTransferWallet).toHaveBeenCalledWith(
transferData.sender_wallet,
transferData.token_transfer_amount_default,
transferData.wallet_id,
transferData.csvJson,
transferData.filePath,
);
});
});
});
Loading
Loading