S++Core systems, human-reviewable
DEMO SOURCE · SPP / APP / PHYRead the book

petshop-05-checks.spp

petshop/order/petshop-05-checks.spp

Download original file · Petstore source contents

1domain petshop;23atomic check checkQuotationInput(Cart, Goods[]) {4    require(cart.lines != null, RejectionCode.EMPTY_CART);5    int count = 0;6    for (CartLine line : cart.lines) {7        require(line != null && line.quantity != null && line.quantity > 0, RejectionCode.INVALID_QUANTITY);8        boolean found = false;9        for (Goods product : goodss) {10            if (product.sku == line.goods.sku) {11                found = true;12            }13        }14        require(found, RejectionCode.PRODUCT_UNAVAILABLE);15        count++;16    }17    require(count > 0, RejectionCode.EMPTY_CART);18}1920atomic check checkUnpaidCancellationAgreementInput(UnpaidCancellationAgreement) {21    require( unpaidCancellationAgreement.reason != BLANK, RejectionCode.REASON_REQUIRED );22}2324atomic check checkUnpaidCancellation(ShopOrder) {25    require(shopOrder.status == OrderStatus.UNPAID, RejectionCode.ORDER_NOT_CANCELLABLE);26}2728atomic check checkPaidCancellationAgreementInput(PaidCancellationAgreement) {29    require( paidCancellationAgreement.reason != BLANK, RejectionCode.REASON_REQUIRED );30}3132atomic check checkPaidCancellation(ShopOrder) {33    require(shopOrder.status == OrderStatus.PAID, RejectionCode.ORDER_NOT_CANCELLABLE);34}3536atomic check checkOrderReservation(ShopOrder, StockLine[]) {37    for (OrderLine line : shopOrder.lines) {38        boolean found = false;39        for (StockLine stock : stockLines) {40            if (stock.goods.sku == line.goods.sku) {41                int needed = 0;42                for (OrderLine ordered : shopOrder.lines) {43                    if (ordered.goods.sku == line.goods.sku) {44                        needed += ordered.quantity;45                    }46                }47                require(stock.available >= needed, RejectionCode.INSUFFICIENT_STOCK);48                found = true;49            }50        }51        require(found, RejectionCode.INVENTORY_ITEM_NOT_FOUND);52    }53}