Subversion Repositories SmartDukaan

Rev

Rev 28754 | Rev 29327 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21378 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
28742 tejbeer 3
import java.time.LocalDateTime;
21378 kshitij.so 4
import java.util.ArrayList;
28742 tejbeer 5
import java.util.Arrays;
24422 amit.gupta 6
import java.util.HashMap;
21378 kshitij.so 7
import java.util.List;
24422 amit.gupta 8
import java.util.Map;
28742 tejbeer 9
import java.util.Set;
29325 tejbeer 10
import java.util.stream.Collectors;
21378 kshitij.so 11
 
12
import javax.servlet.http.HttpServletRequest;
13
 
23959 amit.gupta 14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
21378 kshitij.so 16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
19
import org.springframework.stereotype.Controller;
21707 amit.gupta 20
import org.springframework.transaction.annotation.Transactional;
28742 tejbeer 21
import org.springframework.ui.Model;
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
 
28742 tejbeer 27
import com.spice.profitmandi.common.enumuration.MessageType;
28
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29
import com.spice.profitmandi.common.model.CustomRetailer;
21378 kshitij.so 30
import com.spice.profitmandi.common.model.ProfitMandiConstants;
28742 tejbeer 31
import com.spice.profitmandi.common.model.SendNotificationModel;
21741 ashik.ali 32
import com.spice.profitmandi.common.web.util.ResponseSender;
28653 amit.gupta 33
import com.spice.profitmandi.dao.cart.CartService;
28742 tejbeer 34
import com.spice.profitmandi.dao.entity.auth.AuthUser;
35
import com.spice.profitmandi.dao.entity.catalog.FocusedModelByPassRequest;
36
import com.spice.profitmandi.dao.entity.cs.Position;
37
import com.spice.profitmandi.dao.entity.dtr.User;
23273 ashik.ali 38
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
28742 tejbeer 39
import com.spice.profitmandi.dao.enumuration.catalog.ByPassRequestStatus;
40
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
21735 ashik.ali 41
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
26607 amit.gupta 42
import com.spice.profitmandi.dao.model.AddCartRequest;
43
import com.spice.profitmandi.dao.model.CartItem;
28653 amit.gupta 44
import com.spice.profitmandi.dao.model.CartItemResponseModel;
26607 amit.gupta 45
import com.spice.profitmandi.dao.model.CartResponse;
21378 kshitij.so 46
import com.spice.profitmandi.dao.model.ProductPojo;
21738 amit.gupta 47
import com.spice.profitmandi.dao.model.UserCart;
28742 tejbeer 48
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
49
import com.spice.profitmandi.dao.repository.catalog.FocusedModelByPassRepository;
23360 amit.gupta 50
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
28742 tejbeer 51
import com.spice.profitmandi.dao.repository.cs.CsService;
52
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
21735 ashik.ali 53
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
28742 tejbeer 54
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
21643 ashik.ali 55
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
28742 tejbeer 56
import com.spice.profitmandi.service.FofoUser;
57
import com.spice.profitmandi.service.NotificationService;
58
import com.spice.profitmandi.service.PartnerInvestmentService;
59
import com.spice.profitmandi.service.inventory.ByPassRequestModel;
25962 amit.gupta 60
import com.spice.profitmandi.service.inventory.ItemBucketService;
28742 tejbeer 61
import com.spice.profitmandi.service.user.RetailerService;
21378 kshitij.so 62
import com.spice.profitmandi.thrift.clients.UserClient;
63
import com.spice.profitmandi.web.res.ValidateCartResponse;
64
 
65
import in.shop2020.model.v1.user.ItemQuantity;
21393 amit.gupta 66
import in.shop2020.model.v1.user.UserContextService;
21378 kshitij.so 67
import io.swagger.annotations.ApiImplicitParam;
68
import io.swagger.annotations.ApiImplicitParams;
69
import io.swagger.annotations.ApiOperation;
70
 
