feat(medusa,medusa-js,medusa-react): Add BatchJob API support in medusa-js + medusa-react (#1704)
* Add BatchJob API to medusa-js * Adds BatchJob API hooks * Fix tests in medusa-react
This commit is contained in:
@@ -1247,6 +1247,17 @@
|
||||
"fulfillment_option": {
|
||||
"provider_id": "test-ful",
|
||||
"options": []
|
||||
},
|
||||
"batch_job": {
|
||||
"id": "batch_01F0YES4R67TXXC1QBQ8P54A8Y",
|
||||
"type": "product_export",
|
||||
"created_by": "usr_123412341234",
|
||||
"context": null,
|
||||
"result": null,
|
||||
"dry_run": false,
|
||||
"updated_at": "2021-03-16T21:24:00.389Z",
|
||||
"created_at": "2021-03-16T21:24:00.389Z",
|
||||
"deleted_at": null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,55 @@ import { rest } from "msw"
|
||||
import { fixtures } from "../data"
|
||||
|
||||
export const adminHandlers = [
|
||||
rest.post("/admin/batch-jobs/", (req, res, ctx) => {
|
||||
const body = req.body as Record<string, any>
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
batch_job: {
|
||||
...fixtures.get("batch_job"),
|
||||
...body,
|
||||
},
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/admin/batch-jobs/", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
batch_jobs: fixtures.list("batch_job"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.get("/admin/batch-jobs/:id", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
batch_job: fixtures.get("batch_job"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/batch-jobs/:id/confirm", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
batch_job: fixtures.get("batch_job"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/batch-jobs/:id/cancel", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
batch_job: fixtures.get("batch_job"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/collections/", (req, res, ctx) => {
|
||||
const body = req.body as Record<string, any>
|
||||
return res(
|
||||
@@ -253,27 +302,33 @@ export const adminHandlers = [
|
||||
)
|
||||
}),
|
||||
|
||||
rest.delete("/admin/price-lists/:id/products/:product_id/prices", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
rest.delete(
|
||||
"/admin/price-lists/:id/products/:product_id/prices",
|
||||
(req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.delete("/admin/price-lists/:id/variants/:variant_id/prices", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
rest.delete(
|
||||
"/admin/price-lists/:id/variants/:variant_id/prices",
|
||||
(req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}
|
||||
),
|
||||
|
||||
rest.post("/admin/return-reasons/", (req, res, ctx) => {
|
||||
const body = req.body as Record<string, any>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,74 @@
|
||||
import { AdminBatchJobRes, AdminPostBatchesReq } from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { adminBatchJobsKeys } from "./queries"
|
||||
|
||||
/**
|
||||
* Hook returns functions for creating batch jobs.
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminCreateBatchJob = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminBatchJobRes>,
|
||||
Error,
|
||||
AdminPostBatchesReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostBatchesReq) => client.admin.batchJobs.create(payload),
|
||||
buildOptions(queryClient, adminBatchJobsKeys.lists(), options)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook return functions for canceling a batch job
|
||||
*
|
||||
* @param id - id of the batch job
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminCancelBatchJob = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<Response<AdminBatchJobRes>, Error>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.batchJobs.cancel(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminBatchJobsKeys.lists(), adminBatchJobsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook return functions for confirming a batch job
|
||||
*
|
||||
* @param id - id of the batch job
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminConfirmBatchJob = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<Response<AdminBatchJobRes>, Error>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.batchJobs.confirm(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminBatchJobsKeys.lists(), adminBatchJobsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
AdminBatchJobListRes,
|
||||
AdminBatchJobRes,
|
||||
AdminGetBatchParams,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useQuery } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { queryKeysFactory } from "../../utils/index"
|
||||
|
||||
const ADMIN_COLLECTIONS_QUERY_KEY = `admin_batches` as const
|
||||
|
||||
export const adminBatchJobsKeys = queryKeysFactory(ADMIN_COLLECTIONS_QUERY_KEY)
|
||||
|
||||
type BatchJobsQueryKey = typeof adminBatchJobsKeys
|
||||
|
||||
export const useAdminBatchJobs = (
|
||||
query?: AdminGetBatchParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminBatchJobListRes>,
|
||||
Error,
|
||||
ReturnType<BatchJobsQueryKey["list"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminBatchJobsKeys.list(query),
|
||||
() => client.admin.batchJobs.list(query),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
|
||||
export const useAdminBatchJob = (
|
||||
id: string,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminBatchJobRes>,
|
||||
Error,
|
||||
ReturnType<BatchJobsQueryKey["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminBatchJobsKeys.detail(id),
|
||||
() => client.admin.batchJobs.retrieve(id),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
@@ -1,26 +1,27 @@
|
||||
export * from "./auth"
|
||||
export * from "./collections"
|
||||
export * from "./batch-jobs"
|
||||
export * from "./claims"
|
||||
export * from "./customers"
|
||||
export * from "./collections"
|
||||
export * from "./customer-groups"
|
||||
export * from "./customers"
|
||||
export * from "./discounts"
|
||||
export * from "./draft-orders"
|
||||
export * from "./gift-cards"
|
||||
export * from "./invites"
|
||||
export * from "./notes"
|
||||
export * from "./notifications"
|
||||
export * from "./orders"
|
||||
export * from "./products"
|
||||
export * from "./price-lists"
|
||||
export * from "./product-tags"
|
||||
export * from "./product-types"
|
||||
export * from "./price-lists"
|
||||
export * from "./return-reasons"
|
||||
export * from "./products"
|
||||
export * from "./regions"
|
||||
export * from "./return-reasons"
|
||||
export * from "./returns"
|
||||
export * from "./shipping-options"
|
||||
export * from "./shipping-profiles"
|
||||
export * from "./notes"
|
||||
export * from "./invites"
|
||||
export * from "./notifications"
|
||||
export * from "./returns"
|
||||
export * from "./store"
|
||||
export * from "./swaps"
|
||||
export * from "./tax-rates"
|
||||
export * from "./users"
|
||||
export * from "./variants"
|
||||
export * from "./tax-rates"
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import {
|
||||
useAdminCancelBatchJob,
|
||||
useAdminConfirmBatchJob,
|
||||
useAdminCreateBatchJob,
|
||||
} from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useAdminCreateBatchJob hook", () => {
|
||||
test("creates a batch job and returns it", async () => {
|
||||
const batch = {
|
||||
type: "product_export",
|
||||
dry_run: false,
|
||||
context: {},
|
||||
}
|
||||
|
||||
const { result, waitFor } = renderHook(() => useAdminCreateBatchJob(), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
result.current.mutate(batch)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data?.response.status).toEqual(200)
|
||||
expect(result.current.data?.batch_job).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("batch_job"),
|
||||
...batch,
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminCancelBatchJob hook", () => {
|
||||
test("cancels a batch job and returns it", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminCancelBatchJob(fixtures.get("batch_job").id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data?.response.status).toEqual(200)
|
||||
expect(result.current.data?.batch_job).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("batch_job"),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminConfirmBatchJob hook", () => {
|
||||
test("confirms a batch job and returns it", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminConfirmBatchJob(fixtures.get("batch_job").id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data?.response.status).toEqual(200)
|
||||
expect(result.current.data?.batch_job).toEqual(
|
||||
expect.objectContaining({
|
||||
...fixtures.get("batch_job"),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { useAdminBatchJob, useAdminBatchJobs } from "../../../../src"
|
||||
import { createWrapper } from "../../../utils"
|
||||
|
||||
describe("useAdminBatchJobs hook", () => {
|
||||
test("returns a list of batch job", async () => {
|
||||
const batchJobs = fixtures.list("batch_job")
|
||||
const { result, waitFor } = renderHook(() => useAdminBatchJobs(), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response?.status).toEqual(200)
|
||||
expect(result.current.batch_jobs).toEqual(batchJobs)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminBatchJob hook", () => {
|
||||
test("returns a batch job", async () => {
|
||||
const batchJob = fixtures.get("batch_job")
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminBatchJob(batchJob.id),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.response?.status).toEqual(200)
|
||||
expect(result.current.batch_job).toEqual(batchJob)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user