-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/api-v2-invite-endpoints
- Loading branch information
Showing
17 changed files
with
377 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/types": patch | ||
--- | ||
|
||
feat(cart): `POST /store/carts/:id` |
63 changes: 63 additions & 0 deletions
63
integration-tests/plugins/__tests__/cart/store/update-cart.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { ICartModuleService } 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 } | ||
|
||
describe("POST /store/carts/:id", () => { | ||
let dbConnection | ||
let appContainer | ||
let shutdownServer | ||
let cartModuleService: ICartModuleService | ||
|
||
beforeAll(async () => { | ||
const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) | ||
dbConnection = await initDb({ cwd, env } as any) | ||
shutdownServer = await startBootstrapApp({ cwd, env }) | ||
appContainer = getContainer() | ||
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART) | ||
}) | ||
|
||
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 cart", async () => { | ||
const api = useApi() as any | ||
|
||
const cart = await cartModuleService.create({ | ||
currency_code: "usd", | ||
}) | ||
|
||
const response = await api.post(`/store/carts/${cart.id}`, { | ||
email: "tony@stark.com", | ||
}) | ||
|
||
expect(response.status).toEqual(200) | ||
expect(response.data.cart).toEqual( | ||
expect.objectContaining({ | ||
id: cart.id, | ||
currency_code: "usd", | ||
email: "tony@stark.com", | ||
}) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./create-carts"; | ||
export * from "./create-carts" | ||
export * from "./update-carts" | ||
|
61 changes: 61 additions & 0 deletions
61
packages/core-flows/src/definition/cart/steps/update-carts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { | ||
CartDTO, | ||
FilterableCartProps, | ||
ICartModuleService, | ||
UpdateCartDataDTO, | ||
} from "@medusajs/types" | ||
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
type UpdateCartsStepInput = { | ||
selector: FilterableCartProps | ||
update: UpdateCartDataDTO | ||
} | ||
|
||
export const updateCartsStepId = "update-carts" | ||
export const updateCartsStep = createStep( | ||
updateCartsStepId, | ||
async (data: UpdateCartsStepInput, { container }) => { | ||
const service = container.resolve<ICartModuleService>( | ||
ModuleRegistrationName.CART | ||
) | ||
|
||
const { selects, relations } = getSelectsAndRelationsFromObjectArray([ | ||
data.update, | ||
]) | ||
|
||
const prevCarts = await service.list(data.selector, { | ||
select: selects, | ||
relations, | ||
}) | ||
|
||
const updatedCarts = await service.update( | ||
data.selector as Partial<CartDTO>, | ||
data.update | ||
) | ||
|
||
return new StepResponse(updatedCarts, prevCarts) | ||
}, | ||
async (previousCarts, { container }) => { | ||
if (!previousCarts?.length) { | ||
return | ||
} | ||
|
||
const service = container.resolve<ICartModuleService>( | ||
ModuleRegistrationName.CART | ||
) | ||
|
||
const toRestore = previousCarts.map((c) => ({ | ||
id: c.id, | ||
region_id: c.region_id, | ||
customer_id: c.customer_id, | ||
sales_channel_id: c.sales_channel_id, | ||
email: c.email, | ||
currency_code: c.currency_code, | ||
metadata: c.metadata, | ||
})) | ||
|
||
await service.update(toRestore) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./create-carts"; | ||
export * from "./create-carts" | ||
export * from "./update-carts" | ||
|
20 changes: 20 additions & 0 deletions
20
packages/core-flows/src/definition/cart/workflows/update-carts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
CartDTO, | ||
FilterableCartProps, | ||
UpdateCartDataDTO, | ||
} from "@medusajs/types" | ||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" | ||
import { updateCartsStep } from "../steps/update-carts" | ||
|
||
type WorkflowInput = { | ||
selector: FilterableCartProps | ||
update: UpdateCartDataDTO | ||
} | ||
|
||
export const updateCartsWorkflowId = "update-carts" | ||
export const updateCartsWorkflow = createWorkflow( | ||
updateCartsWorkflowId, | ||
(input: WorkflowData<WorkflowInput>): WorkflowData<CartDTO[]> => { | ||
return updateCartsStep(input) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.