| 1981 |
varun.gupt |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 2752 |
varun.gupt |
3 |
import in.shop2020.datalogger.EventType;
|
| 11823 |
amit.gupta |
4 |
import in.shop2020.model.v1.user.Cart;
|
| 1981 |
varun.gupt |
5 |
import in.shop2020.model.v1.user.PromotionException;
|
| 3224 |
vikas |
6 |
import in.shop2020.model.v1.user.PromotionService;
|
| 1981 |
varun.gupt |
7 |
import in.shop2020.model.v1.user.UserContextService;
|
| 11980 |
amit.gupta |
8 |
import in.shop2020.serving.interceptors.UserInterceptor;
|
| 3224 |
vikas |
9 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 3126 |
rajveer |
10 |
import in.shop2020.thrift.clients.UserClient;
|
| 3224 |
vikas |
11 |
import in.shop2020.utils.DataLogger;
|
| 1981 |
varun.gupt |
12 |
|
| 11980 |
amit.gupta |
13 |
import javax.servlet.http.Cookie;
|
|
|
14 |
|
| 3224 |
vikas |
15 |
import org.apache.log4j.Logger;
|
| 4839 |
varun.gupt |
16 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
17 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3224 |
vikas |
18 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
19 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
20 |
|
| 2145 |
chandransh |
21 |
@SuppressWarnings("serial")
|
| 4839 |
varun.gupt |
22 |
|
|
|
23 |
@InterceptorRefs({
|
|
|
24 |
@InterceptorRef("myDefault")
|
|
|
25 |
})
|
|
|
26 |
|
| 1981 |
varun.gupt |
27 |
@Results({
|
|
|
28 |
@Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
|
|
|
29 |
})
|
|
|
30 |
public class PromotionController extends BaseController {
|
|
|
31 |
|
|
|
32 |
public PromotionController() {
|
|
|
33 |
super();
|
|
|
34 |
}
|
|
|
35 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
36 |
|
|
|
37 |
public String create() {
|
|
|
38 |
String action = request.getParameter("action");
|
|
|
39 |
log.info("Action is: " + action);
|
|
|
40 |
|
| 3126 |
rajveer |
41 |
UserClient userServiceClient = null;
|
|
|
42 |
PromotionClient promotionServiceClient = null;
|
| 1981 |
varun.gupt |
43 |
|
|
|
44 |
try {
|
|
|
45 |
long cartId = userinfo.getCartId();
|
|
|
46 |
|
|
|
47 |
if(action == null || action.isEmpty()) {
|
|
|
48 |
addActionError("Invalid Request Action");
|
|
|
49 |
return "redirect";
|
|
|
50 |
}
|
| 2752 |
varun.gupt |
51 |
String couponCode = request.getParameter("coupon_code");
|
|
|
52 |
|
| 4839 |
varun.gupt |
53 |
if (action.equalsIgnoreCase("applycoupon")) {
|
| 1981 |
varun.gupt |
54 |
if (couponCode == null || couponCode.isEmpty()) {
|
|
|
55 |
addActionError("Coupon Code field cannot be left empty");
|
|
|
56 |
return "redirect";
|
| 11980 |
amit.gupta |
57 |
} else if (couponCode.equals("saholicdeals")) {
|
|
|
58 |
Cookie co = cookiesMap.get(UserInterceptor.DEAL_COUPON_REMOVED);
|
|
|
59 |
if (co != null) {
|
|
|
60 |
co.setMaxAge(0);
|
| 12007 |
amit.gupta |
61 |
co.setDomain("/");
|
|
|
62 |
co.setDomain(domainName);
|
| 11980 |
amit.gupta |
63 |
}
|
| 1981 |
varun.gupt |
64 |
}
|
| 11823 |
amit.gupta |
65 |
promotionServiceClient = new PromotionClient();
|
|
|
66 |
PromotionService.Client promotionClient = promotionServiceClient.getClient();
|
|
|
67 |
String str = "Coupon applied";
|
|
|
68 |
Cart cart = promotionClient.applyCoupon(couponCode, cartId);
|
| 11854 |
amit.gupta |
69 |
if(cart.getMessage() != null && !cart.getMessage().equals("")){
|
| 11823 |
amit.gupta |
70 |
str = str + ", " + cart.getMessage();
|
| 4839 |
varun.gupt |
71 |
}
|
| 11851 |
amit.gupta |
72 |
addActionMessage(str);
|
| 11823 |
amit.gupta |
73 |
DataLogger.logData(EventType.COUPON_APPLIED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
|
| 1981 |
varun.gupt |
74 |
}
|
|
|
75 |
else if (action.equals("removecoupon")) {
|
| 11980 |
amit.gupta |
76 |
if(userinfo.isPrivateDealUser()) {
|
|
|
77 |
if (cookiesMap.get(UserInterceptor.DEAL_COUPON_REMOVED)==null) {
|
|
|
78 |
Cookie co = new Cookie(UserInterceptor.DEAL_COUPON_REMOVED, "true");
|
| 11985 |
amit.gupta |
79 |
co.setDomain(this.domainName);
|
| 12008 |
amit.gupta |
80 |
co.setMaxAge(-1);
|
| 11988 |
amit.gupta |
81 |
co.setPath("/");
|
| 12007 |
amit.gupta |
82 |
cookiesMap.put(UserInterceptor.DEAL_COUPON_REMOVED, co);
|
| 11980 |
amit.gupta |
83 |
}
|
|
|
84 |
}
|
| 3126 |
rajveer |
85 |
userServiceClient = new UserClient();
|
| 1981 |
varun.gupt |
86 |
UserContextService.Client userClient = userServiceClient.getClient();
|
|
|
87 |
userClient.removeCoupon(cartId);
|
| 3224 |
vikas |
88 |
DataLogger.logData(EventType.COUPON_REMOVED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
|
| 1981 |
varun.gupt |
89 |
}
|
|
|
90 |
} catch (PromotionException e) {
|
| 3171 |
rajveer |
91 |
log.info("Invalid coupon: " + e.getMessage());
|
| 1981 |
varun.gupt |
92 |
addActionError(e.getMessage());
|
|
|
93 |
} catch (Exception e) {
|
| 2949 |
chandransh |
94 |
log.error("Unable to apply or remove coupon", e);
|
| 1981 |
varun.gupt |
95 |
}
|
|
|
96 |
return "redirect";
|
|
|
97 |
}
|
|
|
98 |
}
|