87 lines
2.7 KiB
Java
87 lines
2.7 KiB
Java
package run.webup.platform;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.constraints.*;
|
|
import java.util.*;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/admin/integrations")
|
|
@PreAuthorize("hasAuthority('integrations:write')")
|
|
public class IntegrationController {
|
|
private final IntegrationService service;
|
|
private final JdbcTemplate db;
|
|
|
|
public IntegrationController(IntegrationService service, JdbcTemplate db) {
|
|
this.service = service;
|
|
this.db = db;
|
|
}
|
|
|
|
public record Save(@NotNull @Size(max = 40) Map<String, String> values, boolean enabled) {}
|
|
|
|
public record Send(
|
|
@NotBlank @Size(max = 254) String target,
|
|
@NotBlank @Size(max = 200) String subject,
|
|
@NotBlank @Size(max = 20000) String text,
|
|
@NotBlank @Size(max = 4000) String templateParams,
|
|
@NotBlank @Size(max = 200) String idempotencyKey) {}
|
|
|
|
@GetMapping
|
|
public Object list() {
|
|
return service.list();
|
|
}
|
|
|
|
@PutMapping("/{kind}")
|
|
public Object save(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable String kind,
|
|
@Valid @RequestBody Save v,
|
|
HttpServletRequest req) {
|
|
return service.save(kind, who.id(), v.values(), v.enabled(), AuthController.ip(req));
|
|
}
|
|
|
|
@PostMapping("/{kind}/test")
|
|
public Object test(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable String kind,
|
|
HttpServletRequest req) {
|
|
return service.test(kind, who.id(), AuthController.ip(req));
|
|
}
|
|
|
|
@PostMapping("/{kind}/send-test")
|
|
public Object send(
|
|
@AuthenticationPrincipal SecurityConfig.Identity who,
|
|
@PathVariable String kind,
|
|
@Valid @RequestBody Send v,
|
|
HttpServletRequest req) {
|
|
return service.send(
|
|
kind,
|
|
who.id(),
|
|
v.target(),
|
|
v.subject(),
|
|
v.text(),
|
|
v.templateParams(),
|
|
v.idempotencyKey(),
|
|
AuthController.ip(req));
|
|
}
|
|
|
|
@GetMapping("/logs")
|
|
public Object logs() {
|
|
return db.queryForList(
|
|
"SELECT l.id,c.kind,l.action,l.status,l.message,l.request_id,l.created_at FROM"
|
|
+ " integration_test_logs l JOIN integration_configs c ON c.id=l.config_id ORDER BY"
|
|
+ " l.created_at DESC LIMIT 200");
|
|
}
|
|
|
|
@GetMapping("/messages")
|
|
public Object messages() {
|
|
return db.queryForList(
|
|
"SELECT id,channel,recipient,status,error,request_id,created_at FROM message_records ORDER"
|
|
+ " BY created_at DESC LIMIT 200");
|
|
}
|
|
}
|