Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37759 vikas 1
package com.spice.profitmandi.service.lms;
2
 
3
import java.util.LinkedHashMap;
4
import java.util.Map;
5
 
6
/**
7
 * What an LMS call or disposition attempt produced, in terms a controller can map onto its own
8
 * module's response envelope.
9
 *
10
 * <p>The LMS is now driven from two WARs with different response conventions — {@code
11
 * ResponseSender}'s {@code ProfitMandiResponse} in profitmandi-fofo, and both that and v2's {@code
12
 * ApiResponse} in profitmandi-web. The services below the controllers therefore say what happened
13
 * rather than how to say it, and each controller renders it its own way.
14
 */
15
public final class LmsOutcome {
16
 
17
    public enum Status {
18
        /** Proceed; {@link #data} carries the payload. */
19
        OK,
20
        /** The lead or call does not exist. */
21
        NOT_FOUND,
22
        /** Understood but declined — DND, calling hours, no endpoint, a vendor error, bad input. */
23
        REFUSED,
24
        /** No agent could be resolved from the request. */
25
        UNAUTHORIZED,
26
        /** The call belongs to somebody else. */
27
        FORBIDDEN,
28
        /** Server-placed calling is not configured in this environment. */
29
        UNAVAILABLE
30
    }
31
 
32
    public final Status status;
33
 
34
    /**
35
     * Machine-readable refusal reason — DND, OUTSIDE_HOURS, NO_NUMBER, NO_ENDPOINT, PLACE_FAILED,
36
     * INVALID. Null on success. The client branches on this; {@link #message} is for the human.
37
     */
38
    public final String code;
39
 
40
    public final String message;
41
 
42
    public final Map<String, Object> data = new LinkedHashMap<>();
43
 
44
    private LmsOutcome(Status status, String code, String message) {
45
        this.status = status;
46
        this.code = code;
47
        this.message = message;
48
    }
49
 
50
    static LmsOutcome ok() {
51
        return new LmsOutcome(Status.OK, null, null);
52
    }
53
 
54
    static LmsOutcome notFound(String message) {
55
        return new LmsOutcome(Status.NOT_FOUND, null, message);
56
    }
57
 
58
    static LmsOutcome refused(String code, String message) {
59
        return new LmsOutcome(Status.REFUSED, code, message);
60
    }
61
 
62
    static LmsOutcome invalid(String message) {
63
        return new LmsOutcome(Status.REFUSED, "INVALID", message);
64
    }
65
 
66
    static LmsOutcome unauthorized() {
67
        return new LmsOutcome(Status.UNAUTHORIZED, null, "Not signed in");
68
    }
69
 
70
    static LmsOutcome forbidden() {
71
        return new LmsOutcome(Status.FORBIDDEN, null, "That call is not yours");
72
    }
73
 
74
    static LmsOutcome unavailable(String message) {
75
        return new LmsOutcome(Status.UNAVAILABLE, null, message);
76
    }
77
 
78
    LmsOutcome with(String key, Object value) {
79
        data.put(key, value);
80
        return this;
81
    }
82
 
83
    public boolean isOk() {
84
        return status == Status.OK;
85
    }
86
 
87
    /**
88
     * The refusal as a 200-style body: {@code {ok:false, code, reason}}.
89
     *
90
     * <p>The pre-check answers this way rather than with a 4xx because "this retailer is on the DND
91
     * register" is a normal answer to "may I dial?", not a client error — and the caller needs the
92
     * code to decide what to show.
93
     */
94
    public Map<String, Object> asRefusalBody() {
95
        Map<String, Object> body = new LinkedHashMap<>();
96
        body.put("ok", false);
97
        body.put("code", code);
98
        body.put("reason", message);
99
        return body;
100
    }
101
}