Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import javax.servlet.http.HttpServletRequest;

import com.spice.profitmandi.dao.model.FofoRBMReportingModel;
import com.spice.profitmandi.dao.model.FofoReportingModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
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.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.auth.AuthUser;
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.cs.CsService;
import com.spice.profitmandi.web.res.SupportTeamResponse;
import com.spice.profitmandi.web.res.SupportTeamResponse.PersonDetail;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

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

    private static final Logger LOGGER = LogManager.getLogger(SupportController.class);

    @Autowired
    private ResponseSender<?> responseSender;

    @Autowired
    private CsService csService;

    @Autowired
    private AuthRepository authRepository;

    @RequestMapping(value = "/support/team", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    @ApiOperation(value = "Get assigned ABM, BM, RBM and RBM Manager for a FOFO store")
    public ResponseEntity<?> getSupportTeam(HttpServletRequest request, @RequestParam(value = "fofoId") int fofoId) throws Throwable {
        LOGGER.info("Getting support team for fofoId: {}", fofoId);

        SupportTeamResponse response = new SupportTeamResponse();
        // ABM = SALES category, L1
        try {
            int abmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
            if (abmAuthUserId > 0) {
                AuthUser abmUser = authRepository.selectById(abmAuthUserId);
                if (abmUser != null) {
                    response.setAbm(toPersonDetail(abmUser, "Sales support and regional queries"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch ABM for fofoId: {}", fofoId, e);
        }

        // BM = SALES category, L2
        try {
            int bmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2, fofoId);
            if (bmAuthUserId > 0) {
                AuthUser bmUser = authRepository.selectById(bmAuthUserId);
                if (bmUser != null) {
                    response.setBm(toPersonDetail(bmUser, "Handles: Orders, Stock, Schemes"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch BM for fofoId: {}", fofoId, e);
        }

        // RBM = RBM category, L1
        try {
            int rbmAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
            if (rbmAuthUserId > 0) {
                AuthUser rbmUser = authRepository.selectById(rbmAuthUserId);
                if (rbmUser != null) {
                    response.setRbm(toPersonDetail(rbmUser, "First point of contact for all queries"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch RBM for fofoId: {}", fofoId, e);
        }

        // RBM Manager = RBM category, L2
        try {
            int rbmManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L2, fofoId);
            if (rbmManagerAuthUserId > 0) {
                AuthUser rbmManagerUser = authRepository.selectById(rbmManagerAuthUserId);
                if (rbmManagerUser != null) {
                    response.setRbmManager(toPersonDetail(rbmManagerUser,"For escalated regional matters\n" +
                            "\n"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch RBM Manager for fofoId: {}", fofoId, e);
        }

        // Grievance Manager = CATEGORY_LEGAL category, L3
        try {
            int grievanceManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_LEGAL, EscalationType.L3, fofoId);
            if (grievanceManagerAuthUserId > 0) {
                AuthUser grievanceManagerUser = authRepository.selectById(grievanceManagerAuthUserId);
                if (grievanceManagerUser != null) {
                    response.setGrievanceManager(toPersonDetail(grievanceManagerUser,"For unresolved issues & formal complaints"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch Grievance Manager for fofoId: {}", fofoId, e);
        }

        // Affordability Manager = TICKET_CATEGORY_FINANCIAL_SERVICES category, L2
        try {
            int affordabilityManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, EscalationType.L2, fofoId);
            if (affordabilityManagerAuthUserId > 0) {
                AuthUser affordabilityManagerUser = authRepository.selectById(affordabilityManagerAuthUserId);
                if (affordabilityManagerUser != null) {
                    response.setBrandingManager(toPersonDetail(affordabilityManagerUser,"Credit, payment plans, and operations"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch Affordability Manager for fofoId: {}", fofoId, e);
        }

        // Branding Manager = CATEGORY_DESIGN category, L1
        try {
            int brandingManagerAuthUserId = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_DESIGN, EscalationType.L1, fofoId);
            if (brandingManagerAuthUserId > 0) {
                AuthUser brandingManagerUser = authRepository.selectById(brandingManagerAuthUserId);
                if (brandingManagerUser != null) {
                    response.setBrandingManager(toPersonDetail(brandingManagerUser,"Brand guidelines and marketing support"));
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not fetch Branding Manager for fofoId: {}", fofoId, e);
        }

        return responseSender.ok(response);
    }

    private PersonDetail toPersonDetail(AuthUser authUser, String description) {
        return new PersonDetail(
                authUser.getId(),
                authUser.getFullName(),
                authUser.getEmailId(),
                authUser.getMobileNumber(),
                description
        );
    }
}