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