Skip to main content

Promotions

Struct Promotions 

Source
pub struct Promotions { /* private fields */ }
Expand description

Promotions and discounts management interface.

Implementations§

Source§

impl Promotions

Source

pub fn create(&self, input: CreatePromotion) -> Result<Promotion>

Create a new promotion.

§Example
use stateset_embedded::{Commerce, CreatePromotion, PromotionType};
use rust_decimal_macros::dec;

let commerce = Commerce::new(":memory:")?;

// Create a percentage off promotion
let promo = commerce.promotions().create(CreatePromotion {
    name: "20% Off Everything".into(),
    promotion_type: PromotionType::PercentageOff,
    percentage_off: Some(dec!(0.20)),
    ..Default::default()
})?;
Source

pub fn get(&self, id: PromotionId) -> Result<Option<Promotion>>

Get a promotion by ID.

Source

pub fn get_by_code(&self, code: &str) -> Result<Option<Promotion>>

Get a promotion by its internal code.

Source

pub fn list(&self, filter: PromotionFilter) -> Result<Vec<Promotion>>

List promotions with optional filtering.

§Example
use stateset_embedded::{Commerce, PromotionFilter, PromotionStatus};

let commerce = Commerce::new(":memory:")?;

// List active promotions
let promos = commerce.promotions().list(PromotionFilter {
    is_active: Some(true),
    ..Default::default()
})?;
Source

pub fn update( &self, id: PromotionId, input: UpdatePromotion, ) -> Result<Promotion>

Update a promotion.

Source

pub fn delete(&self, id: PromotionId) -> Result<()>

Delete a promotion.

Source

pub fn activate(&self, id: PromotionId) -> Result<Promotion>

Activate a promotion (make it available for use).

§Example
use stateset_embedded::Commerce;
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;
commerce.promotions().activate(Uuid::new_v4())?;
Source

pub fn deactivate(&self, id: PromotionId) -> Result<Promotion>

Deactivate (pause) a promotion.

Source

pub fn create_coupon(&self, input: CreateCouponCode) -> Result<CouponCode>

Create a coupon code for a promotion.

§Example
use stateset_embedded::{Commerce, CreateCouponCode};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

let coupon = commerce.promotions().create_coupon(CreateCouponCode {
    promotion_id: Uuid::new_v4(),
    code: "SUMMER25".into(),
    usage_limit: Some(100),
    ..Default::default()
})?;
Source

pub fn get_coupon(&self, id: Uuid) -> Result<Option<CouponCode>>

Get a coupon by ID.

Source

pub fn get_coupon_by_code(&self, code: &str) -> Result<Option<CouponCode>>

Get a coupon by its code (the code customers enter).

Source

pub fn list_coupons(&self, filter: CouponFilter) -> Result<Vec<CouponCode>>

List coupons with optional filtering.

Source

pub fn validate_coupon(&self, code: &str) -> Result<Option<CouponCode>>

Validate a coupon code (check if it’s valid and can be used).

§Example
use stateset_embedded::Commerce;

let commerce = Commerce::new(":memory:")?;

match commerce.promotions().validate_coupon("SUMMER25")? {
    Some(coupon) => println!("Valid coupon for promotion: {:?}", coupon.promotion_id),
    None => println!("Invalid or expired coupon"),
}
Source

pub fn apply( &self, request: ApplyPromotionsRequest, ) -> Result<ApplyPromotionsResult>

Apply promotions to a request (cart or order).

This is the main entry point for promotion calculation. It:

  1. Finds all applicable automatic promotions
  2. Validates any coupon codes provided
  3. Checks all promotion conditions
  4. Calculates discounts respecting stacking rules
  5. Returns detailed breakdown of applied discounts
§Example
use stateset_embedded::{Commerce, ApplyPromotionsRequest, PromotionLineItem};
use rust_decimal_macros::dec;

let commerce = Commerce::new(":memory:")?;

let result = commerce.promotions().apply(ApplyPromotionsRequest {
    subtotal: dec!(150.00),
    shipping_amount: dec!(10.00),
    coupon_codes: vec!["SUMMER25".into()],
    line_items: vec![PromotionLineItem {
        id: "item-1".into(),
        quantity: 2,
        unit_price: dec!(75.00),
        line_total: dec!(150.00),
        ..Default::default()
    }],
    ..Default::default()
})?;

println!("Total discount: ${}", result.total_discount);
println!("Final total: ${}", result.grand_total);
Source

pub fn record_usage( &self, promotion_id: PromotionId, coupon_id: Option<Uuid>, customer_id: Option<CustomerId>, order_id: Option<OrderId>, cart_id: Option<CartId>, discount_amount: Decimal, currency: &str, ) -> Result<PromotionUsage>

Record promotion usage (called after order completion).

This increments usage counts and creates an audit trail.

Source

pub fn get_active(&self) -> Result<Vec<Promotion>>

Get all active promotions.

Source

pub fn is_valid(&self, id: PromotionId) -> Result<bool>

Check if a promotion is currently valid.

Source

pub fn add_condition( &self, promotion_id: PromotionId, condition: CreatePromotionCondition, ) -> Result<Promotion>

Add a condition to an existing promotion.

Trait Implementations§

Source§

impl Debug for Promotions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more