Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 22, 2024
1 parent 68b3be5 commit 3c0338c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ describe("Cart Module Service", () => {

const [method] = await service.addShippingMethods(createdCart.id, [
{
cart_id: createdCart.id,
amount: 100,
name: "Test",
},
Expand All @@ -720,7 +719,6 @@ describe("Cart Module Service", () => {
const error = await service
.addShippingMethods(createdCart.id, [
{
cart_id: createdCart.id,
amount: -100,
name: "Test",
},
Expand Down Expand Up @@ -785,7 +783,6 @@ describe("Cart Module Service", () => {

const [method] = await service.addShippingMethods(createdCart.id, [
{
cart_id: createdCart.id,
amount: 100,
name: "test",
},
Expand Down Expand Up @@ -1008,6 +1005,7 @@ describe("Cart Module Service", () => {
await service.setLineItemAdjustments(createdCart.id, [
{
id: adjustments[0].id,
item_id: itemOne.id,
amount: 50,
code: "50%",
},
Expand Down Expand Up @@ -1328,7 +1326,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand All @@ -1339,7 +1336,6 @@ describe("Cart Module Service", () => {
{
amount: 200,
name: "test-2",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1389,7 +1385,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1460,7 +1455,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1518,7 +1512,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1591,7 +1584,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1631,7 +1623,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand All @@ -1641,7 +1632,6 @@ describe("Cart Module Service", () => {
{
amount: 200,
name: "test-2",
cart_id: createdCart.id,
},
]
)
Expand Down Expand Up @@ -1694,14 +1684,12 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: cartOne.id,
},
])
const [shippingMethodTwo] = await service.addShippingMethods(cartTwo.id, [
{
amount: 200,
name: "test-2",
cart_id: cartTwo.id,
},
])

Expand Down Expand Up @@ -1775,7 +1763,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: cartOne.id,
},
])

Expand Down Expand Up @@ -1807,7 +1794,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
])

Expand Down Expand Up @@ -1846,7 +1832,6 @@ describe("Cart Module Service", () => {
{
amount: 100,
name: "test",
cart_id: createdCart.id,
},
]
)
Expand Down
4 changes: 2 additions & 2 deletions packages/cart/src/models/line-item-adjustment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
import AdjustmentLine from "./adjustment-line"
import LineItem from "./line-item"

@Entity({ tableName: "cart_line_item_adjustment_line" })
@Entity({ tableName: "cart_line_item_adjustment" })
@Check<LineItemAdjustment>({
expression: (columns) => `${columns.amount} >= 0`,
})
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne(() => LineItem, {
onDelete: "cascade",
nullable: true,
index: "IDX_adjustment_line_item_id",
index: "IDX_adjustment_item_id",
})
item?: LineItem | null

Expand Down
2 changes: 1 addition & 1 deletion packages/cart/src/models/shipping-method-adjustment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import AdjustmentLine from "./adjustment-line"
import ShippingMethod from "./shipping-method"

@Entity({ tableName: "cart_shipping_method_adjustment_line" })
@Entity({ tableName: "cart_shipping_method_adjustment" })
export default class ShippingMethodAdjustment extends AdjustmentLine {
@ManyToOne(() => ShippingMethod, {
onDelete: "cascade",
Expand Down
30 changes: 22 additions & 8 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ export default class CartModuleService implements ICartModuleService {
): Promise<CartTypes.CartShippingMethodDTO[]>
async addShippingMethods(
cartId: string,
methods: CartTypes.CreateShippingMethodDTO[],
methods: CartTypes.CreateShippingMethodForSingleCartDTO[],
sharedContext?: Context
): Promise<CartTypes.CartShippingMethodDTO[]>

Expand Down Expand Up @@ -928,7 +928,7 @@ export default class CartModuleService implements ICartModuleService {
const methodIds = cart.shipping_methods?.map((method) => method.id)

const existingAdjustments = await this.listShippingMethodAdjustments(
{ shipping_method_id: methodIds },
{ shipping_method_id: methodIds ?? [] },
{ select: ["id"] },
sharedContext
)
Expand Down Expand Up @@ -956,10 +956,12 @@ export default class CartModuleService implements ICartModuleService {
}
)

await this.shippingMethodAdjustmentService_.delete(
toDelete.map((adj) => adj!.id),
sharedContext
)
if (toDelete.length) {
await this.shippingMethodAdjustmentService_.delete(
toDelete.map((adj) => adj!.id),
sharedContext
)
}

let result: ShippingMethodAdjustment[] = []

Expand Down Expand Up @@ -992,7 +994,7 @@ export default class CartModuleService implements ICartModuleService {
): Promise<CartTypes.ShippingMethodAdjustmentDTO[]>
async addShippingMethodAdjustments(
adjustment: CartTypes.CreateShippingMethodAdjustmentDTO
): Promise<CartTypes.ShippingMethodAdjustmentDTO[]>
): Promise<CartTypes.ShippingMethodAdjustmentDTO>
async addShippingMethodAdjustments(
cartId: string,
adjustments: CartTypes.CreateShippingMethodAdjustmentDTO[],
Expand All @@ -1007,7 +1009,10 @@ export default class CartModuleService implements ICartModuleService {
| CartTypes.CreateShippingMethodAdjustmentDTO,
adjustments?: CartTypes.CreateShippingMethodAdjustmentDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<CartTypes.ShippingMethodAdjustmentDTO[]> {
): Promise<
| CartTypes.ShippingMethodAdjustmentDTO[]
| CartTypes.ShippingMethodAdjustmentDTO
> {
let addedAdjustments: ShippingMethodAdjustment[] = []
if (isString(cartIdOrData)) {
const cart = await this.retrieve(
Expand Down Expand Up @@ -1040,6 +1045,15 @@ export default class CartModuleService implements ICartModuleService {
)
}

if (isObject(cartIdOrData)) {
return await this.baseRepository_.serialize<CartTypes.ShippingMethodAdjustmentDTO>(
addedAdjustments[0],
{
populate: true,
}
)
}

return await this.baseRepository_.serialize<
CartTypes.ShippingMethodAdjustmentDTO[]
>(addedAdjustments, {
Expand Down
10 changes: 6 additions & 4 deletions packages/types/src/cart/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FilterableCartProps,
FilterableLineItemAdjustmentProps,
FilterableLineItemProps,
FilterableShippingMethodAdjustmentProps,
FilterableShippingMethodProps,
LineItemAdjustmentDTO,
ShippingMethodAdjustmentDTO,
Expand All @@ -22,6 +23,7 @@ import {
CreateLineItemForCartDTO,
CreateShippingMethodAdjustmentDTO,
CreateShippingMethodDTO,
CreateShippingMethodForSingleCartDTO,
UpdateAddressDTO,
UpdateCartDTO,
UpdateLineItemDTO,
Expand Down Expand Up @@ -140,7 +142,7 @@ export interface ICartModuleService extends IModuleService {
): Promise<CartShippingMethodDTO[]>
addShippingMethods(
cartId: string,
methods: CreateShippingMethodDTO[],
methods: CreateShippingMethodForSingleCartDTO[],
sharedContext?: Context
): Promise<CartShippingMethodDTO[]>

Expand Down Expand Up @@ -194,7 +196,7 @@ export interface ICartModuleService extends IModuleService {
): Promise<void>

listShippingMethodAdjustments(
filters: FilterableShippingMethodProps,
filters: FilterableShippingMethodAdjustmentProps,
config?: FindConfig<ShippingMethodAdjustmentDTO>,
sharedContext?: Context
): Promise<ShippingMethodAdjustmentDTO[]>
Expand All @@ -204,7 +206,7 @@ export interface ICartModuleService extends IModuleService {
): Promise<ShippingMethodAdjustmentDTO[]>
addShippingMethodAdjustments(
data: CreateShippingMethodAdjustmentDTO
): Promise<ShippingMethodAdjustmentDTO[]>
): Promise<ShippingMethodAdjustmentDTO>
addShippingMethodAdjustments(
cartId: string,
data: CreateShippingMethodAdjustmentDTO[],
Expand All @@ -225,7 +227,7 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<void>
removeShippingMethodAdjustments(
adjustmentIds: string,
adjustmentId: string,
sharedContext?: Context
): Promise<void>
removeShippingMethodAdjustments(
Expand Down

0 comments on commit 3c0338c

Please sign in to comment.