-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(customer): manage default address selection (#6295)
**What** - Catches unique constraints on customer_id, is_default_billing/is_default_shipping and reformats - Adds an step to create and update of addresses that unsets the previous default shipping/billing address if necessary. - This creates a behavior in the API where you can always set an address to be default and it will automatically unset the previous one for you.
- Loading branch information
Showing
16 changed files
with
506 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/core-flows/src/customer/steps/maybe-unset-default-billing-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
ICustomerModuleService, | ||
CreateCustomerAddressDTO, | ||
FilterableCustomerAddressProps, | ||
CustomerAddressDTO, | ||
} from "@medusajs/types" | ||
import { createStep } from "@medusajs/workflows-sdk" | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { unsetForUpdate, unsetForCreate } from "./utils" | ||
import { isDefined } from "@medusajs/utils" | ||
|
||
type StepInput = { | ||
create?: CreateCustomerAddressDTO[] | ||
update?: { | ||
selector: FilterableCustomerAddressProps | ||
update: Partial<CustomerAddressDTO> | ||
} | ||
} | ||
|
||
export const maybeUnsetDefaultBillingAddressesStepId = | ||
"maybe-unset-default-billing-customer-addresses" | ||
export const maybeUnsetDefaultBillingAddressesStep = createStep( | ||
maybeUnsetDefaultBillingAddressesStepId, | ||
async (data: StepInput, { container }) => { | ||
const customerModuleService = container.resolve<ICustomerModuleService>( | ||
ModuleRegistrationName.CUSTOMER | ||
) | ||
|
||
if (isDefined(data.create)) { | ||
return unsetForCreate( | ||
data.create, | ||
customerModuleService, | ||
"is_default_billing" | ||
) | ||
} | ||
|
||
if (isDefined(data.update)) { | ||
return unsetForUpdate( | ||
data.update, | ||
customerModuleService, | ||
"is_default_billing" | ||
) | ||
} | ||
|
||
throw new Error("Invalid step input") | ||
}, | ||
async (addressesToSet, { container }) => { | ||
if (!addressesToSet?.length) { | ||
return | ||
} | ||
|
||
const customerModuleService = container.resolve<ICustomerModuleService>( | ||
ModuleRegistrationName.CUSTOMER | ||
) | ||
|
||
await customerModuleService.updateAddress( | ||
{ id: addressesToSet }, | ||
{ is_default_billing: true } | ||
) | ||
} | ||
) |
60 changes: 60 additions & 0 deletions
60
packages/core-flows/src/customer/steps/maybe-unset-default-shipping-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
ICustomerModuleService, | ||
CreateCustomerAddressDTO, | ||
FilterableCustomerAddressProps, | ||
CustomerAddressDTO, | ||
} from "@medusajs/types" | ||
import { createStep } from "@medusajs/workflows-sdk" | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { unsetForUpdate, unsetForCreate } from "./utils" | ||
import { isDefined } from "@medusajs/utils" | ||
|
||
type StepInput = { | ||
create?: CreateCustomerAddressDTO[] | ||
update?: { | ||
selector: FilterableCustomerAddressProps | ||
update: Partial<CustomerAddressDTO> | ||
} | ||
} | ||
|
||
export const maybeUnsetDefaultShippingAddressesStepId = | ||
"maybe-unset-default-shipping-customer-addresses" | ||
export const maybeUnsetDefaultShippingAddressesStep = createStep( | ||
maybeUnsetDefaultShippingAddressesStepId, | ||
async (data: StepInput, { container }) => { | ||
const customerModuleService = container.resolve<ICustomerModuleService>( | ||
ModuleRegistrationName.CUSTOMER | ||
) | ||
if (isDefined(data.create)) { | ||
return unsetForCreate( | ||
data.create, | ||
customerModuleService, | ||
"is_default_shipping" | ||
) | ||
} | ||
|
||
if (isDefined(data.update)) { | ||
return unsetForUpdate( | ||
data.update, | ||
customerModuleService, | ||
"is_default_shipping" | ||
) | ||
} | ||
|
||
throw new Error("Invalid step input") | ||
}, | ||
async (addressesToSet, { container }) => { | ||
if (!addressesToSet?.length) { | ||
return | ||
} | ||
|
||
const customerModuleService = container.resolve<ICustomerModuleService>( | ||
ModuleRegistrationName.CUSTOMER | ||
) | ||
|
||
await customerModuleService.updateAddress( | ||
{ id: addressesToSet }, | ||
{ is_default_shipping: true } | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./unset-address-for-create" | ||
export * from "./unset-address-for-update" |
33 changes: 33 additions & 0 deletions
33
packages/core-flows/src/customer/steps/utils/unset-address-for-create.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
CreateCustomerAddressDTO, | ||
ICustomerModuleService, | ||
} from "@medusajs/types" | ||
import { StepResponse } from "@medusajs/workflows-sdk" | ||
|
||
export const unsetForCreate = async ( | ||
data: CreateCustomerAddressDTO[], | ||
customerService: ICustomerModuleService, | ||
field: "is_default_billing" | "is_default_shipping" | ||
) => { | ||
const customerIds = data.reduce<string[]>((acc, curr) => { | ||
if (curr[field]) { | ||
acc.push(curr.customer_id) | ||
} | ||
return acc | ||
}, []) | ||
|
||
const customerDefaultAddresses = await customerService.listAddresses({ | ||
customer_id: customerIds, | ||
[field]: true, | ||
}) | ||
|
||
await customerService.updateAddress( | ||
{ customer_id: customerIds, [field]: true }, | ||
{ [field]: false } | ||
) | ||
|
||
return new StepResponse( | ||
void 0, | ||
customerDefaultAddresses.map((address) => address.id) | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/core-flows/src/customer/steps/utils/unset-address-for-update.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
CustomerAddressDTO, | ||
FilterableCustomerAddressProps, | ||
ICustomerModuleService, | ||
} from "@medusajs/types" | ||
import { StepResponse } from "@medusajs/workflows-sdk" | ||
|
||
export const unsetForUpdate = async ( | ||
data: { | ||
selector: FilterableCustomerAddressProps | ||
update: Partial<CustomerAddressDTO> | ||
}, | ||
customerService: ICustomerModuleService, | ||
field: "is_default_billing" | "is_default_shipping" | ||
) => { | ||
if (!data.update[field]) { | ||
return new StepResponse(void 0) | ||
} | ||
|
||
const affectedCustomers = await customerService.listAddresses(data.selector, { | ||
select: ["id", "customer_id"], | ||
}) | ||
|
||
const customerIds = affectedCustomers.map((address) => address.customer_id) | ||
|
||
const customerDefaultAddresses = await customerService.listAddresses({ | ||
customer_id: customerIds, | ||
[field]: true, | ||
}) | ||
|
||
await customerService.updateAddress( | ||
{ customer_id: customerIds, [field]: true }, | ||
{ [field]: false } | ||
) | ||
|
||
return new StepResponse( | ||
void 0, | ||
customerDefaultAddresses.map((address) => address.id) | ||
) | ||
} |
Oops, something went wrong.