| 21378 |
kshitij.so |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.Arrays;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
|
|
|
8 |
import javax.servlet.http.HttpServletRequest;
|
|
|
9 |
|
|
|
10 |
import org.apache.thrift.TException;
|
|
|
11 |
import org.json.JSONArray;
|
|
|
12 |
import org.json.JSONException;
|
|
|
13 |
import org.json.JSONObject;
|
|
|
14 |
import org.slf4j.Logger;
|
|
|
15 |
import org.slf4j.LoggerFactory;
|
|
|
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
17 |
import org.springframework.http.HttpStatus;
|
|
|
18 |
import org.springframework.http.MediaType;
|
|
|
19 |
import org.springframework.http.ResponseEntity;
|
|
|
20 |
import org.springframework.stereotype.Controller;
|
| 21707 |
amit.gupta |
21 |
import org.springframework.transaction.annotation.Transactional;
|
| 21378 |
kshitij.so |
22 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21393 |
amit.gupta |
25 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21378 |
kshitij.so |
26 |
|
|
|
27 |
import com.google.gson.Gson;
|
|
|
28 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21741 |
ashik.ali |
29 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
|
|
30 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
|
|
31 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 21735 |
ashik.ali |
32 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
|
|
33 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| 21378 |
kshitij.so |
34 |
import com.spice.profitmandi.dao.model.ProductPojo;
|
| 21738 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.model.UserCart;
|
| 21735 |
ashik.ali |
36 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 22568 |
amit.gupta |
37 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 21643 |
ashik.ali |
38 |
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
|
|
|
39 |
import com.spice.profitmandi.dao.util.LogisticsService;
|
| 21378 |
kshitij.so |
40 |
import com.spice.profitmandi.thrift.clients.UserClient;
|
|
|
41 |
import com.spice.profitmandi.web.req.AddCartRequest;
|
|
|
42 |
import com.spice.profitmandi.web.req.CartItems;
|
|
|
43 |
import com.spice.profitmandi.web.res.CartResponse;
|
|
|
44 |
import com.spice.profitmandi.web.res.ValidateCartResponse;
|
|
|
45 |
|
|
|
46 |
import in.shop2020.logistics.DeliveryType;
|
|
|
47 |
import in.shop2020.model.v1.user.ItemQuantity;
|
| 21393 |
amit.gupta |
48 |
import in.shop2020.model.v1.user.UserContextService;
|
| 21378 |
kshitij.so |
49 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
|
|
50 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
51 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
52 |
import io.swagger.annotations.ApiOperation;
|
|
|
53 |
|
|
|
54 |
@Controller
|
| 22037 |
amit.gupta |
55 |
@Transactional(rollbackFor=Throwable.class)
|
| 21378 |
kshitij.so |
56 |
public class CartController {
|
|
|
57 |
|
|
|
58 |
private static final Logger logger=LoggerFactory.getLogger(CartController.class);
|
| 21440 |
ashik.ali |
59 |
|
|
|
60 |
@Autowired
|
| 22931 |
ashik.ali |
61 |
private ResponseSender<?> responseSender;
|
| 21378 |
kshitij.so |
62 |
|
|
|
63 |
@Autowired
|
| 22931 |
ashik.ali |
64 |
private UserAccountRepository userAccountRepository;
|
| 22173 |
amit.gupta |
65 |
|
|
|
66 |
@Autowired
|
| 22931 |
ashik.ali |
67 |
private ContentPojoPopulator contentPojoPopulator;
|
| 22568 |
amit.gupta |
68 |
|
| 21378 |
kshitij.so |
69 |
|
|
|
70 |
@RequestMapping(value = ProfitMandiConstants.URL_CART, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
71 |
@ApiImplicitParams({
|
|
|
72 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
73 |
required = true, dataType = "string", paramType = "header")
|
|
|
74 |
})
|
|
|
75 |
@ApiOperation(value = "Add items to cart")
|
| 21681 |
amit.gupta |
76 |
public ResponseEntity<?> validateCart(HttpServletRequest request,@RequestBody AddCartRequest cartRequest, @RequestParam(value="pincode", defaultValue="110001") String pincode) throws Throwable{
|
| 21378 |
kshitij.so |
77 |
UserAccounts userAccount = null;
|
|
|
78 |
ValidateCartResponse vc = null;
|
| 21458 |
kshitij.so |
79 |
int userId = (int)request.getAttribute("userId");
|
| 21681 |
amit.gupta |
80 |
|
|
|
81 |
userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.cartId);
|
|
|
82 |
logger.info("UserAccount "+userAccount);
|
| 21378 |
kshitij.so |
83 |
|
|
|
84 |
List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
|
|
|
85 |
CartItems[] cartItems = cartRequest.getCartItems();
|
|
|
86 |
List<Integer> itemsList = new ArrayList<Integer>();
|
|
|
87 |
for (CartItems ci: cartItems){
|
|
|
88 |
itemsList.add(ci.getItemId());
|
|
|
89 |
ItemQuantity iq = new ItemQuantity(ci.getItemId(), ci.getQuantity());
|
|
|
90 |
itemQuantities.add(iq);
|
|
|
91 |
}
|
|
|
92 |
Client userClient = null;
|
|
|
93 |
try {
|
|
|
94 |
userClient = new UserClient().getClient();
|
|
|
95 |
userClient.addItemsToCart(Integer.valueOf(userAccount.getAccount_key()), itemQuantities, null);
|
| 21383 |
kshitij.so |
96 |
} catch (Exception e) {
|
| 21378 |
kshitij.so |
97 |
vc = new ValidateCartResponse(null, "Error","Problem occurred while updating cart");
|
|
|
98 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, vc);
|
|
|
99 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
100 |
}
|
|
|
101 |
String cartString=null;
|
| 22568 |
amit.gupta |
102 |
//Use source id temporarily for purpose of tag id as it is just one lets hardcode the tag id
|
|
|
103 |
int source = -1;
|
|
|
104 |
cartString = userClient.validateCartNew(Integer.valueOf(userAccount.getAccount_key()), pincode, source);
|
| 21378 |
kshitij.so |
105 |
JSONObject cartObj = new JSONObject(cartString);
|
|
|
106 |
JSONArray arr = cartObj.getJSONArray("cartItems");
|
|
|
107 |
int maxEstimate=-2;
|
|
|
108 |
boolean allSame=true;
|
|
|
109 |
int removedCount = 0;
|
|
|
110 |
cartObj.put("cartMessagesMerged", 0);
|
|
|
111 |
for (int j=0; j<arr.length(); j++){
|
|
|
112 |
JSONObject itemObj = arr.getJSONObject(j-removedCount);
|
|
|
113 |
if(!itemsList.contains(itemObj.getInt("itemId"))){
|
|
|
114 |
if(itemObj.getInt("quantity")==0){
|
|
|
115 |
arr.remove(j-removedCount);
|
|
|
116 |
removedCount++;
|
|
|
117 |
if (itemObj.has("estimate") && itemObj.getInt("estimate")==-1){
|
|
|
118 |
cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
|
|
|
119 |
} else {
|
|
|
120 |
cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
|
|
|
121 |
}
|
|
|
122 |
continue;
|
|
|
123 |
} else {
|
|
|
124 |
JSONArray messagesArray = new JSONArray();
|
|
|
125 |
itemObj.put("cartItemMessages", messagesArray);
|
|
|
126 |
if(itemsList.size()>0){
|
|
|
127 |
JSONObject message=new JSONObject();
|
|
|
128 |
message.put("type","info");
|
|
|
129 |
message.put("messageText","Added from earlier cart");
|
|
|
130 |
messagesArray.put(message);
|
|
|
131 |
cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
}
|
| 22173 |
amit.gupta |
135 |
ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
|
| 21389 |
kshitij.so |
136 |
// String productProperties = ContentServingService.getSnippet(Utils.PRODUCT_PROPERTIES_SNIPPET, pp.getId() +"", -1);
|
|
|
137 |
// JSONObject productPropertiesInJson = new JSONObject(productProperties);
|
|
|
138 |
// pp.setCategoryName(productPropertiesInJson.getString("categoryName"));
|
| 21378 |
kshitij.so |
139 |
|
|
|
140 |
if(itemObj.has("estimate")){
|
|
|
141 |
if(allSame){
|
|
|
142 |
allSame = maxEstimate==-2 || maxEstimate==itemObj.getInt("estimate");
|
|
|
143 |
}
|
|
|
144 |
if(itemObj.getInt("estimate") > maxEstimate){
|
|
|
145 |
maxEstimate = itemObj.getInt("estimate");
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
itemObj.put("imageUrl", pp.getImageUrl());
|
|
|
149 |
itemObj.put("title", pp.getTitle());
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
|
|
|
153 |
if (arr != null) {
|
|
|
154 |
for (int i=0;i<arr.length();i++){
|
|
|
155 |
JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
|
|
|
156 |
if(itemMessages!=null && itemMessages.length()>0){
|
|
|
157 |
listdata.add(0,arr.getJSONObject(i));
|
|
|
158 |
} else {
|
|
|
159 |
listdata.add(arr.getJSONObject(i));
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
arr = new JSONArray();
|
|
|
164 |
for(int i=0;i<listdata.size();i++){
|
|
|
165 |
arr.put(listdata.get(i));
|
|
|
166 |
}
|
|
|
167 |
cartObj.put("cartItems", arr);
|
|
|
168 |
JSONArray cartMessagesArray = new JSONArray();
|
|
|
169 |
for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
|
|
|
170 |
int count = cartObj.getInt(message);
|
|
|
171 |
if (count>0) {
|
|
|
172 |
String type="danger";
|
|
|
173 |
JSONObject cartMessage=new JSONObject();
|
|
|
174 |
if (message.equals("cartMessagesMerged")) {
|
|
|
175 |
type = "info";
|
|
|
176 |
if (count==1){
|
|
|
177 |
cartMessage.put("messageText","1 item is added from earlier cart");
|
|
|
178 |
}else {
|
|
|
179 |
cartMessage.put("messageText","Few items are added from earlier cart");
|
|
|
180 |
}
|
|
|
181 |
} else if (message.equals("cartMessageOOS")) {
|
|
|
182 |
if (count==1){
|
|
|
183 |
cartMessage.put("messageText","One item is currently Out of Stock");
|
|
|
184 |
}else {
|
|
|
185 |
cartMessage.put("messageText","Few items are currently Out of Stock");
|
|
|
186 |
}
|
|
|
187 |
} else if (message.equals("cartMessageUndeliverable")) {
|
|
|
188 |
if (count==1){
|
|
|
189 |
cartMessage.put("messageText","One item is undeliverable");
|
|
|
190 |
}else {
|
|
|
191 |
cartMessage.put("messageText","Few items are underiverable");
|
|
|
192 |
}
|
|
|
193 |
} else {
|
|
|
194 |
if (count==1){
|
|
|
195 |
cartMessage.put("messageText","One item qty has changed");
|
|
|
196 |
}else {
|
|
|
197 |
cartMessage.put("messageText","Few items qty have changed");
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
cartMessage.put("type",type);
|
|
|
201 |
cartMessagesArray.put(cartMessage);
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
cartObj.put("cartMessages", cartMessagesArray);
|
|
|
205 |
|
|
|
206 |
if (maxEstimate==-1){
|
|
|
207 |
cartObj.put("estimateString", "Can't ship here");
|
|
|
208 |
}
|
|
|
209 |
else if(maxEstimate==-2){
|
|
|
210 |
cartObj.put("estimateString", "Out of Stock");
|
|
|
211 |
} else {
|
|
|
212 |
try {
|
| 21452 |
amit.gupta |
213 |
cartObj.put("cod", cartObj.getBoolean("codAllowed"));
|
| 21378 |
kshitij.so |
214 |
cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
|
|
|
215 |
} catch (NumberFormatException | JSONException | TException e) {
|
|
|
216 |
// TODO Auto-generated catch block
|
|
|
217 |
e.printStackTrace();
|
|
|
218 |
vc = new ValidateCartResponse(null, "Error","Problem occurred while getting delivery estimates");
|
| 21393 |
amit.gupta |
219 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() ,
|
|
|
220 |
HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, vc);
|
| 21378 |
kshitij.so |
221 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
}
|
|
|
225 |
cartObj.put("maxEstimate", maxEstimate);
|
|
|
226 |
CartResponse cartResponse = new Gson().fromJson(cartObj.toString(), CartResponse.class);
|
|
|
227 |
vc = new ValidateCartResponse(cartResponse, "Success","Items added to cart successfully");
|
|
|
228 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, vc);
|
|
|
229 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
|
|
|
230 |
}
|
| 21393 |
amit.gupta |
231 |
|
| 21407 |
kshitij.so |
232 |
@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21393 |
amit.gupta |
233 |
@ApiImplicitParams({
|
|
|
234 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
235 |
required = true, dataType = "string", paramType = "header")
|
|
|
236 |
})
|
|
|
237 |
|
|
|
238 |
@ApiOperation(value = "Change address")
|
| 21738 |
amit.gupta |
239 |
public ResponseEntity<?> changeAddress(HttpServletRequest request, @RequestParam(value="addressId") long addressId) throws Throwable{
|
|
|
240 |
UserCart uc = userAccountRepository.getUserCart((int)request.getAttribute("userId"));
|
| 21666 |
amit.gupta |
241 |
UserContextService.Client userClient = new UserClient().getClient();
|
| 21738 |
amit.gupta |
242 |
userClient.addAddressToCart(uc.getCartId(), addressId);
|
| 21666 |
amit.gupta |
243 |
return responseSender.ok("Address Changed successfully");
|
| 21393 |
amit.gupta |
244 |
}
|
| 21378 |
kshitij.so |
245 |
}
|