diff --git a/integration-tests/api/__tests__/batch-jobs/__snapshots__/api.js.snap b/integration-tests/api/__tests__/batch-jobs/__snapshots__/api.js.snap index 3d3ac2b500..78911629c3 100644 --- a/integration-tests/api/__tests__/batch-jobs/__snapshots__/api.js.snap +++ b/integration-tests/api/__tests__/batch-jobs/__snapshots__/api.js.snap @@ -1,6 +1,88 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`/admin/batch-jobs GET /admin/batch-jobs lists batch jobs created by the user 1`] = ` +Object { + "batch_jobs": Array [ + Object { + "canceled_at": null, + "completed_at": "2022-06-27T22:00:00.000Z", + "confirmed_at": null, + "context": Object {}, + "created_at": Any, + "created_by": "admin_user", + "deleted_at": null, + "dry_run": false, + "failed_at": null, + "id": "job_5", + "pre_processed_at": null, + "processing_at": null, + "result": null, + "status": "completed", + "type": "product-export", + "updated_at": Any, + }, + Object { + "canceled_at": null, + "completed_at": null, + "confirmed_at": null, + "context": Object {}, + "created_at": Any, + "created_by": "admin_user", + "deleted_at": null, + "dry_run": false, + "failed_at": null, + "id": "job_3", + "pre_processed_at": null, + "processing_at": null, + "result": null, + "status": "created", + "type": "product-export", + "updated_at": Any, + }, + Object { + "canceled_at": null, + "completed_at": null, + "confirmed_at": null, + "context": Object {}, + "created_at": Any, + "created_by": "admin_user", + "deleted_at": null, + "dry_run": false, + "failed_at": null, + "id": "job_2", + "pre_processed_at": null, + "processing_at": null, + "result": null, + "status": "created", + "type": "product-export", + "updated_at": Any, + }, + Object { + "canceled_at": null, + "completed_at": null, + "confirmed_at": null, + "context": Object {}, + "created_at": Any, + "created_by": "admin_user", + "deleted_at": null, + "dry_run": false, + "failed_at": null, + "id": "job_1", + "pre_processed_at": null, + "processing_at": null, + "result": null, + "status": "created", + "type": "product-export", + "updated_at": Any, + }, + ], + "count": 4, + "limit": 10, + "offset": 0, +} +`; + +exports[`/admin/batch-jobs GET /admin/batch-jobs lists batch jobs created by the user and where completed_at is null 1`] = ` Object { "batch_jobs": Array [ Object { diff --git a/integration-tests/api/__tests__/batch-jobs/api.js b/integration-tests/api/__tests__/batch-jobs/api.js index d00edf0b25..8e03eab1a6 100644 --- a/integration-tests/api/__tests__/batch-jobs/api.js +++ b/integration-tests/api/__tests__/batch-jobs/api.js @@ -43,6 +43,13 @@ const setupJobDb = async (dbConnection) => { status: "awaiting_confirmation", created_by: "member-user", }) + await simpleBatchJobFactory(dbConnection, { + id: "job_5", + type: "product-export", + status: "completed", + completed_at: "2022-06-27T22:00:00.000Z", + created_by: "admin_user", + }) } catch (err) { console.log(err) throw err @@ -80,21 +87,60 @@ describe("/admin/batch-jobs", () => { const api = useApi() const response = await api.get("/admin/batch-jobs", adminReqConfig) + expect(response.status).toEqual(200) + expect(response.data.batch_jobs.length).toEqual(4) + expect(response.data).toMatchSnapshot({ + batch_jobs: [ + { + id: "job_5", + created_at: expect.any(String), + updated_at: expect.any(String), + created_by: "admin_user" + }, + { + id: "job_3", + created_at: expect.any(String), + updated_at: expect.any(String), + created_by: "admin_user" + }, + { + id: "job_2", + created_at: expect.any(String), + updated_at: expect.any(String), + created_by: "admin_user" + }, + { + id: "job_1", + created_at: expect.any(String), + updated_at: expect.any(String), + created_by: "admin_user" + }, + ], + }) + }) + + it("lists batch jobs created by the user and where completed_at is null ", async () => { + const api = useApi() + const response = await api.get("/admin/batch-jobs?completed_at=null", adminReqConfig) + expect(response.status).toEqual(200) expect(response.data.batch_jobs.length).toEqual(3) expect(response.data).toMatchSnapshot({ batch_jobs: [ { + id: "job_3", created_at: expect.any(String), updated_at: expect.any(String), created_by: "admin_user" }, { + id: "job_2", created_at: expect.any(String), updated_at: expect.any(String), created_by: "admin_user" }, { + id: "job_1", created_at: expect.any(String), updated_at: expect.any(String), created_by: "admin_user" diff --git a/packages/medusa/src/api/routes/admin/batch/list-batch-jobs.ts b/packages/medusa/src/api/routes/admin/batch/list-batch-jobs.ts index fd11b124c8..48a138b8a1 100644 --- a/packages/medusa/src/api/routes/admin/batch/list-batch-jobs.ts +++ b/packages/medusa/src/api/routes/admin/batch/list-batch-jobs.ts @@ -1,10 +1,9 @@ -import { Type } from "class-transformer" +import { Transform, Type } from "class-transformer" import { IsArray, IsNumber, IsOptional, IsString, - ValidateNested, } from "class-validator" import { pickBy } from "lodash" import BatchJobService from "../../../../services/batch-job" @@ -22,15 +21,15 @@ import { Request } from "express" * - (query) limit {string} The number of collections to return. * - (query) offset {string} The offset of collections to return. * - (query) type {string | string[]} Filter by the batch type - * - (query) confirmed_at {DateComparisonOperator} Date comparison for when resulting collections was confirmed, i.e. less than, greater than etc. - * - (query) pre_processed_at {DateComparisonOperator} Date comparison for when resulting collections was pre processed, i.e. less than, greater than etc. - * - (query) completed_at {DateComparisonOperator} Date comparison for when resulting collections was completed, i.e. less than, greater than etc. - * - (query) failed_at {DateComparisonOperator} Date comparison for when resulting collections was failed, i.e. less than, greater than etc. - * - (query) canceled_at {DateComparisonOperator} Date comparison for when resulting collections was canceled, i.e. less than, greater than etc. + * - (query) confirmed_at {DateComparisonOperator | null} Date comparison for when resulting collections was confirmed, i.e. less than, greater than etc. + * - (query) pre_processed_at {DateComparisonOperator | null} Date comparison for when resulting collections was pre processed, i.e. less than, greater than etc. + * - (query) completed_at {DateComparisonOperator | null} Date comparison for when resulting collections was completed, i.e. less than, greater than etc. + * - (query) failed_at {DateComparisonOperator | null} Date comparison for when resulting collections was failed, i.e. less than, greater than etc. + * - (query) canceled_at {DateComparisonOperator | null} Date comparison for when resulting collections was canceled, i.e. less than, greater than etc. * - (query) order {string} Order used when retrieving batch jobs * - (query) expand[] {string} (Comma separated) Which fields should be expanded in each order of the result. * - (query) fields[] {string} (Comma separated) Which fields should be included in each order of the result. - * - (query) deleted_at {DateComparisonOperator} Date comparison for when resulting collections was deleted, i.e. less than, greater than etc. + * - (query) deleted_at {DateComparisonOperator | null} Date comparison for when resulting collections was deleted, i.e. less than, greater than etc. * - (query) created_at {DateComparisonOperator} Date comparison for when resulting collections was created, i.e. less than, greater than etc. * - (query) updated_at {DateComparisonOperator} Date comparison for when resulting collections was updated, i.e. less than, greater than etc. * tags: @@ -102,31 +101,35 @@ export class AdminGetBatchParams extends AdminGetBatchPaginationParams { type?: string[] @IsOptional() + @Transform(({ value }) => (value === "null" ? null : value)) @Type(() => DateComparisonOperator) - confirmed_at?: DateComparisonOperator + confirmed_at?: DateComparisonOperator | null @IsOptional() + @Transform(({ value }) => (value === "null" ? null : value)) @Type(() => DateComparisonOperator) - pre_processed_at?: DateComparisonOperator + pre_processed_at?: DateComparisonOperator | null @IsOptional() + @Transform(({ value }) => (value === "null" ? null : value)) @Type(() => DateComparisonOperator) - completed_at?: DateComparisonOperator + completed_at?: DateComparisonOperator | null @IsOptional() + @Transform(({ value }) => (value === "null" ? null : value)) @Type(() => DateComparisonOperator) - failed_at?: DateComparisonOperator + failed_at?: DateComparisonOperator | null @IsOptional() + @Transform(({ value }) => (value === "null" ? null : value)) @Type(() => DateComparisonOperator) - canceled_at?: DateComparisonOperator + canceled_at?: DateComparisonOperator | null @IsType([DateComparisonOperator]) @IsOptional() created_at?: DateComparisonOperator @IsOptional() - @ValidateNested() @Type(() => DateComparisonOperator) updated_at?: DateComparisonOperator } diff --git a/packages/medusa/src/utils/build-query.ts b/packages/medusa/src/utils/build-query.ts index 4be9f14f75..0859b641f3 100644 --- a/packages/medusa/src/utils/build-query.ts +++ b/packages/medusa/src/utils/build-query.ts @@ -4,7 +4,7 @@ import { Selector, Writable, } from "../types/common" -import { FindOperator, In, Raw } from "typeorm" +import { FindOperator, In, IsNull, Raw } from "typeorm" /** * Used to build TypeORM queries. @@ -24,6 +24,11 @@ export function buildQuery( return acc } + if (value === null) { + acc[key] = IsNull() + return acc + } + const subquery: { operator: "<" | ">" | "<=" | ">=" value: unknown