Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(region): Region create, delete, update admin endpoints #6332

Merged
merged 15 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("POST /admin/regions", () => {
let dbConnection
let appContainer
let shutdownServer
let service: IRegionModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.REGION)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should create a region", async () => {
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
const api = useApi() as any
const response = await api.post(
`/admin/regions`,
{
currency_code: "usd",
},
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.region).toEqual(
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
})
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("DELETE /admin/regions/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let service: IRegionModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.REGION)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should delete a region", async () => {
const region = await service.create({
currency_code: "usd",
name: "US",
})

const api = useApi() as any
const response = await api.delete(
`/admin/regions/${region.id}`,
adminHeaders
)

expect(response.status).toEqual(200)

const deletedRegion = await service.retrieve(region.id, {
withDeleted: true,
})
expect(deletedRegion.deleted_at).toBeTruthy()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("POST /admin/regions/:id", () => {
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
let dbConnection
let appContainer
let shutdownServer
let service: IRegionModuleService

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.REGION)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should update a region", async () => {
const region = await service.create({
currency_code: "usd",
name: "US",
})

const api = useApi() as any
const response = await api.post(
`/admin/regions/${region.id}`,
{
name: "United States",
},
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.region).toEqual(
expect.objectContaining({
id: expect.any(String),
currency_code: "usd",
name: "United States",
})
)
})
})
5 changes: 3 additions & 2 deletions packages/core-flows/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./customer"
export * from "./customer-group"
export * from "./definition"
export * from "./definitions"
export * as Handlers from "./handlers"
export * from "./promotion"
export * from "./customer"
export * from "./customer-group"
export * from "./region"
2 changes: 2 additions & 0 deletions packages/core-flows/src/region/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./steps"
export * from "./workflows"
31 changes: 31 additions & 0 deletions packages/core-flows/src/region/steps/create-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CreateRegionDTO, IRegionModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"

export const createRegionsStepId = "create-regions"
export const createRegionsStep = createStep(
createRegionsStepId,
async (data: CreateRegionDTO[], { container }) => {
const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

const created = await service.create(data)

return new StepResponse(
created,
created.map((region) => region.id)
)
},
async (createdIds, { container }) => {
if (!createdIds?.length) {
return
}

const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

await service.delete(createdIds)
}
)
28 changes: 28 additions & 0 deletions packages/core-flows/src/region/steps/delete-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"

export const deleteRegionsStepId = "delete-regions"
export const deleteRegionsStep = createStep(
deleteRegionsStepId,
async (ids: string[], { container }) => {
const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

await service.softDelete(ids)

return new StepResponse(void 0, ids)
},
async (prevIds, { container }) => {
if (!prevIds?.length) {
return
}

const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

await service.restore(prevIds)
}
)
3 changes: 3 additions & 0 deletions packages/core-flows/src/region/steps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./create-regions"
export * from "./delete-regions"
export * from "./update-regions"
59 changes: 59 additions & 0 deletions packages/core-flows/src/region/steps/update-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
FilterableRegionProps,
IRegionModuleService,
UpdatableRegionFields,
} from "@medusajs/types"
import {
getSelectsAndRelationsFromObjectArray,
promiseAll,
} from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"

type UpdateRegionsStepInput = {
selector: FilterableRegionProps
update: UpdatableRegionFields
}

export const updateRegionsStepId = "update-region"
export const updateRegionsStep = createStep(
updateRegionsStepId,
async (data: UpdateRegionsStepInput, { container }) => {
const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

const { selects, relations } = getSelectsAndRelationsFromObjectArray([
data.update,
])

const prevData = await service.list(data.selector, {
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
select: selects,
relations,
})

const regions = await service.update(data.selector, data.update)

return new StepResponse(regions, prevData)
},
async (prevData, { container }) => {
if (!prevData?.length) {
return
}

const service = container.resolve<IRegionModuleService>(
ModuleRegistrationName.REGION
)

await promiseAll(
prevData.map(
async (region) =>
await service.update(region.id, {
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
name: region.name,
currency_code: region.currency_code,
metadata: region.metadata,
})
)
)
}
)
13 changes: 13 additions & 0 deletions packages/core-flows/src/region/workflows/create-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CreateRegionDTO, RegionDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createRegionsStep } from "../steps"

type WorkflowInput = { regionsData: CreateRegionDTO[] }

export const createRegionsWorkflowId = "create-regions"
export const createRegionsWorkflow = createWorkflow(
createRegionsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<RegionDTO[]> => {
return createRegionsStep(input.regionsData)
}
)
12 changes: 12 additions & 0 deletions packages/core-flows/src/region/workflows/delete-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteRegionsStep } from "../steps"

type WorkflowInput = { ids: string[] }

export const deleteRegionsWorkflowId = "delete-regions"
export const deleteRegionsWorkflow = createWorkflow(
deleteRegionsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteRegionsStep(input.ids)
}
)
3 changes: 3 additions & 0 deletions packages/core-flows/src/region/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./create-regions"
export * from "./delete-regions"
export * from "./update-regions"
22 changes: 22 additions & 0 deletions packages/core-flows/src/region/workflows/update-regions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
FilterableRegionProps,
RegionDTO,
UpdatableRegionFields,
} from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateRegionsStep } from "../steps"

type UpdateRegionsStepInput = {
selector: FilterableRegionProps
update: UpdatableRegionFields
}

type WorkflowInput = UpdateRegionsStepInput

export const updateRegionsWorkflowId = "update-regions"
export const updateRegionsWorkflow = createWorkflow(
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
updateRegionsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<RegionDTO[]> => {
return updateRegionsStep(input)
}
)
Loading
Loading