diff --git a/.changeset/wet-forks-scream.md b/.changeset/wet-forks-scream.md new file mode 100644 index 0000000000000..e801a918d0f22 --- /dev/null +++ b/.changeset/wet-forks-scream.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chore(medusa): Validate required id in `[someService].retrieve` diff --git a/packages/medusa/src/services/batch-job.ts b/packages/medusa/src/services/batch-job.ts index eb9de5d985158..5f682bfc11f31 100644 --- a/packages/medusa/src/services/batch-job.ts +++ b/packages/medusa/src/services/batch-job.ts @@ -11,7 +11,7 @@ import { } from "../types/batch-job" import { FindConfig } from "../types/common" import { TransactionBaseService } from "../interfaces" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import { MedusaError } from "medusa-core-utils" import { EventBusService, StrategyResolverService } from "./index" import { Request } from "express" @@ -109,6 +109,13 @@ class BatchJobService extends TransactionBaseService { batchJobId: string, config: FindConfig = {} ): Promise { + if (!isDefined(batchJobId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"batchJobId" must be defined` + ) + } + const manager = this.manager_ const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_) diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index bd6d1a9dbca94..8fb3ccda1fd5f 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -214,6 +214,13 @@ class CartService extends TransactionBaseService { options: FindConfig = {}, totalsConfig: TotalsConfig = {} ): Promise { + if (!isDefined(cartId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"cartId" must be defined` + ) + } + const { totalsToSelect } = this.transformQueryForTotals_(options) if (totalsToSelect.length) { diff --git a/packages/medusa/src/services/claim-item.ts b/packages/medusa/src/services/claim-item.ts index 1e405e0f5ee97..e7e1fb9f30db7 100644 --- a/packages/medusa/src/services/claim-item.ts +++ b/packages/medusa/src/services/claim-item.ts @@ -7,7 +7,7 @@ import { ClaimItemRepository } from "../repositories/claim-item" import { ClaimTagRepository } from "../repositories/claim-tag" import { CreateClaimItemInput } from "../types/claim" import { FindConfig, Selector } from "../types/common" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import EventBusService from "./event-bus" import LineItemService from "./line-item" @@ -226,24 +226,31 @@ class ClaimItemService extends TransactionBaseService { /** * Gets a claim item by id. - * @param {string} id - id of ClaimItem to retrieve + * @param {string} claimItemId - id of ClaimItem to retrieve * @param {Object} config - configuration for the find operation * @return {Promise} the ClaimItem */ async retrieve( - id: string, + claimItemId: string, config: FindConfig = {} ): Promise { + if (!isDefined(claimItemId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"claimItemId" must be defined` + ) + } + const claimItemRepo = this.manager_.getCustomRepository( this.claimItemRepository_ ) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: claimItemId }, config) const item = await claimItemRepo.findOne(query) if (!item) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Claim item with id: ${id} was not found.` + `Claim item with id: ${claimItemId} was not found.` ) } diff --git a/packages/medusa/src/services/claim.ts b/packages/medusa/src/services/claim.ts index 78c78f61ea89c..bfe06eaa8ee5a 100644 --- a/packages/medusa/src/services/claim.ts +++ b/packages/medusa/src/services/claim.ts @@ -830,24 +830,31 @@ export default class ClaimService extends TransactionBaseService { /** * Gets an order by id. - * @param id - id of the claim order to retrieve + * @param claimId - id of the claim order to retrieve * @param config - the config object containing query settings * @return the order document */ async retrieve( - id: string, + claimId: string, config: FindConfig = {} ): Promise { + if (!isDefined(claimId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"claimId" must be defined` + ) + } + const manager = this.manager_ const claimRepo = manager.getCustomRepository(this.claimRepository_) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: claimId }, config) const claim = await claimRepo.findOne(query) if (!claim) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Claim with ${id} was not found` + `Claim with ${claimId} was not found` ) } diff --git a/packages/medusa/src/services/customer-group.ts b/packages/medusa/src/services/customer-group.ts index 385400795150e..37afd0b64e6d1 100644 --- a/packages/medusa/src/services/customer-group.ts +++ b/packages/medusa/src/services/customer-group.ts @@ -42,18 +42,25 @@ class CustomerGroupService extends TransactionBaseService { this.customerService_ = customerService } - async retrieve(id: string, config = {}): Promise { + async retrieve(customerGroupId: string, config = {}): Promise { + if (!isDefined(customerGroupId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"customerGroupId" must be defined` + ) + } + const cgRepo = this.manager_.getCustomRepository( this.customerGroupRepository_ ) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: customerGroupId }, config) const customerGroup = await cgRepo.findOne(query) if (!customerGroup) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `CustomerGroup with id ${id} was not found` + `CustomerGroup with id ${customerGroupId} was not found` ) } diff --git a/packages/medusa/src/services/customer.ts b/packages/medusa/src/services/customer.ts index f3b22c6d4fd89..52ba9b612ad6e 100644 --- a/packages/medusa/src/services/customer.ts +++ b/packages/medusa/src/services/customer.ts @@ -196,6 +196,13 @@ class CustomerService extends TransactionBaseService { email: string, config: FindConfig = {} ): Promise { + if (!isDefined(email)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"email" must be defined` + ) + } + return await this.retrieve_({ email: email.toLowerCase() }, config) } @@ -247,6 +254,13 @@ class CustomerService extends TransactionBaseService { customerId: string, config: FindConfig = {} ): Promise { + if (!isDefined(customerId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"customerId" must be defined` + ) + } + return this.retrieve_({ id: customerId }, config) } diff --git a/packages/medusa/src/services/discount-condition.ts b/packages/medusa/src/services/discount-condition.ts index 3467e3835ec0f..058b468ab54ae 100644 --- a/packages/medusa/src/services/discount-condition.ts +++ b/packages/medusa/src/services/discount-condition.ts @@ -14,7 +14,7 @@ import { DiscountConditionRepository } from "../repositories/discount-condition" import { FindConfig } from "../types/common" import { DiscountConditionInput } from "../types/discount" import { TransactionBaseService } from "../interfaces" -import { buildQuery, PostgresError } from "../utils" +import { buildQuery, isDefined, PostgresError } from "../utils" type InjectedDependencies = { manager: EntityManager @@ -50,6 +50,13 @@ class DiscountConditionService extends TransactionBaseService { conditionId: string, config?: FindConfig ): Promise { + if (!isDefined(conditionId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"conditionId" must be defined` + ) + } + const manager = this.manager_ const conditionRepo = manager.getCustomRepository( this.discountConditionRepository_ @@ -108,7 +115,7 @@ class DiscountConditionService extends TransactionBaseService { async upsertCondition( data: DiscountConditionInput, - overrideExisting: boolean = true + overrideExisting = true ): Promise< ( | DiscountConditionProduct diff --git a/packages/medusa/src/services/discount.ts b/packages/medusa/src/services/discount.ts index f609d24940831..cfa436cce274a 100644 --- a/packages/medusa/src/services/discount.ts +++ b/packages/medusa/src/services/discount.ts @@ -36,7 +36,7 @@ import { UpdateDiscountInput, UpdateDiscountRuleInput, } from "../types/discount" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import { isFuture, isPast } from "../utils/date-helpers" import { FlagRouter } from "../utils/flag-router" import CustomerService from "./customer" @@ -253,6 +253,13 @@ class DiscountService extends TransactionBaseService { discountId: string, config: FindConfig = {} ): Promise { + if (!isDefined(discountId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"discountId" must be defined` + ) + } + const manager = this.manager_ const discountRepo = manager.getCustomRepository(this.discountRepository_) diff --git a/packages/medusa/src/services/draft-order.ts b/packages/medusa/src/services/draft-order.ts index 779d953b5fc2f..ae89a726bcbe2 100644 --- a/packages/medusa/src/services/draft-order.ts +++ b/packages/medusa/src/services/draft-order.ts @@ -7,7 +7,7 @@ import { OrderRepository } from "../repositories/order" import { PaymentRepository } from "../repositories/payment" import { ExtendedFindConfig, FindConfig } from "../types/common" import { DraftOrderCreateProps } from "../types/draft-orders" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import CartService from "./cart" import CustomShippingOptionService from "./custom-shipping-option" import EventBusService from "./event-bus" @@ -80,25 +80,32 @@ class DraftOrderService extends TransactionBaseService { /** * Retrieves a draft order with the given id. - * @param id - id of the draft order to retrieve + * @param draftOrderId - id of the draft order to retrieve * @param config - query object for findOne * @return the draft order */ async retrieve( - id: string, + draftOrderId: string, config: FindConfig = {} ): Promise { + if (!isDefined(draftOrderId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"draftOrderId" must be defined` + ) + } + const manager = this.manager_ const draftOrderRepo = manager.getCustomRepository( this.draftOrderRepository_ ) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: draftOrderId }, config) const draftOrder = await draftOrderRepo.findOne(query) if (!draftOrder) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Draft order with ${id} was not found` + `Draft order with ${draftOrderId} was not found` ) } diff --git a/packages/medusa/src/services/fulfillment.ts b/packages/medusa/src/services/fulfillment.ts index ea882f94da6c6..56de2b12b9fa3 100644 --- a/packages/medusa/src/services/fulfillment.ts +++ b/packages/medusa/src/services/fulfillment.ts @@ -159,27 +159,34 @@ class FulfillmentService extends TransactionBaseService { /** * Retrieves a fulfillment by its id. - * @param id - the id of the fulfillment to retrieve + * @param fulfillmentId - the id of the fulfillment to retrieve * @param config - optional values to include with fulfillmentRepository query * @return the fulfillment */ async retrieve( - id: string, + fulfillmentId: string, config: FindConfig = {} ): Promise { + if (!isDefined(fulfillmentId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"fulfillmentId" must be defined` + ) + } + const manager = this.manager_ const fulfillmentRepository = manager.getCustomRepository( this.fulfillmentRepository_ ) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: fulfillmentId }, config) const fulfillment = await fulfillmentRepository.findOne(query) if (!fulfillment) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Fulfillment with id: ${id} was not found` + `Fulfillment with id: ${fulfillmentId} was not found` ) } return fulfillment diff --git a/packages/medusa/src/services/gift-card.ts b/packages/medusa/src/services/gift-card.ts index 990fe584163a6..aff58df16f70a 100644 --- a/packages/medusa/src/services/gift-card.ts +++ b/packages/medusa/src/services/gift-card.ts @@ -218,6 +218,13 @@ class GiftCardService extends TransactionBaseService { giftCardId: string, config: FindConfig = {} ): Promise { + if (!isDefined(giftCardId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"giftCardId" must be defined` + ) + } + return await this.retrieve_({ id: giftCardId }, config) } @@ -225,6 +232,13 @@ class GiftCardService extends TransactionBaseService { code: string, config: FindConfig = {} ): Promise { + if (!isDefined(code)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"code" must be defined` + ) + } + return await this.retrieve_({ code }, config) } diff --git a/packages/medusa/src/services/idempotency-key.ts b/packages/medusa/src/services/idempotency-key.ts index 11f0955ef15ba..5a492df8b8a0d 100644 --- a/packages/medusa/src/services/idempotency-key.ts +++ b/packages/medusa/src/services/idempotency-key.ts @@ -8,6 +8,7 @@ import { CreateIdempotencyKeyInput, IdempotencyCallbackResult, } from "../types/idempotency-key" +import { isDefined } from "../utils" const KEY_LOCKED_TIMEOUT = 1000 @@ -83,6 +84,13 @@ class IdempotencyKeyService extends TransactionBaseService { * @return idempotency key */ async retrieve(idempotencyKey: string): Promise { + if (!isDefined(idempotencyKey)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"idempotencyKey" must be defined` + ) + } + const idempotencyKeyRepo = this.manager_.getCustomRepository( this.idempotencyKeyRepository_ ) diff --git a/packages/medusa/src/services/line-item-adjustment.ts b/packages/medusa/src/services/line-item-adjustment.ts index 62a7dc615991f..caa2125742710 100644 --- a/packages/medusa/src/services/line-item-adjustment.ts +++ b/packages/medusa/src/services/line-item-adjustment.ts @@ -7,7 +7,7 @@ import { FindConfig } from "../types/common" import { FilterableLineItemAdjustmentProps } from "../types/line-item-adjustment" import DiscountService from "./discount" import { TransactionBaseService } from "../interfaces" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import { CalculationContextData } from "../types/totals" type LineItemAdjustmentServiceProps = { @@ -51,24 +51,31 @@ class LineItemAdjustmentService extends TransactionBaseService { /** * Retrieves a line item adjustment by id. - * @param id - the id of the line item adjustment to retrieve + * @param lineItemAdjustmentId - the id of the line item adjustment to retrieve * @param config - the config to retrieve the line item adjustment by * @return the line item adjustment. */ async retrieve( - id: string, + lineItemAdjustmentId: string, config: FindConfig = {} ): Promise { + if (!isDefined(lineItemAdjustmentId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"lineItemAdjustmentId" must be defined` + ) + } + const lineItemAdjustmentRepo: LineItemAdjustmentRepository = this.manager_.getCustomRepository(this.lineItemAdjustmentRepo_) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: lineItemAdjustmentId }, config) const lineItemAdjustment = await lineItemAdjustmentRepo.findOne(query) if (!lineItemAdjustment) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Line item adjustment with id: ${id} was not found` + `Line item adjustment with id: ${lineItemAdjustmentId} was not found` ) } diff --git a/packages/medusa/src/services/note.ts b/packages/medusa/src/services/note.ts index 06bee67e9c475..d14513ebadb45 100644 --- a/packages/medusa/src/services/note.ts +++ b/packages/medusa/src/services/note.ts @@ -5,7 +5,7 @@ import { NoteRepository } from "../repositories/note" import EventBusService from "./event-bus" import { FindConfig, Selector } from "../types/common" import { Note } from "../models" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import { CreateNoteInput } from "../types/note" type InjectedDependencies = { @@ -40,24 +40,31 @@ class NoteService extends TransactionBaseService { /** * Retrieves a specific note. - * @param id - the id of the note to retrieve. + * @param noteId - the id of the note to retrieve. * @param config - any options needed to query for the result. * @return which resolves to the requested note. */ async retrieve( - id: string, + noteId: string, config: FindConfig = {} ): Promise { + if (!isDefined(noteId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"noteId" must be defined` + ) + } + const noteRepo = this.manager_.getCustomRepository(this.noteRepository_) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: noteId }, config) const note = await noteRepo.findOne(query) if (!note) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Note with id: ${id} was not found.` + `Note with id: ${noteId} was not found.` ) } diff --git a/packages/medusa/src/services/oauth.ts b/packages/medusa/src/services/oauth.ts index c7cc83200fba9..205e6d861a094 100644 --- a/packages/medusa/src/services/oauth.ts +++ b/packages/medusa/src/services/oauth.ts @@ -6,7 +6,7 @@ import { OauthRepository } from "../repositories/oauth" import { Selector } from "../types/common" import { MedusaContainer } from "../types/global" import { CreateOauthInput, UpdateOauthInput } from "../types/oauth" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import EventBusService from "./event-bus" type InjectedDependencies = MedusaContainer & { @@ -55,6 +55,13 @@ class Oauth extends TransactionBaseService { } async retrieve(oauthId: string): Promise { + if (!isDefined(oauthId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"oauthId" must be defined` + ) + } + const repo = this.manager.getCustomRepository(this.oauthRepository_) const oauth = await repo.findOne({ id: oauthId, diff --git a/packages/medusa/src/services/order-edit.ts b/packages/medusa/src/services/order-edit.ts index ba3324d66d945..df1cbedd0c33a 100644 --- a/packages/medusa/src/services/order-edit.ts +++ b/packages/medusa/src/services/order-edit.ts @@ -91,6 +91,13 @@ export default class OrderEditService extends TransactionBaseService { orderEditId: string, config: FindConfig = {} ): Promise { + if (!isDefined(orderEditId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"orderEditId" must be defined` + ) + } + const manager = this.transactionManager_ ?? this.manager_ const orderEditRepository = manager.getCustomRepository( this.orderEditRepository_ diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index ee5f9aabeae83..99ade188d263d 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -336,6 +336,13 @@ class OrderService extends TransactionBaseService { orderId: string, config: FindConfig = {} ): Promise { + if (!isDefined(orderId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"orderId" must be defined` + ) + } + const { totalsToSelect } = this.transformQueryForTotals(config) if (totalsToSelect?.length) { @@ -701,7 +708,7 @@ class OrderService extends TransactionBaseService { ), ] }), - cart.shipping_methods.map((method) => { + cart.shipping_methods.map(async (method) => { // TODO: Due to cascade insert we have to remove the tax_lines that have been added by the cart decorate totals. // Is the cascade insert really used? Also, is it really necessary to pass the entire entities when creating or updating? // We normally should only pass what is needed? diff --git a/packages/medusa/src/services/payment-collection.ts b/packages/medusa/src/services/payment-collection.ts index 4605e7cc98f09..bf86615b9d8e3 100644 --- a/packages/medusa/src/services/payment-collection.ts +++ b/packages/medusa/src/services/payment-collection.ts @@ -75,6 +75,13 @@ export default class PaymentCollectionService extends TransactionBaseService { paymentCollectionId: string, config: FindConfig = {} ): Promise { + if (!isDefined(paymentCollectionId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"paymentCollectionId" must be defined` + ) + } + const manager = this.transactionManager_ ?? this.manager_ const paymentCollectionRepository = manager.getCustomRepository( this.paymentCollectionRepository_ diff --git a/packages/medusa/src/services/payment.ts b/packages/medusa/src/services/payment.ts index c6f52524954cd..ae8a6220a9055 100644 --- a/packages/medusa/src/services/payment.ts +++ b/packages/medusa/src/services/payment.ts @@ -5,7 +5,7 @@ import { MedusaError } from "medusa-core-utils" import { Payment, Refund } from "../models" import { TransactionBaseService } from "../interfaces" import { EventBusService, PaymentProviderService } from "./index" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import { FindConfig } from "../types/common" type InjectedDependencies = { @@ -62,6 +62,13 @@ export default class PaymentService extends TransactionBaseService { paymentId: string, config: FindConfig = {} ): Promise { + if (!isDefined(paymentId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"paymentId" must be defined` + ) + } + const manager = this.transactionManager_ ?? this.manager_ const paymentRepository = manager.getCustomRepository( this.paymentRepository_ diff --git a/packages/medusa/src/services/price-list.ts b/packages/medusa/src/services/price-list.ts index af697586a1e56..f353f201d3585 100644 --- a/packages/medusa/src/services/price-list.ts +++ b/packages/medusa/src/services/price-list.ts @@ -18,7 +18,7 @@ import { import ProductService from "./product" import RegionService from "./region" import { TransactionBaseService } from "../interfaces" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" import { FilterableProductProps } from "../types/product" import ProductVariantService from "./product-variant" import { FilterableProductVariantProps } from "../types/product-variant" @@ -89,6 +89,13 @@ class PriceListService extends TransactionBaseService { priceListId: string, config: FindConfig = {} ): Promise { + if (!isDefined(priceListId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"priceListId" must be defined` + ) + } + const priceListRepo = this.manager_.getCustomRepository(this.priceListRepo_) const query = buildQuery({ id: priceListId }, config) diff --git a/packages/medusa/src/services/product-collection.ts b/packages/medusa/src/services/product-collection.ts index 91001703d5316..e4eccb70a8a34 100644 --- a/packages/medusa/src/services/product-collection.ts +++ b/packages/medusa/src/services/product-collection.ts @@ -9,7 +9,7 @@ import { CreateProductCollection, UpdateProductCollection, } from "../types/product-collection" -import { buildQuery, isString, setMetadata } from "../utils" +import { buildQuery, isDefined, isString, setMetadata } from "../utils" import EventBusService from "./event-bus" type InjectedDependencies = { @@ -55,6 +55,13 @@ class ProductCollectionService extends TransactionBaseService { collectionId: string, config: FindConfig = {} ): Promise { + if (!isDefined(collectionId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"collectionId" must be defined` + ) + } + const collectionRepo = this.manager_.getCustomRepository( this.productCollectionRepository_ ) diff --git a/packages/medusa/src/services/product.ts b/packages/medusa/src/services/product.ts index 5df6d9b8e8907..ae6f232c005c5 100644 --- a/packages/medusa/src/services/product.ts +++ b/packages/medusa/src/services/product.ts @@ -182,6 +182,13 @@ class ProductService extends TransactionBaseService { include_discount_prices: false, } ): Promise { + if (!isDefined(productId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"productId" must be defined` + ) + } + return await this.retrieve_({ id: productId }, config) } @@ -196,6 +203,13 @@ class ProductService extends TransactionBaseService { productHandle: string, config: FindProductConfig = {} ): Promise { + if (!isDefined(productHandle)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"productHandle" must be defined` + ) + } + return await this.retrieve_({ handle: productHandle }, config) } @@ -210,6 +224,13 @@ class ProductService extends TransactionBaseService { externalId: string, config: FindProductConfig = {} ): Promise { + if (!isDefined(externalId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"externalId" must be defined` + ) + } + return await this.retrieve_({ external_id: externalId }, config) } diff --git a/packages/medusa/src/services/publishable-api-key.ts b/packages/medusa/src/services/publishable-api-key.ts index e807846ca9451..584d24da2391c 100644 --- a/packages/medusa/src/services/publishable-api-key.ts +++ b/packages/medusa/src/services/publishable-api-key.ts @@ -94,6 +94,13 @@ class PublishableApiKeyService extends TransactionBaseService { publishableApiKeyId: string, config: FindConfig = {} ): Promise { + if (!isDefined(publishableApiKeyId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"publishableApiKeyId" must be defined` + ) + } + return await this.retrieve_({ id: publishableApiKeyId }, config) } diff --git a/packages/medusa/src/services/region.ts b/packages/medusa/src/services/region.ts index f5a92daa889be..90119eb163016 100644 --- a/packages/medusa/src/services/region.ts +++ b/packages/medusa/src/services/region.ts @@ -13,7 +13,7 @@ import { RegionRepository } from "../repositories/region" import { TaxProviderRepository } from "../repositories/tax-provider" import { FindConfig, Selector } from "../types/common" import { CreateRegionInput, UpdateRegionInput } from "../types/region" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import { countries } from "../utils/countries" import { FlagRouter } from "../utils/flag-router" import EventBusService from "./event-bus" @@ -495,6 +495,13 @@ class RegionService extends TransactionBaseService { regionId: string, config: FindConfig = {} ): Promise { + if (!isDefined(regionId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"regionId" must be defined` + ) + } + const regionRepository = this.manager_.getCustomRepository( this.regionRepository_ ) diff --git a/packages/medusa/src/services/return-reason.ts b/packages/medusa/src/services/return-reason.ts index 1e099899dccfb..b1ff1fbf5fa1c 100644 --- a/packages/medusa/src/services/return-reason.ts +++ b/packages/medusa/src/services/return-reason.ts @@ -5,7 +5,7 @@ import { Return, ReturnReason } from "../models" import { ReturnReasonRepository } from "../repositories/return-reason" import { FindConfig, Selector } from "../types/common" import { CreateReturnReason, UpdateReturnReason } from "../types/return-reason" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" type InjectedDependencies = { manager: EntityManager @@ -84,23 +84,30 @@ class ReturnReasonService extends TransactionBaseService { /** * Gets an order by id. - * @param {string} id - id of order to retrieve + * @param {string} returnReasonId - id of order to retrieve * @param {Object} config - config object * @return {Promise} the order document */ async retrieve( - id: string, + returnReasonId: string, config: FindConfig = {} ): Promise { + if (!isDefined(returnReasonId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"returnReasonId" must be defined` + ) + } + const rrRepo = this.manager_.getCustomRepository(this.retReasonRepo_) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: returnReasonId }, config) const item = await rrRepo.findOne(query) if (!item) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Return Reason with id: ${id} was not found.` + `Return Reason with id: ${returnReasonId} was not found.` ) } diff --git a/packages/medusa/src/services/return.ts b/packages/medusa/src/services/return.ts index c6c10a6c79660..959bd76733cb0 100644 --- a/packages/medusa/src/services/return.ts +++ b/packages/medusa/src/services/return.ts @@ -251,26 +251,33 @@ class ReturnService extends TransactionBaseService { /** * Retrieves a return by its id. - * @param id - the id of the return to retrieve + * @param returnId - the id of the return to retrieve * @param config - the config object * @return the return */ async retrieve( - id: string, + returnId: string, config: FindConfig = {} ): Promise { + if (!isDefined(returnId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"returnId" must be defined` + ) + } + const returnRepository = this.manager_.getCustomRepository( this.returnRepository_ ) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: returnId }, config) const returnObj = await returnRepository.findOne(query) if (!returnObj) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Return with id: ${id} was not found` + `Return with id: ${returnId} was not found` ) } return returnObj diff --git a/packages/medusa/src/services/sales-channel.ts b/packages/medusa/src/services/sales-channel.ts index d46edc16d9abd..1bae7f44743e5 100644 --- a/packages/medusa/src/services/sales-channel.ts +++ b/packages/medusa/src/services/sales-channel.ts @@ -11,7 +11,7 @@ import { SalesChannel } from "../models" import { SalesChannelRepository } from "../repositories/sales-channel" import StoreService from "./store" import { TransactionBaseService } from "../interfaces" -import { buildQuery } from "../utils" +import { buildQuery, isDefined } from "../utils" type InjectedDependencies = { salesChannelRepository: typeof SalesChannelRepository @@ -100,6 +100,13 @@ class SalesChannelService extends TransactionBaseService { salesChannelId: string, config: FindConfig = {} ): Promise { + if (!isDefined(salesChannelId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"salesChannelId" must be defined` + ) + } + return await this.retrieve_({ id: salesChannelId }, config) } @@ -114,6 +121,13 @@ class SalesChannelService extends TransactionBaseService { name: string, config: FindConfig = {} ): Promise { + if (!isDefined(name)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"name" must be defined` + ) + } + return await this.retrieve_({ name }, config) } diff --git a/packages/medusa/src/services/shipping-option.ts b/packages/medusa/src/services/shipping-option.ts index 4f5a04de547ab..4982372c642e0 100644 --- a/packages/medusa/src/services/shipping-option.ts +++ b/packages/medusa/src/services/shipping-option.ts @@ -183,6 +183,13 @@ class ShippingOptionService extends TransactionBaseService { optionId, options: { select?: (keyof ShippingOption)[]; relations?: string[] } = {} ): Promise { + if (!isDefined(optionId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"optionId" must be defined` + ) + } + const manager = this.manager_ const soRepo: ShippingOptionRepository = manager.getCustomRepository( this.optionRepository_ diff --git a/packages/medusa/src/services/shipping-profile.ts b/packages/medusa/src/services/shipping-profile.ts index eb5c6a04f3b5a..518e0207f0a04 100644 --- a/packages/medusa/src/services/shipping-profile.ts +++ b/packages/medusa/src/services/shipping-profile.ts @@ -15,7 +15,7 @@ import { CreateShippingProfile, UpdateShippingProfile, } from "../types/shipping-profile" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import CustomShippingOptionService from "./custom-shipping-option" import ProductService from "./product" import ShippingOptionService from "./shipping-option" @@ -136,6 +136,13 @@ class ShippingProfileService extends TransactionBaseService { profileId: string, options: FindConfig = {} ): Promise { + if (!isDefined(profileId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"profileId" must be defined` + ) + } + const profileRepository = this.manager_.getCustomRepository( this.shippingProfileRepository_ ) diff --git a/packages/medusa/src/services/swap.ts b/packages/medusa/src/services/swap.ts index ff312d41d7d5d..2a36d9f8d237d 100644 --- a/packages/medusa/src/services/swap.ts +++ b/packages/medusa/src/services/swap.ts @@ -201,20 +201,27 @@ class SwapService extends TransactionBaseService { /** * Retrieves a swap with the given id. * - * @param id - the id of the swap to retrieve + * @param swapId - the id of the swap to retrieve * @param config - the configuration to retrieve the swap * @return the swap */ async retrieve( - id: string, + swapId: string, config: Omit, "select"> & { select?: string[] } = {} ): Promise { + if (!isDefined(swapId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"swapId" must be defined` + ) + } + const swapRepo = this.manager_.getCustomRepository(this.swapRepository_) const { cartSelects, cartRelations, ...newConfig } = this.transformQueryForCart(config) - const query = buildQuery({ id }, newConfig) + const query = buildQuery({ id: swapId }, newConfig) const relations = query.relations as (keyof Swap)[] delete query.relations diff --git a/packages/medusa/src/services/tax-rate.ts b/packages/medusa/src/services/tax-rate.ts index 8c3afa5ee03ac..3ce370329b85b 100644 --- a/packages/medusa/src/services/tax-rate.ts +++ b/packages/medusa/src/services/tax-rate.ts @@ -67,18 +67,25 @@ class TaxRateService extends TransactionBaseService { } async retrieve( - id: string, + taxRateId: string, config: FindConfig = {} ): Promise { + if (!isDefined(taxRateId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"taxRateId" must be defined` + ) + } + const manager = this.manager_ const taxRateRepo = manager.getCustomRepository(this.taxRateRepository_) - const query = buildQuery({ id }, config) + const query = buildQuery({ id: taxRateId }, config) const taxRate = await taxRateRepo.findOneWithResolution(query) if (!taxRate) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `TaxRate with ${id} was not found` + `TaxRate with ${taxRateId} was not found` ) } diff --git a/packages/medusa/src/services/user.ts b/packages/medusa/src/services/user.ts index 42e2b0cb1efce..ae9e6a69504c2 100644 --- a/packages/medusa/src/services/user.ts +++ b/packages/medusa/src/services/user.ts @@ -12,7 +12,7 @@ import { FilterableUserProps, UpdateUserInput, } from "../types/user" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isDefined, setMetadata } from "../utils" import { FlagRouter } from "../utils/flag-router" import { validateEmail } from "../utils/is-email" import AnalyticsConfigService from "./analytics-config" @@ -80,6 +80,13 @@ class UserService extends TransactionBaseService { * @return {Promise} the user document. */ async retrieve(userId: string, config: FindConfig = {}): Promise { + if (!isDefined(userId)) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `"userId" must be defined` + ) + } + const manager = this.manager_ const userRepo = manager.getCustomRepository(this.userRepository_) const query = buildQuery({ id: userId }, config) @@ -100,7 +107,7 @@ class UserService extends TransactionBaseService { * Gets a user by api token. * Throws in case of DB Error and if user was not found. * @param {string} apiToken - the token of the user to get. - * @param {string[]} relations - relations to include with the user + * @param {string[]} relations - relations to include with the user. * @return {Promise} the user document. */ async retrieveByApiToken(