Files
medusa-store/www/apps/docs/content/references/services/classes/services.DiscountService.mdx
Shahed Nasser bb87db8342 docs: prep for v2 documentation (#6710)
This PR includes documentation that preps for v2 docs (but doesn't introduce new docs).

_Note: The number of file changes in the PR is due to find-and-replace within the `references` which is unavoidable. Let me know if I should move it to another PR._

## Changes

- Change Medusa version in base OAS used for v2.
- Fix to docblock generator related to not catching all path parameters.
- Added typedoc plugin that generates ER Diagrams, which will be used specifically for data model references in commerce modules.
- Changed OAS tool to output references in `www/apps/api-reference/specs-v2` directory when the `--v2` option is used.
- Added a version switcher to the API reference to switch between V1 and V2. This switcher is enabled by an environment variable, so it won't be visible/usable at the moment.
- Upgraded docusaurus to v3.0.1
- Added new Vale rules to ensure correct spelling of Medusa Admin and module names.
- Added new components to the `docs-ui` package that will be used in future documentation changes.
2024-03-18 07:47:35 +00:00

333 lines
23 KiB
Plaintext

---
displayed_sidebar: servicesSidebar
---
import TypeList from "@site/src/components/TypeList"
# DiscountService
Provides layer to manipulate discounts.
## Implements
## constructor
### Parameters
<TypeList types={[{"name":"__namedParameters","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.discountRepository","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.discountRuleRepository","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.giftCardRepository","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.discountConditionRepository","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.discountConditionService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.totalsService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.newTotalsService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.productService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.regionService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.customerService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.eventBusService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.featureFlagRouter","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="new DiscountService"/>
___
## Properties
<TypeList types={[{"name":"manager_","type":"`EntityManager`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionManager_","type":"`undefined` \\| `EntityManager`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__container__","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"Events","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"Events.CREATED","type":"`string`","description":"","optional":false,"defaultValue":"\"discount.created\"","expandable":false,"children":[]},{"name":"discountRepository_","type":"Repository&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customerService_","type":"[CustomerService](services.CustomerService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discountRuleRepository_","type":"Repository&#60;[DiscountRule](../../entities/classes/entities.DiscountRule.mdx)&#62;","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"giftCardRepository_","type":"Repository&#60;[GiftCard](../../entities/classes/entities.GiftCard.mdx)&#62; & `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discountConditionRepository_","type":"Repository&#60;[DiscountCondition](../../entities/classes/entities.DiscountCondition.mdx)&#62; & `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discountConditionService_","type":"[DiscountConditionService](services.DiscountConditionService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"totalsService_","type":"[TotalsService](services.TotalsService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"newTotalsService_","type":"[NewTotalsService](services.NewTotalsService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"productService_","type":"[ProductService](services.ProductService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"regionService_","type":"[RegionService](services.RegionService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"eventBus_","type":"[EventBusService](services.EventBusService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"featureFlagRouter_","type":"`FlagRouter`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__configModule__","type":"`Record<string, unknown>`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"__moduleDeclaration__","type":"`Record<string, unknown>`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="DiscountService"/>
___
## Accessors
### activeManager\_
#### Returns
<TypeList types={[{"name":"EntityManager","type":"`EntityManager`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="activeManager_"/>
___
## Methods
### withTransaction
#### Parameters
<TypeList types={[{"name":"transactionManager","type":"`EntityManager`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="withTransaction"/>
#### Returns
<TypeList types={[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="withTransaction"/>
### shouldRetryTransaction\_
#### Parameters
<TypeList types={[{"name":"err","type":"`Record<string, unknown>` \\| `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="shouldRetryTransaction_"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="shouldRetryTransaction_"/>
### atomicPhase\_
Wraps some work within a transactional block. If the service already has
a transaction manager attached this will be reused, otherwise a new
transaction manager is created.
#### Type Parameters
<TypeList types={[{"name":"TResult","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"TError","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="atomicPhase_"/>
#### Parameters
<TypeList types={[{"name":"work","type":"(`transactionManager`: `EntityManager`) => Promise&#60;TResult&#62;","description":"the transactional work to be done","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationOrErrorHandler","type":"`IsolationLevel` \\| (`error`: TError) => Promise&#60;void \\| TResult&#62;","description":"the isolation level to be used for the work.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"maybeErrorHandlerOrDontFail","type":"(`error`: TError) => Promise&#60;void \\| TResult&#62;","description":"Potential error handler","optional":true,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="atomicPhase_"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;TResult&#62;","optional":false,"defaultValue":"","description":"the result of the transactional work","expandable":false,"children":[]}]} sectionTitle="atomicPhase_"/>
### validateDiscountRule\_
Creates a discount rule with provided data given that the data is validated.
#### Type Parameters
<TypeList types={[{"name":"T","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
#### Parameters
<TypeList types={[{"name":"discountRule","type":"T","description":"the discount rule to create","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
#### Returns
<TypeList types={[{"name":"T","type":"T","optional":false,"defaultValue":"","description":"the result of the create operation","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
### list
#### Parameters
<TypeList types={[{"name":"selector","type":"`FilterableDiscountProps`","description":"the query object for find","optional":false,"defaultValue":"{}","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"the config object containing query settings","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="list"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)[]&#62;","optional":false,"defaultValue":"","description":"the result of the find operation","expandable":false,"children":[]}]} sectionTitle="list"/>
### listAndCount
#### Parameters
<TypeList types={[{"name":"selector","type":"`FilterableDiscountProps`","description":"the query object for find","optional":false,"defaultValue":"{}","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"the config object containing query settings","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="listAndCount"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[[Discount](../../entities/classes/entities.Discount.mdx)[], number]&#62;","optional":false,"defaultValue":"","description":"the result of the find operation","expandable":false,"children":[]}]} sectionTitle="listAndCount"/>
### create
Creates a discount with provided data given that the data is validated.
Normalizes discount code to uppercase.
#### Parameters
<TypeList types={[{"name":"discount","type":"`CreateDiscountInput`","description":"the discount data to create","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="create"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the result of the create operation","expandable":false,"children":[]}]} sectionTitle="create"/>
### retrieve
Gets a discount by id.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"id of discount to retrieve","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"the config object containing query settings","optional":false,"defaultValue":"{}","expandable":false,"children":[]}]} sectionTitle="retrieve"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the discount","expandable":false,"children":[]}]} sectionTitle="retrieve"/>
### retrieveByCode
Gets the discount by discount code.
#### Parameters
<TypeList types={[{"name":"discountCode","type":"`string`","description":"discount code of discount to retrieve","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"the config object containing query settings","optional":false,"defaultValue":"{}","expandable":false,"children":[]}]} sectionTitle="retrieveByCode"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the discount","expandable":false,"children":[]}]} sectionTitle="retrieveByCode"/>
### listByCodes
List all the discounts corresponding to the given codes
#### Parameters
<TypeList types={[{"name":"discountCodes","type":"`string`[]","description":"discount codes of discounts to retrieve","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","description":"the config object containing query settings","optional":false,"defaultValue":"{}","expandable":false,"children":[]}]} sectionTitle="listByCodes"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)[]&#62;","optional":false,"defaultValue":"","description":"the discounts","expandable":false,"children":[]}]} sectionTitle="listByCodes"/>
### update
Updates a discount.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"discount id of discount to update","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"update","type":"`UpdateDiscountInput`","description":"the data to update the discount with","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="update"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the result of the update operation","expandable":false,"children":[]}]} sectionTitle="update"/>
### createDynamicCode
Creates a dynamic code for a discount id.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"the id of the discount to create a code for","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`CreateDynamicDiscountInput`","description":"the object containing a code to identify the discount by","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="createDynamicCode"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the newly created dynamic code","expandable":false,"children":[]}]} sectionTitle="createDynamicCode"/>
### deleteDynamicCode
Deletes a dynamic code for a discount id.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"the id of the discount to create a code for","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"code","type":"`string`","description":"the code to identify the discount by","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="deleteDynamicCode"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"the newly created dynamic code","expandable":false,"children":[]}]} sectionTitle="deleteDynamicCode"/>
### addRegion
Adds a region to the discount regions array.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"id of discount","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"regionId","type":"`string`","description":"id of region to add","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="addRegion"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the result of the update operation","expandable":false,"children":[]}]} sectionTitle="addRegion"/>
### removeRegion
Removes a region from the discount regions array.
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"id of discount","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"regionId","type":"`string`","description":"id of region to remove","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="removeRegion"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the result of the update operation","expandable":false,"children":[]}]} sectionTitle="removeRegion"/>
### delete
Deletes a discount idempotently
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"id of discount to delete","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="delete"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"the result of the delete operation","expandable":false,"children":[]}]} sectionTitle="delete"/>
### validateDiscountForProduct
#### Parameters
<TypeList types={[{"name":"discountRuleId","type":"`string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"productId","type":"`string`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForProduct"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForProduct"/>
### calculateDiscountForLineItem
#### Parameters
<TypeList types={[{"name":"discountId","type":"`string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"lineItem","type":"[LineItem](../../entities/classes/entities.LineItem.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"calculationContextData","type":"`CalculationContextData`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="calculateDiscountForLineItem"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;number&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="calculateDiscountForLineItem"/>
### validateDiscountForCartOrThrow
#### Parameters
<TypeList types={[{"name":"cart","type":"[Cart](../../entities/classes/entities.Cart.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx) \\| [Discount](../../entities/classes/entities.Discount.mdx)[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForCartOrThrow"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForCartOrThrow"/>
### hasCustomersGroupCondition
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasCustomersGroupCondition"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasCustomersGroupCondition"/>
### hasReachedLimit
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasReachedLimit"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasReachedLimit"/>
### hasNotStarted
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasNotStarted"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasNotStarted"/>
### hasExpired
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasExpired"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasExpired"/>
### isDisabled
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="isDisabled"/>
#### Returns
<TypeList types={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="isDisabled"/>
### isValidForRegion
#### Parameters
<TypeList types={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"region_id","type":"`string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="isValidForRegion"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="isValidForRegion"/>
### canApplyForCustomer
#### Parameters
<TypeList types={[{"name":"discountRuleId","type":"`string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customerId","type":"`undefined` \\| `string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="canApplyForCustomer"/>
#### Returns
<TypeList types={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="canApplyForCustomer"/>