Skip to main content
Skip to main content

DiscountService

Provides layer to manipulate discounts.

Implements

Constructors

constructor

**new DiscountService**(«destructured»)

Parameters
__namedParametersObjectRequired

Properties

__configModule__Record<string, unknown>
__container__anyRequired
__moduleDeclaration__Record<string, unknown>
customerService_CustomerServiceRequired
discountConditionRepository_Repository<DiscountCondition> & { addConditionResources: Method addConditionResources ; canApplyForCustomer: Method canApplyForCustomer ; findOneWithDiscount: Method findOneWithDiscount ; getJoinTableResourceIdentifiers: Method getJoinTableResourceIdentifiers ; isValidForProduct: Method isValidForProduct ; queryConditionTable: Method queryConditionTable ; removeConditionResources: Method removeConditionResources }Required
discountConditionService_DiscountConditionServiceRequired
discountRepository_Repository<Discount>Required
discountRuleRepository_Repository<DiscountRule>Required
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
giftCardRepository_Repository<GiftCard> & { listGiftCardsAndCount: Method listGiftCardsAndCount }Required
manager_EntityManagerRequired
newTotalsService_NewTotalsServiceRequired
productService_ProductServiceRequired
regionService_RegionServiceRequired
totalsService_TotalsServiceRequired
transactionManager_undefined | EntityManagerRequired

Accessors

activeManager_

Protected activeManager_: [object Object]

Methods

addRegion

**addRegion**(discountId, regionId): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Adds a region to the discount regions array.

Parameters
discountIdstringRequired
id of discount
regionIdstringRequired
id of region to add
Returns

Promise<Discount>

PromisePromise<Discount>Required
the result of the update operation

atomicPhase_

Protected **atomicPhase_**<TypeParameter TResult, TypeParameter TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise&#60;TResult&#62;

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
TResultobjectRequired
TErrorobjectRequired
Parameters
work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler
Returns

Promise<TResult>

PromisePromise<TResult>Required
the result of the transactional work

calculateDiscountForLineItem

**calculateDiscountForLineItem**(discountId, lineItem, calculationContextData): Promise&#60;number&#62;

Parameters
discountIdstringRequired
lineItemLineItemRequired
Line Items are created when a product is added to a Cart. When Line Items are purchased they will get copied to the resulting order, swap, or claim, and can eventually be referenced in Fulfillments and Returns. Line items may also be used for order edits.
calculationContextDataCalculationContextDataRequired
Returns

Promise<number>

PromisePromise<number>Required

canApplyForCustomer

**canApplyForCustomer**(discountRuleId, customerId): Promise&#60;boolean&#62;

Parameters
discountRuleIdstringRequired
customerIdundefined | stringRequired
Returns

Promise<boolean>

PromisePromise<boolean>Required

create

**create**(discount): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Creates a discount with provided data given that the data is validated. Normalizes discount code to uppercase.

Parameters
discountCreateDiscountInputRequired
the discount data to create
Returns

Promise<Discount>

PromisePromise<Discount>Required
the result of the create operation

createDynamicCode

**createDynamicCode**(discountId, data): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Creates a dynamic code for a discount id.

Parameters
discountIdstringRequired
the id of the discount to create a code for
the object containing a code to identify the discount by
Returns

Promise<Discount>

PromisePromise<Discount>Required
the newly created dynamic code

delete

**delete**(discountId): Promise&#60;void&#62;

Deletes a discount idempotently

Parameters
discountIdstringRequired
id of discount to delete
Returns

Promise<void>

PromisePromise<void>Required
the result of the delete operation

deleteDynamicCode

**deleteDynamicCode**(discountId, code): Promise&#60;void&#62;

Deletes a dynamic code for a discount id.

Parameters
discountIdstringRequired
the id of the discount to create a code for
codestringRequired
the code to identify the discount by
Returns

Promise<void>

PromisePromise<void>Required
the newly created dynamic code

hasCustomersGroupCondition

**hasCustomersGroupCondition**(discount): boolean

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
Returns

boolean

booleanboolean

hasExpired

**hasExpired**(discount): boolean

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
Returns

boolean

booleanboolean

hasNotStarted

**hasNotStarted**(discount): boolean

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
Returns

