Subversion Repositories SmartDukaan

Rev

Rev 23942 | Rev 23985 | 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
 
23936 tejbeer 3
import java.text.MessageFormat;
23942 tejbeer 4
import java.util.List;
23936 tejbeer 5
 
6
import javax.mail.internet.InternetAddress;
7
import javax.mail.internet.MimeMessage;
21532 kshitij.so 8
import javax.servlet.http.HttpServletRequest;
9
 
23568 govind 10
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.LogManager;
21532 kshitij.so 12
import org.springframework.beans.factory.annotation.Autowired;
23936 tejbeer 13
import org.springframework.beans.factory.annotation.Qualifier;
21532 kshitij.so 14
import org.springframework.http.MediaType;
15
import org.springframework.http.ResponseEntity;
23936 tejbeer 16
import org.springframework.mail.javamail.JavaMailSender;
17
import org.springframework.mail.javamail.MimeMessageHelper;
21532 kshitij.so 18
import org.springframework.stereotype.Controller;
21702 ashik.ali 19
import org.springframework.transaction.annotation.Transactional;
23936 tejbeer 20
import org.springframework.web.bind.annotation.RequestBody;
21532 kshitij.so 21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
23942 tejbeer 23
import org.springframework.web.bind.annotation.RequestParam;
21532 kshitij.so 24
 
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23942 tejbeer 26
import com.spice.profitmandi.common.model.CustomRetailer;
21532 kshitij.so 27
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22609 ashik.ali 28
import com.spice.profitmandi.common.web.util.ResponseSender;
23936 tejbeer 29
import com.spice.profitmandi.dao.entity.dtr.User;
30
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
31
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
32
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
33
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
22555 amit.gupta 35
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
23942 tejbeer 36
import com.spice.profitmandi.service.user.RetailerService;
22605 ashik.ali 37
import com.spice.profitmandi.service.wallet.WalletService;
23936 tejbeer 38
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
21532 kshitij.so 39
import io.swagger.annotations.ApiImplicitParam;
40
import io.swagger.annotations.ApiImplicitParams;
41
import io.swagger.annotations.ApiOperation;
42
 
43
@Controller
23936 tejbeer 44
@Transactional(rollbackFor = Throwable.class)
21532 kshitij.so 45
public class WalletController {
46
 
23936 tejbeer 47
	private static final Logger log = LogManager.getLogger(WalletController.class);
21532 kshitij.so 48
 
49
	@Autowired
23037 ashik.ali 50
	UserWalletRepository userWalletRepository;
23936 tejbeer 51
 
21532 kshitij.so 52
	@Autowired
23936 tejbeer 53
	AddWalletRequestRepository addWalletRequestRepository;
54
 
55
	@Autowired
23037 ashik.ali 56
	WalletService walletService;
23936 tejbeer 57
 
23037 ashik.ali 58
	@Autowired
23936 tejbeer 59
	JavaMailSender mailSender;
60
 
61
	@Autowired
62
	@Qualifier("userRepository")
63
	private UserRepository userRepository;
64
 
65
	@Autowired
66
	private UserAccountRepository userAccountRepository;
23942 tejbeer 67
 
68
	@Autowired
69
	private RetailerService retailerService;
23936 tejbeer 70
 
23942 tejbeer 71
 
23936 tejbeer 72
	@Autowired
23037 ashik.ali 73
	ResponseSender<?> responseSender;
74
 
23936 tejbeer 75
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 76
	@ApiImplicitParams({
23936 tejbeer 77
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21532 kshitij.so 78
	@ApiOperation(value = "")
23936 tejbeer 79
	public ResponseEntity<?> getMyWallet(HttpServletRequest request) throws ProfitMandiBusinessException {
80
		int userId = (int) request.getAttribute("userId");
81
		try {
23037 ashik.ali 82
			return responseSender.ok(walletService.getUserWalletByUserId(userId));
23936 tejbeer 83
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 84
			return responseSender.badRequest(profitMandiBusinessException);
85
		}
21532 kshitij.so 86
	}
87
 
23936 tejbeer 88
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 89
	@ApiImplicitParams({
23936 tejbeer 90
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
91
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
92
		int userId = (int) request.getAttribute("userId");
93
		try {
23037 ashik.ali 94
			return responseSender.ok(walletService.getUserWalletHistoryByUserId(userId));
23936 tejbeer 95
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 96
			return responseSender.badRequest(profitMandiBusinessException);
97
		}
21532 kshitij.so 98
	}
99
 
23936 tejbeer 100
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
101
	@ApiImplicitParams({
102
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
103
	public ResponseEntity<?> AddMoneyToWallet(HttpServletRequest request,
104
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
105
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
106
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
23942 tejbeer 107
		CustomRetailer customRetailer=retailerService.getFofoRetailer(retailerId);
23936 tejbeer 108
		AddWalletRequest addWalletrequest = new AddWalletRequest();
109
		addWalletrequest.setRetailerId(retailerId);
110
		addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
111
		addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
112
		addWalletrequest.setStatus(AddWalletRequestStatus.pending);
113
		addWalletRequestRepository.persist(addWalletrequest);
23942 tejbeer 114
		log.info("fofoinfo" + customRetailer);
23936 tejbeer 115
		String subject = "Add money to wallet request";
23942 tejbeer 116
		String messageText = MessageFormat.format("User Id - {0}\n Name -{1}\n Email -{2}\n Reference - {3}\n Amount - Rs.{4} \n link - {5}",
117
				new Integer(addWalletrequest.getRetailerId()),customRetailer.getBusinessName(),customRetailer.getEmail(), addWalletrequest.getTransaction_reference(),
23936 tejbeer 118
				new Float(addWalletrequest.getAmount()),"http://partners.smartdukaan.com/dashboard");
23943 tejbeer 119
		String email = "neeraj.gupta@smartdukaan.com";
23936 tejbeer 120
		this.sendMailWithAttachments(email, subject, messageText);
121
 
122
		return responseSender.ok(true);
123
	}
124
 
125
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
126
		log.info("message" + messageText);
127
		MimeMessage message = mailSender.createMimeMessage();
128
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
129
		helper.setSubject(subject);
130
		helper.setText(messageText);
131
		helper.setTo(email);
132
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
133
		helper.setFrom(senderAddress);
134
		mailSender.send(message);
135
 
136
	}
137
 
138
 
23942 tejbeer 139
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
140
	@ApiImplicitParams({
141
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
142
	public ResponseEntity<?> getAddMoneyToWalletHistory(HttpServletRequest request,@RequestParam(name = "offset", defaultValue = "0") int offset,
143
			@RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
144
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
145
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
146
		List<AddWalletRequest> walletRequest= addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
147
		return  responseSender.ok(walletRequest);
148
	}
149
 
150
 
21532 kshitij.so 151
}