Skip to content

Commit

Permalink
feat: add GET /transer_id & GET /transfer_id/tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Dec 15, 2020
1 parent 8554aad commit ab8aaf6
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 0 deletions.
26 changes: 26 additions & 0 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,32 @@ class Wallet{
return result;
}

async getTransferById(id){
const transfers = await this.getTransfers();
const transfer = transfers.reduce((a,c) => {
if(c.id === id){
return c;
}else{
return a;
}
}, undefined);
if(!transfer){
throw new HttpError(404, "Can not find this transfer or it is related to this wallet");
}
return transfer;
}

async getTokensByTransferId(id){
const transfer = await this.getTransferById(id);
let tokens;
if(transfer.state === Transfer.STATE.completed){
tokens = await this.tokenService.getTokensByTransferId(transfer.id);
}else{
tokens = await this.tokenService.getTokensByPendingTransferId(transfer.id);
}
return tokens;
}

/*
* Check if it is deduct, if ture, throw 403, cuz we do not support it yet
*/
Expand Down
48 changes: 48 additions & 0 deletions server/models/Wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,4 +1177,52 @@ describe("Wallet", () => {
});
});

describe("getTransferById", () => {

it("Successfully", async () => {
sinon.stub(Wallet.prototype, "getTransfers").resolves([{
id: 1,
}]);
const transfer = await wallet.getTransferById(1);
expect(transfer).property("id").eq(1);
});

it("Not fond", async () => {
sinon.stub(Wallet.prototype, "getTransfers").resolves([]);
await jestExpect(async () => {
await wallet.getTransferById(1);
}).rejects.toThrow(/not find/);
});
});

describe("getTokensByTransferId", () => {

it("Completed transfer", async () => {
const transfer = {
id: 1,
state: Transfer.STATE.completed,
};
const token = new Token(1);
sinon.stub(Wallet.prototype, "getTransferById").resolves(transfer);
const fn = sinon.stub(TokenService.prototype, "getTokensByTransferId").resolves([token]);
const tokens = await wallet.getTokensByTransferId(1);
expect(tokens).lengthOf(1);
expect(fn).calledWith(1);
});

it("Pending/requested transfer", async () => {
const transfer = {
id: 1,
state: Transfer.STATE.pending,
};
const token = new Token(1);
sinon.stub(Wallet.prototype, "getTransferById").resolves(transfer);
const fn = sinon.stub(TokenService.prototype, "getTokensByPendingTransferId").resolves([token]);
const tokens = await wallet.getTokensByTransferId(1);
expect(tokens).lengthOf(1);
expect(fn).calledWith(1);
});

});

});
45 changes: 45 additions & 0 deletions server/routes/transferRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,49 @@ transferRouter.get("/",
})
);

transferRouter.get('/:transfer_id',
helper.apiKeyHandler,
helper.verifyJWTHandler,
helper.handlerWrapper(async (req, res) => {
Joi.assert(
req.params,
Joi.object({
transfer_id: Joi.number().required(),
})
);
const session = new Session();
const walletService = new WalletService(session);
const transferService = new TransferService(session);
const walletLogin = await walletService.getById(res.locals.wallet_id);
const transferObject = await walletLogin.getTransferById(parseInt(req.params.transfer_id));
const transferJson = await transferService.convertToResponse(transferObject);
res.status(200).json(transferJson);
})
);

transferRouter.get('/:transfer_id/tokens',
helper.apiKeyHandler,
helper.verifyJWTHandler,
helper.handlerWrapper(async (req, res) => {
Joi.assert(
req.params,
Joi.object({
transfer_id: Joi.number().required(),
})
);
const session = new Session();
const walletService = new WalletService(session);
const walletLogin = await walletService.getById(res.locals.wallet_id);
const tokens = await walletLogin.getTokensByTransferId(parseInt(req.params.transfer_id));
const tokensJson = [];
for(const token of tokens){
const json = await token.toJSON();
tokensJson.push(json);
}
res.status(200).json({
tokens: tokensJson,
});
})
);

module.exports = transferRouter;
35 changes: 35 additions & 0 deletions server/routes/transferRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,39 @@ describe("transferRouter", () => {
});
});

describe("GET /{transfer_id}", () => {

it("Successfully", async () => {
const wallet = new Wallet(1);
const transfer = {id:2};
const fn = sinon.stub(WalletService.prototype, "getById").resolves(wallet);
const fn2 = sinon.stub(Wallet.prototype, "getTransferById").resolves(transfer);
const fn3 = sinon.stub(TransferService.prototype, "convertToResponse").resolves(transfer);
const res = await request(app)
.get("/2");
expect(fn).calledWith(1);
expect(fn2).calledWith(2);
expect(fn3).calledWith(transfer);
expect(res).property("statusCode").eq(200);
expect(res.body).property("id").eq(2);
});
});

describe.only("GET /{transfer_id}/tokens", () => {

it("Successfully", async () => {
const wallet = new Wallet(1);
const transfer = {id:2};
const token = new Token({id:3});
const fn = sinon.stub(WalletService.prototype, "getById").resolves(wallet);
const fn2 = sinon.stub(Wallet.prototype, "getTokensByTransferId").resolves([token]);
const res = await request(app)
.get("/2/tokens");
expect(fn).calledWith(1);
expect(fn2).calledWith(2);
expect(res).property("statusCode").eq(200);
expect(res.body).property("tokens").lengthOf(1);
});
})

});
11 changes: 11 additions & 0 deletions server/services/TokenService.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const Token = require("../models/Token");
const TokenRepository = require("../repositories/TokenRepository");
const TransactionRepository = require("../repositories/TransactionRepository");
const expect = require("expect-runtime");

class TokenService{

constructor(session){
this._session = session
this.tokenRepository = new TokenRepository(session);
this.transactionRepository = new TransactionRepository(session);
const WalletService = require("../services/WalletService");
this.walletService = new WalletService(session);
}
Expand Down Expand Up @@ -97,6 +99,15 @@ class TokenService{
return result;
}

async getTokensByTransferId(transferId){
const result = await this.transactionRepository.getByFilter({
transfer_id: transferId,
});
return result.map(object => {
return new Token(object, this._session);
});
}

}

module.exports = TokenService;
14 changes: 14 additions & 0 deletions server/services/TokenService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Wallet = require("../models/Wallet");
const Token = require("../models/Token");
const WalletService = require("../services/WalletService");
const Session = require("../models/Session");
const TransactionRepository = require("../repositories/TransactionRepository");

describe("Token", () => {
let tokenService;
Expand Down Expand Up @@ -83,4 +84,17 @@ describe("Token", () => {
expect(result).property("receiver_wallet").eq("testName");
});

describe("getTokensByTransferId", () => {

it("Successfuly", async () => {
const fn = sinon.stub(TransactionRepository.prototype, "getByFilter").resolves([{id:1}]);
const tokens = await tokenService.getTokensByTransferId(1);
expect(tokens).lengthOf(1);
expect(fn).calledWith({
transfer_id: 1,
})
});
});


});

0 comments on commit ab8aaf6

Please sign in to comment.