Subversion Repositories SmartDukaan

Rev

Rev 37094 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37094 Rev 37157
Line 147... Line 147...
147
     *  do not pass it. */
147
     *  do not pass it. */
148
    @GetMapping("/cart/open")
148
    @GetMapping("/cart/open")
149
    public ResponseEntity<?> openCart(HttpServletRequest request)
149
    public ResponseEntity<?> openCart(HttpServletRequest request)
150
            throws ProfitMandiBusinessException {
150
            throws ProfitMandiBusinessException {
151
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
151
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
152
        if (!auth.isPresent()) return unauthorized();
152
        if (!auth.isPresent()) return authFailureResponse(request);
153
        AuthCtx ctx = auth.get();
153
        AuthCtx ctx = auth.get();
154
        OpenCartValidationResult result = validateAndApplyCarryBagPrice(
154
        OpenCartValidationResult result = validateAndApplyCarryBagPrice(
155
                ctx.cartId, ctx.storeId);
155
                ctx.cartId, ctx.storeId);
156
        return ResponseEntity.ok(ApiResponse.success(result));
156
        return ResponseEntity.ok(ApiResponse.success(result));
157
    }
157
    }
Line 165... Line 165...
165
    @PostMapping("/cart/validate")
165
    @PostMapping("/cart/validate")
166
    public ResponseEntity<?> validateForCheckout(HttpServletRequest request,
166
    public ResponseEntity<?> validateForCheckout(HttpServletRequest request,
167
                                                 @RequestParam(value = "addressId") long addressId)
167
                                                 @RequestParam(value = "addressId") long addressId)
168
            throws ProfitMandiBusinessException {
168
            throws ProfitMandiBusinessException {
169
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
169
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
170
        if (!auth.isPresent()) return unauthorized();
170
        if (!auth.isPresent()) return authFailureResponse(request);
171
        AuthCtx ctx = auth.get();
171
        AuthCtx ctx = auth.get();
172
        CheckoutValidationResult result = cartValidationService.validateForCheckout(
172
        CheckoutValidationResult result = cartValidationService.validateForCheckout(
173
                ctx.cartId, ctx.userId, ctx.storeId, addressId, SaleType.PARTNER_PROCUREMENT);
173
                ctx.cartId, ctx.userId, ctx.storeId, addressId, SaleType.PARTNER_PROCUREMENT);
174
        return ResponseEntity.ok(ApiResponse.success(result));
174
        return ResponseEntity.ok(ApiResponse.success(result));
175
    }
175
    }
Line 183... Line 183...
183
    @PostMapping("/cart/items")
183
    @PostMapping("/cart/items")
184
    public ResponseEntity<?> addItem(HttpServletRequest request,
184
    public ResponseEntity<?> addItem(HttpServletRequest request,
185
                                     @RequestBody AddCartItemRequest body)
185
                                     @RequestBody AddCartItemRequest body)
