Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Jul 25, 2024
1 parent e5dc5ac commit 2f5be74
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,6 @@ describe("Internal Module Service Factory", () => {
instance = new IMedusaInternalService(containerMock)
})

it("should throw on update with wrong arguments", async () => {
const entity = { id: "1", name: "Item" }
containerMock[modelRepositoryName].find.mockResolvedValueOnce([entity])

let err = await instance
// @ts-ignore
.update("fake_id", { prop: "fake_data" })
.catch((e) => e)

expect(err.message).toBe(
"Unable to update with input: fake_id. Please provide the following input: an object or an array of object to update or { selector: {}, data: {} } or an array of the same."
)
})

it("should throw model id undefined error on retrieve if id is not defined", async () => {
const err = await instance.retrieve().catch((e) => e)
expect(err.message).toBe("model - id must be defined")
Expand All @@ -86,7 +72,7 @@ describe("Internal Module Service Factory", () => {
})

it("should throw NOT_FOUND error on retrieve if entity not found", async () => {
containerMock[modelRepositoryName].find.mockResolvedValueOnce([])
containerMock[modelRepositoryName].find.mockReturnValue([])

const err = await instance.retrieve("1").catch((e) => e)
expect(err.message).toBe("Model with id: 1 was not found")
Expand Down
27 changes: 27 additions & 0 deletions packages/core/utils/src/modules-sdk/medusa-internal-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,39 @@ export function MedusaInternalService<
selectorAndData: SelectorAndData[],
sharedContext?: Context
): Promise<InferEntityType<TEntity>[]>
update(
idOrSelector: string | SelectorAndData["selector"],
data: any,
sharedContext?: Context
): Promise<InferEntityType<TEntity>[]>

@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
async update(
input: any | any[] | SelectorAndData | SelectorAndData[],
maybeDataOrContext?: any,
@MedusaContext() sharedContext: Context = {}
): Promise<InferEntityType<TEntity> | InferEntityType<TEntity>[]> {
if (arguments.length === 2) {
Object.assign(sharedContext, maybeDataOrContext)
} else if (arguments.length === 3) {
const input_ = {
selector: {},
data: {} as any,
}

if (isString(input)) {
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
input_.selector = {
[primaryKeys[0]]: input,
}
input_.data = maybeDataOrContext
} else {
input_.selector = input
input_.data = maybeDataOrContext
}
input = input_
}

if (
!isDefined(input) ||
(Array.isArray(input) && input.length === 0) ||
Expand Down
43 changes: 42 additions & 1 deletion packages/core/utils/src/modules-sdk/types/medusa-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export type ExtractKeysFromConfig<ModelsConfig> = ModelsConfig extends {
? string
: keyof ModelsConfig

export type InferReturnType<
TModelsDtoConfig extends ModelConfigurationsToConfigTemplate<any>,
TKey extends keyof TModelsDtoConfig
> = TModelsDtoConfig[TKey]["dto"] extends never
? TModelsDtoConfig[TKey]["model"] extends never
? Record<any, any>
: TModelsDtoConfig[TKey]["model"]
: TModelsDtoConfig[TKey]["dto"]

export type AbstractModuleService<
TModelsDtoConfig extends Record<string, any>
> = {
Expand Down Expand Up @@ -187,7 +196,39 @@ export type AbstractModuleService<
TModelsDtoConfig,
TModelName
>}`]: {
(...args: any[]): Promise<any>
(
data: {
selector: object
data: object
},
sharedContext?: Context
): Promise<InferReturnType<TModelsDtoConfig, TModelName>[]>

(
data: {
selector: object
data: object
}[],
sharedContext?: Context
): Promise<InferReturnType<TModelsDtoConfig, TModelName>[]>

// TODO: This is a temporary fix to support the old way of updating a single entity
(id: string, data: any, sharedContext?: Context): Promise<
InferReturnType<TModelsDtoConfig, TModelName>
>

// TODO: This is a temporary fix to support the old way of updating a single entity
(selector: object, data: any, sharedContext?: Context): Promise<
InferReturnType<TModelsDtoConfig, TModelName>[]
>

(data: any[], sharedContext?: Context): Promise<
InferReturnType<TModelsDtoConfig, TModelName>[]
>

(data: any, sharedContext?: Context): Promise<
InferReturnType<TModelsDtoConfig, TModelName>
>
}
}

Expand Down
22 changes: 15 additions & 7 deletions packages/modules/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import {
ModulesSdkTypes,
} from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
createRawPropertiesFromBigNumber,
decorateCartTotals,
deduplicate,
InjectManager,
InjectTransactionManager,
isObject,
isString,
MedusaContext,
MedusaError,
ModulesSdkUtils,
} from "@medusajs/utils"
import {
Address,
Expand Down Expand Up @@ -303,18 +303,21 @@ export default class CartModuleService
async updateCarts(
data: CartTypes.UpdateCartDTO[]
): Promise<CartTypes.CartDTO[]>
// @ts-expect-error
async updateCarts(
cartId: string,
data: CartTypes.UpdateCartDataDTO,
sharedContext?: Context
): Promise<CartTypes.CartDTO>
// @ts-expect-error
async updateCarts(
selector: Partial<CartTypes.CartDTO>,
data: CartTypes.UpdateCartDataDTO,
sharedContext?: Context
): Promise<CartTypes.CartDTO[]>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateCarts(
dataOrIdOrSelector:
| CartTypes.UpdateCartDTO[]
Expand Down Expand Up @@ -447,22 +450,25 @@ export default class CartModuleService
return await this.lineItemService_.create(data, sharedContext)
}

// @ts-ignore
// @ts-expect-error
updateLineItems(
data: CartTypes.UpdateLineItemWithSelectorDTO[]
): Promise<CartTypes.CartLineItemDTO[]>
// @ts-expect-error
updateLineItems(
selector: Partial<CartTypes.CartLineItemDTO>,
data: CartTypes.UpdateLineItemDTO,
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO[]>
// @ts-expect-error
updateLineItems(
lineItemId: string,
data: Partial<CartTypes.UpdateLineItemDTO>,
sharedContext?: Context
): Promise<CartTypes.CartLineItemDTO>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateLineItems(
lineItemIdOrDataOrSelector:
| string
Expand Down Expand Up @@ -577,17 +583,19 @@ export default class CartModuleService
return await this.addressService_.create(data, sharedContext)
}

// @ts-ignore
// @ts-expect-error
async updateAddresses(
data: CartTypes.UpdateAddressDTO,
sharedContext?: Context
): Promise<CartTypes.CartAddressDTO>
// @ts-expect-error
async updateAddresses(
data: CartTypes.UpdateAddressDTO[],
sharedContext?: Context
): Promise<CartTypes.CartAddressDTO[]>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateAddresses(
data: CartTypes.UpdateAddressDTO[] | CartTypes.UpdateAddressDTO,
@MedusaContext() sharedContext: Context = {}
Expand Down
16 changes: 11 additions & 5 deletions packages/modules/inventory-next/src/services/inventory-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import {
} from "@medusajs/types"
import { IInventoryService } from "@medusajs/types/dist/inventory"
import {
arrayDifference,
BigNumber,
CommonEvents,
EmitEvents,
InjectManager,
InjectTransactionManager,
InventoryEvents,
isDefined,
isString,
MathBN,
MedusaContext,
MedusaError,
MedusaService,
arrayDifference,
isDefined,
isString,
partitionArray,
} from "@medusajs/utils"
import { InventoryItem, InventoryLevel, ReservationItem } from "@models"
Expand Down Expand Up @@ -425,13 +425,15 @@ export default class InventoryModuleService
input: InventoryTypes.UpdateInventoryItemInput[],
context?: Context
): Promise<InventoryTypes.InventoryItemDTO[]>
// @ts-expect-error
updateInventoryItems(
input: InventoryTypes.UpdateInventoryItemInput,
context?: Context
): Promise<InventoryTypes.InventoryItemDTO>

@InjectManager("baseRepository_")
@EmitEvents()
// @ts-expect-error
async updateInventoryItems(
input:
| InventoryTypes.UpdateInventoryItemInput
Expand Down Expand Up @@ -535,18 +537,20 @@ export default class InventoryModuleService
return await this.inventoryLevelService_.delete(inventoryLevel.id, context)
}

// @ts-ignore
// @ts-expect-error
async updateInventoryLevels(
updates: InventoryTypes.BulkUpdateInventoryLevelInput[],
context?: Context
): Promise<InventoryTypes.InventoryLevelDTO[]>
// @ts-expect-error
async updateInventoryLevels(
updates: InventoryTypes.BulkUpdateInventoryLevelInput,
context?: Context
): Promise<InventoryTypes.InventoryLevelDTO>

@InjectManager("baseRepository_")
@EmitEvents()
// @ts-expect-error
async updateInventoryLevels(
updates:
| InventoryTypes.BulkUpdateInventoryLevelInput[]
Expand Down Expand Up @@ -622,18 +626,20 @@ export default class InventoryModuleService
* @param context
* @return The updated inventory level
*/
// @ts-ignore
// @ts-expect-error
async updateReservationItems(
input: InventoryTypes.UpdateReservationItemInput[],
context?: Context
): Promise<InventoryTypes.ReservationItemDTO[]>
// @ts-expect-error
async updateReservationItems(
input: InventoryTypes.UpdateReservationItemInput,
context?: Context
): Promise<InventoryTypes.ReservationItemDTO>

@InjectManager("baseRepository_")
@EmitEvents()
// @ts-expect-error
async updateReservationItems(
input:
| InventoryTypes.UpdateReservationItemInput
Expand Down
13 changes: 11 additions & 2 deletions packages/modules/order/src/services/order-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const generateMethodForModels = {
}

// TODO: rm template args here, keep it for later to not collide with carlos work at least as little as possible
// @ts-expect-error
export default class OrderModuleService<
TOrder extends Order = Order,
TAddress extends Address = Address,
Expand Down Expand Up @@ -660,18 +661,21 @@ export default class OrderModuleService<
async updateOrders(
data: OrderTypes.UpdateOrderDTO[]
): Promise<OrderTypes.OrderDTO[]>
// @ts-expect-error
async updateOrders(
orderId: string,
data: OrderTypes.UpdateOrderDTO,
sharedContext?: Context
): Promise<OrderTypes.OrderDTO>
// @ts-expect-error
async updateOrders(
selector: Partial<OrderTypes.FilterableOrderProps>,
data: OrderTypes.UpdateOrderDTO,
sharedContext?: Context
): Promise<OrderTypes.OrderDTO[]>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateOrders(
dataOrIdOrSelector:
| OrderTypes.UpdateOrderDTO[]
Expand Down Expand Up @@ -851,22 +855,25 @@ export default class OrderModuleService<
return lineItems
}

// @ts-ignore
// @ts-expect-error
updateLineItems(
data: OrderTypes.UpdateOrderLineItemWithSelectorDTO[]
): Promise<OrderTypes.OrderLineItemDTO[]>
// @ts-expect-error
updateLineItems(
selector: Partial<OrderTypes.FilterableOrderLineItemProps>,
data: OrderTypes.UpdateOrderLineItemDTO,
sharedContext?: Context
): Promise<OrderTypes.OrderLineItemDTO[]>
// @ts-expect-error
updateLineItems(
lineItemId: string,
data: Partial<OrderTypes.UpdateOrderLineItemDTO>,
sharedContext?: Context
): Promise<OrderTypes.OrderLineItemDTO>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateLineItems(
lineItemIdOrDataOrSelector:
| string
Expand Down Expand Up @@ -1102,17 +1109,19 @@ export default class OrderModuleService<
return await this.addressService_.create(data, sharedContext)
}

// @ts-ignore
// @ts-expect-error
async updateAddresses(
data: OrderTypes.UpdateOrderAddressDTO,
sharedContext?: Context
): Promise<OrderTypes.OrderAddressDTO>
// @ts-expect-error
async updateAddresses(
data: OrderTypes.UpdateOrderAddressDTO[],
sharedContext?: Context
): Promise<OrderTypes.OrderAddressDTO[]>

@InjectManager("baseRepository_")
// @ts-expect-error
async updateAddresses(
data: OrderTypes.UpdateOrderAddressDTO[] | OrderTypes.UpdateOrderAddressDTO,
@MedusaContext() sharedContext: Context = {}
Expand Down
Loading

0 comments on commit 2f5be74

Please sign in to comment.