Files
medusa-store/www/apps/docs/content/references/services/classes/services.DiscountService.mdx
2024-01-22 18:38:35 +01:00

333 lines
22 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.customerService","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.discountRepository","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"__namedParameters.discountRuleRepository","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":[]},{"name":"__namedParameters.giftCardRepository","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.totalsService","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
___
## Properties
<ParameterTypes parameters={[{"name":"__container__","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customerService_","type":"[CustomerService](services.CustomerService.mdx)","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":"discountRepository_","type":"Repository&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","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":"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":"giftCardRepository_","type":"Repository&#60;[GiftCard](../../entities/classes/entities.GiftCard.mdx)&#62; & `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"manager_","type":"`EntityManager`","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":"totalsService_","type":"[TotalsService](services.TotalsService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionManager_","type":"`undefined` \\| `EntityManager`","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":"__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":[]}]} />
___
## Accessors
### activeManager\_
#### Returns
<ParameterTypes parameters={[{"name":"EntityManager","type":"`EntityManager`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
___
## Methods
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;TResult&#62;","optional":false,"defaultValue":"","description":"the result of the transactional work","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;number&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### delete
Deletes a discount idempotently
#### Parameters
<ParameterTypes parameters={[{"name":"discountId","type":"`string`","description":"id of discount to delete","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"the result of the delete operation","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"the newly created dynamic code","expandable":false,"children":[]}]} />
### hasCustomersGroupCondition
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### hasExpired
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### hasNotStarted
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### hasReachedLimit
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### isDisabled
#### Parameters
<ParameterTypes parameters={[{"name":"discount","type":"[Discount](../../entities/classes/entities.Discount.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)[]&#62;","optional":false,"defaultValue":"","description":"the discounts","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the discount","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Discount](../../entities/classes/entities.Discount.mdx)&#62;","optional":false,"defaultValue":"","description":"the discount","expandable":false,"children":[]}]} />
### shouldRetryTransaction\_
#### Parameters
<ParameterTypes parameters={[{"name":"err","type":"`Record<string, unknown>` \\| `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### 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":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />
### 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":[]}]} />
#### Parameters
<ParameterTypes parameters={[{"name":"discountRule","type":"T","description":"the discount rule to create","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"T","type":"T","optional":false,"defaultValue":"","description":"the result of the create operation","expandable":false,"children":[]}]} />
### withTransaction
#### Parameters
<ParameterTypes parameters={[{"name":"transactionManager","type":"`EntityManager`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />
#### Returns
<ParameterTypes parameters={[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />