Skip to content

Commit

Permalink
fix: batch status filter
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed May 9, 2024
1 parent b018956 commit 2db77de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/PrintOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
InFilter,
inFilterToQuery,
invertedFilterToQuery,
mapInFilter,
PaginationOptions,
sortToQuery,
} from "./utils";
Expand All @@ -31,6 +32,7 @@ import { ICsvOrder } from "./models/_interfaces/ICsvOrder";
import { Batch, CreateBatch } from "./models/Batch";
import { IBatch } from "./models/_interfaces/IBatch";
import { BatchStatus } from "./enums/BatchStatus";
import { stringify } from "ts-jest";

export type RequestHandler = new (
token: string,
Expand Down Expand Up @@ -461,7 +463,10 @@ export class PrintOne {
...dateFilterToQuery("updatedAt", options.filter?.updatedAt),
...inFilterToQuery("finish", options.filter?.finish),
...inFilterToQuery("format", options.filter?.format),
...inFilterToQuery("status", options.filter?.status),
...inFilterToQuery(
"status",
mapInFilter(options.filter?.status, (v) => v.toUpperCase()),
),
};

if (typeof options.filter?.isBillable === "boolean") {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ export function sortToQuery<T extends string>(
return query;
}

export function mapInFilter<T extends string, R extends string>(
values: undefined | InFilter<T>,
mapper: (value: T) => R,
): undefined | InFilter<R> {
if (values === undefined) {
return undefined;
}

if (typeof values === "string") {
return mapper(values);
} else {
return values.map(mapper);
}
}

export type InFilter<T = string> = T | T[];

export function inFilterToQuery(
Expand Down
27 changes: 27 additions & 0 deletions test/PrintOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1872,4 +1872,31 @@ describe("getBatches", function () {
expect(batch).toBeDefined();
expect(batch.templateId).toEqual(template.id);
});

it("should use the status filter", async function () {
// arrange

// act
const batches = await client.getBatches({
limit: 1,
filter: {
status: [BatchStatus.batch_created, BatchStatus.batch_user_ready],
},
});
const batch = batches.data[0];

if (batch === undefined) {
console.warn("No batches found, skipping test");
return;
}

// assert
expect(batch).toBeDefined();
expect(batch.status).toEqual(
expect.toBeOneOf([
BatchStatus.batch_created,
BatchStatus.batch_user_ready,
]),
);
});
});

0 comments on commit 2db77de

Please sign in to comment.