Subversion Repositories SmartDukaan

Rev

Rev 23568 | Rev 23942 | 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;
4
 
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;
23
 
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22609 ashik.ali 26
import com.spice.profitmandi.common.web.util.ResponseSender;
23936 tejbeer 27
import com.spice.profitmandi.dao.entity.dtr.User;
28
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
29
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
30
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
31
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
32
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
22555 amit.gupta 33
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
22605 ashik.ali 34
import com.spice.profitmandi.service.wallet.WalletService;
23936 tejbeer 35
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
21532 kshitij.so 36
import io.swagger.annotations.ApiImplicitParam;
37
import io.swagger.annotations.ApiImplicitParams;
38
import io.swagger.annotations.ApiOperation;
39
 
40
@Controller
23936 tejbeer 41
@Transactional(rollbackFor = Throwable.class)
21532 kshitij.so 42
public class WalletController {
43
 
23936 tejbeer 44
	private static final Logger log = LogManager.getLogger(WalletController.class);
21532 kshitij.so 45
 
46
	@Autowired
23037 ashik.ali 47
	UserWalletRepository userWalletRepository;
23936 tejbeer 48
 
21532 kshitij.so 49
	@Autowired
23936 tejbeer 50
	AddWalletRequestRepository addWalletRequestRepository;
51
 
52
	@Autowired
23037 ashik.ali 53
	WalletService walletService;
23936 tejbeer 54
 
23037 ashik.ali 55
	@Autowired
23936 tejbeer 56
	JavaMailSender mailSender;
57
 
58
	@Autowired
59
	@Qualifier("userRepository")
60
	private UserRepository userRepository;
61
 
62
	@Autowired
63
	private UserAccountRepository userAccountRepository;
64
 
65
	@Autowired
23037 ashik.ali 66
	ResponseSender<?> responseSender;
67
 
23936 tejbeer 68
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 69
	@ApiImplicitParams({
23936 tejbeer 70
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
21532 kshitij.so 71
	@ApiOperation(value = "")
23936 tejbeer 72
	public ResponseEntity<?> getMyWallet(HttpServletRequest request) throws ProfitMandiBusinessException {
73
		int userId = (int) request.getAttribute("userId");
74
		try {
23037 ashik.ali 75
			return responseSender.ok(walletService.getUserWalletByUserId(userId));
23936 tejbeer 76
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 77
			return responseSender.badRequest(profitMandiBusinessException);
78
		}
21532 kshitij.so 79
	}
80
 
23936 tejbeer 81
	@RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
21532 kshitij.so 82
	@ApiImplicitParams({
23936 tejbeer 83
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
84
	public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
85
		int userId = (int) request.getAttribute("userId");
86
		try {
23037 ashik.ali 87
			return responseSender.ok(walletService.getUserWalletHistoryByUserId(userId));
23936 tejbeer 88
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23037 ashik.ali 89
			return responseSender.badRequest(profitMandiBusinessException);
90
		}
21532 kshitij.so 91
	}
92
 
23936 tejbeer 93
	@RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
94
	@ApiImplicitParams({
95
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
96
	public ResponseEntity<?> AddMoneyToWallet(HttpServletRequest request,
97
			@RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
98
		int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
99
		int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
100
		AddWalletRequest addWalletrequest = new AddWalletRequest();
101
		addWalletrequest.setRetailerId(retailerId);
102
		addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
103
		addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
104
		addWalletrequest.setStatus(AddWalletRequestStatus.pending);
105
		addWalletRequestRepository.persist(addWalletrequest);
106
		String subject = "Add money to wallet request";
107
		String messageText = MessageFormat.format("User Id - {0}\n Reference - {1}\n Amount - Rs.{2} \n link - {3}",
108
				new Integer(addWalletrequest.getRetailerId()), addWalletrequest.getTransaction_reference(),
109
				new Float(addWalletrequest.getAmount()),"http://partners.smartdukaan.com/dashboard");
110
		String email = "neeraj.gupta@smartdukaan.com";
111
		this.sendMailWithAttachments(email, subject, messageText);
112
 
113
		return responseSender.ok(true);
114
	}
115
 
116
	private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
117
		log.info("message" + messageText);
118
		MimeMessage message = mailSender.createMimeMessage();
119
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
120
		helper.setSubject(subject);
121
		helper.setText(messageText);
122
		helper.setTo(email);
123
		InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
124
		helper.setFrom(senderAddress);
125
		mailSender.send(message);
126
 
127
	}
128
 
129
 
21532 kshitij.so 130
}