Subversion Repositories SmartDukaan

Rev

Rev 24867 | Rev 28257 | 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 java.util.List;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
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.user.RetailerService;
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
        private RetailerService retailerService;

        @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");
                return responseSender.ok(walletService.getUserWalletByUserId(userId));
        }

        @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);
                log.info("user_id" + userId);
                int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
                CustomRetailer customRetailer = retailerService.getFofoRetailer(retailerId);
                AddWalletRequest addWalletrequest = new AddWalletRequest();
                addWalletrequest.setRetailerId(retailerId);
                addWalletrequest.setAmount(createAddMoneyRequest.getAmount());
                addWalletrequest.setTransaction_reference(createAddMoneyRequest.getTransaction_reference());
                addWalletrequest.setReference_date(createAddMoneyRequest.getReference_date());
                addWalletrequest.setBank_name(createAddMoneyRequest.getBank_name());
                addWalletrequest.setStatus(AddWalletRequestStatus.pending);
                log.info("info" + addWalletrequest);
                addWalletRequestRepository.persist(addWalletrequest);
                log.info("fofoinfo" + customRetailer);
                String subject = "Add money to wallet request";
                String messageText = MessageFormat.format(
                                "User Id - {0}\n Name -{1}\n Email -{2}\n Reference - {3}\n Amount - Rs.{4} \n link - {5}",
                                new Integer(addWalletrequest.getRetailerId()), customRetailer.getBusinessName(),
                                customRetailer.getEmail(), 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);
                // helper.setCc("neerajgupta2021@gmail.com");
                InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "ProfitMandi Admin");
                helper.setFrom(senderAddress);
                mailSender.send(message);

        }

        @RequestMapping(value = ProfitMandiConstants.URL_ADD_MONEY_TO_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<?> getAddMoneyToWalletHistory(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
                int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
                int retailerId = userAccountRepository.selectRetailerIdByUserId(userId);
                List<AddWalletRequest> walletRequest = addWalletRequestRepository.selectByRetailerId(offset, limit, retailerId);
                return responseSender.ok(walletRequest);
        }

}