Created
February 19, 2022 19:31
-
-
Save pablohdzvizcarra/893906faa702e5ac2245c09eaf384e3b to your computer and use it in GitHub Desktop.
campaignController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chatbot.bulkmessages.controller; | |
import java.io.IOException; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.util.LinkedMultiValueMap; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
import org.springframework.web.bind.annotation.DeleteMapping; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestHeader; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.chatbot.bulkmessages.common.error.ApiErrorsDefinition; | |
import com.chatbot.bulkmessages.common.exception.ApiRequestException; | |
import com.chatbot.bulkmessages.common.jwt.JWTAuthorization; | |
import com.chatbot.bulkmessages.common.jwt.JwtDto; | |
import com.chatbot.bulkmessages.common.response.ApiResponse; | |
import com.chatbot.bulkmessages.common.response.ApiResponseMessage; | |
import com.chatbot.bulkmessages.common.sucess.ApiSuccessDefinition; | |
import com.chatbot.bulkmessages.dto.ConfigureDto; | |
import com.chatbot.bulkmessages.service.ConfigureService; | |
import com.chatbot.bulkmessages.util.CustomStringUtils; | |
@RestController | |
@CrossOrigin(origins = "*") | |
@RequestMapping("/") | |
public class CampaignController { | |
private final JWTAuthorization authService; | |
private final ConfigureService service; | |
@Autowired | |
public CampaignController(JWTAuthorization authService, ConfigureService service) { | |
this.authService = authService; | |
this.service = service; | |
} | |
@GetMapping | |
public ApiResponse<?> getAll( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@RequestParam() String status, | |
@RequestParam() String startDate, | |
@RequestParam() String endDate) { | |
JwtDto authorizedUser = this.authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
if (!startDate.isEmpty()) | |
if (!CustomStringUtils.validateDate(startDate)) | |
throw new ApiRequestException(ApiErrorsDefinition.DATE_INCORRECT_FORMAT, language); | |
if (!endDate.isEmpty()) | |
if (!CustomStringUtils.validateDate(endDate)) | |
throw new ApiRequestException(ApiErrorsDefinition.DATE_INCORRECT_FORMAT, language); | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
new ApiResponseMessage(HttpStatus.OK.getReasonPhrase()), | |
service.getAllByCompanyIdStatusAndDates( | |
authorizedUser.getCompanyId(), status, startDate, endDate, language)); | |
} | |
@GetMapping("/{id}") | |
public ApiResponse<?> getById( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id) { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
new ApiResponseMessage(HttpStatus.OK.getReasonPhrase()), | |
service.getById(id, language)); | |
} | |
@PostMapping | |
public ApiResponse<?> create( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@RequestBody ConfigureDto data) | |
throws IOException { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
if (data.getDateToSend() != null && !data.getDateToSend().isEmpty()) { | |
if (data.getHourToSend() != null) { | |
data.setHourToSend(data.getHourToSend().substring(0, 5) + ":00"); | |
} | |
if (!CustomStringUtils.validateDate(data.getDateToSend())) | |
throw new ApiRequestException(ApiErrorsDefinition.DATE_INCORRECT_FORMAT, language); | |
if (!CustomStringUtils.validateHour(data.getHourToSend())) | |
throw new ApiRequestException(ApiErrorsDefinition.HOUR_INCORRECT_FORMAT, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
ApiSuccessDefinition.SENDING_CAMPAING, | |
service.create(data, authorizedUser.getCompanyId(), jwsString, language), | |
language); | |
} | |
@GetMapping("/resend/{id}") | |
public ApiResponse<?> resend( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id, | |
@RequestParam() boolean unanswered) | |
throws IOException { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
ApiSuccessDefinition.SENDING_CAMPAING, | |
service.resend(id, jwsString, unanswered, language), | |
language); | |
} | |
@GetMapping("/pause/{id}") | |
public ApiResponse<?> pause( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id, | |
@RequestParam(required = false) boolean unanswered) { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
ApiSuccessDefinition.PAUSE, | |
service.pause(id, authorizedUser.getCompanyId(), language), | |
language); | |
} | |
@GetMapping("/cancel/{id}") | |
public ApiResponse<?> cancel( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id, | |
@RequestParam(required = false) boolean unanswered) { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
ApiSuccessDefinition.CANCEL, | |
service.cancel(id, authorizedUser.getCompanyId(), language), | |
language); | |
} | |
@GetMapping("/resume/{id}") | |
public ApiResponse<?> resume( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id) | |
throws IOException { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
return new ApiResponse<>( | |
HttpStatus.OK, | |
ApiSuccessDefinition.RESUME, | |
service.resumeCampaign(id, jwsString, false, language), | |
language); | |
} | |
@DeleteMapping("/{id}") | |
public ApiResponse<?> delete( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@PathVariable String id) { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
service.delete(id, language); | |
return new ApiResponse<>(HttpStatus.OK, ApiSuccessDefinition.DELETED, null, language); | |
} | |
@GetMapping("/status") | |
public ResponseEntity<String> status( | |
@RequestHeader(name = "Authorization", required = false) String jwsString, | |
@RequestHeader(name = "Accept-Language", required = false, defaultValue = "es") | |
String language, | |
@RequestParam(required = false) String startDate, | |
@RequestParam(required = false) String endDate) | |
throws IOException { | |
JwtDto authorizedUser = authService.convertFromToken(jwsString); | |
if (authorizedUser == null) { | |
throw new ApiRequestException(ApiErrorsDefinition.INVALID_TOKEN, language); | |
} | |
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | |
headers.set("Content-Type", "application/json;charset=UTF-8"); | |
return new ResponseEntity<>( | |
service.getStatus(authorizedUser.getCompanyId(), startDate, endDate).toString(), | |
headers, | |
HttpStatus.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment