Subversion Repositories SmartDukaan

Rev

Rev 23568 | Rev 23942 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.text.MessageFormat;


import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.dtr.User;
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
import com.spice.profitmandi.service.wallet.WalletService;
import com.spice.profitmandi.web.req.CreateAddMoneyRequest;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class WalletController {

        private static final Logger log = LogManager.getLogger(WalletController.class);

        @Autowired
        UserWalletRepository userWalletRepository;

        @Autowired
        AddWalletRequestRepository addWalletRequestRepository;

        @Autowired
        WalletService walletService;

        @Autowired
        JavaMailSender mailSender;

        @Autowired
        @Qualifier("userRepository")
        private UserRepository userRepository;

        @Autowired
        private UserAccountRepository userAccountRepository;

        @Autowired
        ResponseSender<?> responseSender;

        @RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "")
        public ResponseEntity<?> getMyWallet(HttpServletRequest request) throws ProfitMandiBusinessException {
                int userId = (int) request.getAttribute("userId");
                try {
                        return responseSender.ok(walletService.getUserWalletByUserId(userId));
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }

        @RequestMapping(value = ProfitMandiConstants.URL_MY_WALLET_HISTORY, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        public ResponseEntity<?> getMyWalletHistory(HttpServletRequest request) throws ProfitMandiBusinessException {
                int userId = (int) request.getAttribute("userId");
                try {
                        return responseSender.ok(walletService.getUserWalletHistoryByUserId(userId));
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }

        @RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_WALLET, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        public ResponseEntity<?> AddMoneyToWallet(HttpServletRequest request,
                        @RequestBody CreateAddMoneyRequest createAddMoneyRequest) throws Exception {
                int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
                int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
                AddWalletRequest addWalletrequest = new AddWalletRequest();
                addWalletrequest.setRetailerId(retailerId);
                addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
                addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
                addWalletrequest.setStatus(AddWalletRequestStatus.pending);
                addWalletRequestRepository.persist(addWalletrequest);
                String subject = "Add money to wallet request";
                String messageText = MessageFormat.format("User Id - {0}\n Reference - {1}\n Amount - Rs.{2} \n link - {3}",
                                new Integer(addWalletrequest.getRetailerId()), addWalletrequest.getTransaction_reference(),
                                new Float(addWalletrequest.getAmount()),"http://partners.smartdukaan.com/dashboard");
                String email = "neeraj.gupta@smartdukaan.com";
                this.sendMailWithAttachments(email, subject, messageText);

                return responseSender.ok(true);
        }

        private void sendMailWithAttachments(String email, String subject, String messageText) throws Exception {
                log.info("message" + messageText);
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setSubject(subject);
                helper.setText(messageText);
                helper.setTo(email);
                InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
                helper.setFrom(senderAddress);
                mailSender.send(message);

        }
        
        
}