Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Fix transactions advanced search - Closes #758 #806

Merged
merged 8 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions lib/api/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,34 +284,30 @@ module.exports = function (app) {
directionQueries.push(`${baseQuery}&senderIdOrRecipientId=${address}`);
} else {
// advanced search
let advanced = '';
const offset = param(params.offset, 0);
const limit = param(params.limit, 100);
const sort = params.sort || 'timestamp:desc';
let filters = ['recipientId', 'recipientPublicKey', 'senderId', 'senderPublicKey', 'height', 'minAmount', 'maxAmount', 'fromTimestamp', 'toTimestamp', 'blockId'];

let advancedQuery = `sort=${sort}&offset=${offset}&limit=${limit}`;

// If recipientId is the same as senderId, use senderIdOrRecipientId instead.
if (params.senderId && params.recipientId && params.senderId === params.recipientId) {
advancedQuery += `&senderIdOrRecipientId=${params.recipientId}`;
filters = filters.filter(item => item !== 'senderId' && item !== 'recipientId');
}

Object.keys(params).forEach((key) => {
if (!(/key|url|parent|sort|offset|limit|type|recipientId|query/.test(key))) {
advanced += `&and:${key}=${params[key]}`;
if ((filters.indexOf(key) >= 0)) {
advancedQuery += `&${key}=${params[key]}`;
}
});

// type might be comma separate or undefined
if (params.type) {
params.type.split(',').forEach((type) => {
if (type) {
directionQueries.push(`${baseQuery}${advanced}&type=${type}`);
}
});
params.type.split(',').forEach(type => directionQueries.push(`${advancedQuery}&type=${type}`));
} else {
directionQueries.push(`${baseQuery}${advanced}`);
}

// If recipientId is the same as senderId, create an extra request for it.
if (coreUtils.parseAddress(params.recipientId) === coreUtils.parseAddress(params.senderId)) {
const reqs = directionQueries.length;
for (let i = 0; i < reqs; i++) {
directionQueries.push(directionQueries[i].replace('senderId', 'recipientId'));
}
} else if (params.recipientId && !params.senderId) {
directionQueries.forEach((directionQuery, index) => {
directionQueries[index] = `${directionQuery}&recipientId=${coreUtils.parseAddress(params.recipientId)}`;
});
directionQueries.push(`${advancedQuery}`);
}
}

Expand Down
54 changes: 41 additions & 13 deletions src/components/address/address.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import template from './address.html';

const AddressConstructor = function ($rootScope, $stateParams, $location, $http, addressTxs) {
const vm = this;
vm.searchModel = [];

vm.getAddress = () => {
$http.get('/api/getAccount', {
params: {
Expand Down Expand Up @@ -54,15 +56,19 @@ const AddressConstructor = function ($rootScope, $stateParams, $location, $http,
{ key: 'type', name: 'Type', placeholder: 'Comma separated...' },
{ key: 'senderPublicKey', name: 'SenderPk', placeholder: 'Sender Public Key...' },
{ key: 'recipientPublicKey', name: 'RecipientPk', placeholder: 'Recipient Public Key...' },
{ key: 'minConfirmations', name: 'Min Confirmations', placeholder: 'Minimum Confirmations...' },
{ key: 'blockId', name: 'blockId', placeholder: 'Block Id...' },
{ key: 'fromHeight', name: 'fromHeight', placeholder: 'From Height...' },
{ key: 'toHeight', name: 'toHeight', placeholder: 'To Height...' },
{ key: 'height', name: 'Block Height', placeholder: 'Block Id...' },
{ key: 'blockId', name: 'Block Id', placeholder: 'Block Id...' },
{ key: 'fromTimestamp', name: 'fromTimestamp', placeholder: 'From Timestamp...' },
{ key: 'toTimestamp', name: 'toTimestamp', placeholder: 'To Timestamp...' },
{ key: 'limit', name: 'limit', placeholder: 'Limit...' },
{ key: 'offset', name: 'offset', placeholder: 'Offset...' },
{ key: 'orderBy', name: 'orderBy', placeholder: 'Order By...' },
{
key: 'sort',
name: 'orderBy',
placeholder: 'Order By...',
restrictToSuggestedValues: true,
suggestedValues: ['amount:asc', 'amount:desc', 'fee:asc', 'fee:desc', 'type:asc', 'type:desc', 'timestamp:asc', 'timestamp:desc'],
},
];
vm.parametersDisplayLimit = vm.availableSearchParams.length;

Expand Down Expand Up @@ -96,36 +102,58 @@ const AddressConstructor = function ($rootScope, $stateParams, $location, $http,

const isValidAddress = id => /([0-9]+)L$/.test(id);

$rootScope.$on('advanced-searchbox:modelUpdated', (event, model) => {
const onSearchChange = () => {
const params = {};
Object.keys(model).forEach((key) => {
if (model[key] !== undefined && model[key] !== '') {
params[key] = model[key];
Object.keys(vm.searchModel).forEach((key) => {
if (vm.searchModel[key] !== undefined && vm.searchModel[key] !== '') {
params[key] = vm.searchModel[key];
}
if ((key === 'minAmount' || key === 'maxAmount') && params[key] !== '') {
params[key] = Math.floor(parseFloat(params[key]) * 1e8);
}
});

if (Object.keys(params).length > 0 && !params.recipientId && !params.senderId) {
params.recipientId = $stateParams.address;
params.senderId = $stateParams.address;
if (params.query) {
params.senderId = params.query;
params.recipientId = params.query;
} else {
params.senderId = params.senderId || $stateParams.address;
params.recipientId = params.recipientId || $stateParams.address;
}

if (Object.keys(params).length > 0 &&
(isValidAddress(params.recipientId) ||
isValidAddress(params.senderId))) {
searchByParams(params);
} else if (Object.keys(model).length === 0) {
} else if (Object.keys(vm.searchModel).length === 0) {
onSearchBoxCleaned();
} else {
vm.invalidParams = true;
}
};

$rootScope.$on('advanced-searchbox:modelUpdated', (event, model) => {
if (vm.searchModel.query !== model.query) {
vm.searchModel = Object.assign({}, model);
return onSearchChange();
}

return vm.searchModel = Object.assign({}, model);
});

$rootScope.$on('advanced-searchbox:removedSearchParam', (event, searchParameter) => {
delete vm.searchModel[searchParameter.key];
onSearchChange();
});

$rootScope.$on('advanced-searchbox:removedAllSearchParam', () => {
onSearchBoxCleaned();
});

$rootScope.$on('advanced-searchbox:leavedEditMode', () => {
onSearchChange();
});

vm.getAddress();
vm.txs = addressTxs({ address: $stateParams.address });
};
Expand Down
8 changes: 4 additions & 4 deletions src/services/less-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const LessMore = function ($http, $q, params) {
this.url = params.url || '';
this.parent = params.parent || 'parent';
this.key = params.key || '';
this.offset = params.offset || 0;
this.maximum = params.maximum || 2000;
this.limit = params.limit || 50;
this.offset = Number(params.offset) || 0;
this.maximum = Number(params.maximum) || 2000;
this.limit = Number(params.limit) || 50;

['url', 'parent', 'key', 'offset', 'maximum', 'limit'].forEach((key) => {
delete params[key];
Expand Down Expand Up @@ -113,7 +113,7 @@ LessMore.prototype.acceptData = function (data) {
};

LessMore.prototype.loadData = function () {
this.getData(0, (this.limit + 1),
this.getData(this.offset, (this.limit + 1),
(data) => {
this.acceptData(data);
});
Expand Down