186
            throws ProfitMandiBusinessException {
186
            throws ProfitMandiBusinessException {
187
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
187
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
188
        if (!auth.isPresent()) return unauthorized();
188
        if (!auth.isPresent()) return authFailureResponse(request);
189
        AuthCtx ctx = auth.get();
189
        AuthCtx ctx = auth.get();
190
        if (body == null || body.productId <= 0 || body.quantity <= 0) {
190
        if (body == null || body.productId <= 0 || body.quantity <= 0) {
191
            throw new ProfitMandiBusinessException("body", body, "CART_ITEM_INVALID_PAYLOAD");
191
            throw new ProfitMandiBusinessException("body", body, "CART_ITEM_INVALID_PAYLOAD");
192
        }
192
        }
193
        List<CartItem> desired = currentLinesAsItems(ctx.cartId);
193
        List<CartItem> desired = currentLinesAsItems(ctx.cartId);
Line 210... Line 210...
210
    public ResponseEntity<?> updateQuantity(HttpServletRequest request,
210
    public ResponseEntity<?> updateQuantity(HttpServletRequest request,
211
                                            @PathVariable int productId,
211
                                            @PathVariable int productId,
212
                                            @RequestParam int quantity)
212
                                            @RequestParam int quantity)
213
            throws ProfitMandiBusinessException {
213
            throws ProfitMandiBusinessException {
214
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
214
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
215
        if (!auth.isPresent()) return unauthorized();
215
        if (!auth.isPresent()) return authFailureResponse(request);
216
        AuthCtx ctx = auth.get();
216
        AuthCtx ctx = auth.get();
217
        if (quantity < 0) {
217
        if (quantity < 0) {
218
            throw new ProfitMandiBusinessException("quantity", quantity, "CART_ITEM_NEGATIVE_QTY");
218
            throw new ProfitMandiBusinessException("quantity", quantity, "CART_ITEM_NEGATIVE_QTY");
219
        }
219
        }
220
        List<CartItem> desired = currentLinesAsItems(ctx.cartId);
220
        List<CartItem> desired = currentLinesAsItems(ctx.cartId);
Line 249... Line 249...
249
    @DeleteMapping("/cart/items/{productId}")
249
    @DeleteMapping("/cart/items/{productId}")
250
    public ResponseEntity<?> removeItem(HttpServletRequest request,
250
    public ResponseEntity<?> removeItem(HttpServletRequest request,
251
                                        @PathVariable int productId)
251
                                        @PathVariable int productId)
252
            throws ProfitMandiBusinessException {
252
            throws ProfitMandiBusinessException {
253
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
253
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
254
        if (!auth.isPresent()) return unauthorized();
254
        if (!auth.isPresent()) return authFailureResponse(request);
255
        AuthCtx ctx = auth.get();
255
        AuthCtx ctx = auth.get();
256
        List<CartItem> desired = currentLinesAsItems(ctx.cartId).stream()
256
        List<CartItem> desired = currentLinesAsItems(ctx.cartId).stream()
257
                .filter(ci -> ci.getItemId() != productId)
257
                .filter(ci -> ci.getItemId() != productId)
258
                .collect(Collectors.toList());
258
                .collect(Collectors.toList());
259
        // Skip carry bag rebalance if user explicitly removed the carry bag
259
        // Skip carry bag rebalance if user explicitly removed the carry bag
Line 269... Line 269...
269
    /** Clear the entire cart. */
269
    /** Clear the entire cart. */
270
    @DeleteMapping("/cart")
270
    @DeleteMapping("/cart")
271
    public ResponseEntity<?> clearCart(HttpServletRequest request)
271
    public ResponseEntity<?> clearCart(HttpServletRequest request)
272
            throws ProfitMandiBusinessException {
272
            throws ProfitMandiBusinessException {
273
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
273
        java.util.Optional<AuthCtx> auth = resolveAuth(request);
274
        if (!auth.isPresent()) return unauthorized();
274
        if (!auth.isPresent()) return authFailureResponse(request);
275
        AuthCtx ctx = auth.get();
275
        AuthCtx ctx = auth.get();
276
        cartService.clearCart(ctx.cartId);
276
        cartService.clearCart(ctx.cartId);
277
        return ResponseEntity.ok(ApiResponse.success(
277
        return ResponseEntity.ok(ApiResponse.success(
278
                validateAndApplyCarryBagPrice(ctx.cartId, ctx.storeId)));
278
                validateAndApplyCarryBagPrice(ctx.cartId, ctx.storeId)));
279
    }
279
    }
Line 525... Line 525...
525
            return java.util.Optional.empty();
525
            return java.util.Optional.empty();
526
        }
526
        }
527
        return java.util.Optional.of(new AuthCtx(userId, storeId, uc.getCartId()));
527
        return java.util.Optional.of(new AuthCtx(userId, storeId, uc.getCartId()));
528
    }
528
    }
529
 
529
 
-
 
530
    /**
-
 
531
     * Distinguishes a genuinely unauthenticated request from a valid login with
-
 
532
     * no procurement cart mapped (missing dtr.user_accounts cartId row). The
-
 
533
     * latter used to answer "Authentication is required", which mislabels a data
-
 
534
     * gap as an auth failure.
-
 
535
     */
-
 
536
    private ResponseEntity<?> authFailureResponse(HttpServletRequest request) {
-
 
537
        Object userIdAttr = request.getAttribute("userId");
-
 
538
        if (userIdAttr instanceof Integer && (int) userIdAttr > 0) {
-
 
539
            java.util.Map<String, Object> body = new java.util.HashMap<>();
-
 
540
            body.put("responseStatus", "FAILURE");
-
 
541
            body.put("statusCode", 404);
-
 
542
            body.put("statusMessage", "NO_PROCUREMENT_CART");
-
 
543
            body.put("message", "No procurement cart is mapped to this user. Contact support to fix the account linkage.");
-
 
544
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(body);
-
 
545
        }
-
 
546
        return unauthorized();
-
 
547
    }
-
 
548
 
530
    private ResponseEntity<?> unauthorized() {
549
    private ResponseEntity<?> unauthorized() {
531
        java.util.Map<String, Object> body = new java.util.HashMap<>();
550
        java.util.Map<String, Object> body = new java.util.HashMap<>();
532
        body.put("responseStatus", "FAILURE");
551
        body.put("responseStatus", "FAILURE");
533
        body.put("statusCode", 401);
552
        body.put("statusCode", 401);
534
        body.put("statusMessage", "LOGIN_REQUIRED");
553
        body.put("statusMessage", "LOGIN_REQUIRED");