Skip to content

Commit

Permalink
feat(core-api): add additional fields to bridgechains search sc… (Ark…
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored and faustbrian committed Oct 30, 2019
1 parent cb8289a commit dee3a5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe("Filter Rows", () => {
});
});

describe("every", () => {
it("match objects that include all values of the parameters", () => {
expect(filterRows(rows, { c: ["dummy-3", "dummy-1", "dummy-4"] }, { every: ["c"] })).toEqual([rows[2]]);
});
});

// This filter is not used yet
describe("any", () => {
it("match objects that include some values of the parameters", () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/core-api/src/handlers/bridgechains/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,20 @@ export const search: object = {
bridgechainId: Joi.number()
.integer()
.min(1),
bridgechainRepository: Joi.string().max(80),
businessId: Joi.number()
.integer()
.min(1),
genesisHash: Joi.string()
.hex()
.length(64),
name: Joi.string()
.regex(/^[a-zA-Z0-9_-]+$/)
.max(40),
seedNodes: Joi.array()
.unique()
.min(1)
.max(10)
.items(Joi.string().ip()),
},
};
17 changes: 17 additions & 0 deletions packages/core-database/src/repositories/utils/filter-rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ export = <T = any>(rows: ReadonlyArray<T>, params: Database.IParameters, filters
}
}

if (filters.hasOwnProperty("every")) {
for (const elem of filters.every) {
if (params[elem] && getProperty(item, elem)) {
if (Array.isArray(item[elem])) {
if (Array.isArray(params[elem])) {
// @ts-ignore
return params[elem].every(a => item[elem].includes(a));
} else {
throw new Error('Filtering by "every" requires an Array');
}
} else {
throw new Error("Property must be an array");
}
}
}
}

// NOTE: it was used to filter by `votes`, but that field was rejected and
// replaced by `vote`. This filter is kept here just in case
if (filters.hasOwnProperty("any")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ export class WalletsBusinessRepository implements Database.IWalletsBusinessRepos

private searchBridgechains(params: Database.IParameters = {}): ISearchContext<any> {
const query: Record<string, string[]> = {
exact: ["bridgechainId", "businessId", "name", "genesishash"],
exact: ["bridgechainId", "businessId", "genesisHash"],
like: ["bridgechainRepository", "name"],
every: ["seedNodes"],
};

const entries: any[] = this.databaseServiceProvider()
Expand Down

0 comments on commit dee3a5c

Please sign in to comment.