Subversion Repositories SmartDukaan

Rev

Rev 32440 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.conn.HttpHostConnectException;
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.Value;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.auth0.jwt.interfaces.Signature;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.fofo.Customer;
import com.spice.profitmandi.dao.model.thriwe.benefit.BenefitGroup;
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;

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

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

    @Autowired
    private ResponseSender<?> responseSender;

    @Autowired
    RestClient restClient;


    @Value("${thriwe.account.token}")
    private String token;

    @Autowired
    private CustomerRepository customerRepository;


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


    @RequestMapping(value = "/thriwe/validate/customer", 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<?> validateCustomer(HttpServletRequest request) throws HttpHostConnectException, ProfitMandiBusinessException {

        Map<String, String> params = new HashMap<String, String>();

        Customer customer = customerRepository.selectById(32697);

        // String authToken = this.getToken(customer);

        Map<String, String> headers = new HashMap<String, String>();


        headers.put("token", token);
        // headers.put("X-Auth-Token", authToken);
        headers.put("Project-Code", "SMART_DUKAAN");

      /*  Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);

        // JSON data as a Map (Key-Value pairs)
        Map<String, Object> jsonData = new HashMap<>();
        jsonData.put("exp", 1675145552L);
        jsonData.put("iat", 1674540752L);
        jsonData.put("userName", "75gtf-765dvy-atd6-ffdfa");
        jsonData.put("firstName", "Ishaan");
        jsonData.put("lastName", "Sharma");
        jsonData.put("email", "ishaan.sharma@thriwe.com");
        jsonData.put("mobile", "9560998243");
        jsonData.put("validTill", "12-May-2024");

        // Create the JWT
        String jwt = Jwts.builder()
                .setClaims(jsonData)
                .signWith(key)
                .compact();

        System.out.println("Generated JWT: " + jwt);*/

       // String secretKey = "your-secret-key"; // Replace with your actual secret key

  //      String token = Jwts.builder().setPayload(jsonData).signWith(SignatureAlgorithm.HS256, secretKey).compact();

    //    System.out.println("Generated JWT: " + token);
        
        // JSON data as a string
        // Generate the JWT
      // Create the JWT

        String url = "http://localhost:8083/validate-user";
        String response = restClient.get(url, params, headers);

        LOGGER.info("response {}", response);

        return responseSender.ok(true);
    }


    private static Key generateSecretKey() throws NoSuchAlgorithmException {
        SecureRandom secureRandom = new SecureRandom();
        byte[] bytes = new byte[32]; // 256 bits
        secureRandom.nextBytes(bytes);
        return new javax.crypto.spec.SecretKeySpec(bytes, "HmacSHA256");
    }


    @RequestMapping(value = "/thriwe/benefitConfiguration", 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<?> benefitConfiguration(HttpServletRequest request) throws HttpHostConnectException, ProfitMandiBusinessException {

        Map<String, String> params = new HashMap<String, String>();

        Map<String, String> headers = new HashMap<String, String>();

        headers.put("token", token);

        headers.put("Project-Code", "SMART_DUKAAN");

        String url = "https://staging-india-api-gateway.thriwe.com/client/benefit-configs";
        String response = restClient.get(url, params, headers);

        LOGGER.info("response {}", response);

        return responseSender.ok(true);
    }


    @RequestMapping(value = "/thriwe/benefits", 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<?> benefits(HttpServletRequest request, @RequestParam String configId) throws HttpHostConnectException, ProfitMandiBusinessException {

        Map<String, String> params = new HashMap<String, String>();

        params.put("configId", configId);

        Map<String, String> headers = new HashMap<String, String>();

        headers.put("token", token);
        headers.put("Project-Code", "SMART_DUKAAN");

        String url = "https://staging-india-api-gateway.thriwe.com/client/benefit-configs";
        String response = restClient.get(url, params, headers);

        LOGGER.info("response {}", response);

        return responseSender.ok(true);
    }


    @RequestMapping(value = "/thriwe/createBooking", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
    @ApiOperation(value = "")
    public ResponseEntity<?> createBooking(HttpServletRequest request, @RequestBody BenefitGroup benefitGroup) throws HttpHostConnectException, ProfitMandiBusinessException {


        Map<String, String> headers = new HashMap<String, String>();

        headers.put("token", token);
        headers.put("Project-Code", "SMART_DUKAAN");

        String url = "https://staging-india-api-gateway.thriwe.com/create-booking";
        String response = restClient.post(url, benefitGroup.toString(), headers);

        LOGGER.info("response {}", response);
        return responseSender.ok(true);
    }


}