Subversion Repositories SmartDukaan

Rev

Rev 36376 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.repository.user;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.cart.v2.CartLifecycleStatus;
import com.spice.profitmandi.dao.cart.v2.SaleType;
import com.spice.profitmandi.dao.entity.user.Cart;

@Repository
public interface CartRepository {
        public void persist(Cart cart);
        public Cart selectById(int id) throws ProfitMandiBusinessException;

        /**
         * The cart row, locked FOR UPDATE, as the first statement of anything that mutates this cart.
         *
         * This exists to fix a lock-ordering deadlock, not to guard a read. Two request paths used to
         * take the same two rows in opposite orders: adding a line INSERTs into user.line, whose
         * line_cart_id_fk shared-locks the parent cart FIRST and the line second, while validation
         * mutated the line rows FIRST and wrote cart.total_price second. Run concurrently on one cart
         * that is a cycle, and InnoDB rolls one side back ("Deadlock found when trying to get lock").
         * Taking the cart row here, before any line is touched, gives every path the same cart -> line
         * order and makes the cycle impossible. It also serialises concurrent edits to the SAME cart,
         * which is what the @Version column on Cart was already trying and failing to express.
         *
         * Different carts are different rows, so this costs nothing across partners.
         */
        public Cart selectByIdForUpdate(int id);

        /**
         * Authorization-aware lookup: returns the cart only if it belongs to the
         * given retailer AND matches the expected sale type. Used by
         * V2BillingController so one partner cannot touch another partner's draft.
         */
        public Cart findByIdAndRetailerAndSaleType(int id, int retailerId, SaleType saleType);

        /**
         * All open tertiary drafts for a partner (most recent first). Procurement
         * carts and closed tertiary carts are excluded.
         */
        public List<Cart> findActiveDraftsByRetailer(int retailerId, SaleType saleType, CartLifecycleStatus status);
}