Subversion Repositories SmartDukaan

Rev

Rev 21469 | Rev 21483 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21248 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
21428 amit.gupta 4
import java.util.HashMap;
21277 ashik.ali 5
import java.util.Map;
21428 amit.gupta 6
import java.util.Set;
21469 amit.gupta 7
import java.util.function.Predicate;
21248 ashik.ali 8
 
9
import javax.servlet.http.HttpServletRequest;
10
 
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
21278 ashik.ali 13
import org.springframework.beans.factory.annotation.Autowired;
21414 kshitij.so 14
import org.springframework.beans.factory.annotation.Value;
21248 ashik.ali 15
import org.springframework.http.HttpStatus;
16
import org.springframework.http.ResponseEntity;
17
import org.springframework.stereotype.Controller;
21366 kshitij.so 18
import org.springframework.web.bind.annotation.RequestBody;
21248 ashik.ali 19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestParam;
22
 
23
import com.spice.profitmandi.common.ResponseCodeHolder;
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21469 amit.gupta 26
import com.spice.profitmandi.common.model.UserInfo;
21282 ashik.ali 27
import com.spice.profitmandi.common.util.JWTUtil;
21426 ashik.ali 28
import com.spice.profitmandi.dao.entity.Permission;
21414 kshitij.so 29
import com.spice.profitmandi.dao.entity.Role;
21278 ashik.ali 30
import com.spice.profitmandi.dao.entity.User;
21426 ashik.ali 31
import com.spice.profitmandi.dao.entity.UserRole;
21469 amit.gupta 32
import com.spice.profitmandi.dao.enumuration.RoleType;
21426 ashik.ali 33
import com.spice.profitmandi.dao.repository.PermissionRepository;
34
import com.spice.profitmandi.dao.repository.RoleRepository;
21248 ashik.ali 35
import com.spice.profitmandi.dao.repository.UserRepository;
21426 ashik.ali 36
import com.spice.profitmandi.dao.repository.UserRoleRepository;
21469 amit.gupta 37
import com.spice.profitmandi.web.enumuration.UserStatus;
21248 ashik.ali 38
import com.spice.profitmandi.web.model.ProfitMandiResponse;
39
import com.spice.profitmandi.web.model.ResponseStatus;
21277 ashik.ali 40
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
21426 ashik.ali 41
import com.spice.profitmandi.web.req.UserAddRoleRequest;
21366 kshitij.so 42
import com.spice.profitmandi.web.req.UserRequest;
21448 ashik.ali 43
import com.spice.profitmandi.web.util.ResponseSender;
21248 ashik.ali 44
 
21469 amit.gupta 45
import io.swagger.annotations.ApiImplicitParam;
46
import io.swagger.annotations.ApiImplicitParams;
47
 
21248 ashik.ali 48
/**
49
 * @author ashikali
50
 *
51
 */
52
@Controller
53
public class UserController {
21469 amit.gupta 54
 
21448 ashik.ali 55
	@Autowired
56
	ResponseSender<?> responseSender;
21469 amit.gupta 57
 
58
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
59
 
21414 kshitij.so 60
	@Value("${admin.token}")
61
	private String validAdminToken;
21469 amit.gupta 62
 
21278 ashik.ali 63
	@Autowired
64
	UserRepository userRepository;
21469 amit.gupta 65
 
21278 ashik.ali 66
	@Autowired
21426 ashik.ali 67
	RoleRepository roleRepository;
21469 amit.gupta 68
 
21426 ashik.ali 69
	@Autowired
70
	UserRoleRepository userRoleRepository;
21469 amit.gupta 71
 
21426 ashik.ali 72
	@Autowired
73
	PermissionRepository permissionRepository;
21469 amit.gupta 74
 
21426 ashik.ali 75
	@Autowired
21278 ashik.ali 76
	GoogleLoginProcessor googleLoginProcessor;
21469 amit.gupta 77
 
21277 ashik.ali 78
	@SuppressWarnings("unchecked")
21469 amit.gupta 79
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
80
	public ResponseEntity<?> googleLogin(HttpServletRequest request) {
81
		LOGGER.info("requested url : " + request.getRequestURL().toString());
82
		final Map<String, Object> googleLoginMap = (Map<String, Object>) request
83
				.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
21277 ashik.ali 84
		request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
85
		try {
21448 ashik.ali 86
			return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
21469 amit.gupta 87
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
88
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 89
			return responseSender.badRequest(profitMandiBusinessException);
21277 ashik.ali 90
		}
91
	}
21469 amit.gupta 92
 
93
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
94
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token) {
95
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21282 ashik.ali 96
		try {
21448 ashik.ali 97
			return responseSender.ok(JWTUtil.isExpired(token));
21469 amit.gupta 98
 
99
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
100
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 101
			return responseSender.badRequest(profitMandiBusinessException);
21282 ashik.ali 102
		}
103
	}
21469 amit.gupta 104
 
105
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_INFO, method = RequestMethod.GET)
106
	@ApiImplicitParams({
107
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
108
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
109
		Map<String, Object> responseMap = new HashMap<>();
110
		LOGGER.info("requested url : " + request.getRequestURL().toString());
111
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21479 amit.gupta 112
		if (userInfo.getUserId() > 0) {
21469 amit.gupta 113
			User user = userRepository.selectById(userInfo.getUserId());
114
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
115
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
116
			// if user is retailer
117
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
118
				@Override
119
				public boolean test(Role t) {
120
					return t.getType().equals(RoleType.RETAILER);
121
				}
122
			})) {
123
				// TODO: This should be from retailer Table
124
				// if retailer is activated and migrated is 0 then verified
125
				// retailer
126
				// if retailer is not activated and and migrated is 0 then not
127
				// verified retailer
128
				// if retailer is activated and migrated is 1 then retailer is
129
				// retailer.
130
				if (user.isActivated()) {
131
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
132
				} else {
133
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
134
				}
135
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
136
				@Override
137
				public boolean test(Role t) {
138
					return t.getType().equals(RoleType.USER);
139
				}
140
			})) {
141
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
21479 amit.gupta 142
			} else {
143
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 144
			}
