Skip to content

Commit

Permalink
feat(filter-between-dates): filter AGO items by modified date
Browse files Browse the repository at this point in the history
filter AGO items by modified date

AFFECTS PACKAGES:
@esri/hub-common
@esri/hub-search
  • Loading branch information
pranavkulkarni committed Oct 31, 2019
1 parent b4c8b91 commit a6291f0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
16 changes: 16 additions & 0 deletions packages/common/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,19 @@ export function extend(
return obj;
}, extended);
}

/**
* Add number of days to a given date
*
* @export
* @param {string} date
* @param {number} numOfDays
* @returns {number} unix timestamp with numOfDays added to the given date
*/
export function addDays(date: string, numOfDays: number): string {
const given = new Date(date);
const dateString = new Date(
given.setDate(given.getDate() + numOfDays)
).toISOString();
return dateString.split("T")[0];
}
10 changes: 9 additions & 1 deletion packages/common/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
maybeAdd,
maybePush,
unique,
extend
extend,
addDays
} from "../src/util";

describe("util functions", () => {
Expand Down Expand Up @@ -662,4 +663,11 @@ describe("util functions", () => {
expect(result).toEqual(expected);
});
});

describe("addDays", () => {
it("should add days to the given date", () => {
expect(addDays("2019/10/30", 1)).toBe("2019-10-31");
expect(addDays("2019/12/31", 1)).toBe("2020-01-01");
});
});
});
25 changes: 20 additions & 5 deletions packages/search/src/ago/helpers/filters/build-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProp } from "@esri/hub-common";
import { getProp, addDays } from "@esri/hub-common";

export function buildFilter(queryFilters: any, key: string) {
const terms = getProp(queryFilters, `${key}.terms`) || [];
Expand All @@ -7,9 +7,23 @@ export function buildFilter(queryFilters: any, key: string) {
// lowerCase here because we use `orgId` for Hub index, and need it as `orgid`
// for AGO. This will allow us to use whatever casing for Hub and still
// adhere to AGO requirements
let filter = terms
.map((term: string) => `${key.toLowerCase()}:"${term}"`)
.join(agoJoin(joinType));
let filter;
if (joinType === "between") {
const startDate = terms[0];
let endDate = terms[1];
if (startDate === endDate) {
// add 1 day
endDate = addDays(startDate, 1);
}
const timestamps = [startDate, endDate].map((term: string) =>
new Date(term).getTime()
);
filter = `${key.toLowerCase()}: [${timestamps.join(agoJoin(joinType))}]`;
} else {
filter = terms
.map((term: string) => `${key.toLowerCase()}:"${term}"`)
.join(agoJoin(joinType));
}
if (joinType === "not") {
// "not" filter means everything but not those given terms
filter = `NOT ${filter}`;
Expand All @@ -32,7 +46,8 @@ function agoJoin(joinType: string) {
const joinMap: { [key: string]: string } = {
any: " OR ",
all: " AND ",
not: " NOT "
not: " NOT ",
between: " TO "
};
return joinMap[key];
}
2 changes: 1 addition & 1 deletion packages/search/src/ago/helpers/filters/create-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createFilters(params: ISearchParams): any {
}

export function generateFilter(values: string, filterDefinition: any) {
const match = values.match(/(any|all)\((.+)\)/);
const match = values.match(/(any|all|between)\((.+)\)/);
if (match) {
return {
fn: match[1],
Expand Down
5 changes: 5 additions & 0 deletions packages/search/src/ago/helpers/filters/filter-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ const filterSchema: any = {
type: "filter",
dataType: "string",
defaultOp: "any"
},
modified: {
type: "filter",
dataType: "string",
defaultOp: "between"
}
};

Expand Down
26 changes: 26 additions & 0 deletions packages/search/test/ago/helpers/filters/build-filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,30 @@ describe("buildFilter test", () => {
'(orgid:"MNF5ypRINzAAlFbv")'
);
});

it("builds filter with join type between - different dates", () => {
const queryFilters = {
modified: {
fn: "between",
terms: ["2019/10/30", "2019/10/31"],
catalogDefinition: false
}
};
expect(buildFilter(queryFilters, "modified")).toBe(
"(modified: [1572408000000 TO 1572494400000])"
);
});

it("builds filter with join type between - same dates", () => {
const queryFilters = {
modified: {
fn: "between",
terms: ["2019/10/30", "2019/10/30"],
catalogDefinition: false
}
};
expect(buildFilter(queryFilters, "modified")).toBe(
"(modified: [1572408000000 TO 1572480000000])"
);
});
});

0 comments on commit a6291f0

Please sign in to comment.