refactor: migrate cart module to DML (#10385)

FIXES: FRMW-2815

This PR has no breaking changes
This commit is contained in:
Harminder Virk
2024-12-05 12:30:50 +00:00
committed by GitHub
parent 7013c37fda
commit 0a16efa426
16 changed files with 808 additions and 1275 deletions
@@ -5,6 +5,7 @@ import {
DAL,
FindConfig,
ICartModuleService,
InferEntityType,
InternalModuleDeclaration,
ModulesSdkTypes,
} from "@medusajs/framework/types"
@@ -76,14 +77,30 @@ export default class CartModuleService
implements ICartModuleService
{
protected baseRepository_: DAL.RepositoryService
protected cartService_: ModulesSdkTypes.IMedusaInternalService<Cart>
protected addressService_: ModulesSdkTypes.IMedusaInternalService<Address>
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<LineItem>
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethodAdjustment>
protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethod>
protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<LineItemAdjustment>
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<LineItemTaxLine>
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<ShippingMethodTaxLine>
protected cartService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof Cart>
>
protected addressService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof Address>
>
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof LineItem>
>
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof ShippingMethodAdjustment>
>
protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof ShippingMethod>
>
protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof LineItemAdjustment>
>
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof LineItemTaxLine>
>
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof ShippingMethodTaxLine>
>
constructor(
{
@@ -274,7 +291,7 @@ export default class CartModuleService
@MedusaContext() sharedContext: Context = {}
) {
const lineItemsToCreate: CreateLineItemDTO[] = []
const createdCarts: Cart[] = []
const createdCarts: InferEntityType<typeof Cart>[] = []
for (const { items, ...cart } of data) {
const [created] = await this.cartService_.create([cart], sharedContext)
@@ -397,7 +414,7 @@ export default class CartModuleService
data?: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[]> {
let items: LineItem[] = []
let items: InferEntityType<typeof LineItem>[] = []
if (isString(cartIdOrData)) {
items = await this.addLineItems_(
cartIdOrData,
@@ -422,7 +439,7 @@ export default class CartModuleService
cartId: string,
items: CartTypes.CreateLineItemDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
): Promise<InferEntityType<typeof LineItem>[]> {
const cart = await this.retrieveCart(
cartId,
{ select: ["id"] },
@@ -443,7 +460,7 @@ export default class CartModuleService
protected async addLineItemsBulk_(
data: CreateLineItemDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
): Promise<InferEntityType<typeof LineItem>[]> {
return await this.lineItemService_.create(data, sharedContext)
}
@@ -471,7 +488,7 @@ export default class CartModuleService
data?: CartTypes.UpdateLineItemDTO | Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
let items: LineItem[] = []
let items: InferEntityType<typeof LineItem>[] = []
if (isString(lineItemIdOrDataOrSelector)) {
const item = await this.updateLineItem_(
lineItemIdOrDataOrSelector,
@@ -511,7 +528,7 @@ export default class CartModuleService
lineItemId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem> {
): Promise<InferEntityType<typeof LineItem>> {
const [item] = await this.lineItemService_.update(
[{ id: lineItemId, ...data }],
sharedContext
@@ -524,7 +541,7 @@ export default class CartModuleService
protected async updateLineItemsWithSelector_(
updates: CartTypes.UpdateLineItemWithSelectorDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<LineItem[]> {
): Promise<InferEntityType<typeof LineItem>[]> {
let toUpdate: UpdateLineItemDTO[] = []
for (const { selector, data } of updates) {
const items = await this.listLineItems({ ...selector }, {}, sharedContext)
@@ -639,7 +656,7 @@ export default class CartModuleService
): Promise<
CartTypes.CartShippingMethodDTO[] | CartTypes.CartShippingMethodDTO
> {
let methods: ShippingMethod[]
let methods: InferEntityType<typeof ShippingMethod>[]
if (isString(cartIdOrData)) {
methods = await this.addShippingMethods_(
cartIdOrData,
@@ -664,7 +681,7 @@ export default class CartModuleService
cartId: string,
data: CartTypes.CreateShippingMethodForSingleCartDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<ShippingMethod[]> {
): Promise<InferEntityType<typeof ShippingMethod>[]> {
const cart = await this.retrieveCart(
cartId,
{ select: ["id"] },
@@ -685,7 +702,7 @@ export default class CartModuleService
protected async addShippingMethodsBulk_(
data: CartTypes.CreateShippingMethodDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<ShippingMethod[]> {
): Promise<InferEntityType<typeof ShippingMethod>[]> {
return await this.shippingMethodService_.create(
data as unknown as CreateShippingMethodDTO[],
sharedContext
@@ -713,7 +730,7 @@ export default class CartModuleService
adjustments?: CartTypes.CreateLineItemAdjustmentDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.LineItemAdjustmentDTO[]> {
let addedAdjustments: LineItemAdjustment[] = []
let addedAdjustments: InferEntityType<typeof LineItemAdjustment>[] = []
if (isString(cartIdOrData)) {
const cart = await this.retrieveCart(
cartIdOrData,
@@ -888,7 +905,8 @@ export default class CartModuleService
| CartTypes.ShippingMethodAdjustmentDTO[]
| CartTypes.ShippingMethodAdjustmentDTO
> {
let addedAdjustments: ShippingMethodAdjustment[] = []
let addedAdjustments: InferEntityType<typeof ShippingMethodAdjustment>[] =
[]
if (isString(cartIdOrData)) {
const cart = await this.retrieveCart(
cartIdOrData,
@@ -961,7 +979,7 @@ export default class CartModuleService
| CartTypes.CreateLineItemTaxLineDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.LineItemTaxLineDTO[] | CartTypes.LineItemTaxLineDTO> {
let addedTaxLines: LineItemTaxLine[]
let addedTaxLines: InferEntityType<typeof LineItemTaxLine>[]
if (isString(cartIdOrData)) {
// existence check
await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext)
@@ -1077,7 +1095,7 @@ export default class CartModuleService
): Promise<
CartTypes.ShippingMethodTaxLineDTO[] | CartTypes.ShippingMethodTaxLineDTO
> {
let addedTaxLines: ShippingMethodTaxLine[]
let addedTaxLines: InferEntityType<typeof ShippingMethodTaxLine>[]
if (isString(cartIdOrData)) {
// existence check
await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext)