Skip to content

Commit

Permalink
Changed response for support rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed May 8, 2024
1 parent 5b7003a commit 5763258
Showing 1 changed file with 14 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package it.pagopa.selfcare.dashboard.core;

import freemarker.template.Template;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import it.pagopa.selfcare.dashboard.connector.api.UserRegistryConnector;
import it.pagopa.selfcare.dashboard.connector.exception.ResourceNotFoundException;
import it.pagopa.selfcare.dashboard.connector.exception.SupportException;
import it.pagopa.selfcare.dashboard.connector.model.support.SupportRequest;
import it.pagopa.selfcare.dashboard.connector.model.support.SupportResponse;
import it.pagopa.selfcare.dashboard.connector.model.support.UserField;
import it.pagopa.selfcare.dashboard.connector.model.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Date;
import java.util.EnumSet;
import java.util.Objects;
import java.util.UUID;

import static it.pagopa.selfcare.dashboard.connector.model.user.User.Fields.*;

Expand All @@ -32,26 +30,22 @@ public class SupportServiceImpl implements SupportService {
private final String returnTo;
private final String actionUrl;
private final UserRegistryConnector userRegistryConnector;
@Qualifier("zendeskFreeMarker")
private final FreeMarkerConfigurer freeMarkerConfigurer;
private static final EnumSet<User.Fields> USER_FIELD_LIST = EnumSet.of(name, familyName, fiscalCode);

public SupportServiceImpl(@Value("${support.api.key}") String supportApiKey,
@Value("${support.api.zendesk.redirectUri}") String returnTo,
@Value("${support.api.zendesk.organization}") String zendeskOrganization,
@Value("${support.api.zendesk.actionUri}") String redirectUrl,
FreeMarkerConfigurer freeMarkerConfigurer,
UserRegistryConnector userRegistryConnector) {
this.supportApiKey = supportApiKey;
this.returnTo = returnTo;
this.zendeskOrganization = zendeskOrganization;
this.actionUrl = redirectUrl;
this.freeMarkerConfigurer = freeMarkerConfigurer;
this.userRegistryConnector = userRegistryConnector;
}

@Override
public String sendRequest(SupportRequest supportRequest) {
public SupportResponse sendRequest(SupportRequest supportRequest) {

log.trace("sendRequest start");
log.debug("sendRequest request = {}", supportRequest);
Expand All @@ -67,43 +61,16 @@ public String sendRequest(SupportRequest supportRequest) {
}

//Retrieve parameters for submitting form
String redirectUrl = getRedirectUrl(supportRequest);
String jwtString = getJWTString(supportRequest);

String html;
try {
Map<String, String> map = Map.of(
"jwt", jwtString,
"returnTo", redirectUrl,
"action", actionUrl
);
Template freemarkerTemplate = freeMarkerConfigurer.getConfiguration()
.getTemplate("/template-zendesk-form.ftl");
html = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, map);
} catch (Exception e){
String errorMessage = "Impossible to retrieve zendesk form template";
log.error(errorMessage);
throw new SupportException(errorMessage, e);
}

log.trace("sendRequest end");
return html;
}

@Override
public String getSupportRequest(SupportRequest supportRequest) {
log.trace("sendRequest start");
log.debug("sendRequest request = {}", supportRequest);
final String redirectUrl = getRedirectUrl(supportRequest);
final String jwtString = getJWTString(supportRequest);
final String redirectUrl = "https://pagopa.zendesk.com/access/jwt?jwt=" + jwtString;
log.debug("sendRequest result = {}", redirectUrl);
log.trace("sendRequest end");

String returnUrl = StringUtils.hasText(supportRequest.getProductId()) ?
URLEncoder.encode(returnTo.concat("?product=" + supportRequest.getProductId()), StandardCharsets.UTF_8) :
URLEncoder.encode(returnTo, StandardCharsets.UTF_8);
log.trace("sendRequest end");

return redirectUrl.concat("&return_to=" + returnUrl);
return SupportResponse.builder()
.jwt(jwtString)
.redirectUrl(redirectUrl)
.actionUrl(actionUrl)
.build();
}

private String getJWTString(SupportRequest supportRequest) {
Expand Down Expand Up @@ -133,7 +100,7 @@ private String getRedirectUrl(SupportRequest supportRequest) {
if(Objects.nonNull(supportRequest.getInstitutionId())) {
urlBuilder.append(urlBuilder.indexOf("?") != -1 ?
"&institution=" + supportRequest.getInstitutionId()
: "?product=" + supportRequest.getInstitutionId());
: "?institution=" + supportRequest.getInstitutionId());
}
return urlBuilder.toString();
}
Expand Down

0 comments on commit 5763258

Please sign in to comment.