| 1981 |
varun.gupt |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 2752 |
varun.gupt |
3 |
import in.shop2020.datalogger.EventType;
|
| 1981 |
varun.gupt |
4 |
import in.shop2020.model.v1.user.PromotionException;
|
| 3224 |
vikas |
5 |
import in.shop2020.model.v1.user.PromotionService;
|
| 1981 |
varun.gupt |
6 |
import in.shop2020.model.v1.user.UserContextService;
|
| 4839 |
varun.gupt |
7 |
import in.shop2020.serving.interceptors.TrackingInterceptor;
|
| 3224 |
vikas |
8 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 3126 |
rajveer |
9 |
import in.shop2020.thrift.clients.UserClient;
|
| 3224 |
vikas |
10 |
import in.shop2020.utils.DataLogger;
|
| 1981 |
varun.gupt |
11 |
|
| 3224 |
vikas |
12 |
import org.apache.log4j.Logger;
|
| 4839 |
varun.gupt |
13 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
14 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3224 |
vikas |
15 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
16 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
17 |
|
| 2145 |
chandransh |
18 |
@SuppressWarnings("serial")
|
| 4839 |
varun.gupt |
19 |
|
|
|
20 |
@InterceptorRefs({
|
|
|
21 |
@InterceptorRef("myDefault")
|
|
|
22 |
})
|
|
|
23 |
|
| 1981 |
varun.gupt |
24 |
@Results({
|
|
|
25 |
@Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
|
|
|
26 |
})
|
|
|
27 |
public class PromotionController extends BaseController {
|
|
|
28 |
|
|
|
29 |
public PromotionController() {
|
|
|
30 |
super();
|
|
|
31 |
}
|
|
|
32 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
33 |
|
|
|
34 |
public String create() {
|
|
|
35 |
String action = request.getParameter("action");
|
|
|
36 |
log.info("Action is: " + action);
|
|
|
37 |
|
| 3126 |
rajveer |
38 |
UserClient userServiceClient = null;
|
|
|
39 |
PromotionClient promotionServiceClient = null;
|
| 1981 |
varun.gupt |
40 |
|
|
|
41 |
try {
|
|
|
42 |
long cartId = userinfo.getCartId();
|
|
|
43 |
|
|
|
44 |
if(action == null || action.isEmpty()) {
|
|
|
45 |
addActionError("Invalid Request Action");
|
|
|
46 |
return "redirect";
|
|
|
47 |
}
|
| 2752 |
varun.gupt |
48 |
String couponCode = request.getParameter("coupon_code");
|
|
|
49 |
|
| 4839 |
varun.gupt |
50 |
if (action.equalsIgnoreCase("applycoupon")) {
|
| 1981 |
varun.gupt |
51 |
if (couponCode == null || couponCode.isEmpty()) {
|
|
|
52 |
addActionError("Coupon Code field cannot be left empty");
|
|
|
53 |
return "redirect";
|
|
|
54 |
}
|
| 4839 |
varun.gupt |
55 |
|
|
|
56 |
boolean qualifiesForCoupon = true;
|
|
|
57 |
|
|
|
58 |
if(couponCode.equalsIgnoreCase("SGalaxy")) {
|
|
|
59 |
if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
|
|
|
60 |
long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
|
|
|
61 |
|
|
|
62 |
//TODO: Affiliate based qualification should be inside service
|
|
|
63 |
if (affId != 5) { //For MySmartPrice
|
|
|
64 |
qualifiesForCoupon = false;
|
|
|
65 |
}
|
|
|
66 |
} else {
|
|
|
67 |
qualifiesForCoupon = false;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
if(qualifiesForCoupon) {
|
|
|
71 |
promotionServiceClient = new PromotionClient();
|
|
|
72 |
PromotionService.Client promotionClient = promotionServiceClient.getClient();
|
|
|
73 |
|
|
|
74 |
promotionClient.applyCoupon(couponCode, cartId);
|
|
|
75 |
DataLogger.logData(EventType.COUPON_APPLIED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
|
|
|
76 |
} else {
|
|
|
77 |
addActionError("You are not eligible for this coupon");
|
|
|
78 |
}
|
| 1981 |
varun.gupt |
79 |
}
|
|
|
80 |
else if (action.equals("removecoupon")) {
|
| 3126 |
rajveer |
81 |
userServiceClient = new UserClient();
|
| 1981 |
varun.gupt |
82 |
UserContextService.Client userClient = userServiceClient.getClient();
|
|
|
83 |
userClient.removeCoupon(cartId);
|
| 3224 |
vikas |
84 |
DataLogger.logData(EventType.COUPON_REMOVED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
|
| 1981 |
varun.gupt |
85 |
}
|
|
|
86 |
} catch (PromotionException e) {
|
| 3171 |
rajveer |
87 |
log.info("Invalid coupon: " + e.getMessage());
|
| 1981 |
varun.gupt |
88 |
addActionError(e.getMessage());
|
|
|
89 |
} catch (Exception e) {
|
| 2949 |
chandransh |
90 |
log.error("Unable to apply or remove coupon", e);
|
| 1981 |
varun.gupt |
91 |
}
|
|
|
92 |
return "redirect";
|
|
|
93 |
}
|
|
|
94 |
}
|