145
		} else {
146
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
147
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
148
		}
149
 
150
		return responseSender.ok(responseMap);
151
 
152
	}
153
 
154
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
155
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest) {
156
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 157
		User user = new User();
158
		user.setFirstName(userRequest.getFirstName());
159
		user.setLastName(userRequest.getLastName());
160
		user.setCity(userRequest.getCity());
161
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
162
		user.setEmailId(userRequest.getEmailId());
163
		user.setUsername("");
164
		user.setPassword("");
165
		user.setMobile_verified(false);
166
		user.setReferral_url("");
167
		user.setGroup_id(1);
168
		user.setStatus(0);
169
		user.setActivated(false);
21414 kshitij.so 170
 
21278 ashik.ali 171
		try {
172
			user.setCreateTimestamp(LocalDateTime.now());
173
			user.setUpdateTimestamp(LocalDateTime.now());
174
			userRepository.persist(user);
21448 ashik.ali 175
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
21469 amit.gupta 176
 
177
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
178
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 179
			return responseSender.badRequest(profitMandiBusinessException);
21278 ashik.ali 180
		}
181
	}
21469 amit.gupta 182
 
183
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
184
	public ResponseEntity<?> getAll(HttpServletRequest request) {
185
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 186
		return responseSender.ok(userRepository.selectAll());
21248 ashik.ali 187
	}
21469 amit.gupta 188
 
189
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
190
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
191
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 192
		try {
21448 ashik.ali 193
			return responseSender.ok(userRepository.selectById(id));
21469 amit.gupta 194
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
195
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 196
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 197
		}
198
	}
21469 amit.gupta 199
 
200
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
201
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
202
			@RequestParam(name = "mobileNumber") String mobileNumber) {
203
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 204
		try {
21448 ashik.ali 205
			return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21469 amit.gupta 206
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
207
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 208
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 209
		}
210
	}
21469 amit.gupta 211
 
21426 ashik.ali 212
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 213
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
214
			@RequestParam(name = "mobileNumber") String mobileNumber) {
215
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 216
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 217
	}
21469 amit.gupta 218
 
219
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
220
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
221
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 222
		try {
21448 ashik.ali 223
			return responseSender.ok(userRepository.selectByEmailId(emailId));
21469 amit.gupta 224
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
225
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 226
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 227
		}
228
	}
21469 amit.gupta 229
 
230
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
231
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
232
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 233
		try {
21426 ashik.ali 234
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
21469 amit.gupta 235
 
21426 ashik.ali 236
			Role role = null;
21469 amit.gupta 237
			try {
238
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(),
239
						userAddRoleRequest.getRole().getType());
240
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
21426 ashik.ali 241
				role = new Role();
242
				role.setName(userAddRoleRequest.getRole().getName());
243
				role.setType(userAddRoleRequest.getRole().getType());
244
				roleRepository.persist(role);
245
			}
246
			Permission permission = new Permission();
247
			permission.setType(userAddRoleRequest.getRole().getPermissionType());
248
			permission.setRoleId(role.getId());
249
			permissionRepository.persist(permission);
250
			UserRole userRole = new UserRole();
251
			userRole.setRoleId(role.getId());
252
			userRole.setUserId(user.getId());
253
			userRoleRepository.persist(userRole);
21448 ashik.ali 254
			return responseSender.ok("");
21469 amit.gupta 255
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
256
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 257
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 258
		}
259
	}
21469 amit.gupta 260
 
261
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
262
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId,
263
			@RequestParam(name = "userId") int userId) {
264
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 265
		try {
21426 ashik.ali 266
			roleRepository.selectById(roleId);
267
			userRepository.selectById(userId);
268
			userRoleRepository.deleteByUserAndRoleId(userId, roleId);
269
			permissionRepository.deleteByRoleId(roleId);
21448 ashik.ali 270
			return responseSender.ok("");
21469 amit.gupta 271
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
272
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 273
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 274
		}
275
	}
21469 amit.gupta 276
 
277
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
278
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
279
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 280
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 281
	}
21414 kshitij.so 282
 
283
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 284
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
285
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
286
		LOGGER.info("requested url : " + request.getRequestURL().toString());
287
		if (!adminToken.equals(validAdminToken)) {
288
			final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
289
					request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
290
					ResponseStatus.FAILURE, null);
21414 kshitij.so 291
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
292
		}
21469 amit.gupta 293
 
21414 kshitij.so 294
		Map<String, Object> responseMap = new HashMap<>(2);
21469 amit.gupta 295
		try {
21414 kshitij.so 296
			User user = userRepository.selectByEmailId(emailId);
297
			Set<Role> roles = user.getRoles();
298
			String[] roleTypes = new String[roles.size()];
299
			int index = 0;
21469 amit.gupta 300
			for (Role role : roles) {
21414 kshitij.so 301
				roleTypes[index++] = role.getType().toString();
302
			}
303
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
304
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
21469 amit.gupta 305
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
306
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 307
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
308
		}
21448 ashik.ali 309
		return responseSender.ok(responseMap);
21469 amit.gupta 310
 
21414 kshitij.so 311
	}
21248 ashik.ali 312
}