| 32367 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.security.Key;
|
|
|
4 |
import java.security.NoSuchAlgorithmException;
|
|
|
5 |
import java.security.SecureRandom;
|
|
|
6 |
import java.util.Date;
|
|
|
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
|
|
|
12 |
import org.apache.http.conn.HttpHostConnectException;
|
|
|
13 |
import org.apache.logging.log4j.LogManager;
|
|
|
14 |
import org.apache.logging.log4j.Logger;
|
|
|
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
16 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
17 |
import org.springframework.http.MediaType;
|
|
|
18 |
import org.springframework.http.ResponseEntity;
|
|
|
19 |
import org.springframework.stereotype.Controller;
|
|
|
20 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
25 |
|
|
|
26 |
import com.auth0.jwt.interfaces.Signature;
|
|
|
27 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
28 |
import com.spice.profitmandi.common.web.client.RestClient;
|
|
|
29 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
30 |
import com.spice.profitmandi.dao.entity.fofo.Customer;
|
|
|
31 |
import com.spice.profitmandi.dao.model.thriwe.benefit.BenefitGroup;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
|
|
33 |
|
|
|
34 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
35 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
36 |
import io.swagger.annotations.ApiOperation;
|
|
|
37 |
|
|
|
38 |
@Controller
|
|
|
39 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
40 |
public class ThriweController {
|
|
|
41 |
|
|
|
42 |
@Autowired
|
|
|
43 |
private ResponseSender<?> responseSender;
|
|
|
44 |
|
|
|
45 |
@Autowired
|
|
|
46 |
RestClient restClient;
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
@Value("${thriwe.account.token}")
|
|
|
50 |
private String token;
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private CustomerRepository customerRepository;
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
private static final Logger LOGGER = LogManager.getLogger(ThriweController.class);
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
@RequestMapping(value = "/thriwe/validate/customer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
60 |
@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
|
|
|
61 |
@ApiOperation(value = "")
|
|
|
62 |
public ResponseEntity<?> validateCustomer(HttpServletRequest request) throws HttpHostConnectException, ProfitMandiBusinessException {
|
|
|
63 |
|
|
|
64 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
65 |
|
|
|
66 |
Customer customer = customerRepository.selectById(32697);
|
|
|
67 |
|
|
|
68 |
// String authToken = this.getToken(customer);
|
|
|
69 |
|
|
|
70 |
Map<String, String> headers = new HashMap<String, String>();
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
headers.put("token", token);
|
|
|
74 |
// headers.put("X-Auth-Token", authToken);
|
|
|
75 |
headers.put("Project-Code", "SMART_DUKAAN");
|
|
|
76 |
|
|
|
77 |
/* Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
|
|
78 |
|
|
|
79 |
// JSON data as a Map (Key-Value pairs)
|
|
|
80 |
Map<String, Object> jsonData = new HashMap<>();
|
|
|
81 |
jsonData.put("exp", 1675145552L);
|
|
|
82 |
jsonData.put("iat", 1674540752L);
|
|
|
83 |
jsonData.put("userName", "75gtf-765dvy-atd6-ffdfa");
|
|
|
84 |
jsonData.put("firstName", "Ishaan");
|
|
|
85 |
jsonData.put("lastName", "Sharma");
|
|
|
86 |
jsonData.put("email", "ishaan.sharma@thriwe.com");
|
|
|
87 |
jsonData.put("mobile", "9560998243");
|
|
|
88 |
jsonData.put("validTill", "12-May-2024");
|
|
|
89 |
|
|
|
90 |
// Create the JWT
|
|
|
91 |
String jwt = Jwts.builder()
|
|
|
92 |
.setClaims(jsonData)
|
|
|
93 |
.signWith(key)
|
|
|
94 |
.compact();
|
|
|
95 |
|
|
|
96 |
System.out.println("Generated JWT: " + jwt);*/
|
|
|
97 |
|
|
|
98 |
// String secretKey = "your-secret-key"; // Replace with your actual secret key
|
|
|
99 |
|
|
|
100 |
// String token = Jwts.builder().setPayload(jsonData).signWith(SignatureAlgorithm.HS256, secretKey).compact();
|
|
|
101 |
|
|
|
102 |
// System.out.println("Generated JWT: " + token);
|
|
|
103 |
|
|
|
104 |
// JSON data as a string
|
|
|
105 |
// Generate the JWT
|
|
|
106 |
// Create the JWT
|
|
|
107 |
|
|
|
108 |
String url = "http://localhost:8083/validate-user";
|
|
|
109 |
String response = restClient.get(url, params, headers);
|
|
|
110 |
|
|
|
111 |
LOGGER.info("response {}", response);
|
|
|
112 |
|
|
|
113 |
return responseSender.ok(true);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
private static Key generateSecretKey() throws NoSuchAlgorithmException {
|
|
|
118 |
SecureRandom secureRandom = new SecureRandom();
|
|
|
119 |
byte[] bytes = new byte[32]; // 256 bits
|
|
|
120 |
secureRandom.nextBytes(bytes);
|
|
|
121 |
return new javax.crypto.spec.SecretKeySpec(bytes, "HmacSHA256");
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
@RequestMapping(value = "/thriwe/benefitConfiguration", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
126 |
@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
|
|
|
127 |
@ApiOperation(value = "")
|
|
|
128 |
public ResponseEntity<?> benefitConfiguration(HttpServletRequest request) throws HttpHostConnectException, ProfitMandiBusinessException {
|
|
|
129 |
|
|
|
130 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
131 |
|
|
|
132 |
Map<String, String> headers = new HashMap<String, String>();
|
|
|
133 |
|
|
|
134 |
headers.put("token", token);
|
|
|
135 |
|
|
|
136 |
headers.put("Project-Code", "SMART_DUKAAN");
|
|
|
137 |
|
|
|
138 |
String url = "https://staging-india-api-gateway.thriwe.com/client/benefit-configs";
|
|
|
139 |
String response = restClient.get(url, params, headers);
|
|
|
140 |
|
|
|
141 |
LOGGER.info("response {}", response);
|
|
|
142 |
|
|
|
143 |
return responseSender.ok(true);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
@RequestMapping(value = "/thriwe/benefits", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
148 |
@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
|
|
|
149 |
@ApiOperation(value = "")
|
|
|
150 |
public ResponseEntity<?> benefits(HttpServletRequest request, @RequestParam String configId) throws HttpHostConnectException, ProfitMandiBusinessException {
|
|
|
151 |
|
|
|
152 |
Map<String, String> params = new HashMap<String, String>();
|
|
|
153 |
|
|
|
154 |
params.put("configId", configId);
|
|
|
155 |
|
|
|
156 |
Map<String, String> headers = new HashMap<String, String>();
|
|
|
157 |
|
|
|
158 |
headers.put("token", token);
|
|
|
159 |
headers.put("Project-Code", "SMART_DUKAAN");
|
|
|
160 |
|
|
|
161 |
String url = "https://staging-india-api-gateway.thriwe.com/client/benefit-configs";
|
|
|
162 |
String response = restClient.get(url, params, headers);
|
|
|
163 |
|
|
|
164 |
LOGGER.info("response {}", response);
|
|
|
165 |
|
|
|
166 |
return responseSender.ok(true);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
@RequestMapping(value = "/thriwe/createBooking", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
171 |
@ApiImplicitParams({@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header")})
|
|
|
172 |
@ApiOperation(value = "")
|
|
|
173 |
public ResponseEntity<?> createBooking(HttpServletRequest request, @RequestBody BenefitGroup benefitGroup) throws HttpHostConnectException, ProfitMandiBusinessException {
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
Map<String, String> headers = new HashMap<String, String>();
|
|
|
177 |
|
|
|
178 |
headers.put("token", token);
|
|
|
179 |
headers.put("Project-Code", "SMART_DUKAAN");
|
|
|
180 |
|
|
|
181 |
String url = "https://staging-india-api-gateway.thriwe.com/create-booking";
|
|
|
182 |
String response = restClient.post(url, benefitGroup.toString(), headers);
|
|
|
183 |
|
|
|
184 |
LOGGER.info("response {}", response);
|
|
|
185 |
return responseSender.ok(true);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
}
|