71
@Controller
24199 amit.gupta 72
@Transactional(rollbackFor = Throwable.class)
21378 kshitij.so 73
public class CartController {
74
 
24199 amit.gupta 75
	private static final Logger logger = LogManager.getLogger(CartController.class);
76
 
21440 ashik.ali 77
	@Autowired
22931 ashik.ali 78
	private ResponseSender<?> responseSender;
21378 kshitij.so 79
 
80
	@Autowired
22931 ashik.ali 81
	private UserAccountRepository userAccountRepository;
24199 amit.gupta 82
 
22173 amit.gupta 83
	@Autowired
25962 amit.gupta 84
	private ItemBucketService itemBucketService;
85
 
86
	@Autowired
28653 amit.gupta 87
	CartService cartService;
88
 
89
	@Autowired
22931 ashik.ali 90
	private ContentPojoPopulator contentPojoPopulator;
24199 amit.gupta 91
 
23360 amit.gupta 92
	@Autowired
93
	private ItemRepository itemRepository;
21378 kshitij.so 94
 
28742 tejbeer 95
	@Autowired
96
	private UserRepository userRepository;
97
 
98
	@Autowired
99
	private AuthRepository authRepository;
100
 
101
	@Autowired
102
	private CsService csService;
103
 
104
	@Autowired
105
	private PositionRepository positionRepository;
106
 
107
	@Autowired
108
	private FocusedModelByPassRepository focusedModelByPassRepository;
109
 
110
	@Autowired
111
	private RetailerService retailerService;
112
 
113
	@Autowired
114
	private PartnerInvestmentService partnerInvestmentService;
115
 
116
	@Autowired
117
	private FofoUser fofoUser;
28743 tejbeer 118
 
28742 tejbeer 119
	@Autowired
120
	private NotificationService notificationService;
121
 
24422 amit.gupta 122
	public static final Map<String, Integer> MIN_BRAND_QTY_LIMIT = new HashMap<>();
123
 
28746 tejbeer 124
	List<EscalationType> esclationType = Arrays.asList(EscalationType.L3, EscalationType.L4, EscalationType.L2);
28742 tejbeer 125
 
29325 tejbeer 126
	List<String> emails = Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com",
127
			"niranjan.kala@smartdukaan.com", "sm@smartdukaan.com");
28742 tejbeer 128
 
24422 amit.gupta 129
	static {
25962 amit.gupta 130
		// MIN_BRAND_QTY_LIMIT.put("Realme", 10);
24464 amit.gupta 131
		MIN_BRAND_QTY_LIMIT.put("Reliance", 5);
24422 amit.gupta 132
	}
133
 
