Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21532 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.time.ZoneId;
5
import java.util.ArrayList;
6
import java.util.List;
7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.HttpStatus;
14
import org.springframework.http.MediaType;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.stereotype.Controller;
21702 ashik.ali 17
import org.springframework.transaction.annotation.Transactional;
21532 kshitij.so 18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
 
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 23
import com.spice.profitmandi.common.model.ProfitMandiResponse;
24
import com.spice.profitmandi.common.model.ResponseStatus;
21735 ashik.ali 25
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
26
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
27
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
28
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
29
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
22555 amit.gupta 30
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
31
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
21532 kshitij.so 32
import com.spice.profitmandi.web.res.UserWalletHistoryResponse;
33
import com.spice.profitmandi.web.res.UserWalletResponse;
34
 
35
import io.swagger.annotations.ApiImplicitParam;
36
import io.swagger.annotations.ApiImplicitParams;
37
import io.swagger.annotations.ApiOperation;
38
 
39
@Controller
22037 amit.gupta 40
@Transactional(rollbackFor=Throwable.class)
21532 kshitij.so 41
public class WalletController {
42
 
43
	private static final Logger log=LoggerFactory.getLogger(WalletController.class);
44
 
45
	@Autowired
22555 amit.gupta 46
	UserWalletRepository userWalletRepository;
21532 kshitij.so 47
 
48
	@Autowired
22555 amit.gupta 49
	UserWalletHistoryRepository userWalletHistoryRepository;
50
 
51
	@Autowired
21532 kshitij.so 52
	UserAccountRepository userAccountRepository;
53
 
54
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
55
	@ApiImplicitParams({
56
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
57
				required = true, dataType = "string", paramType = "header")
58
	})
59
	@ApiOperation(value = "")
60
	public ResponseEntity<?> getMyWallet(HttpServletRequest request){
61
		int userId = (int)request.getAttribute("userId");
62
		UserAccounts userAccount;
63
		UserWallet wallet=null;
64
		try {
65
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
66
		} catch (ProfitMandiBusinessException e) {
67
			log.error("Unable to get user account ",e);
68
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
69
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
70
		}
71
 
72
		try {
22555 amit.gupta 73
			wallet = userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccount_key()));
21532 kshitij.so 74
			log.info("wallet "+wallet);
75
		} catch (NumberFormatException | ProfitMandiBusinessException e) {
76
			log.error("Unable to get userwallet ",e);
77
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
78
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
79
		}
80
		if (wallet == null){
81
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new UserWalletResponse());
82
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
83
		}
84
 
85
		UserWalletResponse uwr = new UserWalletResponse();
86
		uwr.setAmount(wallet.getAmount());
22555 amit.gupta 87
		uwr.setRefundableAmount(wallet.getRefundableAmount());
21532 kshitij.so 88
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, uwr);
89
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
90
 
91
	}
92
 
93
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
94
	@ApiImplicitParams({
95
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
96
				required = true, dataType = "string", paramType = "header")
97
	})
98
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request){
99
		int userId = (int)request.getAttribute("userId");
100
		UserAccounts userAccount;
101
		List<UserWalletHistory> walletHistory;
102
		try {
103
			userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
104
		} catch (ProfitMandiBusinessException e) {
105
			log.error("Unable to get user account ",e);
106
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
107
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
108
		}
109
 
110
		try {
22555 amit.gupta 111
			UserWallet uw = userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccount_key()));
112
			walletHistory = userWalletHistoryRepository.selectByWalletId(uw.getId());
21532 kshitij.so 113
		} catch (NumberFormatException | ProfitMandiBusinessException e) {
114
			log.error("Unable to get wallet history ",e);
115
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, new ArrayList<UserWalletHistoryResponse>());
116
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
117
		}
118
 
119
		ArrayList<UserWalletHistoryResponse> walletHistoryResponse = new ArrayList<UserWalletHistoryResponse>();
120
 
121
		for (UserWalletHistory uwh : walletHistory){
122
			UserWalletHistoryResponse uwhr = new UserWalletHistoryResponse();
123
			uwhr.setAmount(uwh.getAmount());
124
			uwhr.setReference(uwh.getReference());
125
			try{
22555 amit.gupta 126
				uwhr.setReference_type(uwh.getReferenceType().toString());
21532 kshitij.so 127
			}
128
			catch(Exception e){
129
				uwhr.setReference_type("");
130
			}
131
			uwhr.setTimestamp(uwh.getTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
132
			uwhr.setDescription(uwh.getDescription());
133
			walletHistoryResponse.add(uwhr);
134
		}
135
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, walletHistoryResponse);
136
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
137
	}
138
 
139
}