Skip to content

Commit

Permalink
add address methods
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 7, 2024
1 parent 160c08a commit 6981f30
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 25 deletions.
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
@@ -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: "[email protected]",
} 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: "[email protected]",
},
])

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)
})
})
})
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(),
})
}
24 changes: 14 additions & 10 deletions packages/cart/src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Index,
ManyToOne,
OnInit,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Expand Down Expand Up @@ -47,18 +47,22 @@ export default class Cart {
@Property({ columnType: "text" })
currency_code: string

@OneToOne({
entity: () => Address,
joinColumn: "shipping_address_id",
cascade: [Cascade.REMOVE],
@Index({ name: "IDX_cart_shipping_address_id" })
@Property({ columnType: "text", nullable: true })
shipping_address_id?: string | null

@ManyToOne(() => Address, {
fieldName: "shipping_address_id",
nullable: true,
})
shipping_address?: Address | null

@OneToOne({
entity: () => Address,
joinColumn: "billing_address_id",
cascade: [Cascade.REMOVE],
@Index({ name: "IDX_cart_billing_address_id" })
@Property({ columnType: "text", nullable: true })
billing_address_id?: string | null

@ManyToOne(() => Address, {
fieldName: "billing_address_id",
nullable: true,
})
billing_address?: Address | null
Expand Down
Loading

0 comments on commit 6981f30

Please sign in to comment.