Skip to content

Commit

Permalink
Merge pull request #123 from eurofurence/issue-122-dd-interface
Browse files Browse the repository at this point in the history
feat(#122): add addinfo api and update package api
  • Loading branch information
Jumpy-Squirrel authored Feb 4, 2024
2 parents 7658f41 + 82df9aa commit ec84197
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 6 deletions.
15 changes: 11 additions & 4 deletions doc/config-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,35 @@ choices:
price: 5000
vat_percent: 19
read_only: true
constraint: '!dealer-full,!dealer-double,!dealer-quad'
constraint: '!dealer-full,!dealer-fullplus,!dealer-double,!dealer-quad'
constraint_msg: 'Cannot mix dealer table packages.'
dealer-full:
description: 'Dealer Table Fee (Full)'
price: 10000
vat_percent: 19
read_only: true
constraint: '!dealer-half,!dealer-double,!dealer-quad'
constraint: '!dealer-half,!dealer-fullplus,!dealer-double,!dealer-quad'
constraint_msg: 'Cannot mix dealer table packages.'
dealer-fullplus:
description: 'Dealer Table Fee (Full Plus)'
price: 15000
vat_percent: 19
read_only: true
constraint: '!dealer-half,!dealer-full,!dealer-double,!dealer-quad'
constraint_msg: 'Cannot mix dealer table packages.'
dealer-double:
description: 'Dealer Table Fee (Double)'
price: 20000
vat_percent: 19
read_only: true
constraint: '!dealer-half,!dealer-full,!dealer-quad'
constraint: '!dealer-half,!dealer-full,!dealer-fullplus,!dealer-quad'
constraint_msg: 'Cannot mix dealer table packages.'
dealer-quad:
description: 'Dealer Table Fee (Quad)'
price: 40000
vat_percent: 19
read_only: true
constraint: '!dealer-half,!dealer-full,!dealer-double'
constraint: '!dealer-half,!dealer-full,!dealer-fullplus,!dealer-double'
constraint_msg: 'Cannot mix dealer table packages.'
boat-trip:
description: 'Summerboat Boat Trip'
Expand Down
146 changes: 146 additions & 0 deletions src/java/org/eurofurence/regsys/web/services/AddInfoApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package org.eurofurence.regsys.web.services;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.eurofurence.regsys.repositories.attendees.AddInfo;
import org.eurofurence.regsys.repositories.attendees.Attendee;
import org.eurofurence.regsys.repositories.attendees.AttendeeService;
import org.eurofurence.regsys.repositories.auth.RequestAuth;
import org.eurofurence.regsys.repositories.config.Configuration;
import org.eurofurence.regsys.repositories.errors.DownstreamException;
import org.eurofurence.regsys.repositories.errors.NotFoundException;
import org.eurofurence.regsys.web.servlets.HttpMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* An API to get and set an additional info flag value.
*/
public class AddInfoApi extends Service {
// --- parameter constants ---

private static final String PARAM_TOKEN = "token";
private static final String PARAM_ID = "id";
private static final String PARAM_AREA = "area";

// --- variables ---

private long attendeeId = -1L;
private String area = "";
private Set<String> allowedAreas = new HashSet<>();

private AddInfo addInfo = null;
private final AttendeeService attendeeService = new AttendeeService();

// --- auth ---

protected Map<String,String> authForArea() {
Map<String,String> allowed = new HashMap<>();

Configuration.DownstreamConfig downstream = getConfiguration().downstream;
if (downstream != null) {
if (downstream.ddToken != null && !"".equals(downstream.ddToken)) {
allowed.put("dealerreg", downstream.ddToken);
}
}
return allowed;
}

protected void authenticate() {
String token = getRequest().getParameter(PARAM_TOKEN);
if (token == null || "".equals(token))
throw new ServiceException("security.invalid.token", getRequestId());

allowedAreas = authForArea().entrySet().stream()
.filter(e -> e.getValue().equals(token))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());

if (allowedAreas.isEmpty())
throw new ServiceException("security.invalid.token", getRequestId());
}

// --- parameters and preparations ---

protected void parseParameters() {
String idStr = getRequest().getParameter(PARAM_ID);
if ((idStr == null) || !idStr.matches("[1-9][0-9]*"))
throw new ServiceException("addinfoapi.validation.failure.id", getRequestId());
attendeeId = Long.parseLong(idStr);

if (getMethod() == HttpMethod.GET) {
area = "";
} else {
String areaName = getRequest().getParameter(PARAM_AREA);
if (areaName == null || "".equals(areaName))
throw new ServiceException("addinfoapi.validation.failure.area", getRequestId());

// check that the package is allowed to be manipulated by this api and token
if (!allowedAreas.contains(areaName))
throw new ServiceException("security.access.denied", getRequestId());

area = areaName;
}
}

protected static class ResponseDTO {
@JsonProperty("ok")
public boolean ok = true;

@JsonProperty("enabled")
public boolean enabled;
}

protected void process() {
try {
RequestAuth downstreamAuth = new RequestAuth();
downstreamAuth.apiToken = getConfiguration().downstream.apiToken;
String requestId = getRequestId();
attendeeService.performGetAttendee(attendeeId, downstreamAuth, requestId); // ensure present etc.

try {
addInfo = attendeeService.performGetAdditionalInfo(attendeeId, area, downstreamAuth, requestId);
} catch (NotFoundException ignore) {
addInfo = null;
}

if (getMethod() == HttpMethod.POST) {
addInfo = new AddInfo();
addInfo.put("enabled", "yes");
attendeeService.performSetAdditionalInfo(attendeeId, area, addInfo, downstreamAuth, requestId);
} else if (getMethod() == HttpMethod.DELETE) {
try {
attendeeService.performDeleteAdditionalInfo(attendeeId, area, downstreamAuth, requestId);
} catch (NotFoundException ignore) {}
addInfo = null;
}
} catch (NotFoundException e) {
throw new ServiceException("addinfoapi.attendee.notfound", getRequestId());
} catch (DownstreamException e) {
throw new ServiceException("addinfoapi.downstream.failure", getRequestId());
}
}

protected ResponseDTO createResponse() {
ResponseDTO response = new ResponseDTO();
response.enabled = addInfo != null;
return response;
}

@Override
public void handle() {
try {
authenticate();
parseParameters();
process();
jsonServiceResponse(createResponse());
} catch (ServiceException e) {
jsonServiceResponse(e);
}
}
}
19 changes: 17 additions & 2 deletions src/java/org/eurofurence/regsys/web/services/PackageApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected Map<String,String> authForPackage() {
if (downstream.ddToken != null && !"".equals(downstream.ddToken)) {
allowed.put("dealer-half", downstream.ddToken);
allowed.put("dealer-full", downstream.ddToken);
allowed.put("dealer-fullplus", downstream.ddToken);
allowed.put("dealer-double", downstream.ddToken);
allowed.put("dealer-quad", downstream.ddToken);
}
Expand All @@ -57,8 +58,22 @@ protected Map<String,String> authForPackage() {
allowed.put("boat-benefactor", downstream.boatToken);
}
if (downstream.artshowToken != null && !"".equals(downstream.artshowToken)) {
allowed.put("artshow-table", downstream.artshowToken);
allowed.put("artshow-panel", downstream.artshowToken);
allowed.put("artshow-table-half", downstream.artshowToken);
allowed.put("artshow-table-one", downstream.artshowToken);
allowed.put("artshow-table-oneandhalf", downstream.artshowToken);
allowed.put("artshow-table-two", downstream.artshowToken);
allowed.put("artshow-table-twoandhalf", downstream.artshowToken);
allowed.put("artshow-table-three", downstream.artshowToken);
allowed.put("artshow-table-threeandhalf", downstream.artshowToken);
allowed.put("artshow-table-four", downstream.artshowToken);
allowed.put("artshow-panel-half", downstream.artshowToken);
allowed.put("artshow-panel-one", downstream.artshowToken);
allowed.put("artshow-panel-oneandhalf", downstream.artshowToken);
allowed.put("artshow-panel-two", downstream.artshowToken);
allowed.put("artshow-panel-twoandhalf", downstream.artshowToken);
allowed.put("artshow-panel-three", downstream.artshowToken);
allowed.put("artshow-panel-threeandhalf", downstream.artshowToken);
allowed.put("artshow-panel-four", downstream.artshowToken);
}
}
return allowed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.function.Supplier;

import org.eurofurence.regsys.web.services.AddInfoApi;
import org.eurofurence.regsys.web.services.DealersDenApi;
import org.eurofurence.regsys.web.services.NosecounterApi;
import org.eurofurence.regsys.web.services.NotFound;
Expand All @@ -23,6 +24,7 @@ public class ServiceRequestHandlerFactory {
suppliersByPathInfo.put("/summerboat-api", SummerBoatApi::new);
suppliersByPathInfo.put("/security-system-api", SecuritySystemApi::new);
suppliersByPathInfo.put("/package-api", PackageApi::new);
suppliersByPathInfo.put("/addinfo-api", AddInfoApi::new);
}

public static RequestHandler createByPathInfo(String pathInfo) {
Expand Down

0 comments on commit ec84197

Please sign in to comment.