Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#39] Handle orderBy and order #63

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function MBCUSTOMQUERY(events, groupBy, orderBy, limit, offset) {
const queryPath = 'queries';
let payload;
try {
payload = buildCustomQuery(events, groupBy, orderBy, limit, offset);
payload = buildCustomQuery(events, groupBy, orderBy);
} catch (e) {
throw new Error(e.message);
}
Expand Down
72 changes: 62 additions & 10 deletions src/Code.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ function testRunner(testSheetURL) {
to: '0xe9f2E2B0105B683b436Fd0d7A2895BE25c310Af7',
value: '100000000000000000',
gas: 22774,
gasPrice: '122000000000',
gasPrice: '1',
data: '0xd0e30db0',
nonce: 6,
hash: '0x8bb80aa0ef08ff080eaeb5b06b176acbecb2d8c0136cb91dc2ef0a3f96c9974f',
hash: '0xe5e3c864d763257ca21e7a9c2f0caf02298c2d9777902d00dcbc664e8523d9fb',
}),
},
{
Expand Down Expand Up @@ -545,25 +545,77 @@ function testRunner(testSheetURL) {
[1000000000000000000, '0xa616eed6ad7a0cf5d2388301a710c273ca955e05'],
],
},
// TODO: try after https://github.com/curvegrid/multibaas-for-google-sheets/issues/53
{
name: 'TestMBCUSTOMQUERY with wrong selected range',
skip: true,
name: 'TestMBCUSTOMQUERY with order by "account" and default order ("asc")',
skip: false,
only: false,
debug: false,
func: MBCUSTOMQUERY,
isTemplate: false,
args: [
[
['eventName', 'alias', 'index', 'aggregator', 'alias', 'index', 'aggregator', undefined],
['LogDeposited(address,uint256)', 'sender', 0, '', 'amount', 1, '', undefined],
['eventName', 'alias', 'index', 'aggregator', 'alias', 'index', 'aggregator'],
['LogDeposited(address,uint256)', 'account', 0, '', 'amount', 1, ''],
],
'',
'account',
3,
0,
],
expected: [
['account', 'amount'],
['0x005080f78567f8001115f1eee835dd0151bea476', 50000000000000000000],
['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50],
['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50000000000000000000],
],
},
{
name: 'TestMBCUSTOMQUERY with order by "account" and order "asc"',
skip: false,
only: false,
debug: false,
func: MBCUSTOMQUERY,
isTemplate: false,
args: [
[
['eventName', 'alias', 'index', 'aggregator', 'alias', 'index', 'aggregator'],
['LogDeposited(address,uint256)', 'account', 0, '', 'amount', 1, ''],
],
'',
1,
1,
'account:asc',
3,
0,
],
expected: [
['account', 'amount'],
['0x005080f78567f8001115f1eee835dd0151bea476', 50000000000000000000],
['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50],
['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50000000000000000000],
],
},
{
name: 'TestMBCUSTOMQUERY with order by "account" and default order "desc"',
skip: false,
only: false,
debug: false,
func: MBCUSTOMQUERY,
isTemplate: false,
args: [
[
['eventName', 'alias', 'index', 'aggregator', 'alias', 'index', 'aggregator'],
['LogDeposited(address,uint256)', 'account', 0, '', 'amount', 1, ''],
],
'',
'account:desc',
3,
0,
],
expected: [
['account', 'amount'],
['0xbac1cd4051c378bf900087ccc445d7e7d02ad745', 1000000000000000000],
['0xa616eed6ad7a0cf5d2388301a710c273ca955e05', 1000000000000000000],
['0x89d048be68575f2b56a999ba24faacabd1b919fb', 1000000000000000000000000000],
],
expected: ['#ERROR!'],
},
{
name: 'TestMBCUSTOMQUERYTEMPLATE with 2 filters',
Expand Down
22 changes: 19 additions & 3 deletions src/library/Build.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,32 @@ function buildFilters(items, start, numItems) {
return filter;
}

function buildCustomQuery(events, groupBy, orderBy, limit, offset) {
function buildOrderBy(orderByString) {
const [orderBy, order] = orderByString.split(':');
const orderBySet = {
orderBy,
order: 'asc',
};

if (order) {
orderBySet.order = validateOrder(order);
}

return orderBySet;
}

function buildCustomQuery(events, groupBy, orderByString) {
const query = {
events: [],
};

if (groupBy && groupBy !== '') {
query.groupBy = groupBy;
}
if (orderBy && orderBy !== '') {
query.orderBy = orderBy;
if (orderByString && orderByString !== '') {
const orderBySet = buildOrderBy(orderByString);
query.orderBy = orderBySet.orderBy;
query.order = orderBySet.order;
}

// parse and validate header row
Expand Down
18 changes: 14 additions & 4 deletions src/library/Validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ const VALID_OPERANDS = [
'block_number',
'triggered_at',
];
const VALID_ORDERS = ['asc', 'desc'];

// eslint-disable-next-line no-useless-escape
const deploymentHostRegex = new RegExp(`^(https:\/\/([^\/\ ]+)\.(${DEPLOYMENT_DOMAIN})\/).*`);

function validateOrder(order) {
const orderLower = String(order).toLowerCase();
if (!VALID_ORDERS.includes(orderLower)) {
throw new Error(`'${orderLower}' is not a valid order, must be one of ${VALID_ORDERS.join(', ')}`);
}

return orderLower;
}

function validateRule(rule) {
const ruleLower = String(rule).toLowerCase();
if (!VALID_RULES.includes(ruleLower)) {
throw new Error(`'${ruleLower}' is not a valid rule, must be one of ${VALID_RULES.join(',')}`);
throw new Error(`'${ruleLower}' is not a valid rule, must be one of ${VALID_RULES.join(', ')}`);
}

return ruleLower;
Expand All @@ -36,7 +46,7 @@ function validateRule(rule) {
function validateOperand(operand) {
const operandLower = String(operand).toLowerCase();
if (!VALID_OPERANDS.includes(operandLower)) {
throw new Error(`'${operandLower}' is not a valid operand, must be one of ${VALID_OPERANDS.join(',')}`);
throw new Error(`'${operandLower}' is not a valid operand, must be one of ${VALID_OPERANDS.join(', ')}`);
}

return operandLower;
Expand All @@ -45,7 +55,7 @@ function validateOperand(operand) {
function validateOperator(operator) {
const operatorLower = String(operator).toLowerCase();
if (!VALID_OPERATORS.includes(operatorLower)) {
throw new Error(`'${operatorLower}' is not a valid operator, must be one of ${VALID_OPERATORS.join(',')}`);
throw new Error(`'${operatorLower}' is not a valid operator, must be one of ${VALID_OPERATORS.join(', ')}`);
}

return operatorLower;
Expand All @@ -54,7 +64,7 @@ function validateOperator(operator) {
function validateAggregator(aggregator) {
const aggregatorLower = String(aggregator).toLowerCase();
if (!VALID_AGGREGATORS.includes(aggregatorLower)) {
throw new Error(`'${aggregatorLower}' is not a valid aggregator, must be one of ${VALID_AGGREGATORS.join(',')}`);
throw new Error(`'${aggregatorLower}' is not a valid aggregator, must be one of ${VALID_AGGREGATORS.join(', ')}`);
}

return aggregatorLower;
Expand Down