Skip to content

Commit

Permalink
Parsers for /sales/aggregate endpoint parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Nov 6, 2023
1 parent 3fdeba3 commit 907cb73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_SORT_BY, config } from './config.js';
import { parseCollectionName, parseTimestamp, parsePositiveInt, parseListingPriceValue, parseListingPriceSymcode,
parseTransactionId, parseLimit, parseSortBy } from './utils.js';
parseTransactionId, parseLimit, parseSortBy, parseAggregateFunction, parseAggregateColumn } from './utils.js';

export interface Sale {
collection_name: string,
Expand Down Expand Up @@ -87,17 +87,10 @@ export function getAggregate(searchParams: URLSearchParams) {
let query = `SELECT`;

// Aggregate Function
// @TODO: make this valid check in a parser
const valid_agg_functions = ["min", "max", "avg", "sum", "count", "median"];
const aggregate_function = searchParams.get("aggregate_function");
if(!aggregate_function) throw new Error("Aggregate function is required");
if (aggregate_function && !valid_agg_functions.includes(aggregate_function)) throw new Error("Aggregate function not supported");
const aggregate_function = parseAggregateFunction(searchParams.get("aggregate_function"));

// Aggregate Column
// @TODO: make this valid check in a parser
const valid_agg_columns = ["sale_id", "total_asset_ids", "listing_price_amount", "listing_price_value"];
const aggregate_column = searchParams.get("aggregate_column");
if (aggregate_column && !valid_agg_columns.includes(aggregate_column)) throw new Error("Aggregate column not supported");
const aggregate_column = parseAggregateColumn(searchParams.get("aggregate_column"));

if (aggregate_function == "count" && aggregate_column != "total_asset_ids") {
if (aggregate_column) query += ` count(${aggregate_column})`;
Expand Down
24 changes: 23 additions & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from "bun:test";
import { parseCollectionName, parsePositiveInt, parseListingPriceValue, parseListingPriceSymcode, parseTransactionId, parseLimit, parseTimestamp } from "./utils.js";
import { parseCollectionName, parsePositiveInt, parseListingPriceValue, parseListingPriceSymcode, parseTransactionId,
parseLimit, parseTimestamp, parseAggregateFunction, parseAggregateColumn } from "./utils.js";
import { DEFAULT_MAX_LIMIT } from "./config.js";

test("parseCollectionName", () => {
Expand Down Expand Up @@ -70,4 +71,25 @@ test("parseTimestamp", () => {
// errors
expect(() => parseTimestamp(10)).toThrow("Invalid timestamp");
expect(() => parseTimestamp("10")).toThrow("Invalid timestamp");
});

test("parseAggregateFunction", () => {
expect(parseAggregateFunction()).toBeUndefined();

Check failure on line 77 in src/utils.spec.ts

View workflow job for this annotation

GitHub Actions / build-and-test

error: expect(received).toBeUndefined()

Received: "count" at /home/runner/work/substreams-atomicmarket-api/substreams-atomicmarket-api/src/utils.spec.ts:77:4
expect(parseAggregateFunction(null)).toBeUndefined();
expect(parseAggregateFunction("invalid")).toBeUndefined();
expect(parseAggregateFunction("count")).toBe("count");
expect(parseAggregateFunction("sum")).toBe("sum");
expect(parseAggregateFunction("avg")).toBe("avg");
expect(parseAggregateFunction("min")).toBe("min");
expect(parseAggregateFunction("max")).toBe("max");
});

test("parseAggregateColumn", () => {
expect(parseAggregateColumn()).toBeUndefined();
expect(parseAggregateColumn(null)).toBeUndefined();
expect(parseAggregateColumn("invalid")).toBeUndefined();
expect(parseAggregateColumn("sale_id")).toBe("sale_id");
expect(parseAggregateColumn("listing_price_amount")).toBe("listing_price_amount");
expect(parseAggregateColumn("listing_price_value")).toBe("listing_price_value");
expect(parseAggregateColumn("total_asset_ids")).toBe("total_asset_ids");
});
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';
import { Name, Asset } from "@wharfkit/antelope";
import { DEFAULT_SORT_BY, config } from "./config.js";
import { DEFAULT_SORT_BY, DEFAULT_AGGREGATE_FUNCTION, config } from "./config.js";
import { logger } from './logger.js';

export function parseCollectionName(collection_name?: string|null) {
if (!z.string().regex(Name.pattern).safeParse(collection_name).success) {
Expand Down Expand Up @@ -87,4 +88,20 @@ export function parseSortBy(sort_by?: string|null) {
}

return sort_by;
}

export function parseAggregateFunction(aggregate_function?: string|null) {
if (!z.enum(["min", "max", "avg", "sum", "count", "median"]).safeParse(aggregate_function).success) {
logger.info("Aggregate function not supported, using default");
return DEFAULT_AGGREGATE_FUNCTION;
}

return aggregate_function;
}

export function parseAggregateColumn(aggregate_column?: string|null) {
if (!z.enum(["sale_id", "total_asset_ids", "listing_price_amount", "listing_price_value"]).safeParse(aggregate_column).success) {
return undefined;
}
return aggregate_column;
}

0 comments on commit 907cb73

Please sign in to comment.