25962 amit.gupta 134
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
135
	@ApiImplicitParams({
136
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
137
	@ApiOperation(value = "Add items to cart")
138
	public ResponseEntity<?> validateCart(HttpServletRequest request,
139
			@RequestParam(value = "pincode", defaultValue = "110001") String pincode, @RequestParam int bucketId)
140
			throws Throwable {
141
 
142
		AddCartRequest cartRequest = new AddCartRequest();
26607 amit.gupta 143
		List<CartItem> ci = new ArrayList<>();
25962 amit.gupta 144
		itemBucketService.getBucketDetails(bucketId).stream().forEach(x -> {
28653 amit.gupta 145
			ci.add(new CartItem(x.getQuantity(), x.getItemId()));
25962 amit.gupta 146
		});
26607 amit.gupta 147
		cartRequest.setCartItems(ci);
25962 amit.gupta 148
		return this.validateCart(request, cartRequest, "110001");
149
 
150
	}
151
 
24199 amit.gupta 152
	@RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21378 kshitij.so 153
	@ApiImplicitParams({
24199 amit.gupta 154
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21378 kshitij.so 155
	@ApiOperation(value = "Add items to cart")
24199 amit.gupta 156
	public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
157
			@RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
23273 ashik.ali 158
		UserAccount userAccount = null;
21378 kshitij.so 159
		ValidateCartResponse vc = null;
24199 amit.gupta 160
		int userId = (int) request.getAttribute("userId");
161
 
28653 amit.gupta 162
		int cartId = userAccountRepository.selectByUserIdType(userId, AccountType.cartId).getAccountKey();
26607 amit.gupta 163
		List<CartItem> cartItems = cartRequest.getCartItems();
28653 amit.gupta 164
		cartService.addItemsToCart(cartId, cartItems);
165
		CartResponse cartValidationResponse = cartService.getCartValidation(cartId);
166
		for (CartItemResponseModel cartItem : cartValidationResponse.getCartItems()) {
167
			ProductPojo pp = contentPojoPopulator.getShortContent(cartItem.getCatalogItemId());
28742 tejbeer 168
			if (pp != null) {
28653 amit.gupta 169
				cartItem.setImageUrl(pp.getImageUrl());
170
				cartItem.setTitle(pp.getTitle());
24153 amit.gupta 171
			}
172
		}
28653 amit.gupta 173
		vc = new ValidateCartResponse(cartValidationResponse, "Success", "Items added to cart successfully");
23021 ashik.ali 174
		return responseSender.ok(vc);
21378 kshitij.so 175
	}
21393 amit.gupta 176
 
24199 amit.gupta 177
	@RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
21393 amit.gupta 178
	@ApiImplicitParams({
24199 amit.gupta 179
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
180
 
21393 amit.gupta 181
	@ApiOperation(value = "Change address")
24199 amit.gupta 182
	public ResponseEntity<?> changeAddress(HttpServletRequest request,
183
			@RequestParam(value = "addressId") long addressId) throws Throwable {
184
		UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
28653 amit.gupta 185
		cartService.addAddressToCart(uc.getCartId(), addressId);
24199 amit.gupta 186
		return responseSender.ok("Address Changed successfully");
187
	}
28742 tejbeer 188
 
29325 tejbeer 189
	@RequestMapping(value = "/byPassRequests", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
28742 tejbeer 190
	@ApiImplicitParams({
191
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
192
 
29325 tejbeer 193
	@ApiOperation(value = "byPassRequests")
194
	public ResponseEntity<?> byPassRequests(HttpServletRequest request, Model model) throws Exception {
28742 tejbeer 195
 
196
		int userId = (int) request.getAttribute("userId");
197
 
28743 tejbeer 198
		User user = userRepository.selectById(userId);
28742 tejbeer 199
		AuthUser authUser = authRepository.selectByEmailOrMobile(user.getEmailId());
29325 tejbeer 200
 
28742 tejbeer 201
		List<FocusedModelByPassRequest> byPassRequests = null;
202
 
203
		Set<Integer> authfofoIds = null;
29325 tejbeer 204
		boolean actionAccess = false;
205
		logger.info("authUser" + authUser);
28742 tejbeer 206
 
29325 tejbeer 207
		List<Position> positions = positionRepository.selectPositionByAuthId(authUser.getId()).stream()
208
				.filter(x -> (x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_SALES
209
						|| x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_RBM))
210
				.collect(Collectors.toList());
28742 tejbeer 211
 
29325 tejbeer 212
		Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
213
 
28742 tejbeer 214
		if (emails.contains(authUser.getEmailId())) {
29325 tejbeer 215
			byPassRequests = focusedModelByPassRepository.selectByStatus(ByPassRequestStatus.PENDING);
216
			actionAccess = true;
28742 tejbeer 217
		} else {
29325 tejbeer 218
			if (!positions.isEmpty()) {
219
				for (Position ps : positions) {
220
					if (ProfitMandiConstants.TICKET_CATEGORY_RBM == ps.getCategoryId()) {
221
						authfofoIds = storeGuyMap.get(user.getEmailId());
222
						logger.info("authfofoIdsw" + authfofoIds);
223
						actionAccess = true;
224
						break;
225
					} else if (ProfitMandiConstants.TICKET_CATEGORY_SALES == ps.getCategoryId()) {
226
						authfofoIds = storeGuyMap.get(user.getEmailId());
227
						logger.info("authfofoIdsw" + authfofoIds);
228
						actionAccess = false;
229
						break;
230
					}
28742 tejbeer 231
 
232
				}
233
			}
234
		}
235
		logger.info("authfofoIds" + authfofoIds);
236
		if (authfofoIds != null) {
237
			byPassRequests = focusedModelByPassRepository.selectByStatusAndFofoIds(new ArrayList<>(authfofoIds),
238
					ByPassRequestStatus.PENDING);
239
		}
240
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
241
		List<ByPassRequestModel> byPassList = new ArrayList<>();
242
 
243
		for (FocusedModelByPassRequest byPassRequest : byPassRequests) {
244
			Map<String, Object> investments = fofoUser.getInvestments(byPassRequest.getFofoId());
245
			ByPassRequestModel bp = new ByPassRequestModel();
246
			bp.setPartnerName(customRetailerMap.get(byPassRequest.getFofoId()).getBusinessName());
247
			bp.setCity(customRetailerMap.get(byPassRequest.getFofoId()).getAddress().getCity());
248
			bp.setState(customRetailerMap.get(byPassRequest.getFofoId()).getAddress().getState());
249
			bp.setCode(customRetailerMap.get(byPassRequest.getFofoId()).getCode());
250
			bp.setStatus(byPassRequest.getStatus());
251
			bp.setFofoId(byPassRequest.getFofoId());
252
			bp.setByPassRequestId(byPassRequest.getId());
253
			bp.setInvestmentOkDays((Long) investments.get("okDays"));
254
			bp.setInvestmentShort((int) investments.get("short"));
255
			bp.setMobileNumber(customRetailerMap.get(byPassRequest.getFofoId()).getMobileNumber());
29325 tejbeer 256
			bp.setActionAccess(actionAccess);
28742 tejbeer 257
			bp.setCreateTimestamp(byPassRequest.getCreatedTimeStamp());
258
			bp.setUpdateTimestamp(byPassRequest.getUpdatedTimestamp());
259
			byPassList.add(bp);
260
 
261
		}
262
 
263
		return responseSender.ok(byPassList);
264
	}
265
 
266
	@RequestMapping(value = "/byPassRequestAction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
267
	@ApiImplicitParams({
268
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
269
	@ApiOperation(value = "Add items to cart")
270
	public ResponseEntity<?> addAmountToWalletRequestRejected(HttpServletRequest request,
271
			@RequestParam(name = "id", defaultValue = "0") int id, @RequestParam ByPassRequestStatus status,
272
			Model model) throws Exception {
29325 tejbeer 273
		int userId = (int) request.getAttribute("userId");
28742 tejbeer 274
 
29325 tejbeer 275
		User user = userRepository.selectById(userId);
276
		AuthUser authUser = authRepository.selectByEmailOrMobile(user.getEmailId());
277
 
28742 tejbeer 278
		FocusedModelByPassRequest byPassRequest = focusedModelByPassRepository.selectById(id);
279
		if (status.equals(ByPassRequestStatus.APPROVED)) {
280
			byPassRequest.setByPass(true);
29325 tejbeer 281
			byPassRequest.setAuthId(authUser.getId());
28742 tejbeer 282
			byPassRequest.setStatus(ByPassRequestStatus.APPROVED);
283
			byPassRequest.setUpdatedTimestamp(LocalDateTime.now());
284
		} else if (status.equals(ByPassRequestStatus.REJECTED)) {
285
			byPassRequest.setByPass(false);
29325 tejbeer 286
			byPassRequest.setAuthId(authUser.getId());
28742 tejbeer 287
			byPassRequest.setStatus(ByPassRequestStatus.REJECTED);
288
			byPassRequest.setUpdatedTimestamp(LocalDateTime.now());
289
		}
290
 
291
		SendNotificationModel sendNotificationModel = new SendNotificationModel();
292
		sendNotificationModel.setCampaignName("Billing Request");
293
		sendNotificationModel.setTitle("Billing Request");
294
		sendNotificationModel.setMessage(String.format("Your Billing  Request is " + " " + status
28745 tejbeer 295
				+ ". Please ensure to order the missing focus models as soon as possible"));
28742 tejbeer 296
		sendNotificationModel.setType("url");
297
		sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
298
		sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
299
		sendNotificationModel.setMessageType(MessageType.notification);
300
		sendNotificationModel
301
				.setUserIds(Arrays.asList(userAccountRepository.selectUserIdByRetailerId(byPassRequest.getFofoId())));
302
 
303
		notificationService.sendNotification(sendNotificationModel);
304
 
305
		return responseSender.ok(true);
306
	}
21378 kshitij.so 307
}