From 08afc78ff2803d3480e74b11331264ae1e7cfe0c Mon Sep 17 00:00:00 2001 From: xianglupeng Date: Mon, 31 Jul 2023 16:57:27 -0400 Subject: [PATCH] fix: bug fix and removed commented blocks --- server/infra/database/knex.js | 2 -- server/models/Transfer.js | 4 ---- server/models/Trust.js | 5 ----- server/repositories/BaseRepository.js | 21 +----------------- server/repositories/TokenRepository.js | 26 +++++++---------------- server/repositories/TransferRepository.js | 5 ----- server/repositories/WalletRepository.js | 13 ------------ 7 files changed, 9 insertions(+), 67 deletions(-) diff --git a/server/infra/database/knex.js b/server/infra/database/knex.js index f14bbe48..97a9c578 100644 --- a/server/infra/database/knex.js +++ b/server/infra/database/knex.js @@ -1,11 +1,9 @@ require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }); const Joi = require('joi'); -// const expect = require('expect-runtime'); const log = require('loglevel'); const knex = require('knex'); const connection = require('../../../config/config').connectionString; -// expect(connection).to.match(/^postgresql:\//); Joi.assert(connection, Joi.string().pattern(/^postgresql:\//)); const knexConfig = { client: 'pg', diff --git a/server/models/Transfer.js b/server/models/Transfer.js index c5b86a18..f81052da 100644 --- a/server/models/Transfer.js +++ b/server/models/Transfer.js @@ -365,10 +365,6 @@ class Transfer { } else { log.debug('transfer tokens'); const tokens = await this._token.getTokensByPendingTransferId(transferId); - // expect(transfer).match({ - // source_wallet_id: expect.any(String), - // }); - Joi.assert(transfer, Joi.object({ source_wallet_id: Joi.string().required() }).unknown()); diff --git a/server/models/Trust.js b/server/models/Trust.js index efbcb106..6832a2ec 100644 --- a/server/models/Trust.js +++ b/server/models/Trust.js @@ -1,4 +1,3 @@ -const expect = require('expect-runtime'); const Joi = require('joi'); const log = require('loglevel'); const TrustRepository = require('../repositories/TrustRepository'); @@ -355,11 +354,7 @@ class Trust { * target wallet */ async hasTrust(walletLoginId, trustType, senderWallet, receiveWallet) { - // expect(trustType).oneOf( - // Object.keys(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE), - // ); - Joi.assert(trustType, Joi.string() .valid(...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE))); diff --git a/server/repositories/BaseRepository.js b/server/repositories/BaseRepository.js index 7d487e64..c09897b4 100644 --- a/server/repositories/BaseRepository.js +++ b/server/repositories/BaseRepository.js @@ -1,12 +1,9 @@ -// const expect = require('expect-runtime'); const Joi = require('joi'); const log = require('loglevel'); const HttpError = require('../utils/HttpError'); class BaseRepository { constructor(tableName, session) { - // expect(tableName).defined(); - Joi.assert(tableName, Joi.required()); this._tableName = tableName; this._session = session; @@ -28,15 +25,10 @@ class BaseRepository { whereBuilder(object, builder) { let result = builder; if (object.and) { - // expect(Object.keys(object)).lengthOf(1); - // expect(object.and).a(expect.any(Array)); - Joi.assert(object, Joi.object().length(1)); Joi.assert(object.and, Joi.array().required()); - object.and.forEach((one) => { if (!one.or) { - // expect(Object.keys(one)).lengthOf(1); Joi.assert(one, Joi.object().length(1)); } result = result.andWhere((subBuilder) => @@ -44,15 +36,11 @@ class BaseRepository { ); }); } else if (object.or) { - // expect(Object.keys(object)).lengthOf(1); - // expect(object.or).a(expect.any(Array)); - Joi.assert(object, Joi.object().length(1)); Joi.assert(object.or, Joi.array().required()); object.or.forEach((one) => { if (!one.and) { - // expect(Object.keys(one)).lengthOf(1); Joi.assert(one, Joi.object().length(1)); } result = result.orWhere((subBuilder) => @@ -103,7 +91,6 @@ class BaseRepository { promise = promise.limit(options.limit); } const result = await promise; - // expect(result).a(expect.any(Array)); Joi.assert(result, Joi.array().required()); return result; } @@ -114,12 +101,7 @@ class BaseRepository { .count() .table(this._tableName) .where(filter); - // expect(result).match([ - // { - // count: expect.any(String), - // }, - // ]); - + Joi.assert(result, Joi.array().items( Joi.object({ count: Joi.string().required() @@ -173,7 +155,6 @@ class BaseRepository { .getDB() .batchInsert(this._tableName, objects) .returning('id'); - // expect(result).match([expect.any(String)]); Joi.assert(result, Joi.array().items( Joi.string() )); diff --git a/server/repositories/TokenRepository.js b/server/repositories/TokenRepository.js index 8f37df84..4f626e6f 100644 --- a/server/repositories/TokenRepository.js +++ b/server/repositories/TokenRepository.js @@ -1,4 +1,3 @@ -// const expect = require('expect-runtime'); const Joi = require('joi'); const HttpError = require('../utils/HttpError'); const BaseRepository = require('./BaseRepository'); @@ -10,30 +9,21 @@ class TokenRepository extends BaseRepository { this._session = session; } - // async getById(id) { - // const result = await this._session - // .getDB()(this._tableName) - // .where('id', id) - // .first(); - // expect( - // result, - // () => new HttpError(404, `can not found token by id:${id}`), - // ).match({ - // id: expect.any(String), - // }); - // return result; - // } - async getById(id) { - - Joi.assert(id, Joi.string().uuid()); + Joi.assert(id, Joi.string().uuid()); + const result = await this._session .getDB()(this._tableName) .where('id', id) .first(); - if (!result) { + try { + Joi.assert(result, + Joi.object({ id: Joi.string().required() }) + .unknown() + .required()); + } catch (error) { throw new HttpError(404, `can not found token by id:${id}`); } diff --git a/server/repositories/TransferRepository.js b/server/repositories/TransferRepository.js index e2cbbe0c..5aea8f7f 100644 --- a/server/repositories/TransferRepository.js +++ b/server/repositories/TransferRepository.js @@ -1,4 +1,3 @@ -// const expect = require('expect-runtime'); const Joi = require('joi'); const BaseRepository = require('./BaseRepository'); const TransferEnum = require('../utils/transfer-enum'); @@ -46,9 +45,6 @@ class TransferRepository extends BaseRepository { 'destination_wallet.id', ); - // expect(result[0]).match({ - // id: expect.anything(), - // }); Joi.assert(result[0], Joi.object({ id: Joi.exist() @@ -134,7 +130,6 @@ class TransferRepository extends BaseRepository { promise = promise.limit(limitOptions.limit); } const result = await promise; - // expect(result).a(expect.any(Array)); Joi.assert(result, Joi.array().required()); return result; } diff --git a/server/repositories/WalletRepository.js b/server/repositories/WalletRepository.js index bc48981c..a6e9d70e 100644 --- a/server/repositories/WalletRepository.js +++ b/server/repositories/WalletRepository.js @@ -1,7 +1,6 @@ /* * The model for: entity, wallet, entity table and so on */ -// const expect = require('expect-runtime'); const Joi = require('joi'); const HttpError = require('../utils/HttpError'); const TrustRelationshipEnums = require('../utils/trust-enums'); @@ -15,11 +14,6 @@ class WalletRepository extends BaseRepository { } async getByName(wallet) { - // expect( - // wallet, - // () => new HttpError(400, `invalid wallet name:${wallet}`), - // ).match(/^\S+$/); - try { Joi.assert(wallet, Joi.string().pattern(/^\S+$/)); } catch (error) { @@ -31,13 +25,6 @@ class WalletRepository extends BaseRepository { .select() .table(this._tableName) .where('name', wallet); - // expect( - // list, - // () => - // new HttpError(404, `Could not find entity by wallet name: ${wallet}`), - // ) - // .defined() - // .lengthOf(1); try { Joi.assert(list, Joi.array().required().length(1));