feat: admin guest, invite email, check-in and attendance endpoints
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
4e938bb6a9
commit
87dfc48632
19 files changed
+1361
-7
No files matched your search
+6
@@ -35,6 +35,12 @@ public class RestApiCounterServiceConstants {
|
||||
//Event admin
|
||||
public static final String API_REST_COUNTER_EVENT_FIND_BY_PROPERTY_AND_PAGEABLE="/counter/event/findByPropertyAndPageable";
|
||||
public static final String API_REST_COUNTER_EVENT_SAVE="/counter/event/save";
|
||||
public static final String API_REST_COUNTER_EVENT_GUEST_LIST = "/counter/eventGuest/list";
|
||||
public static final String API_REST_COUNTER_EVENT_GUEST_ADD = "/counter/eventGuest/add";
|
||||
public static final String API_REST_COUNTER_EVENT_GUEST_REMOVE = "/counter/eventGuest/remove";
|
||||
public static final String API_REST_COUNTER_EVENT_GUEST_MARK_SENT = "/counter/eventGuest/markSent";
|
||||
public static final String API_REST_COUNTER_CHECKIN_ENSURE_TOKEN = "/counter/checkin/ensureToken";
|
||||
public static final String API_REST_COUNTER_EVENT_ATTENDANCE = "/counter/event/attendance";
|
||||
public static final String API_REST_COUNTER_EVENT_SALESPERSON_FIND_BY_EVENT_UUID="/counter/eventSalesperson/findByEventUuid/{eventUuid}";
|
||||
public static final String API_REST_COUNTER_EVENT_SALESPERSON_REPLACE="/counter/eventSalesperson/replace";
|
||||
public static final String API_REST_COUNTER_INTEREST_FIND_BY_COMPANY="/counter/interest/findByCompany/{companyId}";
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestSendRequest;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestsRequest;
|
||||
import com.solvway.mobile.api.service.AdminEventGuestService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import com.solvway.mobile.api.useraccess.Audited;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Event invitations: guest list, send, check-in token and attendance. Must stay under {@code controller..} for AppAccessAspect. */
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/events/{uuid}")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminEventGuestController {
|
||||
private final AdminEventGuestService guests;
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_EVENTS")
|
||||
@GetMapping("/guests")
|
||||
public ResponseEntity<List<Map<String, Object>>> list(@PathVariable String uuid) {
|
||||
return ResponseEntity.ok(guests.list(uuid));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule(value = "BO_EVENT_GUESTS", also = "BO_EVENTS")
|
||||
@Audited(targetVariable = "uuid")
|
||||
@PostMapping("/guests")
|
||||
public ResponseEntity<Map<String, Object>> add(@PathVariable String uuid, @RequestBody AdminGuestsRequest body,
|
||||
@RequestParam(defaultValue = "false") boolean preview) {
|
||||
return ResponseEntity.ok(guests.add(uuid, body, preview));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule(value = "BO_EVENT_GUESTS", also = "BO_EVENTS")
|
||||
@Audited(targetVariable = "uuid")
|
||||
@DeleteMapping("/guests/{guestId}")
|
||||
public ResponseEntity<Void> remove(@PathVariable String uuid, @PathVariable String guestId) {
|
||||
guests.remove(uuid, guestId);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule(value = "BO_EVENT_GUESTS", also = "BO_EVENTS")
|
||||
@Audited(targetVariable = "uuid")
|
||||
@PostMapping("/guests/send")
|
||||
public ResponseEntity<Map<String, Object>> send(@PathVariable String uuid, @RequestBody AdminGuestSendRequest body) {
|
||||
return ResponseEntity.ok(guests.send(uuid, body));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_EVENTS")
|
||||
@GetMapping("/checkin")
|
||||
public ResponseEntity<Map<String, Object>> checkin(@PathVariable String uuid) {
|
||||
return ResponseEntity.ok(guests.checkin(uuid));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@AdminModule("BO_EVENTS")
|
||||
@GetMapping("/attendance")
|
||||
public ResponseEntity<Map<String, Object>> attendance(@PathVariable String uuid) {
|
||||
return ResponseEntity.ok(guests.attendance(uuid));
|
||||
}
|
||||
}
|
||||
+7
-2
@@ -2,6 +2,11 @@ package com.solvway.mobile.api.dto.admin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** An event lead's working fields. nextFollowUpDate is YYYY-MM-DD; null clears it. */
|
||||
/** An event lead's working fields. nextFollowUpDate is YYYY-MM-DD; null clears it. attended null leaves the flag alone. */
|
||||
public record AdminEventLeadUpdateRequest(Long salespersonId, String temperature, String status, List<Long> interestIds,
|
||||
String nextFollowUpDate, String notes) {}
|
||||
String nextFollowUpDate, String notes, Boolean attended) {
|
||||
public AdminEventLeadUpdateRequest(Long salespersonId, String temperature, String status, List<Long> interestIds,
|
||||
String nextFollowUpDate, String notes) {
|
||||
this(salespersonId, temperature, status, interestIds, nextFollowUpDate, notes, null);
|
||||
}
|
||||
}
|
||||
+8
-3
@@ -6,12 +6,17 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Create/update body. Dates are kept as text so a malformed date answers INVALID_DATE, not a bind error. enabled is update-only.
|
||||
* companyId is read on create only, and only from a super admin; staff get their own company and an edit never moves an event.
|
||||
* capacity and invitationCap (null = no limit) are read only from a caller holding BO_EVENT_GUESTS. companyId is read on create only, and only from a super admin; staff get their own company and an edit never moves an event.
|
||||
*/
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AdminEventRequest(String name, String location, String startDate, String endDate, Boolean enabled, List<Long> salespersonIds,
|
||||
Long companyId) {
|
||||
Long companyId, Integer capacity, Integer invitationCap) {
|
||||
public AdminEventRequest(String name, String location, String startDate, String endDate, Boolean enabled, List<Long> salespersonIds,
|
||||
Long companyId) {
|
||||
this(name, location, startDate, endDate, enabled, salespersonIds, companyId, null, null);
|
||||
}
|
||||
|
||||
public AdminEventRequest(String name, String location, String startDate, String endDate, Boolean enabled, List<Long> salespersonIds) {
|
||||
this(name, location, startDate, endDate, enabled, salespersonIds, null);
|
||||
this(name, location, startDate, endDate, enabled, salespersonIds, null, null, null);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.solvway.mobile.api.dto.admin;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Which guests to email: the listed ids, or every guest not yet sent when allUnsent is true. */
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AdminGuestSendRequest(List<Long> guestIds, Boolean allUnsent) {}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.solvway.mobile.api.dto.admin;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Guests to invite to an event: one line for a single add, many for a paste. Same shape either way. */
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AdminGuestsRequest(List<Guest> guests) {
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record Guest(String name, String mobile, String email, String companyName, Long salespersonUserId) {}
|
||||
}
|
||||
@@ -9,4 +9,6 @@ import lombok.Setter;
|
||||
@Setter
|
||||
public class CompanyEvent extends Event {
|
||||
private Long companyId;
|
||||
private Integer capacity;
|
||||
private Integer invitationCap;
|
||||
}
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.core.enums.EmailEnum;
|
||||
import com.solvway.core.model.Email;
|
||||
import com.solvway.core.model.Recipient;
|
||||
import com.solvway.mobile.api.constant.api.RestApiCounterServiceConstants;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestSendRequest;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestsRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AdminModuleAccess;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Invited-guest list of an event for the admin portal. Validates and normalises at this boundary; services-counter stores.
|
||||
* Every operation resolves the event through AdminEventLeadService.loadEvent, so another company's event is a 404.
|
||||
* Never log a guest's name, mobile or email.
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@SuppressWarnings("unchecked")
|
||||
public class AdminEventGuestService {
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> MAP_LIST = new ParameterizedTypeReference<>() {};
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private final RestTemplateCustomUtil rest;
|
||||
private final AdminEventLeadService leads;
|
||||
private final EmailService emailService;
|
||||
private final String portalBase;
|
||||
|
||||
public AdminEventGuestService(RestTemplateCustomUtil rest, AdminEventLeadService leads, EmailService emailService, @Value("${app.admin-portal-base-url}") String portalBase) {
|
||||
this.rest = rest;
|
||||
this.leads = leads;
|
||||
this.emailService = emailService;
|
||||
this.portalBase = portalBase.replaceAll("/+$", "");
|
||||
log.info("Admin portal base URL for invitation links: {}", this.portalBase);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> list(String uuid) {
|
||||
CompanyEvent event = leads.loadEvent(uuid, null);
|
||||
boolean mayCopyLinks = AdminModuleAccess.holds(rest, leads.caller(), "BO_EVENT_GUESTS");
|
||||
Map<Long, Object> names = new HashMap<>();
|
||||
List<Map<String, Object>> out = new ArrayList<>();
|
||||
for (Map<String, Object> g : guests(event)) {
|
||||
Map<String, Object> row = new LinkedHashMap<>(g);
|
||||
// Only the guest-list module may copy invitation links, so the token stays with it.
|
||||
if (!mayCopyLinks) row.remove("token");
|
||||
if (g.get("salespersonUserId") instanceof Number n) {
|
||||
row.put("salespersonName", names.computeIfAbsent(n.longValue(), id -> {
|
||||
Map<String, Object> user = leads.user(id);
|
||||
return user == null ? null : user.get("name");
|
||||
}));
|
||||
}
|
||||
out.add(row);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> guests(CompanyEvent event) {
|
||||
List<Map<String, Object>> guests = rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_GUEST_LIST), MAP_LIST,
|
||||
eventBody(event)).getBody();
|
||||
return guests == null ? List.of() : guests;
|
||||
}
|
||||
|
||||
/** Valid lines go to the counter (unless previewing); rejections of both kinds come back with the line's original index. */
|
||||
public Map<String, Object> add(String uuid, AdminGuestsRequest r, boolean preview) {
|
||||
CompanyEvent event = leads.loadEvent(uuid, null);
|
||||
if (r == null || r.guests() == null || r.guests().isEmpty()) throw AdminEventLeadService.required(List.of("guests"));
|
||||
List<Map<String, Object>> valid = new ArrayList<>();
|
||||
List<Integer> validIndexes = new ArrayList<>();
|
||||
List<Map<String, Object>> rejected = new ArrayList<>();
|
||||
for (int i = 0; i < r.guests().size(); i++) {
|
||||
try {
|
||||
valid.add(normalise(r.guests().get(i)));
|
||||
validIndexes.add(i);
|
||||
} catch (CodedResponseStatusException e) {
|
||||
rejected.add(rejection(i, e.getCode(), field(e), e.getReason()));
|
||||
}
|
||||
}
|
||||
List<Object> added = new ArrayList<>(valid);
|
||||
if (!preview && !valid.isEmpty()) {
|
||||
Map<String, Object> body = eventBody(event);
|
||||
body.put("guests", valid);
|
||||
body.put("doneBy", leads.caller().getUsername());
|
||||
Map<String, Object> result;
|
||||
try {
|
||||
result = rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_GUEST_ADD), MAP, body).getBody();
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw AdminEventLeadService.error(HttpStatus.NOT_FOUND, "Event not found", "EVENT_NOT_FOUND", null);
|
||||
}
|
||||
added = result == null || result.get("added") == null ? new ArrayList<>() : new ArrayList<>((List<Object>) result.get("added"));
|
||||
if (result != null && result.get("rejected") instanceof List<?> counterRejected) {
|
||||
for (Object o : counterRejected) {
|
||||
Map<String, Object> m = new LinkedHashMap<>((Map<String, Object>) o);
|
||||
int at = m.get("index") instanceof Number n ? n.intValue() : -1;
|
||||
if (at < 0 || at >= validIndexes.size()) {
|
||||
log.warn("Counter rejected a guest line outside the request for event {}", event.getUuid());
|
||||
continue;
|
||||
}
|
||||
m.put("index", validIndexes.get(at));
|
||||
rejected.add(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
rejected.sort(Comparator.comparingInt(m -> (Integer) m.get("index")));
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("added", added);
|
||||
out.put("rejected", rejected);
|
||||
return out;
|
||||
}
|
||||
|
||||
public void remove(String uuid, String guestId) {
|
||||
CompanyEvent event = leads.loadEvent(uuid, null);
|
||||
Map<String, Object> body = eventBody(event);
|
||||
body.put("guestId", AdminEventLeadService.parseOr(guestId,
|
||||
() -> AdminEventLeadService.error(HttpStatus.NOT_FOUND, "Guest not found", "GUEST_NOT_FOUND", null)));
|
||||
body.put("doneBy", leads.caller().getUsername());
|
||||
try {
|
||||
rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_GUEST_REMOVE), MAP, body);
|
||||
} catch (HttpClientErrorException.Conflict e) {
|
||||
throw AdminEventLeadService.error(HttpStatus.CONFLICT, "Guest has registered and cannot be removed", "GUEST_REGISTERED", null);
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
throw AdminEventLeadService.error(HttpStatus.NOT_FOUND, "Guest not found", "GUEST_NOT_FOUND", null);
|
||||
}
|
||||
}
|
||||
|
||||
/** One email per guest, English then Bahasa Malaysia. Only guests whose email was accepted are marked as sent. */
|
||||
public Map<String, Object> send(String uuid, AdminGuestSendRequest r) {
|
||||
CompanyEvent event = leads.loadEvent(uuid, null);
|
||||
boolean allUnsent = r != null && Boolean.TRUE.equals(r.allUnsent());
|
||||
if (!allUnsent && (r == null || r.guestIds() == null || r.guestIds().isEmpty())) {
|
||||
throw AdminEventLeadService.required(List.of("guestIds"));
|
||||
}
|
||||
Set<Long> wanted = allUnsent ? Set.of() : new HashSet<>(r.guestIds());
|
||||
List<Long> sentIds = new ArrayList<>();
|
||||
int skippedNoEmail = 0;
|
||||
try {
|
||||
for (Map<String, Object> g : guests(event)) {
|
||||
long id = ((Number) g.get("id")).longValue();
|
||||
boolean selected = allUnsent ? !(g.get("inviteSentCount") instanceof Number n && n.intValue() > 0) : wanted.contains(id);
|
||||
if (!selected) continue;
|
||||
String to = AdminEventLeadService.trimmed(AdminEventLeadService.str(g.get("email")));
|
||||
if (to == null) {
|
||||
skippedNoEmail++;
|
||||
continue;
|
||||
}
|
||||
if (deliver(event, g, to)) sentIds.add(id);
|
||||
}
|
||||
} finally {
|
||||
// Whatever was queued is marked, even if a later guest blew up, so a retry does not email them twice.
|
||||
if (!sentIds.isEmpty()) {
|
||||
Map<String, Object> body = eventBody(event);
|
||||
body.put("guestIds", sentIds);
|
||||
body.put("doneBy", leads.caller().getUsername());
|
||||
rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_GUEST_MARK_SENT), MAP, body);
|
||||
}
|
||||
}
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("sent", sentIds.size());
|
||||
out.put("skippedNoEmail", skippedNoEmail);
|
||||
return out;
|
||||
}
|
||||
|
||||
public Map<String, Object> checkin(String uuid) {
|
||||
return rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_CHECKIN_ENSURE_TOKEN), MAP,
|
||||
eventBody(leads.loadEvent(uuid, null))).getBody();
|
||||
}
|
||||
|
||||
public Map<String, Object> attendance(String uuid) {
|
||||
return rest.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_ATTENDANCE), MAP,
|
||||
eventBody(leads.loadEvent(uuid, null))).getBody();
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private boolean deliver(CompanyEvent event, Map<String, Object> guest, String to) {
|
||||
try {
|
||||
Email email = new Email();
|
||||
email.setCompanyId(event.getCompanyId());
|
||||
email.setSubject("You are invited / Anda dijemput: " + event.getName());
|
||||
email.setBody(invitationBody(event, guest));
|
||||
email.setStatus(EmailEnum.StatusType.P);
|
||||
email.setRecipients(List.of(new Recipient(null, to, EmailEnum.RecipientType.R, EmailEnum.RecipientStatusType.Y)));
|
||||
Optional<Email> sent = emailService.send(email);
|
||||
return sent != null && sent.isPresent();
|
||||
} catch (RuntimeException e) {
|
||||
log.error("Invitation email failed for event {}: {}", event.getUuid(), e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String invitationBody(CompanyEvent event, Map<String, Object> guest) {
|
||||
String name = esc(guest.get("name"));
|
||||
String link = esc(portalBase + "/i/" + guest.get("token"));
|
||||
String details = "<p><strong>" + esc(event.getName()) + "</strong><br>" + esc(date(event.getDateStart())) + " - "
|
||||
+ esc(date(event.getDateEnd())) + "<br>" + esc(event.getLocation()) + "</p>";
|
||||
return "<p>Dear " + name + ",</p><p>You are invited to our event.</p>" + details
|
||||
+ "<p>Please confirm your attendance: <a href=\"" + link + "\">" + link + "</a></p>"
|
||||
+ "<hr><p><i>Bahasa Malaysia</i></p><p>Tuan/Puan " + name + ",</p><p>Anda dijemput ke acara kami.</p>" + details
|
||||
+ "<p>Sila sahkan kehadiran anda: <a href=\"" + link + "\">" + link + "</a></p>";
|
||||
}
|
||||
|
||||
private static Object date(java.time.LocalDateTime d) {
|
||||
return d == null ? null : d.toLocalDate();
|
||||
}
|
||||
|
||||
private static String esc(Object value) {
|
||||
return HtmlUtils.htmlEscape(String.valueOf(value == null ? "" : value));
|
||||
}
|
||||
|
||||
/** Trims, normalises mobile to E.164 and email to lower case; throws the coded error of the first problem found. */
|
||||
private static Map<String, Object> normalise(AdminGuestsRequest.Guest g) {
|
||||
String name = AdminEventLeadService.trimmed(g == null ? null : g.name());
|
||||
if (name == null) throw AdminEventLeadService.required(List.of("name"));
|
||||
AdminEventLeadService.maxLength("name", name, 100);
|
||||
String mobile = AdminEventLeadService.trimmed(g.mobile());
|
||||
String email = AdminEventLeadService.trimmed(g.email());
|
||||
if (mobile == null && email == null) throw AdminEventLeadService.required(List.of("mobile", "email"));
|
||||
String companyName = AdminEventLeadService.maxLength("companyName", AdminEventLeadService.trimmed(g.companyName()), 150);
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("name", name);
|
||||
m.put("mobileE164", mobile == null ? null : AdminEventLeadService.mobile(mobile));
|
||||
m.put("email", email == null ? null : AdminEventLeadService.maxLength("email", AdminEventLeadService.email(email), 254));
|
||||
m.put("companyName", companyName);
|
||||
m.put("salespersonUserId", g.salespersonUserId());
|
||||
return m;
|
||||
}
|
||||
|
||||
private static Map<String, Object> rejection(int index, String code, String field, String message) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("index", index);
|
||||
m.put("code", code);
|
||||
m.put("field", field);
|
||||
m.put("message", message);
|
||||
return m;
|
||||
}
|
||||
|
||||
private static String field(CodedResponseStatusException e) {
|
||||
return e.getBody().getProperties() != null && e.getBody().getProperties().get("details") instanceof Map<?, ?> d
|
||||
? AdminEventLeadService.str(d.get("field")) : null;
|
||||
}
|
||||
|
||||
private Map<String, Object> eventBody(CompanyEvent event) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("companyId", event.getCompanyId());
|
||||
body.put("eventUuid", event.getUuid());
|
||||
return body;
|
||||
}
|
||||
|
||||
private String url(String api) {
|
||||
return rest.getURLforApiService(api);
|
||||
}
|
||||
}
|
||||
+2
@@ -267,6 +267,7 @@ public class AdminEventLeadService {
|
||||
body.put("interestIds", interestIds);
|
||||
body.put("nextFollowUpDate", followUp == null ? null : followUp.toString());
|
||||
body.put("notes", trimmed(r.notes()));
|
||||
body.put("attended", r.attended());
|
||||
body.put("doneBy", caller.getUsername());
|
||||
Map<String, Object> saved = restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_LEAD_UPDATE), MAP, body).getBody();
|
||||
// field names only: follow-up values are personal data
|
||||
@@ -277,6 +278,7 @@ public class AdminEventLeadService {
|
||||
if (!new HashSet<>(interestIds).equals(new HashSet<>(longs(current.get("interestIds"))))) changed.add("interestIds");
|
||||
if (!Objects.equals(body.get("nextFollowUpDate"), current.get("nextFollowUpDate") == null ? null : current.get("nextFollowUpDate").toString())) changed.add("nextFollowUpDate");
|
||||
if (!Objects.equals(body.get("notes"), current.get("notes"))) changed.add("notes");
|
||||
if (r.attended() != null && !r.attended().equals(current.get("attended"))) changed.add("attended");
|
||||
if (!changed.isEmpty()) AuditChanges.fields(changed.toArray(String[]::new));
|
||||
return view(saved, known, new HashMap<>());
|
||||
}
|
||||
|
||||
+33
-1
@@ -1,6 +1,7 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
@@ -10,12 +11,14 @@ import com.solvway.mobile.api.dto.admin.AdminEventRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
import com.solvway.mobile.api.useraccess.AdminModuleAccess;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -68,6 +71,8 @@ public class AdminEventService {
|
||||
public Map<String, Object> get(String uuid) {
|
||||
CompanyEvent event = load(uuid);
|
||||
Map<String, Object> out = row(event);
|
||||
out.put("capacity", event.getCapacity());
|
||||
out.put("invitationCap", event.getInvitationCap());
|
||||
List<Map<String, Object>> team = new ArrayList<>();
|
||||
for (Long id : teamIds(event.getUuid())) {
|
||||
Map<String, Object> user = user(id);
|
||||
@@ -103,6 +108,11 @@ public class AdminEventService {
|
||||
CompanyEvent event = load(uuid);
|
||||
List<Long> team = r.salespersonIds() == null ? null : checkedTeam(r.salespersonIds(), teamIds(event.getUuid()), event.getCompanyId());
|
||||
apply(event, f);
|
||||
// Capacity and invitation cap belong to the guest-list module; other editors leave them as they are.
|
||||
if (AdminModuleAccess.holds(restTemplateUtil, caller, "BO_EVENT_GUESTS")) {
|
||||
event.setCapacity(positiveOrNull(r.capacity(), "capacity"));
|
||||
event.setInvitationCap(positiveOrNull(r.invitationCap(), "invitationCap"));
|
||||
}
|
||||
if (r.enabled() != null) event.setEnabled(r.enabled() ? GeneralEnum.FlagYesNo.Y : GeneralEnum.FlagYesNo.N);
|
||||
event.setDoneBy(caller.getUsername());
|
||||
CompanyEvent saved = save(event);
|
||||
@@ -110,6 +120,11 @@ public class AdminEventService {
|
||||
return row(saved);
|
||||
}
|
||||
|
||||
private static Integer positiveOrNull(Integer value, String field) {
|
||||
if (value != null && value < 1) throw error(HttpStatus.BAD_REQUEST, field + " must be at least 1", "INVALID_LIMIT", field);
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Active admins of the chosen company; a super admin who has not chosen one is offered nobody. */
|
||||
public List<Map<String, Object>> salespersonOptions(Long companyId) {
|
||||
Long company = scope.scope(caller(), companyId);
|
||||
@@ -236,7 +251,15 @@ public class AdminEventService {
|
||||
}
|
||||
|
||||
private CompanyEvent save(CompanyEvent event) {
|
||||
CompanyEvent saved = restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_SAVE), EVENT, event).getBody();
|
||||
CompanyEvent saved;
|
||||
try {
|
||||
saved = restTemplateUtil.postForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_SAVE), EVENT, event).getBody();
|
||||
} catch (HttpClientErrorException.Conflict e) {
|
||||
// The counter refuses a cap below the guests already on the list (CAP_BELOW_GUESTS).
|
||||
String code = counterField(e, "code", "CONFLICT");
|
||||
throw error(HttpStatus.CONFLICT, "CAP_BELOW_GUESTS".equals(code) ? "Limit is below the guests already invited"
|
||||
: counterField(e, "message", "Event could not be saved"), code, null);
|
||||
}
|
||||
if (saved == null) throw error(HttpStatus.INTERNAL_SERVER_ERROR, "Event could not be saved", "SAVE_FAILED", null);
|
||||
return saved;
|
||||
}
|
||||
@@ -253,6 +276,15 @@ public class AdminEventService {
|
||||
return m;
|
||||
}
|
||||
|
||||
private static String counterField(HttpClientErrorException e, String field, String fallback) {
|
||||
try {
|
||||
String v = new ObjectMapper().readTree(e.getResponseBodyAsString()).path(field).asText();
|
||||
return v.isEmpty() ? fallback : v;
|
||||
} catch (Exception ex) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private User caller() {
|
||||
User caller = adminAuthenticationService.loadUserByToken(HttpReqRespUtils.getJwtToken());
|
||||
if (caller == null) throw error(HttpStatus.UNAUTHORIZED, "Not signed in", "UNAUTHORIZED", null);
|
||||
|
||||
+22
-1
@@ -1,20 +1,41 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public final class AdminModuleAccess {
|
||||
|
||||
private static final String MODULES_BY_AREA = "/module/findAllByArea/{area}";
|
||||
|
||||
private static final long INJECTED_ROLE_ADMIN_ID = 2L; // services-auth adds ROLE_ADMIN to every admin; never a module grant
|
||||
|
||||
private AdminModuleAccess() {
|
||||
}
|
||||
|
||||
/** True for a super admin or a caller whose roles hold the module. A failing lookup fails closed (false, logged). */
|
||||
public static boolean holds(RestTemplateCustomUtil restTemplateUtil, User caller, String module) {
|
||||
if (AdminCompanyScope.isSuper(caller)) return true;
|
||||
Set<Long> roleIds = caller.getRoles() == null ? Set.of() : caller.getRoles().stream()
|
||||
.map(Role::getId).filter(id -> Objects.nonNull(id) && id != INJECTED_ROLE_ADMIN_ID).collect(Collectors.toSet());
|
||||
try {
|
||||
return grantedAny(restTemplateUtil, roleIds, module);
|
||||
} catch (RuntimeException e) {
|
||||
log.error("Admin module lookup for {} failed, treating as not granted: {}", module, e.getClass().getSimpleName());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** True when one of {@code roleIds} is granted any of the named BO modules. */
|
||||
public static boolean grantedAny(RestTemplateCustomUtil restTemplateUtil, Set<Long> roleIds, String... moduleNames) {
|
||||
if (moduleNames.length == 0) {
|
||||
@@ -24,7 +45,7 @@ public final class AdminModuleAccess {
|
||||
ResponseEntity<List<Map<String, Object>>> ent = restTemplateUtil.getForEntity(
|
||||
restTemplateUtil.getURLforApiService(MODULES_BY_AREA),
|
||||
new ParameterizedTypeReference<List<Map<String, Object>>>() {}, "BO");
|
||||
List<Map<String, Object>> modules = ent.getBody() != null ? ent.getBody() : List.of();
|
||||
List<Map<String, Object>> modules = ent != null && ent.getBody() != null ? ent.getBody() : List.of();
|
||||
return modules.stream()
|
||||
.filter(m -> m.get("name") instanceof String name && wanted.contains(name))
|
||||
// services-system writes a role already seen in this response as its bare id
|
||||
|
||||
@@ -5,3 +5,5 @@ spring:
|
||||
project-id: co-lite-local
|
||||
pubsub:
|
||||
emulator-host: pubsub:8085
|
||||
app:
|
||||
admin-portal-base-url: ${ADMIN_PORTAL_BASE_URL:http://localhost:3001}
|
||||
@@ -2,6 +2,8 @@ app:
|
||||
api-key: ${APIKEY:}
|
||||
api-gateway-url: ${APIGATEWAYURL}
|
||||
# TODO: confirm real customer-portal host
|
||||
# Required outside the local profile (no default): invitation emails link to <this>/i/<token>.
|
||||
admin-portal-base-url: ${ADMIN_PORTAL_BASE_URL}
|
||||
customer-portal-base-url: ${CUSTOMER_PORTAL_BASE_URL:https://user.tastefullyexpo.com}
|
||||
system:
|
||||
format:
|
||||
|
||||
Reference in new issue
Block a user