chore: joiner config entity property (#9084)
This commit is contained in:
1 parent
28dc8d4d17
commit
fdd0543011
80 files changed
+464
-294
No files matched your search
+42
-36
@@ -1,7 +1,7 @@
|
||||
import { ICustomerModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { Module, Modules } from "@medusajs/utils"
|
||||
import { CustomerModuleService } from "@services"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -15,7 +15,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual([
|
||||
"address",
|
||||
"customerAddress",
|
||||
"customerGroupCustomer",
|
||||
"customerGroup",
|
||||
"customer",
|
||||
@@ -26,17 +26,19 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
address: {
|
||||
customerAddress: {
|
||||
id: {
|
||||
linkable: "address_id",
|
||||
linkable: "customer_address_id",
|
||||
entity: "CustomerAddress",
|
||||
primaryKey: "id",
|
||||
serviceName: "customer",
|
||||
field: "address",
|
||||
field: "customerAddress",
|
||||
},
|
||||
},
|
||||
customerGroupCustomer: {
|
||||
id: {
|
||||
linkable: "customer_group_customer_id",
|
||||
entity: "CustomerGroupCustomer",
|
||||
primaryKey: "id",
|
||||
serviceName: "customer",
|
||||
field: "customerGroupCustomer",
|
||||
@@ -45,6 +47,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
customerGroup: {
|
||||
id: {
|
||||
linkable: "customer_group_id",
|
||||
entity: "CustomerGroup",
|
||||
primaryKey: "id",
|
||||
serviceName: "customer",
|
||||
field: "customerGroup",
|
||||
@@ -53,6 +56,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
customer: {
|
||||
id: {
|
||||
linkable: "customer_id",
|
||||
entity: "Customer",
|
||||
primaryKey: "id",
|
||||
serviceName: "customer",
|
||||
field: "customer",
|
||||
@@ -196,7 +200,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
}
|
||||
const customer = await service.createCustomers(customerData)
|
||||
|
||||
const [address] = await service.listAddresses({
|
||||
const [address] = await service.listCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
})
|
||||
|
||||
@@ -675,7 +679,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.createAddresses({
|
||||
await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -684,17 +688,17 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
|
||||
// verify that the address was added
|
||||
const customerWithAddress = await service.retrieveCustomer(
|
||||
const customerWithCustomerAddress = await service.retrieveCustomer(
|
||||
customer.id,
|
||||
{
|
||||
relations: ["addresses"],
|
||||
}
|
||||
)
|
||||
expect(customerWithAddress.addresses?.length).toBe(1)
|
||||
expect(customerWithCustomerAddress.addresses?.length).toBe(1)
|
||||
|
||||
await service.deleteCustomers(customer.id)
|
||||
|
||||
const res = await service.listAddresses({
|
||||
const res = await service.listCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
})
|
||||
expect(res.length).toBe(0)
|
||||
@@ -793,25 +797,25 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
})
|
||||
|
||||
describe("addAddresses", () => {
|
||||
describe("addCustomerAddresses", () => {
|
||||
it("should add a single address to a customer", async () => {
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address = await service.createAddresses({
|
||||
const address = await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
postal_code: "10001",
|
||||
country_code: "US",
|
||||
})
|
||||
const [customerWithAddress] = await service.listCustomers(
|
||||
const [customerWithCustomerAddress] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["addresses"] }
|
||||
)
|
||||
|
||||
expect(customerWithAddress.addresses).toEqual([
|
||||
expect(customerWithCustomerAddress.addresses).toEqual([
|
||||
expect.objectContaining({ id: address.id }),
|
||||
])
|
||||
})
|
||||
@@ -821,7 +825,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const addresses = await service.createAddresses([
|
||||
const addresses = await service.createCustomerAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
@@ -837,12 +841,12 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
country_code: "US",
|
||||
},
|
||||
])
|
||||
const [customerWithAddresses] = await service.listCustomers(
|
||||
const [customerWithCustomerAddresses] = await service.listCustomers(
|
||||
{ id: customer.id },
|
||||
{ relations: ["addresses"] }
|
||||
)
|
||||
|
||||
expect(customerWithAddresses.addresses).toEqual(
|
||||
expect(customerWithCustomerAddresses.addresses).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ id: addresses[0].id }),
|
||||
expect.objectContaining({ id: addresses[1].id }),
|
||||
@@ -855,7 +859,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.createAddresses({
|
||||
await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -863,7 +867,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
country_code: "US",
|
||||
is_default_shipping: true,
|
||||
})
|
||||
await service.createAddresses({
|
||||
await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -873,7 +877,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.createAddresses({
|
||||
service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -891,7 +895,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
await service.createAddresses({
|
||||
await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -899,7 +903,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
country_code: "US",
|
||||
is_default_billing: true,
|
||||
})
|
||||
await service.createAddresses({
|
||||
await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -909,7 +913,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.createAddresses({
|
||||
service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
@@ -923,19 +927,19 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAddresses", () => {
|
||||
describe("updateCustomerAddresses", () => {
|
||||
it("should update a single address", async () => {
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address = await service.createAddresses({
|
||||
const address = await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
address_1: "123 Main St",
|
||||
})
|
||||
|
||||
await service.updateAddresses(address.id, {
|
||||
await service.updateCustomerAddresses(address.id, {
|
||||
address_name: "Work",
|
||||
address_1: "456 Main St",
|
||||
})
|
||||
@@ -959,18 +963,18 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const address1 = await service.createAddresses({
|
||||
const address1 = await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
address_1: "123 Main St",
|
||||
})
|
||||
const address2 = await service.createAddresses({
|
||||
const address2 = await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Work",
|
||||
address_1: "456 Main St",
|
||||
})
|
||||
|
||||
await service.updateAddresses(
|
||||
await service.updateCustomerAddresses(
|
||||
{ customer_id: customer.id },
|
||||
{
|
||||
address_name: "Under Construction",
|
||||
@@ -1001,7 +1005,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const [address1, address2] = await service.createAddresses([
|
||||
const [address1, address2] = await service.createCustomerAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
@@ -1014,7 +1018,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
},
|
||||
])
|
||||
|
||||
await service.updateAddresses([address1.id, address2.id], {
|
||||
await service.updateCustomerAddresses([address1.id, address2.id], {
|
||||
address_name: "Under Construction",
|
||||
})
|
||||
|
||||
@@ -1049,27 +1053,29 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
},
|
||||
],
|
||||
})
|
||||
const address = await service.createAddresses({
|
||||
const address = await service.createCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
address_name: "Work",
|
||||
address_1: "456 Main St",
|
||||
})
|
||||
|
||||
await expect(
|
||||
service.updateAddresses(address.id, { is_default_shipping: true })
|
||||
service.updateCustomerAddresses(address.id, {
|
||||
is_default_shipping: true,
|
||||
})
|
||||
).rejects.toThrow(
|
||||
/Customer address with customer_id: .*? already exists./
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAddresses", () => {
|
||||
describe("listCustomerAddresses", () => {
|
||||
it("should list all addresses for a customer", async () => {
|
||||
const customer = await service.createCustomers({
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
})
|
||||
const [address1, address2] = await service.createAddresses([
|
||||
const [address1, address2] = await service.createCustomerAddresses([
|
||||
{
|
||||
customer_id: customer.id,
|
||||
address_name: "Home",
|
||||
@@ -1083,7 +1089,7 @@ moduleIntegrationTestRunner<ICustomerModuleService>({
|
||||
},
|
||||
])
|
||||
|
||||
const addresses = await service.listAddresses({
|
||||
const addresses = await service.listCustomerAddresses({
|
||||
customer_id: customer.id,
|
||||
})
|
||||
|
||||
|
||||
@@ -1,13 +1,3 @@
|
||||
import { defineJoinerConfig, Modules } from "@medusajs/utils"
|
||||
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CUSTOMER, {
|
||||
alias: [
|
||||
{
|
||||
name: ["customer_address", "customer_addresses"],
|
||||
args: {
|
||||
entity: "Address",
|
||||
methodSuffix: "Addresses",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CUSTOMER)
|
||||
@@ -39,7 +39,7 @@ const CustomerAddressUniqueCustomerBillingAddress =
|
||||
@Entity({ tableName: "customer_address" })
|
||||
@CustomerAddressUniqueCustomerShippingAddress.MikroORMIndex()
|
||||
@CustomerAddressUniqueCustomerBillingAddress.MikroORMIndex()
|
||||
export default class Address {
|
||||
export default class CustomerAddress {
|
||||
[OptionalProps]: OptionalAddressProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import Address from "./address"
|
||||
import CustomerAddress from "./address"
|
||||
import CustomerGroup from "./customer-group"
|
||||
import CustomerGroupCustomer from "./customer-group-customer"
|
||||
|
||||
@@ -77,10 +77,10 @@ export default class Customer {
|
||||
})
|
||||
groups = new Collection<Rel<CustomerGroup>>(this)
|
||||
|
||||
@OneToMany(() => Address, (address) => address.customer, {
|
||||
@OneToMany(() => CustomerAddress, (address) => address.customer, {
|
||||
cascade: [Cascade.REMOVE],
|
||||
})
|
||||
addresses = new Collection<Rel<Address>>(this)
|
||||
addresses = new Collection<Rel<CustomerAddress>>(this)
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export { default as Address } from "./address"
|
||||
export { default as CustomerAddress } from "./address"
|
||||
export { default as Customer } from "./customer"
|
||||
export { default as CustomerGroup } from "./customer-group"
|
||||
export { default as CustomerGroupCustomer } from "./customer-group-customer"
|
||||
|
||||
@@ -21,8 +21,8 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { EntityManager } from "@mikro-orm/core"
|
||||
import {
|
||||
Address,
|
||||
Customer,
|
||||
CustomerAddress,
|
||||
CustomerGroup,
|
||||
CustomerGroupCustomer,
|
||||
} from "@models"
|
||||
@@ -31,23 +31,28 @@ import { joinerConfig } from "../joiner-config"
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
customerService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
addressService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
customerAddressService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
customerGroupService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
customerGroupCustomerService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class CustomerModuleService
|
||||
extends MedusaService<{
|
||||
Address: { dto: CustomerAddressDTO }
|
||||
CustomerAddress: { dto: CustomerAddressDTO }
|
||||
Customer: { dto: CustomerDTO }
|
||||
CustomerGroup: { dto: CustomerGroupDTO }
|
||||
CustomerGroupCustomer: { dto: CustomerGroupCustomerDTO }
|
||||
}>({ Address, Customer, CustomerGroup, CustomerGroupCustomer })
|
||||
}>({
|
||||
CustomerAddress,
|
||||
Customer,
|
||||
CustomerGroup,
|
||||
CustomerGroupCustomer,
|
||||
})
|
||||
implements ICustomerModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected customerService_: ModulesSdkTypes.IMedusaInternalService<Customer>
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<Address>
|
||||
protected customerAddressService_: ModulesSdkTypes.IMedusaInternalService<CustomerAddress>
|
||||
protected customerGroupService_: ModulesSdkTypes.IMedusaInternalService<CustomerGroup>
|
||||
protected customerGroupCustomerService_: ModulesSdkTypes.IMedusaInternalService<CustomerGroupCustomer>
|
||||
|
||||
@@ -55,7 +60,7 @@ export default class CustomerModuleService
|
||||
{
|
||||
baseRepository,
|
||||
customerService,
|
||||
addressService,
|
||||
customerAddressService,
|
||||
customerGroupService,
|
||||
customerGroupCustomerService,
|
||||
}: InjectedDependencies,
|
||||
@@ -66,7 +71,7 @@ export default class CustomerModuleService
|
||||
|
||||
this.baseRepository_ = baseRepository
|
||||
this.customerService_ = customerService
|
||||
this.addressService_ = addressService
|
||||
this.customerAddressService_ = customerAddressService
|
||||
this.customerGroupService_ = customerGroupService
|
||||
this.customerGroupCustomerService_ = customerGroupCustomerService
|
||||
}
|
||||
@@ -128,7 +133,10 @@ export default class CustomerModuleService
|
||||
})
|
||||
.flat()
|
||||
|
||||
await this.createAddresses(addressDataWithCustomerIds, sharedContext)
|
||||
await this.createCustomerAddresses(
|
||||
addressDataWithCustomerIds,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return customers as unknown as CustomerTypes.CustomerDTO[]
|
||||
}
|
||||
@@ -321,17 +329,17 @@ export default class CustomerModuleService
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
async createAddresses(
|
||||
async createCustomerAddresses(
|
||||
addresses: CustomerTypes.CreateCustomerAddressDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO[]>
|
||||
async createAddresses(
|
||||
async createCustomerAddresses(
|
||||
address: CustomerTypes.CreateCustomerAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async createAddresses(
|
||||
async createCustomerAddresses(
|
||||
data:
|
||||
| CustomerTypes.CreateCustomerAddressDTO
|
||||
| CustomerTypes.CreateCustomerAddressDTO[],
|
||||
@@ -339,7 +347,7 @@ export default class CustomerModuleService
|
||||
): Promise<
|
||||
CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
|
||||
> {
|
||||
const addresses = await this.createAddresses_(data, sharedContext)
|
||||
const addresses = await this.createCustomerAddresses_(data, sharedContext)
|
||||
|
||||
const serialized = await this.baseRepository_.serialize<
|
||||
CustomerTypes.CustomerAddressDTO[]
|
||||
@@ -353,37 +361,37 @@ export default class CustomerModuleService
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
private async createAddresses_(
|
||||
private async createCustomerAddresses_(
|
||||
data:
|
||||
| CustomerTypes.CreateCustomerAddressDTO
|
||||
| CustomerTypes.CreateCustomerAddressDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
return await this.addressService_.create(
|
||||
return await this.customerAddressService_.create(
|
||||
Array.isArray(data) ? data : [data],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
async updateAddresses(
|
||||
async updateCustomerAddresses(
|
||||
addressId: string,
|
||||
data: CustomerTypes.UpdateCustomerAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO>
|
||||
async updateAddresses(
|
||||
async updateCustomerAddresses(
|
||||
addressIds: string[],
|
||||
data: CustomerTypes.UpdateCustomerAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO[]>
|
||||
async updateAddresses(
|
||||
async updateCustomerAddresses(
|
||||
selector: CustomerTypes.FilterableCustomerAddressProps,
|
||||
data: CustomerTypes.UpdateCustomerAddressDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<CustomerTypes.CustomerAddressDTO[]>
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateAddresses(
|
||||
async updateCustomerAddresses(
|
||||
addressIdOrSelector:
|
||||
| string
|
||||
| string[]
|
||||
@@ -416,7 +424,7 @@ export default class CustomerModuleService
|
||||
}
|
||||
}
|
||||
|
||||
const addresses = await this.addressService_.update(
|
||||
const addresses = await this.customerAddressService_.update(
|
||||
updateData,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user