feat(medusa): Validate required id before retrieving (#2738)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
chore(medusa): Validate required id in `[someService].retrieve`
|
||||
@@ -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<BatchJob> = {}
|
||||
): Promise<BatchJob | never> {
|
||||
if (!isDefined(batchJobId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"batchJobId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const manager = this.manager_
|
||||
const batchJobRepo = manager.getCustomRepository(this.batchJobRepository_)
|
||||
|
||||
|
||||
@@ -214,6 +214,13 @@ class CartService extends TransactionBaseService {
|
||||
options: FindConfig<Cart> = {},
|
||||
totalsConfig: TotalsConfig = {}
|
||||
): Promise<Cart> {
|
||||
if (!isDefined(cartId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"cartId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const { totalsToSelect } = this.transformQueryForTotals_(options)
|
||||
|
||||
if (totalsToSelect.length) {
|
||||
|
||||
@@ -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<Order>} the ClaimItem
|
||||
*/
|
||||
async retrieve(
|
||||
id: string,
|
||||
claimItemId: string,
|
||||
config: FindConfig<ClaimItem> = {}
|
||||
): Promise<ClaimItem> {
|
||||
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.`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ClaimOrder> = {}
|
||||
): Promise<ClaimOrder> {
|
||||
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`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -42,18 +42,25 @@ class CustomerGroupService extends TransactionBaseService {
|
||||
this.customerService_ = customerService
|
||||
}
|
||||
|
||||
async retrieve(id: string, config = {}): Promise<CustomerGroup> {
|
||||
async retrieve(customerGroupId: string, config = {}): Promise<CustomerGroup> {
|
||||
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`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -196,6 +196,13 @@ class CustomerService extends TransactionBaseService {
|
||||
email: string,
|
||||
config: FindConfig<Customer> = {}
|
||||
): Promise<Customer | never> {
|
||||
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<Customer> = {}
|
||||
): Promise<Customer> {
|
||||
if (!isDefined(customerId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"customerId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
return this.retrieve_({ id: customerId }, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<DiscountCondition>
|
||||
): Promise<DiscountCondition | never> {
|
||||
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
|
||||
|
||||
@@ -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<Discount> = {}
|
||||
): Promise<Discount> {
|
||||
if (!isDefined(discountId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"discountId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const manager = this.manager_
|
||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||
|
||||
|
||||
@@ -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<DraftOrder> = {}
|
||||
): Promise<DraftOrder | never> {
|
||||
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`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Fulfillment> = {}
|
||||
): Promise<Fulfillment> {
|
||||
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
|
||||
|
||||
@@ -218,6 +218,13 @@ class GiftCardService extends TransactionBaseService {
|
||||
giftCardId: string,
|
||||
config: FindConfig<GiftCard> = {}
|
||||
): Promise<GiftCard> {
|
||||
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<GiftCard> = {}
|
||||
): Promise<GiftCard> {
|
||||
if (!isDefined(code)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"code" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.retrieve_({ code }, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<IdempotencyKey | never> {
|
||||
if (!isDefined(idempotencyKey)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"idempotencyKey" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const idempotencyKeyRepo = this.manager_.getCustomRepository(
|
||||
this.idempotencyKeyRepository_
|
||||
)
|
||||
|
||||
@@ -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<LineItemAdjustment> = {}
|
||||
): Promise<LineItemAdjustment> {
|
||||
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`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Note> = {}
|
||||
): Promise<Note | never> {
|
||||
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.`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<OAuthModel> {
|
||||
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,
|
||||
|
||||
@@ -91,6 +91,13 @@ export default class OrderEditService extends TransactionBaseService {
|
||||
orderEditId: string,
|
||||
config: FindConfig<OrderEdit> = {}
|
||||
): Promise<OrderEdit> {
|
||||
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_
|
||||
|
||||
@@ -336,6 +336,13 @@ class OrderService extends TransactionBaseService {
|
||||
orderId: string,
|
||||
config: FindConfig<Order> = {}
|
||||
): Promise<Order> {
|
||||
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?
|
||||
|
||||
@@ -75,6 +75,13 @@ export default class PaymentCollectionService extends TransactionBaseService {
|
||||
paymentCollectionId: string,
|
||||
config: FindConfig<PaymentCollection> = {}
|
||||
): Promise<PaymentCollection> {
|
||||
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_
|
||||
|
||||
@@ -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<Payment> = {}
|
||||
): Promise<Payment> {
|
||||
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_
|
||||
|
||||
@@ -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<PriceList> = {}
|
||||
): Promise<PriceList> {
|
||||
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)
|
||||
|
||||
@@ -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<ProductCollection> = {}
|
||||
): Promise<ProductCollection> {
|
||||
if (!isDefined(collectionId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"collectionId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const collectionRepo = this.manager_.getCustomRepository(
|
||||
this.productCollectionRepository_
|
||||
)
|
||||
|
||||
@@ -182,6 +182,13 @@ class ProductService extends TransactionBaseService {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): Promise<Product> {
|
||||
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<Product> {
|
||||
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<Product> {
|
||||
if (!isDefined(externalId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"externalId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.retrieve_({ external_id: externalId }, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -94,6 +94,13 @@ class PublishableApiKeyService extends TransactionBaseService {
|
||||
publishableApiKeyId: string,
|
||||
config: FindConfig<PublishableApiKey> = {}
|
||||
): Promise<PublishableApiKey | never> {
|
||||
if (!isDefined(publishableApiKeyId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"publishableApiKeyId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.retrieve_({ id: publishableApiKeyId }, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Region> = {}
|
||||
): Promise<Region | never> {
|
||||
if (!isDefined(regionId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"regionId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const regionRepository = this.manager_.getCustomRepository(
|
||||
this.regionRepository_
|
||||
)
|
||||
|
||||
@@ -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<Order>} the order document
|
||||
*/
|
||||
async retrieve(
|
||||
id: string,
|
||||
returnReasonId: string,
|
||||
config: FindConfig<ReturnReason> = {}
|
||||
): Promise<ReturnReason | never> {
|
||||
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.`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Return> = {}
|
||||
): Promise<Return | never> {
|
||||
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
|
||||
|
||||
@@ -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<SalesChannel> = {}
|
||||
): Promise<SalesChannel | never> {
|
||||
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<SalesChannel> = {}
|
||||
): Promise<SalesChannel | unknown> {
|
||||
if (!isDefined(name)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"name" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
return await this.retrieve_({ name }, config)
|
||||
}
|
||||
|
||||
|
||||
@@ -183,6 +183,13 @@ class ShippingOptionService extends TransactionBaseService {
|
||||
optionId,
|
||||
options: { select?: (keyof ShippingOption)[]; relations?: string[] } = {}
|
||||
): Promise<ShippingOption> {
|
||||
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_
|
||||
|
||||
@@ -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<ShippingProfile> = {}
|
||||
): Promise<ShippingProfile> {
|
||||
if (!isDefined(profileId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`"profileId" must be defined`
|
||||
)
|
||||
}
|
||||
|
||||
const profileRepository = this.manager_.getCustomRepository(
|
||||
this.shippingProfileRepository_
|
||||
)
|
||||
|
||||
@@ -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<FindConfig<Swap>, "select"> & { select?: string[] } = {}
|
||||
): Promise<Swap | never> {
|
||||
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
|
||||
|
||||
@@ -67,18 +67,25 @@ class TaxRateService extends TransactionBaseService {
|
||||
}
|
||||
|
||||
async retrieve(
|
||||
id: string,
|
||||
taxRateId: string,
|
||||
config: FindConfig<TaxRate> = {}
|
||||
): Promise<TaxRate> {
|
||||
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`
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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<User>} the user document.
|
||||
*/
|
||||
async retrieve(userId: string, config: FindConfig<User> = {}): Promise<User> {
|
||||
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<User>} the user document.
|
||||
*/
|
||||
async retrieveByApiToken(
|
||||
|
||||
Reference in New Issue
Block a user