Skip to content

Commit 112db16

Browse files
authored
fix: Fix support metadata query array (#583)
1 parent fa5c16d commit 112db16

2 files changed

Lines changed: 103 additions & 8 deletions

File tree

src/commands/metadata-query.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const BoxCommand = require('../box-command');
44
const { Flags, Args } = require('@oclif/core');
55
const { omit, mapKeys, snakeCase } = require('lodash');
66

7+
const parseQueryValue = (value) => {
8+
if (value.endsWith('f') && !isNaN(parseFloat(value))) {
9+
return parseFloat(value);
10+
}
11+
return value;
12+
};
13+
714
class MetadataQueryCommand extends BoxCommand {
815
async run() {
916
const { flags, args } = await this.parse(MetadataQueryCommand);
@@ -22,13 +29,13 @@ class MetadataQueryCommand extends BoxCommand {
2229
if (queryParam) {
2330
combinedQueryParams = {
2431
...combinedQueryParams,
25-
...queryParam,
32+
...queryParam.reduce((acc, curr) => ({ ...acc, ...curr }), {}),
2633
};
2734
}
2835
if (queryParamArray) {
2936
combinedQueryParams = {
3037
...combinedQueryParams,
31-
...queryParamArray,
38+
...queryParamArray.reduce((acc, curr) => ({ ...acc, ...curr }), {}),
3239
};
3340
}
3441

@@ -69,9 +76,7 @@ MetadataQueryCommand.flags = {
6976
/* eslint-disable multiline-ternary */
7077
return {
7178
[key]:
72-
value.endsWith('f') && !isNaN(parseFloat(value))
73-
? parseFloat(value)
74-
: value,
79+
parseQueryValue(value),
7580
};
7681
/* eslint-enable multiline-ternary */
7782
}),
@@ -81,23 +86,25 @@ MetadataQueryCommand.flags = {
8186
'query-param': Flags.string({
8287
description: 'One query param key-value pair, i.e. key=value. If this key duplicates with query-params, this flag will take precedence.',
8388
dependsOn: ['query'],
89+
multiple: true,
8490
parse(input) {
8591
const key = input.split('=')[0];
8692
const value = input.substring(key.length + 1);
8793
return {
88-
[key]: value,
94+
[key]: parseQueryValue(value),
8995
};
9096
},
9197
}),
9298
'query-param-array': Flags.string({
9399
description:
94100
'One query param key-multiple-value pair, use for multiple-values fields, i.e. key=value1,value2,value3. If this key duplicates with query-params or query-param, this flag will take precedence.',
95101
dependsOn: ['query'],
102+
multiple: true,
96103
parse(input) {
97104
const key = input.split('=')[0];
98105
const value = input.substring(key.length + 1).split(',');
99106
return {
100-
[key]: value,
107+
[key]: value.map(x => parseQueryValue(x)),
101108
};
102109
},
103110
}),

test/commands/metadata-query.test.js

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,52 @@ describe('Metadata Query', () => {
2727
limit: 100,
2828
},
2929
requestWithMultipleValues = {
30+
from: 'enterprise_123456.contractTemplate',
31+
query: 'name = :customerName',
32+
query_params: {
33+
value: 100,
34+
value2: 'String',
35+
},
36+
fields: [
37+
'created_at',
38+
'metadata.enterprise_123456.contractTemplate.amount',
39+
'metadata.enterprise_123456.contractTemplate.customerName',
40+
],
41+
ancestor_folder_id: '5555',
42+
order_by: [
43+
{
44+
field_key: 'amount',
45+
direction: 'asc',
46+
},
47+
],
48+
limit: 100,
49+
},
50+
requestWithMultipleArrayValues = {
51+
from: 'enterprise_123456.contractTemplate',
52+
query: 'name = :customerName',
53+
query_params: {
54+
customerName: ['John Doe', 'Jane Doe'],
55+
},
56+
fields: [
57+
'created_at',
58+
'metadata.enterprise_123456.contractTemplate.amount',
59+
'metadata.enterprise_123456.contractTemplate.customerName',
60+
],
61+
ancestor_folder_id: '5555',
62+
order_by: [
63+
{
64+
field_key: 'amount',
65+
direction: 'asc',
66+
},
67+
],
68+
limit: 100,
69+
},
70+
requestWithMultipleArrayQueryParams = {
3071
from: 'enterprise_123456.contractTemplate',
3172
query: 'name = :customerName',
3273
query_params: {
3374
customerName: ['John Doe', 'Jane Doe'],
75+
customerName2: ['Doe John', 'Doe Jane'],
3476
},
3577
fields: [
3678
'created_at',
@@ -81,15 +123,61 @@ describe('Metadata Query', () => {
81123
requestWithMultipleValues.from,
82124
requestWithMultipleValues.ancestor_folder_id,
83125
`--query=${requestWithMultipleValues.query}`,
84-
`--query-param-array=customerName=${requestWithMultipleValues.query_params.customerName.join(',')}`,
126+
`--query-param=value=${requestWithMultipleValues.query_params.value}f`,
127+
`--query-param=value2=${requestWithMultipleValues.query_params.value2}`,
85128
`--extra-fields=${requestWithMultipleValues.fields.join(',')}`,
86129
`--order-by=${requestWithMultipleValues.order_by[0].field_key}=${requestWithMultipleValues.order_by[0].direction}`,
87130
`--limit=${requestWithMultipleValues.limit}`,
88131
'--json',
89132
'--token=test',
90133
])
134+
.it('should query metadata with multiple query-param', ctx => {
135+
assert.equal(ctx.stdout, fixture);
136+
});
137+
138+
test
139+
.nock(TEST_API_ROOT, api => api
140+
.post('/2.0/metadata_queries/execute_read', requestWithMultipleArrayValues)
141+
.reply(200, fixture)
142+
)
143+
.stdout()
144+
.command([
145+
'metadata-query',
146+
requestWithMultipleArrayValues.from,
147+
requestWithMultipleArrayValues.ancestor_folder_id,
148+
`--query=${requestWithMultipleArrayValues.query}`,
149+
`--query-param-array=customerName=${requestWithMultipleArrayValues.query_params.customerName.join(',')}`,
150+
`--extra-fields=${requestWithMultipleArrayValues.fields.join(',')}`,
151+
`--order-by=${requestWithMultipleArrayValues.order_by[0].field_key}=${requestWithMultipleArrayValues.order_by[0].direction}`,
152+
`--limit=${requestWithMultipleArrayValues.limit}`,
153+
'--json',
154+
'--token=test',
155+
])
91156
.it('should query metadata with query-param', ctx => {
92157
assert.equal(ctx.stdout, fixture);
93158
});
159+
160+
test
161+
.nock(TEST_API_ROOT, api => api
162+
.post('/2.0/metadata_queries/execute_read', requestWithMultipleArrayQueryParams)
163+
.reply(200, fixture)
164+
)
165+
.stdout()
166+
.command([
167+
'metadata-query',
168+
requestWithMultipleArrayQueryParams.from,
169+
requestWithMultipleArrayQueryParams.ancestor_folder_id,
170+
`--query=${requestWithMultipleArrayQueryParams.query}`,
171+
`--query-param-array=customerName=${requestWithMultipleArrayQueryParams.query_params.customerName.join(',')}`,
172+
`--query-param-array=customerName2=${requestWithMultipleArrayQueryParams.query_params.customerName2.join(',')}`,
173+
`--extra-fields=${requestWithMultipleArrayQueryParams.fields.join(',')}`,
174+
`--order-by=${requestWithMultipleArrayQueryParams.order_by[0].field_key}=${requestWithMultipleArrayQueryParams.order_by[0].direction}`,
175+
`--limit=${requestWithMultipleArrayQueryParams.limit}`,
176+
'--json',
177+
'--token=test',
178+
])
179+
.it('should query metadata with multiple query-params-array', ctx => {
180+
assert.equal(ctx.stdout, fixture);
181+
});
94182
});
95183
});

0 commit comments

Comments
 (0)