Files
medusa-store/www/apps/docs/content/references/services/classes/services.DiscountService.mdx
github-actions[bot] 5a550e73b4 chore(docs): Generated References (#6222)
Generated the following references:
- `entities`
- `file`
- `fulfillment`
- `inventory`
- `js_client`
- `medusa`
- `medusa_config`
- `medusa_react`
- `modules`
- `notification`
- `payment`
- `price_selection`
- `pricing`
- `product`
- `search`
- `services`
- `stock_location`
- `tax`
- `tax_calculation`
- `types`
- `workflows`

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: Shahed Nasser <27354907+shahednasser@users.noreply.github.com>
2024-01-25 18:12:27 +00:00

333 lines
23 KiB
Plaintext

---
displayed_sidebar: servicesSidebar
---
import ParameterTypes from "@site/src/components/ParameterTypes"
# DiscountService
Provides layer to manipulate discounts.
## Implements
## constructor
### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"EntityManager","type":"`EntityManager`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="activeManager_"/>
___
## Methods
### withTransaction
#### Parameters
<ParameterTypes parameters={[{"name":"transactionManager","type":"`EntityManager`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="withTransaction"/>
#### Returns
<ParameterTypes parameters={[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="withTransaction"/>
### shouldRetryTransaction\_
#### Parameters
<ParameterTypes parameters={[{"name":"err","type":"`Record<string, unknown>` \\| `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="shouldRetryTransaction_"/>
#### Returns
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"T","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
#### Parameters
<ParameterTypes parameters={[{"name":"discountRule","type":"T","description":"the discount rule to create","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
#### Returns
<ParameterTypes parameters={[{"name":"T","type":"T","optional":false,"defaultValue":"","description":"the result of the create operation","expandable":false,"children":[]}]} sectionTitle="validateDiscountRule_"/>
### list
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"discount","type":"`CreateDiscountInput`","description":"the discount data to create","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="create"/>
#### Returns
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"discountId","type":"`string`","description":"id of discount to delete","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="delete"/>
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"the result of the delete operation","expandable":false,"children":[]}]} sectionTitle="delete"/>
### validateDiscountForProduct
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForProduct"/>
### calculateDiscountForLineItem
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;number&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="calculateDiscountForLineItem"/>
### validateDiscountForCartOrThrow
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="validateDiscountForCartOrThrow"/>
### hasCustomersGroupCondition
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasCustomersGroupCondition"/>
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasCustomersGroupCondition"/>
### hasReachedLimit
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasReachedLimit"/>
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasReachedLimit"/>
### hasNotStarted
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasNotStarted"/>
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasNotStarted"/>
### hasExpired
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="hasExpired"/>
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="hasExpired"/>
### isDisabled
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} sectionTitle="isDisabled"/>
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="isDisabled"/>
### isValidForRegion
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="isValidForRegion"/>
### canApplyForCustomer
#### Parameters
<ParameterTypes parameters={[{"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
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} sectionTitle="canApplyForCustomer"/>