Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(medusa): Validate required id before retrieving #2738

Merged
merged 18 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
5de5c03
chore: validate defined ids before retrieve
carlos-r-l-rodrigues Dec 7, 2022
70bd6cc
trigger pipeline
carlos-r-l-rodrigues Dec 7, 2022
72875b3
fix: revert event bus
carlos-r-l-rodrigues Dec 7, 2022
01161d7
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 7, 2022
d4a4698
chore: argument names
carlos-r-l-rodrigues Dec 8, 2022
7d1af20
Merge branch 'chore/validate_required_id_retrieve' of github.com:medu…
carlos-r-l-rodrigues Dec 8, 2022
dafe32d
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 8, 2022
a60f263
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 8, 2022
5acb9f7
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 8, 2022
712fafb
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 9, 2022
e216bcc
Create wet-forks-scream.md
olivermrbl Dec 12, 2022
7cda8b3
Merge branch 'develop' into chore/validate_required_id_retrieve
olivermrbl Dec 12, 2022
e31efa3
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 13, 2022
e5b1cab
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 14, 2022
9b6cf3e
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 14, 2022
54f0dc7
Merge branch 'develop' into chore/validate_required_id_retrieve
olivermrbl Dec 14, 2022
8f593eb
Merge branch 'develop' into chore/validate_required_id_retrieve
carlos-r-l-rodrigues Dec 15, 2022
7c494a4
Merge branch 'develop' into chore/validate_required_id_retrieve
olivermrbl Dec 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-forks-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

chore(medusa): Validate required id in `[someService].retrieve`
9 changes: 8 additions & 1 deletion packages/medusa/src/services/batch-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_)

Expand Down
7 changes: 7 additions & 0 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 12 additions & 5 deletions packages/medusa/src/services/claim-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.`
)
}

Expand Down
15 changes: 11 additions & 4 deletions packages/medusa/src/services/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
}

Expand Down
13 changes: 10 additions & 3 deletions packages/medusa/src/services/customer-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
}

Expand Down
14 changes: 14 additions & 0 deletions packages/medusa/src/services/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 9 additions & 2 deletions packages/medusa/src/services/discount-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -108,7 +115,7 @@ class DiscountConditionService extends TransactionBaseService {

async upsertCondition(
data: DiscountConditionInput,
overrideExisting: boolean = true
overrideExisting = true
): Promise<
(
| DiscountConditionProduct
Expand Down
9 changes: 8 additions & 1 deletion packages/medusa/src/services/discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_)

Expand Down
17 changes: 12 additions & 5 deletions packages/medusa/src/services/draft-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`
)
}

Expand Down
15 changes: 11 additions & 4 deletions packages/medusa/src/services/fulfillment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/medusa/src/services/gift-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,27 @@ 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)
}

async retrieveByCode(
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)
}

Expand Down
8 changes: 8 additions & 0 deletions packages/medusa/src/services/idempotency-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CreateIdempotencyKeyInput,
IdempotencyCallbackResult,
} from "../types/idempotency-key"
import { isDefined } from "../utils"

const KEY_LOCKED_TIMEOUT = 1000

Expand Down Expand Up @@ -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_
)
Expand Down
Loading