Subversion Repositories SmartDukaan

Rev

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

Rev 36321 Rev 37084
Line 1... Line 1...
1
package com.spice.profitmandi.web.v2.controller;
1
package com.spice.profitmandi.web.v2.controller;
2
 
2
 
3
import com.spice.profitmandi.common.model.ProfitMandiResponse;
3
import com.spice.profitmandi.common.model.ProfitMandiResponse;
-
 
4
import com.spice.profitmandi.common.model.ResponseStatus;
4
import com.spice.profitmandi.web.v2.response.ApiResponse;
5
import com.spice.profitmandi.web.v2.response.ApiResponse;
-
 
6
import com.spice.profitmandi.web.v2.response.ErrorDetail;
-
 
7
import org.springframework.http.HttpStatus;
5
import org.springframework.http.ResponseEntity;
8
import org.springframework.http.ResponseEntity;
6
 
9
 
7
public abstract class V2BaseController {
10
public abstract class V2BaseController {
8
 
11
 
-
 
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
     */
9
    @SuppressWarnings("rawtypes")
24
    @SuppressWarnings("rawtypes")
10
    protected ResponseEntity<ApiResponse<?>> wrapResponse(ResponseEntity<?> v1Response) {
25
    protected ResponseEntity<ApiResponse<?>> wrapResponse(ResponseEntity<?> v1Response) {
11
        Object body = v1Response.getBody();
26
        Object body = v1Response.getBody();
12
        Object data = body;
27
        Object data = body;
-
 
28
        boolean v1FailureFlag = false;
13
 
29
 
14
        if (body instanceof ProfitMandiResponse) {
30
        if (body instanceof ProfitMandiResponse) {
15
            data = ((ProfitMandiResponse) body).getResponse();
31
            ProfitMandiResponse pmr = (ProfitMandiResponse) body;
-
 
32
            data = pmr.getResponse();
-
 
33
            v1FailureFlag = pmr.getResponseStatus() == ResponseStatus.FAILURE;
-
 
34
        }
-
 
35
 
-
 
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);
16
        }
51
        }
17
 
52
 
18
        ApiResponse<?> apiResponse = ApiResponse.success(data);
53
        ApiResponse<?> apiResponse = ApiResponse.success(data);
19
        return ResponseEntity.ok(apiResponse);
54
        return ResponseEntity.ok(apiResponse);
20
    }
55
    }
-
 
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
    }
21
}
80
}