Skip to content

Commit

Permalink
fix(core-api): sorting with multiple orderBy values (#4636)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Apr 11, 2022
1 parent 7ef3ce1 commit 04f96c5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 47 deletions.
7 changes: 5 additions & 2 deletions __tests__/unit/core-api/controllers/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import "jest-extended";

import Joi from "joi";
import { Controller } from "@packages/core-api/src/controllers/controller";
import { Resource } from "@packages/core-api/src/interfaces";
import { Application, Container } from "@packages/core-kernel";
import { Identifiers } from "@packages/core-kernel/src/ioc";
import { Transactions as MagistrateTransactions } from "@packages/core-magistrate-crypto";
import { TransactionHandlerRegistry } from "@packages/core-transactions/src/handlers/handler-registry";
import { Transactions } from "@packages/crypto";
import Joi from "joi";

import { initApp } from "../__support__";

Expand Down Expand Up @@ -168,7 +168,10 @@ describe("Controller", () => {
it("should parse order", async () => {
const request = {
query: {
orderBy: "test:desc,test2:asc",
orderBy: [
{ direction: "desc", property: "test" },
{ direction: "asc", property: "test2" },
],
},
};

Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-api/controllers/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe("DelegatesController", () => {
query: {
page: 1,
limit: 100,
orderBy: "height:desc",
orderBy: [{ direction: "desc", property: "height" }],
transform: false,
},
},
Expand Down Expand Up @@ -337,7 +337,7 @@ describe("DelegatesController", () => {
query: {
page: 1,
limit: 100,
orderBy: "height:desc",
orderBy: [{ direction: "desc", property: "height" }],
transform: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/core-api/controllers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("LocksController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
},
payload: {
ids: [lock1Id, lock2Id],
Expand Down
32 changes: 16 additions & 16 deletions __tests__/unit/core-api/controllers/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -272,7 +272,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -316,7 +316,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -336,7 +336,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -398,7 +398,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -418,7 +418,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -462,7 +462,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -482,7 +482,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -565,7 +565,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -585,7 +585,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -629,7 +629,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
type: Enums.TransactionType.MultiPayment,
},
Expand All @@ -649,7 +649,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -711,7 +711,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
},
},
Expand All @@ -731,7 +731,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: false,
},
[{ direction: "desc", property: "amount" }],
Expand Down Expand Up @@ -775,7 +775,7 @@ describe("WalletsController", () => {
query: {
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
},
},
Expand All @@ -795,7 +795,7 @@ describe("WalletsController", () => {
// not filtered out from criteria
page: 1,
limit: 100,
orderBy: "amount:desc",
orderBy: [{ direction: "desc", property: "amount" }],
transform: true,
},
[{ direction: "desc", property: "amount" }],
Expand Down
7 changes: 1 addition & 6 deletions packages/core-api/src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ export class Controller {
return [];
}

const orderBy = Array.isArray(request.query.orderBy) ? request.query.orderBy : request.query.orderBy.split(",");

return orderBy.map((s: string) => ({
property: s.split(":")[0],
direction: s.split(":")[1] === "desc" ? "desc" : "asc",
}));
return request.query.orderBy;
}

protected getListingOptions(): Contracts.Search.Options {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/resources-new/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ export const blockCriteriaSchemaObject = {
};

export const blockParamSchema = Joi.alternatives(blockIdSchema, blockHeightSchema);
export const blockSortingSchema = Schemas.createSortingSchema(Schemas.blockCriteriaSchemas, [], false);
export const blockSortingSchema = Schemas.createSortingSchema(Schemas.blockCriteriaSchemas);
2 changes: 1 addition & 1 deletion packages/core-api/src/resources-new/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const transactionCriteriaSchemaObject = {
};

export const transactionParamSchema = transactionIdSchema;
export const transactionSortingSchema = Schemas.createSortingSchema(Schemas.transactionCriteriaSchemas, [], false);
export const transactionSortingSchema = Schemas.createSortingSchema(Schemas.transactionCriteriaSchemas);
36 changes: 18 additions & 18 deletions packages/core-api/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ export const createRangeCriteriaSchema = (item: Joi.Schema): Joi.Schema => {

// Sorting

export const createSortingSchema = (
schemaObject: SchemaObject,
wildcardPaths: string[] = [],
transform: boolean = true,
): Joi.ObjectSchema => {
export const createSortingSchema = (schemaObject: SchemaObject, wildcardPaths: string[] = []): Joi.ObjectSchema => {
const getObjectPaths = (object: SchemaObject): string[] => {
return Object.entries(object)
.map(([key, value]) => {
Expand All @@ -66,8 +62,17 @@ export const createSortingSchema = (

const sorting: Contracts.Search.Sorting = [];

for (const item of value.split(",")) {
let items: string[] = [];

if (Array.isArray(value)) {
items = value;
} else if (value) {
items = [value];
}

for (const item of items) {
const pair = item.split(":");

const property = String(pair[0]);
const direction = pair.length === 1 ? "asc" : pair[1];

Expand All @@ -83,21 +88,13 @@ export const createSortingSchema = (
});
}

if (transform) {
sorting.push({ property, direction: direction as "asc" | "desc" });
} else {
sorting.push(value);
}
sorting.push({ property, direction: direction as "asc" | "desc" });
}

return sorting;
});

if (transform) {
return Joi.object({ orderBy: orderBy.default([]) });
} else {
return Joi.object({ orderBy });
}
return Joi.object({ orderBy: orderBy.default([]) });
};

// Pagination
Expand Down Expand Up @@ -162,8 +159,11 @@ export const orderBy = Joi.alternatives().try(
Joi.array().items(Joi.string().regex(/^[a-z._]{1,40}:(asc|desc)$/i)),
);

export const blocksOrderBy = orderBy.default("height:desc");
export const transactionsOrderBy = orderBy.default(["timestamp:desc", "sequence:desc"]);
export const blocksOrderBy = orderBy.default({ property: "height", direction: "desc" });
export const transactionsOrderBy = orderBy.default([
{ property: "timestamp", direction: "desc" },
{ property: "sequence", direction: "desc" },
]);

const equalCriteria = (value: any) => value;
const numericCriteria = (value: any) =>
Expand Down

0 comments on commit 04f96c5

Please sign in to comment.