IntegrationEventService.java 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.example.hrlab.integration;
  2. import com.example.hrlab.common.JsonUtils;
  3. import com.example.hrlab.security.HmacService;
  4. import java.util.Map;
  5. import org.springframework.dao.DataIntegrityViolationException;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class IntegrationEventService {
  10. private final JdbcTemplate jdbcTemplate;
  11. private final HmacService hmacService;
  12. public IntegrationEventService(JdbcTemplate jdbcTemplate, HmacService hmacService) {
  13. this.jdbcTemplate = jdbcTemplate;
  14. this.hmacService = hmacService;
  15. }
  16. public Long createEvent(String eventType, String businessKey, Map<String, Object> payload) {
  17. String json = JsonUtils.toJson(payload);
  18. try {
  19. return jdbcTemplate.queryForObject("""
  20. INSERT INTO integration_events(event_type, business_key, payload_json, signature, status)
  21. VALUES (?, ?, ?::jsonb, ?, 'PENDING') RETURNING id
  22. """, Long.class, eventType, businessKey, json, hmacService.sign(json));
  23. } catch (DataIntegrityViolationException ex) {
  24. return jdbcTemplate.queryForObject("""
  25. SELECT id FROM integration_events WHERE event_type = ? AND business_key = ?
  26. """, Long.class, eventType, businessKey);
  27. }
  28. }
  29. }