boolean

booleanboolean

hasReachedLimit

**hasReachedLimit**(discount): boolean

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
Returns

boolean

booleanboolean

isDisabled

**isDisabled**(discount): boolean

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
Returns

boolean

booleanboolean

isValidForRegion

**isValidForRegion**(discount, region_id): Promise&#60;boolean&#62;

Parameters
discountDiscountRequired
A discount can be applied to a cart for promotional purposes.
region_idstringRequired
Returns

Promise<boolean>

PromisePromise<boolean>Required

list

**list**(selector?, config?): Promise&#60;[Discount](/references/services/classes/Discount)[]&#62;

Parameters
selectorFilterableDiscountPropsRequired
the query object for find

Default: {}

configFindConfig<Discount>Required
the config object containing query settings
Returns

Promise<Discount[]>

PromisePromise<Discount[]>Required
the result of the find operation

listAndCount

**listAndCount**(selector?, config?): Promise&#60;[[Discount](/references/services/classes/Discount)[], number]&#62;

Parameters
selectorFilterableDiscountPropsRequired
the query object for find

Default: {}

configFindConfig<Discount>Required
the config object containing query settings
Returns

Promise<[Discount[], number]>

PromisePromise<[Discount[], number]>Required
the result of the find operation

listByCodes

**listByCodes**(discountCodes, config?): Promise&#60;[Discount](/references/services/classes/Discount)[]&#62;

List all the discounts corresponding to the given codes

Parameters
discountCodesstring[]Required
discount codes of discounts to retrieve
configFindConfig<Discount>Required
the config object containing query settings

Default: {}

Returns

Promise<Discount[]>

PromisePromise<Discount[]>Required
the discounts

removeRegion

**removeRegion**(discountId, regionId): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Removes a region from the discount regions array.

Parameters
discountIdstringRequired
id of discount
regionIdstringRequired
id of region to remove
Returns

Promise<Discount>

PromisePromise<Discount>Required
the result of the update operation

retrieve

**retrieve**(discountId, config?): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Gets a discount by id.

Parameters
discountIdstringRequired
id of discount to retrieve
configFindConfig<Discount>Required
the config object containing query settings

Default: {}

Returns

Promise<Discount>

PromisePromise<Discount>Required
the discount

retrieveByCode

**retrieveByCode**(discountCode, config?): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Gets the discount by discount code.

Parameters
discountCodestringRequired
discount code of discount to retrieve
configFindConfig<Discount>Required
the config object containing query settings

Default: {}

Returns

Promise<Discount>

PromisePromise<Discount>Required
the discount

shouldRetryTransaction_

Protected **shouldRetryTransaction_**(err): boolean

Parameters
errRecord<string, unknown> | { code: string }Required
Returns

boolean

booleanboolean

update

**update**(discountId, update): Promise&#60;[Discount](/references/services/classes/Discount)&#62;

Updates a discount.

Parameters
discountIdstringRequired
discount id of discount to update
updateUpdateDiscountInputRequired
the data to update the discount with
Returns

Promise<Discount>

PromisePromise<Discount>Required
the result of the update operation

validateDiscountForCartOrThrow

**validateDiscountForCartOrThrow**(cart, discount): Promise&#60;void&#62;

Parameters
cartCartRequired
A cart represents a virtual shopping bag. It can be used to complete an order, a swap, or a claim.
discountDiscount | Discount[]Required
Returns

Promise<void>

PromisePromise<void>Required

validateDiscountForProduct

**validateDiscountForProduct**(discountRuleId, productId?): Promise&#60;boolean&#62;

Parameters
discountRuleIdstringRequired
productIdstring
Returns

Promise<boolean>

PromisePromise<boolean>Required

validateDiscountRule_

**validateDiscountRule_**<TypeParameter T>(discountRule): T

Creates a discount rule with provided data given that the data is validated.

Type Parameters
TobjectRequired
Parameters
discountRuleTRequired
the discount rule to create
Returns

T

TTRequired
the result of the create operation

withTransaction

**withTransaction**(transactionManager?): [DiscountService](/references/services/classes/DiscountService)

Parameters
transactionManagerEntityManager
Returns

DiscountService

DiscountServiceDiscountServiceRequired
Was this section helpful?