Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-4674] feat: Changed response for zendesk support API #428

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 6 additions & 104 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2451,109 +2451,6 @@
}
}
},
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/html" : {
"schema" : {
"type" : "string"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Unauthorized",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
}
},
"/v1/support/request" : {
"post" : {
"tags" : [ "support" ],
"summary" : "getSupportRedirectUrl",
"description" : "Service to retrieve Support contact's form",
"operationId" : "getSupportRedirectUrlUsingPOST",
"parameters" : [ {
"name" : "authenticated",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "boolean"
}
}, {
"name" : "authorities[0].authority",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "credentials",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "object"
}
}, {
"name" : "details",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "object"
}
}, {
"name" : "principal",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "object"
}
} ],
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/SupportRequestDto"
}
}
}
},
"responses" : {
"200" : {
"description" : "OK",
Expand Down Expand Up @@ -2596,7 +2493,6 @@
}
}
},
"deprecated" : true,
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
Expand Down Expand Up @@ -6394,6 +6290,12 @@
"title" : "SupportResponse",
"type" : "object",
"properties" : {
"actionUrl" : {
"type" : "string"
},
"jwt" : {
"type" : "string"
},
"redirectUrl" : {
"type" : "string"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package it.pagopa.selfcare.dashboard.connector.model.support;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;

@Data
@Builder
@ToString
@NoArgsConstructor @AllArgsConstructor
public class SupportResponse {
private String redirectUrl;
private String jwt;
private String actionUrl;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.pagopa.selfcare.dashboard.core;

import it.pagopa.selfcare.dashboard.connector.model.support.SupportRequest;
import it.pagopa.selfcare.dashboard.connector.model.support.SupportResponse;

public interface SupportService {
String sendRequest(SupportRequest supportRequest);
String getSupportRequest(SupportRequest supportRequest);
SupportResponse sendRequest(SupportRequest supportRequest);
}
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package it.pagopa.selfcare.dashboard.core.config;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
Expand All @@ -19,7 +16,6 @@
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.net.URL;
import java.util.Properties;
Expand All @@ -33,14 +29,12 @@ class CoreConfig implements AsyncConfigurer {

private final URL rootTemplateUrl;


@Autowired
public CoreConfig(@Value("${dashboard.notification.template.default-url}") URL rootTemplateUrl) {
log.trace("Initializing {}", CoreConfig.class.getSimpleName());
this.rootTemplateUrl = rootTemplateUrl;
}


@Bean
public freemarker.template.Configuration customFreeMarkerConfiguration() throws TemplateException {
Properties settings = new Properties();
Expand All @@ -52,29 +46,16 @@ public freemarker.template.Configuration customFreeMarkerConfiguration() throws
return configuration;
}

@Bean("zendeskFreeMarker")
public FreeMarkerConfigurer freemarkerClassLoaderConfig() {
freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_27);
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/template-zendesk");
configuration.setTemplateLoader(templateLoader);
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setConfiguration(configuration);
return freeMarkerConfigurer;
}


@Bean
public SimpleAsyncUncaughtExceptionHandler simpleAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}


@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return simpleAsyncUncaughtExceptionHandler();
}


@Bean
public DelegatingSecurityContextAsyncTaskExecutor taskExecutor(TaskExecutorBuilder taskExecutorBuilder) {
final ThreadPoolTaskExecutor delegate = taskExecutorBuilder.build();
Expand Down

This file was deleted.

Loading
Loading