feat: Implement Phase 6 Shopping and Checkout
CI / test (push) Has been cancelled
CI / test (pull_request) Has been cancelled

- Introduced authenticated shopping state and atomic checkout process without creating orders or payments.
- Reused existing components from previous phases including identities, addresses, catalogues, pricing, and inventory.
- Developed cart architecture to support one active cart per user with unique items per variant.
- Implemented dynamic cart pricing with various precedence rules for promotions and coupons.
- Created a wishlist feature that exposes only visible active products without reserving inventory.
- Integrated inventory validation to ensure availability of items before adding to cart.
- Developed a checkout architecture that locks the cart, revalidates pricing and shipping, and reserves inventory.
- Added shipping zones, methods, and rates with configurable options for international shipping and duty/tax boundaries.
- Implemented checkout snapshots to capture immutable checkout details.
- Introduced idempotency for checkout requests to prevent duplicate processing.
- Added functionality for coupon integration and business checkout validation.
- Established permissions for managing shipping zones, methods, and rates.
- Created comprehensive unit tests covering various aspects of the shopping and checkout processes.
- Added cron job for handling checkout expiry reconciliation.
- Created migration script to set up new database tables and constraints for carts, checkout sessions, and shipping.
This commit is contained in:
Sathira Sri Sathara
2026-09-03 23:45:01 +05:30
parent 5643d89236
commit ad9287b804
33 changed files with 187 additions and 10 deletions
+2
View File
@@ -93,6 +93,8 @@ db.ProductReview = require("./catalogue/productReview.model")(sequelize, DataTyp
db.Warehouse=require("./inventory/warehouse.model")(sequelize,DataTypes);db.InventoryBalance=require("./inventory/inventoryBalance.model")(sequelize,DataTypes);db.InventoryTransaction=require("./inventory/inventoryTransaction.model")(sequelize,DataTypes);db.InventoryReservation=require("./inventory/inventoryReservation.model")(sequelize,DataTypes);db.InventoryTransfer=require("./inventory/inventoryTransfer.model")(sequelize,DataTypes);
db.BusinessTier=require("./pricing/businessTier.model")(sequelize,DataTypes);db.VariantBusinessPrice=require("./pricing/variantBusinessPrice.model")(sequelize,DataTypes);db.BusinessPriceTier=require("./pricing/businessPriceTier.model")(sequelize,DataTypes);
db.Promotion=require("./merchandising/promotion.model")(sequelize,DataTypes);db.PromotionTarget=require("./merchandising/promotionTarget.model")(sequelize,DataTypes);db.Coupon=require("./merchandising/coupon.model")(sequelize,DataTypes);db.Banner=require("./merchandising/banner.model")(sequelize,DataTypes);db.BannerTranslation=require("./merchandising/bannerTranslation.model")(sequelize,DataTypes);
db.Cart=require("./shopping/cart.model")(sequelize,DataTypes);db.CartItem=require("./shopping/cartItem.model")(sequelize,DataTypes);db.WishlistItem=require("./shopping/wishlistItem.model")(sequelize,DataTypes);db.CheckoutSession=require("./shopping/checkoutSession.model")(sequelize,DataTypes);db.CheckoutItem=require("./shopping/checkoutItem.model")(sequelize,DataTypes);
db.ShippingZone=require("./shipping/shippingZone.model")(sequelize,DataTypes);db.ShippingZoneRegion=require("./shipping/shippingZoneRegion.model")(sequelize,DataTypes);db.ShippingMethod=require("./shipping/shippingMethod.model")(sequelize,DataTypes);db.ShippingRate=require("./shipping/shippingRate.model")(sequelize,DataTypes);
/* Associations */
Object.keys(db).forEach(model => {
@@ -0,0 +1 @@
module.exports=(s,D)=>s.define("ShippingMethod",{id:{type:D.STRING,primaryKey:true},code:{type:D.STRING(60),allowNull:false,unique:true},name:{type:D.STRING(150),allowNull:false},description:D.TEXT,status:{type:D.ENUM("ACTIVE","INACTIVE"),allowNull:false,defaultValue:"ACTIVE"},estimated_min_days:{type:D.INTEGER,allowNull:false},estimated_max_days:{type:D.INTEGER,allowNull:false},tracking_supported:{type:D.BOOLEAN,allowNull:false,defaultValue:false}},{tableName:"shipping_methods",timestamps:true});
@@ -0,0 +1 @@
module.exports=(s,D)=>{const M=s.define("ShippingRate",{id:{type:D.STRING,primaryKey:true},shipping_zone_id:{type:D.STRING,allowNull:false},shipping_method_id:{type:D.STRING,allowNull:false},currency:{type:D.STRING(3),allowNull:false},base_amount:{type:D.DECIMAL(15,2),allowNull:false},free_shipping_threshold:D.DECIMAL(15,2),minimum_subtotal:D.DECIMAL(15,2),maximum_subtotal:D.DECIMAL(15,2),status:{type:D.ENUM("ACTIVE","INACTIVE"),allowNull:false,defaultValue:"ACTIVE"},starts_at:D.DATE,ends_at:D.DATE},{tableName:"shipping_rates",timestamps:true});M.associate=db=>M.belongsTo(db.ShippingMethod,{foreignKey:"shipping_method_id",as:"method"});return M;};
@@ -0,0 +1 @@
module.exports=(s,D)=>s.define("ShippingZone",{id:{type:D.STRING,primaryKey:true},code:{type:D.STRING(60),allowNull:false,unique:true},name:{type:D.STRING(150),allowNull:false},status:{type:D.ENUM("ACTIVE","INACTIVE"),allowNull:false,defaultValue:"ACTIVE"},duty_mode:{type:D.ENUM("NONE","ESTIMATED","PAYABLE_ON_DELIVERY","INCLUDED","UNKNOWN"),allowNull:false,defaultValue:"UNKNOWN"}},{tableName:"shipping_zones",timestamps:true});
@@ -0,0 +1 @@
module.exports=(s,D)=>s.define("ShippingZoneRegion",{id:{type:D.STRING,primaryKey:true},shipping_zone_id:{type:D.STRING,allowNull:false},country_code:{type:D.STRING(2),allowNull:false},province:D.STRING(100),district:D.STRING(100)},{tableName:"shipping_zone_regions",timestamps:true});
+1
View File
@@ -0,0 +1 @@
module.exports=(s,D)=>{const M=s.define("Cart",{id:{type:D.STRING,primaryKey:true},user_id:{type:D.STRING,allowNull:false},status:{type:D.ENUM("ACTIVE","CHECKOUT_LOCKED","CONVERTED","ABANDONED"),allowNull:false,defaultValue:"ACTIVE"},currency:{type:D.STRING(3),allowNull:false,defaultValue:"LKR"},coupon_code:D.STRING(50),version:{type:D.INTEGER,allowNull:false,defaultValue:0},expires_at:D.DATE},{tableName:"carts",timestamps:true,indexes:[{unique:true,fields:["user_id","status"]}]});M.associate=db=>{M.hasMany(db.CartItem,{foreignKey:"cart_id",as:"items"});M.belongsTo(db.User,{foreignKey:"user_id",as:"owner"});};return M;};
+1
View File
@@ -0,0 +1 @@
module.exports=(s,D)=>{const M=s.define("CartItem",{id:{type:D.STRING,primaryKey:true},cart_id:{type:D.STRING,allowNull:false},variant_id:{type:D.STRING,allowNull:false},quantity:{type:D.INTEGER,allowNull:false}},{tableName:"cart_items",timestamps:true,indexes:[{unique:true,fields:["cart_id","variant_id"]}]});M.associate=db=>M.belongsTo(db.ProductVariant,{foreignKey:"variant_id",as:"variant"});return M;};
@@ -0,0 +1 @@
module.exports=(s,D)=>s.define("CheckoutItem",{id:{type:D.STRING,primaryKey:true},checkout_session_id:{type:D.STRING,allowNull:false},product_id:{type:D.STRING,allowNull:false},variant_id:{type:D.STRING,allowNull:false},reservation_key:{type:D.STRING(160),allowNull:false},sku:{type:D.STRING(100),allowNull:false},product_name:{type:D.STRING(255),allowNull:false},variant_description:D.STRING(255),quantity:{type:D.INTEGER,allowNull:false},unit_price:{type:D.DECIMAL(15,2),allowNull:false},discount_amount:{type:D.DECIMAL(15,2),allowNull:false},line_total:{type:D.DECIMAL(15,2),allowNull:false},currency:{type:D.STRING(3),allowNull:false},metadata:D.JSON},{tableName:"checkout_items",timestamps:true});
@@ -0,0 +1 @@
module.exports=(s,D)=>{const M=s.define("CheckoutSession",{id:{type:D.STRING,primaryKey:true},checkout_number:{type:D.STRING(80),allowNull:false,unique:true},user_id:{type:D.STRING,allowNull:false},business_customer_id:D.STRING,cart_id:{type:D.STRING,allowNull:false},cart_version:{type:D.INTEGER,allowNull:false},status:{type:D.ENUM("PENDING","READY","EXPIRED","CANCELLED","CONVERTED"),allowNull:false,defaultValue:"PENDING"},currency:{type:D.STRING(3),allowNull:false},shipping_address_snapshot:{type:D.JSON,allowNull:false},billing_address_snapshot:{type:D.JSON,allowNull:false},shipping_method_id:{type:D.STRING,allowNull:false},shipping_snapshot:{type:D.JSON,allowNull:false},subtotal:{type:D.DECIMAL(15,2),allowNull:false},discount_total:{type:D.DECIMAL(15,2),allowNull:false},shipping_amount:{type:D.DECIMAL(15,2),allowNull:false},tax_amount:{type:D.DECIMAL(15,2),allowNull:false},duty_amount:D.DECIMAL(15,2),grand_total:{type:D.DECIMAL(15,2),allowNull:false},coupon_code:D.STRING(50),idempotency_key:{type:D.STRING(160),allowNull:false},request_fingerprint:{type:D.STRING(64),allowNull:false},expires_at:{type:D.DATE,allowNull:false}},{tableName:"checkout_sessions",timestamps:true,indexes:[{unique:true,fields:["user_id","idempotency_key"]}]});M.associate=db=>M.hasMany(db.CheckoutItem,{foreignKey:"checkout_session_id",as:"items"});return M;};
@@ -0,0 +1 @@
module.exports=(s,D)=>{const M=s.define("WishlistItem",{id:{type:D.STRING,primaryKey:true},user_id:{type:D.STRING,allowNull:false},product_id:{type:D.STRING,allowNull:false}},{tableName:"wishlist_items",timestamps:true,indexes:[{unique:true,fields:["user_id","product_id"]}]});M.associate=db=>M.belongsTo(db.Product,{foreignKey:"product_id",as:"product"});return M;};