feat(cart): Partial module service implementation (#6012)
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
This commit is contained in:
@@ -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>({
|
||||
|
||||
@@ -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 ({
|
||||
@@ -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) {
|
||||
@@ -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(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@ import {
|
||||
Cascade,
|
||||
Collection,
|
||||
Entity,
|
||||
Index,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
@@ -19,7 +20,7 @@ import ShippingMethod from "./shipping-method"
|
||||
type OptionalCartProps =
|
||||
| "shipping_address"
|
||||
| "billing_address"
|
||||
| DAL.EntityDateColumns // TODO: To be revisited when more clear
|
||||
| DAL.EntityDateColumns
|
||||
|
||||
@Entity({ tableName: "cart" })
|
||||
export default class Cart {
|
||||
@@ -47,18 +48,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
|
||||
@@ -116,7 +121,7 @@ export default class Cart {
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
created_at?: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
@@ -124,7 +129,10 @@ export default class Cart {
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
updated_at?: Date
|
||||
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at?: Date
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
|
||||
@@ -6,3 +6,4 @@ export { default as LineItemTaxLine } from "./line-item-tax-line"
|
||||
export { default as ShippingMethod } from "./shipping-method"
|
||||
export { default as ShippingMethodAdjustmentLine } from "./shipping-method-adjustment-line"
|
||||
export { default as ShippingMethodTaxLine } from "./shipping-method-tax-line"
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import {
|
||||
AddressDTO,
|
||||
CartAddressDTO,
|
||||
CartDTO,
|
||||
Context,
|
||||
CreateCartDTO,
|
||||
@@ -11,11 +13,13 @@ import {
|
||||
UpdateCartDTO,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { FilterableAddressProps } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/utils"
|
||||
import { CreateAddressDTO, UpdateAddressDTO } from "@types"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
import AddressService from "./address"
|
||||
import CartService from "./cart"
|
||||
@@ -32,10 +36,12 @@ export default class CartModuleService implements ICartModuleService {
|
||||
protected addressService_: AddressService
|
||||
|
||||
constructor(
|
||||
{ baseRepository }: InjectedDependencies,
|
||||
{ baseRepository, cartService, addressService }: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
this.baseRepository_ = baseRepository
|
||||
this.cartService_ = cartService
|
||||
this.addressService_ = addressService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -157,15 +163,9 @@ export default class CartModuleService implements ICartModuleService {
|
||||
return await this.cartService_.update(data, sharedContext)
|
||||
}
|
||||
|
||||
async delete(
|
||||
ids: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
async delete(ids: string[], sharedContext?: Context): Promise<void>
|
||||
|
||||
async delete(
|
||||
ids: string,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
async delete(ids: string, sharedContext?: Context): Promise<void>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async delete(
|
||||
@@ -175,4 +175,93 @@ export default class CartModuleService implements ICartModuleService {
|
||||
const cartIds = Array.isArray(ids) ? ids : [ids]
|
||||
await this.cartService_.delete(cartIds, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAddresses(
|
||||
filters: FilterableAddressProps = {},
|
||||
config: FindConfig<AddressDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const addresses = await this.addressService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartAddressDTO[]>(addresses, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
async createAddresses(data: CreateAddressDTO, sharedContext?: Context)
|
||||
async createAddresses(data: CreateAddressDTO[], sharedContext?: Context)
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async createAddresses(
|
||||
data: CreateAddressDTO[] | CreateAddressDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const addresses = await this.createAddresses_(input, sharedContext)
|
||||
|
||||
const result = await this.listAddresses(
|
||||
{ id: addresses.map((p) => p.id) },
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| AddressDTO
|
||||
| AddressDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async createAddresses_(
|
||||
data: CreateAddressDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.addressService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
async updateAddresses(data: UpdateAddressDTO, sharedContext?: Context)
|
||||
async updateAddresses(data: UpdateAddressDTO[], sharedContext?: Context)
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateAddresses(
|
||||
data: UpdateAddressDTO[] | UpdateAddressDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
const addresses = await this.updateAddresses_(input, sharedContext)
|
||||
|
||||
const result = await this.listAddresses(
|
||||
{ id: addresses.map((p) => p.id) },
|
||||
{},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
| AddressDTO
|
||||
| AddressDTO[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
protected async updateAddresses_(
|
||||
data: UpdateAddressDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.addressService_.update(data, sharedContext)
|
||||
}
|
||||
|
||||
async deleteAddresses(ids: string[], sharedContext?: Context)
|
||||
async deleteAddresses(ids: string, sharedContext?: Context)
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async deleteAddresses(
|
||||
ids: string[] | string,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const addressIds = Array.isArray(ids) ? ids : [ids]
|
||||
await this.addressService_.delete(addressIds, sharedContext)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user