Skip to content

Commit

Permalink
feat: refactor getTokenByTransferId to using join SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Feb 24, 2021
1 parent 5bd44ab commit ba53211
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
13 changes: 13 additions & 0 deletions server/repositories/TokenRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ class TokenRepository extends BaseRepository{
return result;
}

/*
* select transaction table by transfer id, return matched tokens
*/
async getByTransferId(transferId){
const result = await this._session.getDB().raw(`
SELECT "token".* FROM "token"
JOIN "transaction"
ON "token".id = "transaction".token_id
WHERE "transaction".transfer_id = '226f76cd-52b0-486b-b58a-98230696c748'
`);
return result;
}

}

module.exports = TokenRepository;
9 changes: 9 additions & 0 deletions server/repositories/TokenRepository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ describe("TokenRepository", () => {
expect(token).property("token").eq("testUuid");
});

it("getByTransferId", async () => {
tracker.on("query", (query) => {
expect(query.sql).match(/select.*token.*transaction.*transfer_id/is);
query.response([{id:1, token: "testUuid"}]);
});
const tokens = await tokenRepository.getByTransferId("testUuid");
expect(tokens).lengthOf(1);
});

});
6 changes: 2 additions & 4 deletions server/services/TokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ class TokenService{
}

async getTokensByTransferId(transferId){
const result = await this.transactionRepository.getByFilter({
transfer_id: transferId,
});
const result = await this.tokenRepository.getByTransferId(transferId);
const tokens = [];
for(const r of result){
const token = await this.getById(r.token_id);
const token = new Token(r);
tokens.push(token);
}
return tokens;
Expand Down
15 changes: 2 additions & 13 deletions server/services/TokenService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,11 @@ describe("Token", () => {
describe("getTokensByTransferId", () => {

it("Successfuly", async () => {
const tokenId1 = uuid.v4();
const tokenId2 = uuid.v4();
const transferId1 = uuid.v4();
const transactionId1 = uuid.v4();
const token = new Token({id:tokenId2});
const transaction = {
id: transactionId1,
token_id: tokenId2,
};
const fn = sinon.stub(TransactionRepository.prototype, "getByFilter").resolves([transaction]);
const fn2 = sinon.stub(TokenService.prototype, "getById").resolves(token);
const fn = sinon.stub(TokenRepository.prototype, "getByTransferId").resolves([{id:tokenId2}]);
const tokens = await tokenService.getTokensByTransferId(transferId1);
expect(fn).calledWith({
transfer_id: transferId1,
})
expect(fn2).calledWith(tokenId2);
expect(fn).calledWith(transferId1);
expect(tokens).lengthOf(1);
});
});
Expand Down

0 comments on commit ba53211

Please sign in to comment.