Skip to content

Commit

Permalink
Merge branch 'develop' into chore/workflow-passing-context
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Jan 30, 2024
2 parents e5ec8d0 + 374b9b1 commit 10066c5
Show file tree
Hide file tree
Showing 35 changed files with 1,244 additions and 95 deletions.
71 changes: 71 additions & 0 deletions integration-tests/plugins/__tests__/cart/store/get-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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"

const env = { MEDUSA_FF_MEDUSA_V2: true }

describe("GET /store/: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 get cart", async () => {
const cart = await cartModuleService.create({
currency_code: "usd",
items: [
{
unit_price: 1000,
quantity: 1,
title: "Test item",
},
],
})

const api = useApi() as any
const response = await api.get(`/store/carts/${cart.id}`)

expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
currency_code: "usd",
items: expect.arrayContaining([
expect.objectContaining({
unit_price: 1000,
quantity: 1,
title: "Test item",
}),
]),
})
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } 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"

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

describe("POST /admin/customers", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

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

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 customer", async () => {
const api = useApi() as any
const response = await api.post(
`/admin/customers`,
{
first_name: "John",
last_name: "Doe",
},
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "John",
last_name: "Doe",
created_by: "admin_user",
})
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } 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"

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

describe("DELETE /admin/customers/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

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

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 customer", async () => {
const customer = await customerModuleService.create({
first_name: "John",
last_name: "Doe",
})

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

expect(response.status).toEqual(200)

const deletedCustomer = await customerModuleService.retrieve(customer.id, {
withDeleted: true,
})
expect(deletedCustomer.deleted_at).toBeTruthy()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } 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"

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

describe("POST /admin/customers/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

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

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 customer", async () => {
const customer = await customerModuleService.create({
first_name: "John",
last_name: "Doe",
})

const api = useApi() as any
const response = await api.post(
`/admin/customers/${customer.id}`,
{
first_name: "Jane",
},
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data.customer).toEqual(
expect.objectContaining({
id: expect.any(String),
first_name: "Jane",
last_name: "Doe",
})
)
})
})
5 changes: 5 additions & 0 deletions integration-tests/plugins/medusa-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,10 @@ module.exports = {
resources: "shared",
resolve: "@medusajs/sales-channel",
},
[Modules.CART]: {
scope: "internal",
resources: "shared",
resolve: "@medusajs/cart",
},
},
}
2 changes: 2 additions & 0 deletions packages/cart/src/migrations/CartModuleSetup20240122122952.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class CartModuleSetup20240122122952 extends Migration {
);
ALTER TABLE "cart" ADD COLUMN IF NOT EXISTS "currency_code" TEXT NOT NULL;
ALTER TABLE "cart" ALTER COLUMN "region_id" DROP NOT NULL;
ALTER TABLE "cart" ALTER COLUMN "email" DROP NOT NULL;
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_242205c81c1152fab1b6e848470";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_484c329f4783be4e18e5e2ff090";
Expand Down
2 changes: 2 additions & 0 deletions packages/core-flows/src/customer/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/customer/steps/create-customers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"

export const createCustomersStepId = "create-customers"
export const createCustomersStep = createStep(
createCustomersStepId,
async (data: CreateCustomerDTO[], { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

const createdCustomers = await service.create(data)

return new StepResponse(
createdCustomers,
createdCustomers.map((createdCustomers) => createdCustomers.id)
)
},
async (createdCustomerIds, { container }) => {
if (!createdCustomerIds?.length) {
return
}

const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

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

type DeleteCustomerStepInput = string[]

export const deleteCustomerStepId = "delete-customer"
export const deleteCustomerStep = createStep(
deleteCustomerStepId,
async (ids: DeleteCustomerStepInput, { container }) => {
const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

await service.softDelete(ids)

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

const service = container.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER
)

await service.restore(prevCustomerIds)
}
)
3 changes: 3 additions & 0 deletions packages/core-flows/src/customer/steps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./create-customers"
export * from "./update-customers"
export * from "./delete-customers"

0 comments on commit 10066c5

Please sign in to comment.