| 21378 |
kshitij.so |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Arrays;
|
| 24422 |
amit.gupta |
5 |
import java.util.HashMap;
|
| 21378 |
kshitij.so |
6 |
import java.util.List;
|
| 24422 |
amit.gupta |
7 |
import java.util.Map;
|
| 21378 |
kshitij.so |
8 |
|
|
|
9 |
import javax.servlet.http.HttpServletRequest;
|
|
|
10 |
|
| 23959 |
amit.gupta |
11 |
import org.apache.logging.log4j.LogManager;
|
|
|
12 |
import org.apache.logging.log4j.Logger;
|
| 21378 |
kshitij.so |
13 |
import org.apache.thrift.TException;
|
|
|
14 |
import org.json.JSONArray;
|
|
|
15 |
import org.json.JSONException;
|
|
|
16 |
import org.json.JSONObject;
|
|
|
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
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.web.util.ResponseSender;
|
| 23360 |
amit.gupta |
30 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 23273 |
ashik.ali |
31 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 21735 |
ashik.ali |
32 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| 21378 |
kshitij.so |
33 |
import com.spice.profitmandi.dao.model.ProductPojo;
|
| 21738 |
amit.gupta |
34 |
import com.spice.profitmandi.dao.model.UserCart;
|
| 23360 |
amit.gupta |
35 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 21735 |
ashik.ali |
36 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 21643 |
ashik.ali |
37 |
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
|
|
|
38 |
import com.spice.profitmandi.dao.util.LogisticsService;
|
| 25962 |
amit.gupta |
39 |
import com.spice.profitmandi.service.inventory.ItemBucketService;
|
| 21378 |
kshitij.so |
40 |
import com.spice.profitmandi.thrift.clients.UserClient;
|
|
|
41 |
import com.spice.profitmandi.web.req.AddCartRequest;
|
| 23965 |
amit.gupta |
42 |
import com.spice.profitmandi.web.req.CartItems;
|
| 21378 |
kshitij.so |
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
|
| 24199 |
amit.gupta |
55 |
@Transactional(rollbackFor = Throwable.class)
|
| 21378 |
kshitij.so |
56 |
public class CartController {
|
|
|
57 |
|
| 24199 |
amit.gupta |
58 |
private static final Logger logger = LogManager.getLogger(CartController.class);
|
|
|
59 |
|
| 21440 |
ashik.ali |
60 |
@Autowired
|
| 22931 |
ashik.ali |
61 |
private ResponseSender<?> responseSender;
|
| 21378 |
kshitij.so |
62 |
|
|
|
63 |
@Autowired
|
| 22931 |
ashik.ali |
64 |
private UserAccountRepository userAccountRepository;
|
| 24199 |
amit.gupta |
65 |
|
| 22173 |
amit.gupta |
66 |
@Autowired
|
| 25962 |
amit.gupta |
67 |
private ItemBucketService itemBucketService;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
| 22931 |
ashik.ali |
70 |
private ContentPojoPopulator contentPojoPopulator;
|
| 24199 |
amit.gupta |
71 |
|
| 23360 |
amit.gupta |
72 |
@Autowired
|
|
|
73 |
private ItemRepository itemRepository;
|
| 21378 |
kshitij.so |
74 |
|
| 24422 |
amit.gupta |
75 |
public static final Map<String, Integer> MIN_BRAND_QTY_LIMIT = new HashMap<>();
|
|
|
76 |
|
|
|
77 |
static {
|
| 25962 |
amit.gupta |
78 |
// MIN_BRAND_QTY_LIMIT.put("Realme", 10);
|
| 24464 |
amit.gupta |
79 |
MIN_BRAND_QTY_LIMIT.put("Reliance", 5);
|
| 24422 |
amit.gupta |
80 |
}
|
|
|
81 |
|
| 25962 |
amit.gupta |
82 |
@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
83 |
@ApiImplicitParams({
|
|
|
84 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
85 |
@ApiOperation(value = "Add items to cart")
|
|
|
86 |
public ResponseEntity<?> validateCart(HttpServletRequest request,
|
|
|
87 |
@RequestParam(value = "pincode", defaultValue = "110001") String pincode, @RequestParam int bucketId)
|
|
|
88 |
throws Throwable {
|
|
|
89 |
|
|
|
90 |
AddCartRequest cartRequest = new AddCartRequest();
|
|
|
91 |
List<CartItems> ci = new ArrayList<>();
|
|
|
92 |
itemBucketService.getBucketDetails(bucketId).stream().forEach(x -> {
|
| 25965 |
amit.gupta |
93 |
ci.add(new CartItems(x.getQuantity(),x.getItemId()));
|
| 25962 |
amit.gupta |
94 |
});
|
|
|
95 |
cartRequest.setCartItems(ci.toArray(new CartItems[0]));
|
|
|
96 |
return this.validateCart(request, cartRequest, "110001");
|
|
|
97 |
|
|
|
98 |
}
|
|
|
99 |
|
| 24199 |
amit.gupta |
100 |
@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21378 |
kshitij.so |
101 |
@ApiImplicitParams({
|
| 24199 |
amit.gupta |
102 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 21378 |
kshitij.so |
103 |
@ApiOperation(value = "Add items to cart")
|
| 24199 |
amit.gupta |
104 |
public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
|
|
|
105 |
@RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
|
| 23273 |
ashik.ali |
106 |
UserAccount userAccount = null;
|
| 21378 |
kshitij.so |
107 |
ValidateCartResponse vc = null;
|
| 24199 |
amit.gupta |
108 |
int userId = (int) request.getAttribute("userId");
|
|
|
109 |
|
| 23273 |
ashik.ali |
110 |
userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.cartId);
|
| 24199 |
amit.gupta |
111 |
logger.info("UserAccount................." + userAccount);
|
| 24422 |
amit.gupta |
112 |
Map<String, Integer> brandQtyMap = new HashMap<>();
|
| 21378 |
kshitij.so |
113 |
List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
|
|
|
114 |
CartItems[] cartItems = cartRequest.getCartItems();
|
|
|
115 |
List<Integer> itemsList = new ArrayList<Integer>();
|
| 24424 |
amit.gupta |
116 |
// Only Keep Non Billable Stock if partner adds both
|
| 24199 |
amit.gupta |
117 |
boolean nogst = false;
|
| 25962 |
amit.gupta |
118 |
|
| 24199 |
amit.gupta |
119 |
for (CartItems ci : cartItems) {
|
| 24424 |
amit.gupta |
120 |
if (ci.getQuantity() > 0 && itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
|
| 24199 |
amit.gupta |
121 |
nogst = true;
|
| 24153 |
amit.gupta |
122 |
}
|
|
|
123 |
}
|
| 24199 |
amit.gupta |
124 |
|
|
|
125 |
JSONObject cartMessage = null;
|
|
|
126 |
for (CartItems ci : cartItems) {
|
|
|
127 |
if (nogst && !itemRepository.selectById(ci.getItemId()).getHsnCode().equals("NOGST")) {
|
|
|
128 |
if (cartMessage == null) {
|
| 24198 |
amit.gupta |
129 |
cartMessage = new JSONObject();
|
|
|
130 |
cartMessage.put("messageText", "Billable items can't be billed along with non Billable items");
|
|
|
131 |
}
|
|
|
132 |
continue;
|
| 24199 |
amit.gupta |
133 |
|
| 24153 |
amit.gupta |
134 |
}
|
| 21378 |
kshitij.so |
135 |
itemsList.add(ci.getItemId());
|
|
|
136 |
ItemQuantity iq = new ItemQuantity(ci.getItemId(), ci.getQuantity());
|
|
|
137 |
itemQuantities.add(iq);
|
|
|
138 |
}
|
| 25962 |
amit.gupta |
139 |
if (cartMessage == null) {
|
| 24198 |
amit.gupta |
140 |
cartMessage = new JSONObject();
|
|
|
141 |
}
|
|
|
142 |
|
| 21378 |
kshitij.so |
143 |
Client userClient = null;
|
|
|
144 |
try {
|
|
|
145 |
userClient = new UserClient().getClient();
|
| 23273 |
ashik.ali |
146 |
userClient.addItemsToCart(Integer.valueOf(userAccount.getAccountKey()), itemQuantities, null);
|
| 21383 |
kshitij.so |
147 |
} catch (Exception e) {
|
| 24199 |
amit.gupta |
148 |
vc = new ValidateCartResponse(null, "Error", "Problem occurred while updating cart");
|
| 23021 |
ashik.ali |
149 |
return responseSender.internalServerError(vc);
|
| 21378 |
kshitij.so |
150 |
}
|
| 24199 |
amit.gupta |
151 |
String cartString = null;
|
|
|
152 |
// Use source id temporarily for purpose of tag id as it is just one lets
|
|
|
153 |
// hardcode the tag id
|
| 22568 |
amit.gupta |
154 |
int source = -1;
|
| 23273 |
ashik.ali |
155 |
cartString = userClient.validateCartNew(Integer.valueOf(userAccount.getAccountKey()), pincode, source);
|
| 21378 |
kshitij.so |
156 |
JSONObject cartObj = new JSONObject(cartString);
|
|
|
157 |
JSONArray arr = cartObj.getJSONArray("cartItems");
|
| 24199 |
amit.gupta |
158 |
int maxEstimate = -2;
|
|
|
159 |
boolean allSame = true;
|
| 21378 |
kshitij.so |
160 |
int removedCount = 0;
|
|
|
161 |
cartObj.put("cartMessagesMerged", 0);
|
| 25962 |
amit.gupta |
162 |
|
| 24199 |
amit.gupta |
163 |
for (int j = 0; j < arr.length(); j++) {
|
|
|
164 |
JSONObject itemObj = arr.getJSONObject(j - removedCount);
|
|
|
165 |
if (!itemsList.contains(itemObj.getInt("itemId"))) {
|
|
|
166 |
if (itemObj.getInt("quantity") == 0) {
|
|
|
167 |
arr.remove(j - removedCount);
|
| 21378 |
kshitij.so |
168 |
removedCount++;
|
| 24199 |
amit.gupta |
169 |
if (itemObj.has("estimate") && itemObj.getInt("estimate") == -1) {
|
| 21378 |
kshitij.so |
170 |
cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
|
|
|
171 |
} else {
|
|
|
172 |
cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
|
|
|
173 |
}
|
|
|
174 |
continue;
|
|
|
175 |
} else {
|
|
|
176 |
JSONArray messagesArray = new JSONArray();
|
|
|
177 |
itemObj.put("cartItemMessages", messagesArray);
|
| 24199 |
amit.gupta |
178 |
if (itemsList.size() > 0) {
|
|
|
179 |
JSONObject message = new JSONObject();
|
|
|
180 |
message.put("type", "info");
|
|
|
181 |
message.put("messageText", "Added from earlier cart");
|
| 21378 |
kshitij.so |
182 |
messagesArray.put(message);
|
|
|
183 |
cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
}
|
| 24422 |
amit.gupta |
187 |
Item item = itemRepository.selectById(itemObj.getInt("itemId"));
|
| 25962 |
amit.gupta |
188 |
if (!brandQtyMap.containsKey(item.getBrand())) {
|
| 24422 |
amit.gupta |
189 |
brandQtyMap.put(item.getBrand(), 0);
|
|
|
190 |
}
|
|
|
191 |
brandQtyMap.put(item.getBrand(), brandQtyMap.get(item.getBrand()) + itemObj.getInt("quantity"));
|
| 23360 |
amit.gupta |
192 |
try {
|
| 24199 |
amit.gupta |
193 |
ProductPojo pp = contentPojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
|
| 21378 |
kshitij.so |
194 |
|
| 24199 |
amit.gupta |
195 |
if (itemObj.has("estimate")) {
|
|
|
196 |
if (allSame) {
|
|
|
197 |
allSame = maxEstimate == -2 || maxEstimate == itemObj.getInt("estimate");
|
|
|
198 |
}
|
|
|
199 |
if (itemObj.getInt("estimate") > maxEstimate) {
|
|
|
200 |
maxEstimate = itemObj.getInt("estimate");
|
|
|
201 |
}
|
| 21378 |
kshitij.so |
202 |
}
|
| 23360 |
amit.gupta |
203 |
itemObj.put("imageUrl", pp.getImageUrl());
|
|
|
204 |
itemObj.put("title", pp.getTitle());
|
|
|
205 |
} catch (Throwable t) {
|
|
|
206 |
itemObj.put("imageUrl", "");
|
|
|
207 |
itemObj.put("title", item.getBrand() + " " + item.getModelName() + "" + item.getModelNumber());
|
|
|
208 |
}
|
| 21378 |
kshitij.so |
209 |
}
|
|
|
210 |
|
| 24199 |
amit.gupta |
211 |
ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
|
|
|
212 |
if (arr != null) {
|
|
|
213 |
for (int i = 0; i < arr.length(); i++) {
|
| 21378 |
kshitij.so |
214 |
JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
|
| 24199 |
amit.gupta |
215 |
if (itemMessages != null && itemMessages.length() > 0) {
|
|
|
216 |
listdata.add(0, arr.getJSONObject(i));
|
| 21378 |
kshitij.so |
217 |
} else {
|
|
|
218 |
listdata.add(arr.getJSONObject(i));
|
|
|
219 |
}
|
| 24199 |
amit.gupta |
220 |
}
|
| 21378 |
kshitij.so |
221 |
}
|
|
|
222 |
arr = new JSONArray();
|
| 24199 |
amit.gupta |
223 |
for (int i = 0; i < listdata.size(); i++) {
|
| 21378 |
kshitij.so |
224 |
arr.put(listdata.get(i));
|
|
|
225 |
}
|
| 24426 |
amit.gupta |
226 |
|
|
|
227 |
cartObj.put("cartItems", arr);
|
|
|
228 |
JSONArray cartMessagesArray = new JSONArray();
|
| 25962 |
amit.gupta |
229 |
for (Map.Entry<String, Integer> minBrandQtyLimit : MIN_BRAND_QTY_LIMIT.entrySet()) {
|
|
|
230 |
if (brandQtyMap.containsKey(minBrandQtyLimit.getKey())) {
|
|
|
231 |
if (brandQtyMap.get(minBrandQtyLimit.getKey()) < minBrandQtyLimit.getValue()) {
|
|
|
232 |
cartMessagesArray
|
|
|
233 |
.put(new JSONObject()
|
|
|
234 |
.put("messageText",
|
|
|
235 |
String.format("Minimum qty for %s brand should be %d",
|
|
|
236 |
minBrandQtyLimit.getKey(), minBrandQtyLimit.getValue()))
|
|
|
237 |
.put("type", "warn"));
|
| 24422 |
amit.gupta |
238 |
}
|
|
|
239 |
}
|
|
|
240 |
}
|
| 25962 |
amit.gupta |
241 |
|
| 24199 |
amit.gupta |
242 |
for (String message : Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged",
|
|
|
243 |
"cartMessagesMerged")) {
|
| 21378 |
kshitij.so |
244 |
int count = cartObj.getInt(message);
|
| 24199 |
amit.gupta |
245 |
if (count > 0) {
|
|
|
246 |
String type = "danger";
|
| 21378 |
kshitij.so |
247 |
if (message.equals("cartMessagesMerged")) {
|
|
|
248 |
type = "info";
|
| 24199 |
amit.gupta |
249 |
if (count == 1) {
|
|
|
250 |
cartMessage.put("messageText", "1 item is added from earlier cart");
|
|
|
251 |
} else {
|
|
|
252 |
cartMessage.put("messageText", "Few items are added from earlier cart");
|
| 21378 |
kshitij.so |
253 |
}
|
|
|
254 |
} else if (message.equals("cartMessageOOS")) {
|
| 24199 |
amit.gupta |
255 |
if (count == 1) {
|
|
|
256 |
cartMessage.put("messageText", "One item is currently Out of Stock");
|
|
|
257 |
} else {
|
|
|
258 |
cartMessage.put("messageText", "Few items are currently Out of Stock");
|
| 21378 |
kshitij.so |
259 |
}
|
|
|
260 |
} else if (message.equals("cartMessageUndeliverable")) {
|
| 24199 |
amit.gupta |
261 |
if (count == 1) {
|
|
|
262 |
cartMessage.put("messageText", "One item is undeliverable");
|
|
|
263 |
} else {
|
|
|
264 |
cartMessage.put("messageText", "Few items are underiverable");
|
| 21378 |
kshitij.so |
265 |
}
|
| 25962 |
amit.gupta |
266 |
} else if (message.equals("cartMessageChanged")) {
|
| 24199 |
amit.gupta |
267 |
if (count == 1) {
|
|
|
268 |
cartMessage.put("messageText", "One item qty has changed");
|
|
|
269 |
} else {
|
|
|
270 |
cartMessage.put("messageText", "Few items qty have changed");
|
| 21378 |
kshitij.so |
271 |
}
|
|
|
272 |
}
|
| 24199 |
amit.gupta |
273 |
cartMessage.put("type", type);
|
| 21378 |
kshitij.so |
274 |
cartMessagesArray.put(cartMessage);
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
cartObj.put("cartMessages", cartMessagesArray);
|
| 25962 |
amit.gupta |
278 |
// Hardcode maxEstimate to -3 for managing brands quantity
|
| 24199 |
amit.gupta |
279 |
if (maxEstimate == -1) {
|
| 21378 |
kshitij.so |
280 |
cartObj.put("estimateString", "Can't ship here");
|
| 24199 |
amit.gupta |
281 |
} else if (maxEstimate == -2) {
|
| 21378 |
kshitij.so |
282 |
cartObj.put("estimateString", "Out of Stock");
|
|
|
283 |
} else {
|
|
|
284 |
try {
|
| 21452 |
amit.gupta |
285 |
cartObj.put("cod", cartObj.getBoolean("codAllowed"));
|
| 24199 |
amit.gupta |
286 |
cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate, DeliveryType.COD));
|
| 21378 |
kshitij.so |
287 |
} catch (NumberFormatException | JSONException | TException e) {
|
|
|
288 |
// TODO Auto-generated catch block
|
|
|
289 |
e.printStackTrace();
|
| 24199 |
amit.gupta |
290 |
vc = new ValidateCartResponse(null, "Error", "Problem occurred while getting delivery estimates");
|
| 23021 |
ashik.ali |
291 |
return responseSender.internalServerError(vc);
|
| 21378 |
kshitij.so |
292 |
}
|
| 24199 |
amit.gupta |
293 |
|
| 21378 |
kshitij.so |
294 |
}
|
|
|
295 |
cartObj.put("maxEstimate", maxEstimate);
|
|
|
296 |
CartResponse cartResponse = new Gson().fromJson(cartObj.toString(), CartResponse.class);
|
| 24199 |
amit.gupta |
297 |
vc = new ValidateCartResponse(cartResponse, "Success", "Items added to cart successfully");
|
| 23021 |
ashik.ali |
298 |
return responseSender.ok(vc);
|
| 21378 |
kshitij.so |
299 |
}
|
| 21393 |
amit.gupta |
300 |
|
| 24199 |
amit.gupta |
301 |
@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21393 |
amit.gupta |
302 |
@ApiImplicitParams({
|
| 24199 |
amit.gupta |
303 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
304 |
|
| 21393 |
amit.gupta |
305 |
@ApiOperation(value = "Change address")
|
| 24199 |
amit.gupta |
306 |
public ResponseEntity<?> changeAddress(HttpServletRequest request,
|
|
|
307 |
@RequestParam(value = "addressId") long addressId) throws Throwable {
|
|
|
308 |
UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
|
|
|
309 |
UserContextService.Client userClient = new UserClient().getClient();
|
|
|
310 |
userClient.addAddressToCart(uc.getCartId(), addressId);
|
|
|
311 |
return responseSender.ok("Address Changed successfully");
|
|
|
312 |
}
|
| 21378 |
kshitij.so |
313 |
}
|