Subversion Repositories SmartDukaan

Rev

Rev 36321 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
36321 vikas 1
package com.spice.profitmandi.web.v2.controller;
2
 
3
import com.spice.profitmandi.common.model.ProfitMandiResponse;
37084 ranu 4
import com.spice.profitmandi.common.model.ResponseStatus;
36321 vikas 5
import com.spice.profitmandi.web.v2.response.ApiResponse;
37084 ranu 6
import com.spice.profitmandi.web.v2.response.ErrorDetail;
7
import org.springframework.http.HttpStatus;
36321 vikas 8
import org.springframework.http.ResponseEntity;
9
 
10
public abstract class V2BaseController {
11
 
37084 ranu 12
    /**
13
     * Bridges a v1 controller's ResponseEntity into the v2 ApiResponse envelope.
14
     *
15
     * Fix for silent-success bug: v1 code often signals validation failure by
16
     * returning {@code responseSender.badRequest(payload)} — a 4xx response
17
     * whose body carries context (e.g. cart with price-changed messages from
18
     * {@code OrderController.createOrder}). The previous version blindly wrapped
19
     * everything as {@code ApiResponse.success} + 200, so the React client
20
     * treated a rejected order as a placed order and navigated to "Order
21
     * Placed!" while no transaction was written. We now preserve the v1 status
22
     * so axios rejects and the caller's catch-block runs.
23
     */
36321 vikas 24
    @SuppressWarnings("rawtypes")
25
    protected ResponseEntity<ApiResponse<?>> wrapResponse(ResponseEntity<?> v1Response) {
26
        Object body = v1Response.getBody();
27
        Object data = body;
37084 ranu 28
        boolean v1FailureFlag = false;
36321 vikas 29
 
30
        if (body instanceof ProfitMandiResponse) {
37084 ranu 31
            ProfitMandiResponse pmr = (ProfitMandiResponse) body;
32
            data = pmr.getResponse();
33
            v1FailureFlag = pmr.getResponseStatus() == ResponseStatus.FAILURE;
36321 vikas 34
        }
35
 
37084 ranu 36
        HttpStatus status = v1Response.getStatusCode();
37
        boolean httpError = status != null && status.value() >= 400;
38
 
39
        if (httpError || v1FailureFlag) {
40
            HttpStatus errorStatus = httpError ? status : HttpStatus.BAD_REQUEST;
41
            String message = extractMessage(data, errorStatus);
42
            ErrorDetail detail = new ErrorDetail();
43
            detail.setType("V1BadRequest");
44
            detail.setMessage(message);
45
            @SuppressWarnings({"unchecked", "rawtypes"})
46
            ApiResponse errorResponse = ApiResponse.error(errorStatus.value(), message, detail);
47
            // Preserve v1's original payload so the client can still surface
48
            // context (e.g. price-changed cart messages) if it wants to.
49
            errorResponse.setData(data);
50
            return ResponseEntity.status(errorStatus).body(errorResponse);
51
        }
52
 
36321 vikas 53
        ApiResponse<?> apiResponse = ApiResponse.success(data);
54
        return ResponseEntity.ok(apiResponse);
55
    }
37084 ranu 56
 
57
    private String extractMessage(Object data, HttpStatus fallbackStatus) {
58
        if (data == null) return fallbackStatus.getReasonPhrase();
59
        try {
60
            java.lang.reflect.Method m = data.getClass().getMethod("getCartMessages");
61
            Object messages = m.invoke(data);
62
            if (messages instanceof java.util.List && !((java.util.List<?>) messages).isEmpty()) {
63
                Object first = ((java.util.List<?>) messages).get(0);
64
                java.lang.reflect.Method textMethod = first.getClass().getMethod("getMessageText");
65
                Object text = textMethod.invoke(first);
66
                if (text != null && !text.toString().isEmpty()) return text.toString();
67
            }
68
        } catch (Exception ignore) {
69
            // fall through to generic message
70
        }
71
        try {
72
            java.lang.reflect.Method m = data.getClass().getMethod("getMessage");
73
            Object text = m.invoke(data);
74
            if (text != null && !text.toString().isEmpty()) return text.toString();
75
        } catch (Exception ignore) {
76
            // fall through
77
        }
78
        return fallbackStatus.getReasonPhrase();
79
    }
36321 vikas 80
}