Skip to content

Commit

Permalink
fix: add /wallet/uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Nov 5, 2022
1 parent e94c05e commit 34b6f55
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .env.ci

This file was deleted.

56 changes: 42 additions & 14 deletions docs/api/spec/treetracker-wallet-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ paths:
'401':
$ref: '#/components/responses/UnauthorizedError'
deprecated: false
'/wallets/{wallet_uuid}':
get:
tags:
- Managed wallets
summary: Get details for one wallet, including number of tokens
parameters:
- $ref: '#/components/parameters/treetrackerApiKeyParam'
- $ref: '#/components/parameters/contentTypeJsonHeader'
- name: wallet_uuid
description: ''
in: path
required: true
schema:
type: string
example: 496ffa8e-2fa2-488c-98e1-acf9b57c230b
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/walletItem'
'401':
$ref: '#/components/responses/UnauthorizedError'
deprecated: false
/transfers:
post:
tags:
Expand Down Expand Up @@ -760,17 +785,17 @@ components:
capture:
type: string
example:
id: "ca1a99c3-9797-465d-8ee3-8dbd26a08faa"
capture_id: "8df635f6-2df8-496f-93da-3060fc1e3180"
wallet_id: "c63da6b3-d7a3-444a-9ff0-10644036eccd"
id: 'ca1a99c3-9797-465d-8ee3-8dbd26a08faa'
capture_id: '8df635f6-2df8-496f-93da-3060fc1e3180'
wallet_id: 'c63da6b3-d7a3-444a-9ff0-10644036eccd'
transfer_pending: false
transfer_pending_id: null
created_at: "2021-07-02T18:31:07.208Z"
updated_at: "2021-07-02T18:31:07.208Z"
created_at: '2021-07-02T18:31:07.208Z'
updated_at: '2021-07-02T18:31:07.208Z'
links:
type: object
properties:
capture: "/webmap/tree?uuid=8df635f6-2df8-496f-93da-3060fc1e3180"
capture: '/webmap/tree?uuid=8df635f6-2df8-496f-93da-3060fc1e3180'
accountrequest:
title: accountrequest
required:
Expand All @@ -791,6 +816,9 @@ components:
walletItem:
title: Wallet Details
properties:
id:
type: string
format: uuid
wallet:
type: string
email:
Expand All @@ -810,12 +838,12 @@ components:
description: 'Explicitly transfer tokens'
sender_wallet:
oneOf:
- type: string
- type: number
- type: string
- type: number
receiver_wallet:
oneOf:
- type: string
- type: number
- type: string
- type: number
required:
- tokens
- sender_wallet
Expand All @@ -830,12 +858,12 @@ components:
description: required number of trees to transfer
sender_wallet:
oneOf:
- type: string
- type: number
- type: string
- type: number
receiver_wallet:
oneOf:
- type: string
- type: number
- type: string
- type: number
required:
- bundle
- sender_wallet
Expand Down
19 changes: 19 additions & 0 deletions server/handlers/walletHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ describe('walletRouter', () => {
});
});

describe('get /wallets/:wallet_id', () => {
it('walletId should be guid', async () => {
const res = await request(app).get(`/wallets/walletId`);
expect(res).property('statusCode').eq(422);
expect(res.body.message).match(/wallet_id.*GUID/);
});

it('successfully', async () => {
const walletId = uuid.v4();
const getWalletStub = sinon
.stub(WalletService.prototype, 'getWallet')
.resolves({ id: walletId });
const res = await request(app).get(`/wallets/${walletId}`);
expect(res).property('statusCode').eq(200);
expect(res.body).eql({ id: walletId });
expect(getWalletStub.calledOnceWithExactly(walletId)).eql(true);
});
});

