Skip to content

Commit

Permalink
fix: bug fix and removed commented blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
xianglupeng committed Jul 31, 2023
1 parent 8d01c4a commit 08afc78
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 67 deletions.
2 changes: 0 additions & 2 deletions server/infra/database/knex.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
4 changes: 0 additions & 4 deletions server/models/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 0 additions & 5 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const expect = require('expect-runtime');
const Joi = require('joi');
const log = require('loglevel');
const TrustRepository = require('../repositories/TrustRepository');
Expand Down Expand Up @@ -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)));
Expand Down
21 changes: 1 addition & 20 deletions server/repositories/BaseRepository.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,31 +25,22 @@ 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) =>
this.whereBuilder(one, subBuilder),
);
});
} 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) =>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down Expand Up @@ -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()
));
Expand Down
26 changes: 8 additions & 18 deletions server/repositories/TokenRepository.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// const expect = require('expect-runtime');
const Joi = require('joi');
const HttpError = require('../utils/HttpError');
const BaseRepository = require('./BaseRepository');
Expand All @@ -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}`);
}

Expand Down
5 changes: 0 additions & 5 deletions server/repositories/TransferRepository.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// const expect = require('expect-runtime');
const Joi = require('joi');
const BaseRepository = require('./BaseRepository');
const TransferEnum = require('../utils/transfer-enum');
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 0 additions & 13 deletions server/repositories/WalletRepository.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {
Expand All @@ -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));
Expand Down

0 comments on commit 08afc78

Please sign in to comment.