Promotions and Discounts
Relevant source files
The following files were used as context for generating this wiki page:
- packages/common/src/entities/Promotion.ts
- packages/core/src/graphql/products/promotions/promotion.types.graphql
- packages/core/src/services/products/PromotionService.ts
- packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.ts
- packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.html
- packages/admin-web-angular/src/app/pages/+fakeData/fakeData.component.ts
The Promotions and Discounts system provides a way for merchants to create and manage special offers for their products. This feature is a core component of the e-commerce functionality, allowing for dynamic pricing and marketing campaigns. Merchants can define promotions for specific products within their warehouse, set promotional prices, and specify the duration of the offer.
The system is implemented with a backend service that exposes a GraphQL API for all CRUD (Create, Read, Update, Delete) operations. On the frontend, the Merchant Tablet application provides a user interface for managing these promotions, including a table view to list existing promotions and forms to create or edit them.
Data Model
The core of the promotions feature is the Promotion entity, which defines the structure of a promotion record in the database.
Sources: packages/common/src/entities/Promotion.ts
Entity Relationship Diagram
The diagram below shows how the Promotion entity relates to the Product and Warehouse entities. Each promotion is tied to a single product and a single warehouse.
Promotion Entity Properties
The Promotion entity contains the following fields:
| Property | Type | Description |
|---|---|---|
title | ITranslate[] | The multilingual title of the promotion. |
description | ITranslate[] | The multilingual description of the promotion. |
promoPrice | number | The special price for the product during the promotion. |
warehouse | Warehouse | A reference to the warehouse where the promotion is active. |
product | Product | A reference to the product being promoted. |
warehouseId | string | The ID of the associated warehouse. |
productId | string | The ID of the associated product. |
active | boolean | A flag to indicate if the promotion is currently active. |
activeFrom | Date | The start date and time of the promotion. |
activeTo | Date | The end date and time of the promotion. |
image | string | URL for an image associated with the promotion. |
purchasesCount | number | A counter for how many times the promotion has been used. |
Sources: packages/common/src/entities/Promotion.ts:15-37
Backend API (GraphQL)
The backend exposes a GraphQL API for managing promotions. This API is used by client applications, such as the Merchant Tablet app, to perform CRUD operations.
Sources: packages/core/src/graphql/products/promotions/promotion.types.graphql
GraphQL Schema Types
The main types defined in the GraphQL schema for promotions are Promotion, PromotionInput, and PromotionsFindInput.
| Type | Description |
|---|---|
Promotion | Represents a promotion object returned by a query. |
PromotionInput | Defines the input structure for creating or updating a promotion. |
PromotionsFindInput | Defines the input for querying a list of promotions, e.g., by warehouse. |
Sources: packages/core/src/graphql/products/promotions/promotion.types.graphql:1-38
Queries and Mutations
The API provides one query to fetch promotions and several mutations to create, update, and delete them.
| Operation | Type | Description |
|---|---|---|
promotions | Query | Retrieves a list of promotions, filterable by warehouse. |
createPromotion | Mutation | Creates a new promotion using PromotionInput. |
updatePromotion | Mutation | Updates an existing promotion identified by its ID. |
removePromotion | Mutation | Deletes a single promotion by its ID. |
removePromotionsByIds | Mutation | Deletes multiple promotions by a list of IDs. |
Sources: packages/core/src/graphql/products/promotions/promotion.types.graphql:34-43
Merchant Application Integration
The merchant-tablet-ionic application provides a user interface for merchants to manage their promotions. This is handled through a dedicated page that lists all promotions and allows for creating, editing, and deleting them.
Sources: packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.ts, packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.html
Promotion Management Flow
The following diagram illustrates the flow of managing promotions within the merchant application.
This diagram shows the process from loading promotions to performing CRUD operations via the UI.
Sources: packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.ts:44-188
Promotion Table Component
The PromotionTable component in the merchant app is responsible for rendering the list of promotions and handling user interactions.
Key Responsibilities:
- Loading Data: In
ngOnInit, it calls_loadPromotions()which uses thePromotionServiceto fetch promotions for the current merchant's warehouse. - Displaying Data: It uses
ng2-smart-tableto display the promotions. The table configuration is defined in_loadSettingsSmartTable. - Handling Actions: It listens for
create,edit, anddeleteevents from the smart table and triggers the corresponding modal popups (openAddPromotion,editPromotion,deletePromotion).
Sources: packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.ts:20-188
Smart Table Configuration
The ng2-smart-table is configured with specific columns to display promotion details.
| Column | Title | Type | Value Preparation |
|---|---|---|---|
image | Image | custom | Renders ImageTableComponent. |
title | Title | string | Displays the translated promotion title. |
active | Status | html | Shows '✔' for active and '✘' for inactive. |
activeFrom | Active From | html | Formats the date using formatTableDate. |
activeTo | Active To | html | Formats the date using formatTableDate. |
purchasesCount | Purchases Count | number | Displays the number of times the promotion has been used. |
Sources: packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.ts:86-121
The view template binds user actions to component methods:
<ng2-smart-table
class="smart-table"
[settings]="settingsSmartTable"
[source]="sourceSmartTable"
(create)="openAddPromotion()"
(edit)="editPromotion($event)"
(delete)="deletePromotion($event)"
>
</ng2-smart-table>
Sources: packages/merchant-tablet-ionic/src/pages/+promotions/promotion-table-page/promotion-table.html:23-31
Creating Promotions via Fake Data Generation
The system also includes a mechanism for generating fake data for testing and demonstration purposes, which includes creating products. While not directly creating promotions, the fakeData.component.ts shows how products, which are a prerequisite for promotions, are created programmatically. For instance, the createPeperoniAndMushroomPizzaProduct method creates a product that could then have a promotion applied to it.
Sources: packages/admin-web-angular/src/app/pages/+fakeData/fakeData.component.ts:311-326