Skip to content

Commit

Permalink
feat(cart): Partial module service implementation (#6012)
Browse files Browse the repository at this point in the history
Awaiting #6000 #6008  

**What**
- CRUD for Address in Cart Module service
- Tests for CRUD Carts + Address

**Not**
- Line items, shipping methods, tax lines, adjustment lines
  • Loading branch information
olivermrbl authored Jan 12, 2024
1 parent 8472460 commit 192bc33
Show file tree
Hide file tree
Showing 16 changed files with 433 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .changeset/soft-papayas-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/inventory": patch
"@medusajs/stock-location": patch
"@medusajs/types": patch
---

feat(cart): Partial module service implementation
2 changes: 1 addition & 1 deletion packages/cart/integration-tests/__fixtures__/cart/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const defaultCartsData = [
first_name: "Tony",
last_name: "Stark",
},
billing_address_id: {
billing_address: {
address_1: "Stark Industries",
city: "New York",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Address Service", () => {
.catch((e) => e)

expect(error.message).toContain(
"Address with id \"none-existing\" not found"
'Address with id "none-existing" not found'
)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import { ICartModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { initialize } from "../../../../src/initialize"
import { DB_URL, MikroOrmWrapper } from "../../../utils"

jest.setTimeout(30000)

describe("Cart Module Service", () => {
let service: ICartModuleService
let repositoryManager: SqlEntityManager

beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()

service = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_CART_DB_SCHEMA,
},
})
})

afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})

describe("create", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.create([
{
email: "test@email.com",
} as any,
])
.catch((e) => e)

expect(error.message).toContain(
"Value for Cart.currency_code is required, 'undefined' found"
)
})

it("should create a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [cart] = await service.list({ id: [createdCart.id] })

expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
})
)
})

it("should create a cart with billing + shipping address successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
billing_address: {
first_name: "John",
last_name: "Doe",
},
shipping_address: {
first_name: "John",
last_name: "Doe",
},
},
])

const [cart] = await service.list(
{ id: [createdCart.id] },
{ relations: ["billing_address", "shipping_address"] }
)

expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
billing_address: expect.objectContaining({
first_name: "John",
last_name: "Doe",
}),
shipping_address: expect.objectContaining({
first_name: "John",
last_name: "Doe",
}),
})
)
})

it("should create a cart with billing id + shipping id successfully", async () => {
const [createdAddress] = await service.createAddresses([
{
first_name: "John",
last_name: "Doe",
},
])

const [createdCart] = await service.create([
{
currency_code: "eur",
billing_address_id: createdAddress.id,
shipping_address_id: createdAddress.id,
},
])

expect(createdCart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
billing_address: expect.objectContaining({
id: createdAddress.id,
first_name: "John",
last_name: "Doe",
}),
shipping_address: expect.objectContaining({
id: createdAddress.id,
first_name: "John",
last_name: "Doe",
}),
})
)
})
})

describe("update", () => {
it("should throw an error if cart does not exist", async () => {
const error = await service
.update([
{
id: "none-existing",
},
])
.catch((e) => e)

expect(error.message).toContain('Cart with id "none-existing" not found')
})

it("should update a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

const [updatedCart] = await service.update([
{
id: createdCart.id,
email: "test@email.com",
},
])

const [cart] = await service.list({ id: [createdCart.id] })

expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
email: updatedCart.email,
})
)
})
})

describe("delete", () => {
it("should delete a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])

await service.delete([createdCart.id])

const carts = await service.list({ id: [createdCart.id] })

expect(carts.length).toEqual(0)
})
})

describe("createAddresses", () => {
it("should create an address successfully", async () => {
const [createdAddress] = await service.createAddresses([
{
first_name: "John",
},
])

const [address] = await service.listAddresses({
id: [createdAddress.id!],
})

expect(address).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "John",
})
)
})
})

describe("updateAddresses", () => {
it("should update an address successfully", async () => {
const [createdAddress] = await service.createAddresses([
{
first_name: "John",
},
])

const [updatedAddress] = await service.updateAddresses([
{ id: createdAddress.id!, first_name: "Jane" },
])

expect(updatedAddress).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "Jane",
})
)
})
})

describe("deleteAddresses", () => {
it("should delete an address successfully", async () => {
const [createdAddress] = await service.createAddresses([
{
first_name: "John",
},
])

await service.deleteAddresses([createdAddress.id!])

const [address] = await service.listAddresses({
id: [createdAddress.id!],
})

expect(address).toBe(undefined)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Cart Service", () => {
testManager = await MikroOrmWrapper.forkManager()

const cartRepository = new CartRepository({
manager: repositoryManager,
manager: repositoryManager
})

service = new CartService({
Expand Down Expand Up @@ -74,9 +74,7 @@ describe("Cart Service", () => {
])
.catch((e) => e)

expect(error.message).toContain(
"Cart with id \"none-existing\" not found"
)
expect(error.message).toContain('Cart with id "none-existing" not found')
})

it("should update a cart successfully", async () => {
Expand All @@ -87,7 +85,6 @@ describe("Cart Service", () => {
])

const [updatedCart] = await service.update([

{
id: createdCart.id,
email: "test@email.com",
Expand Down
2 changes: 1 addition & 1 deletion packages/cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
},
"devDependencies": {
"@medusajs/types": "workspace:^",
"@mikro-orm/cli": "5.9.7",
"cross-env": "^5.2.1",
"jest": "^29.6.3",
Expand All @@ -51,7 +52,6 @@
},
"dependencies": {
"@medusajs/modules-sdk": "^1.12.5",
"@medusajs/types": "^1.11.9",
"@medusajs/utils": "^1.11.2",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
Expand Down
6 changes: 5 additions & 1 deletion packages/cart/src/initialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { moduleDefinition } from "../module-definition"
import { InitializeModuleInjectableDependencies } from "../types"

export const initialize = async (
options?: ModulesSdkTypes.ModuleBootstrapDeclaration,
options?:
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
| ExternalModuleDeclaration
| InternalModuleDeclaration,
injectedDependencies?: InitializeModuleInjectableDependencies
): Promise<ICartModuleService> => {
const loaded = await MedusaModule.bootstrap<ICartModuleService>({
Expand Down
6 changes: 5 additions & 1 deletion packages/cart/src/loaders/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as defaultRepositories from "@repositories"
import { LoaderOptions } from "@medusajs/modules-sdk"
import { ModulesSdkTypes } from "@medusajs/types"
import { loadCustomRepositories } from "@medusajs/utils"
import * as defaultServices from "@services"
import { asClass } from "awilix"

export default async ({
Expand All @@ -17,7 +18,8 @@ export default async ({
)?.repositories

container.register({
// cartService: asClass(defaultServices.CartService).singleton(),
cartService: asClass(defaultServices.CartService).singleton(),
addressService: asClass(defaultServices.AddressService).singleton(),
})

if (customRepositories) {
Expand All @@ -34,5 +36,7 @@ export default async ({
function loadDefaultRepositories({ container }) {
container.register({
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
cartRepository: asClass(defaultRepositories.CartRepository).singleton(),
addressRepository: asClass(defaultRepositories.AddressRepository).singleton(),
})
}
Loading

1 comment on commit 192bc33

@vercel
Copy link

@vercel vercel bot commented on 192bc33 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

medusa-dashboard – ./packages/admin-next/dashboard

medusa-dashboard-git-develop-medusajs.vercel.app
medusa-dashboard-medusajs.vercel.app
medusa-dashboard.vercel.app
admin-preview.medusajs.com

Please sign in to comment.