Subversion Repositories SmartDukaan

Rev

Rev 23568 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.ProfitMandiResponse;
import com.spice.profitmandi.common.model.ResponseStatus;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.model.LimitedMasterDataPojo;
import com.spice.profitmandi.dao.repository.dtr.Mongo;
import com.spice.profitmandi.dao.repository.inventory.StateRepository;

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

@Controller
public class MasterDataController {
        
        private static final Logger logger=LogManager.getLogger(MasterDataController.class);

        @Autowired
        private Mongo mongoClient;
        
        @Autowired
        private ResponseSender<?> responseSender;

        @Autowired
        private StateRepository stateRepository;
        

        @RequestMapping(value = ProfitMandiConstants.URL_MASTER_DATA, 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 limited info of master data for skuBundleId")
        public ResponseEntity<?> getMasterDataBySkuBundleId(HttpServletRequest request, @PathVariable(value="id") long skuBundleId) throws Exception{

                JSONObject jsonObject = mongoClient.getItemsByBundleId(skuBundleId);
                LimitedMasterDataPojo masterData = new Gson().fromJson(jsonObject.toString(), LimitedMasterDataPojo.class);
                return responseSender.ok(masterData);
        }

        /**
         * The canonical state list, straight from inventory.statemaster.
         *
         * <p>Clients must render their state pickers from this rather than carrying their own list.
         * Every stored state string is later resolved back through the same master by name, so a
         * client-side list that has drifted writes values nothing can resolve - which is how invoices
         * ended up with states like "Daman &amp; Diu" that no longer exist in the master. Serving the
         * list here means a correction to the master reaches every client without an app release.
         */
        @RequestMapping(value = ProfitMandiConstants.URL_MASTER_STATES, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Canonical state list (name + GST state code)")
        public ResponseEntity<?> getStates(HttpServletRequest request) throws Exception {
                List<StateModel> states = stateRepository.selectAll().stream()
                                .map(state -> new StateModel(state.getName(), state.getCode()))
                                .sorted(Comparator.comparing(StateModel::getName))
                                .collect(Collectors.toList());
                return responseSender.ok(states);
        }

        /** A state as clients need it: the name they must store, and its GST state code. */
        public static class StateModel {

                private final String name;
                private final String code;

                StateModel(String name, String code) {
                        this.name = name;
                        this.code = code;
                }

                public String getName() {
                        return name;
                }

                public String getCode() {
                        return code;
                }
        }

}