describe('post /wallets', () => {
const walletId = uuid.v4();
const mockWallet = { id: walletId, wallet: 'test-wallet-2' };
Expand Down
15 changes: 14 additions & 1 deletion server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const walletGet = async (req, res) => {
res.status(200).json({ wallets });
};

const walletSingleGet = async (req, res) => {
await walletIdParamSchema.validateAsync(req.params, { abortEarly: false });
const walletService = new WalletService();

const wallet = await walletService.getWallet(req.params.wallet_id);
res.status(200).send(wallet);
};

const walletGetTrustRelationships = async (req, res) => {
await walletIdParamSchema.validateAsync(req.params, { abortEarly: false });
await walletGetTrustRelationshipsSchema.validateAsync(req.query, {
Expand Down Expand Up @@ -53,4 +61,9 @@ const walletPost = async (req, res) => {
});
};

module.exports = { walletPost, walletGetTrustRelationships, walletGet };
module.exports = {
walletPost,
walletGetTrustRelationships,
walletGet,
walletSingleGet,
};
15 changes: 15 additions & 0 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const WalletRepository = require('../repositories/WalletRepository');
const TrustRepository = require('../repositories/TrustRepository');
const TrustRelationshipEnums = require('../utils/trust-enums');
const HttpError = require('../utils/HttpError');
const TokenRepository = require('../repositories/TokenRepository');

class Wallet {
constructor(session) {
this._session = session;
this._walletRepository = new WalletRepository(session);
this._trustRepository = new TrustRepository(session);
this._tokenRepository = new TokenRepository(session);
}

async createWallet(loggedInWalletId, wallet) {
Expand Down Expand Up @@ -48,6 +50,19 @@ class Wallet {
return this._walletRepository.getById(id);
}

async getWallet(walletId) {
const wallet = await this._walletRepository.getById(walletId);
const tokenCount = await this._tokenRepository.countByFilter({
wallet_id: walletId,
});
const walletName = wallet.name;
delete wallet.password;
delete wallet.salt;
delete wallet.created_at;
delete wallet.name;
return { ...wallet, wallet: walletName, tokens_in_wallet: tokenCount };
}

async getByName(name) {
return this._walletRepository.getByName(name);
}
Expand Down
18 changes: 18 additions & 0 deletions server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const TrustRepository = require('../repositories/TrustRepository');
const HttpError = require('../utils/HttpError');
const Session = require('../infra/database/Session');
const TrustRelationshipEnums = require('../utils/trust-enums');
const TokenRepository = require('../repositories/TokenRepository');

describe('Wallet Model', () => {
let walletModel;
Expand Down Expand Up @@ -99,6 +100,23 @@ describe('Wallet Model', () => {
expect(walletRepositoryStub.getById).calledOnceWithExactly(walletId);
});

it('getWallet function', async () => {
const walletId = uuid();
walletRepositoryStub.getById.resolves({ id: walletId, name: 'wallet' });
const tokenRepositoryStub = sinon
.stub(TokenRepository.prototype, 'countByFilter')
.resolves(20);
const result = await walletModel.getWallet(walletId);

expect(result).eql({
id: walletId,
wallet: 'wallet',
tokens_in_wallet: 20,
});
expect(walletRepositoryStub.getById).calledOnceWithExactly(walletId);
expect(tokenRepositoryStub).calledOnceWithExactly({ wallet_id: walletId });
});

it('getByName function', async () => {
const walletId = uuid();
const wallet = 'wallet';
Expand Down
2 changes: 2 additions & 0 deletions server/routes/walletRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const {
walletGet,
walletGetTrustRelationships,
walletPost,
walletSingleGet,
} = require('../handlers/walletHandler');

router.get('/', handlerWrapper(walletGet));
router.get('/:wallet_id', handlerWrapper(walletSingleGet));

// TO DO: Add below route to yaml
router.get(
Expand Down
4 changes: 4 additions & 0 deletions server/services/WalletService.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class WalletService {
return this._wallet.getByName(name);
}

async getWallet(walletId) {
return this._wallet.getWallet(walletId);
}

async getByIdOrName(idOrName) {
let wallet;
if (uuidValidate(idOrName)) {
Expand Down
12 changes: 12 additions & 0 deletions server/services/WalletService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe('WalletService', () => {
Wallet.prototype.getById.restore();
});

it('getWallet', async () => {
const walletId1 = uuid.v4();
sinon
.stub(Wallet.prototype, 'getWallet')
.resolves({ id: walletId1, name: 'walletId1' });
expect(walletService).instanceOf(WalletService);
const wallet = await walletService.getWallet(walletId1);
expect(wallet.id).eql(walletId1);
expect(wallet.name).eql('walletId1');
Wallet.prototype.getWallet.restore();
});

it('getByName', async () => {
const walletId1 = uuid.v4();
sinon
Expand Down

0 comments on commit 34b6f55

Please sign in to comment.