feat(system): versioned company privacy notice
GET and POST /company/{id}/privacyNotice: the newest row is current and
saving always adds a version, so consent records keep pointing at the
text the visitor saw.
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
85b40424ce
commit
ab1643e36e
6 files changed
+323
No files matched your search
+37
@@ -0,0 +1,37 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.services.system.model.CompanyPrivacyNotice;
|
||||
import com.solvway.services.system.service.CompanyPrivacyNoticeService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/company")
|
||||
public class CompanyPrivacyNoticeController {
|
||||
|
||||
public record NoticeRequest(String textEn, String textMs, String doneBy) {}
|
||||
|
||||
private final CompanyPrivacyNoticeService service;
|
||||
|
||||
public CompanyPrivacyNoticeController(CompanyPrivacyNoticeService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/privacyNotice")
|
||||
public CompanyPrivacyNotice current(@PathVariable Long id) {
|
||||
return service.current(id);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/privacyNotice")
|
||||
public CompanyPrivacyNotice add(@PathVariable Long id, @RequestBody NoticeRequest body) {
|
||||
return service.add(id, body.textEn(), body.textMs(), body.doneBy());
|
||||
}
|
||||
|
||||
@ExceptionHandler({IllegalArgumentException.class, HttpMessageNotReadableException.class})
|
||||
public ResponseEntity<Map<String, String>> badRequest(RuntimeException e) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", String.valueOf(e.getMessage())));
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.solvway.services.system.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** Append-only privacy notice version; the newest row (highest id) of a company is current. */
|
||||
@Entity
|
||||
@Table(name = "company_privacy_notice")
|
||||
@Getter
|
||||
@Setter
|
||||
public class CompanyPrivacyNotice {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private Long companyId;
|
||||
|
||||
private String textEn;
|
||||
|
||||
private String textMs;
|
||||
|
||||
private String createdBy;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.solvway.services.system.repo;
|
||||
|
||||
import com.solvway.services.system.model.CompanyPrivacyNotice;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CompanyPrivacyNoticeRepository extends JpaRepository<CompanyPrivacyNotice, Long> {
|
||||
|
||||
Optional<CompanyPrivacyNotice> findFirstByCompanyIdOrderByIdDesc(Long companyId);
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.solvway.services.system.service;
|
||||
|
||||
import com.solvway.services.system.model.CompanyPrivacyNotice;
|
||||
import com.solvway.services.system.repo.CompanyPrivacyNoticeRepository;
|
||||
import com.solvway.services.system.repo.CompanyRepository;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class CompanyPrivacyNoticeService {
|
||||
|
||||
private static final int TEXT_MAX = 5000;
|
||||
|
||||
private final CompanyPrivacyNoticeRepository notices;
|
||||
private final CompanyRepository companies;
|
||||
|
||||
public CompanyPrivacyNoticeService(CompanyPrivacyNoticeRepository notices, CompanyRepository companies) {
|
||||
this.notices = notices;
|
||||
this.companies = companies;
|
||||
}
|
||||
|
||||
public CompanyPrivacyNotice current(Long companyId) {
|
||||
return notices.findFirstByCompanyIdOrderByIdDesc(companyId)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No privacy notice"));
|
||||
}
|
||||
|
||||
/** Always inserts a new version; existing rows are never updated or deleted. */
|
||||
public CompanyPrivacyNotice add(Long companyId, String textEn, String textMs, String doneBy) {
|
||||
if (!companies.existsById(companyId)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Company not found");
|
||||
CompanyPrivacyNotice n = new CompanyPrivacyNotice();
|
||||
n.setCompanyId(companyId);
|
||||
n.setTextEn(text(textEn, "English"));
|
||||
n.setTextMs(text(textMs, "Bahasa Malaysia"));
|
||||
n.setCreatedBy(doneBy);
|
||||
n.setCreatedAt(LocalDateTime.now());
|
||||
return notices.save(n);
|
||||
}
|
||||
|
||||
private static String text(String raw, String language) {
|
||||
String v = raw == null ? "" : raw.trim();
|
||||
if (v.isEmpty()) throw new IllegalArgumentException(language + " text is required");
|
||||
if (v.length() > TEXT_MAX) throw new IllegalArgumentException(language + " text must be at most " + TEXT_MAX + " characters");
|
||||
return v;
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.services.system.model.CompanyPrivacyNotice;
|
||||
import com.solvway.services.system.service.CompanyPrivacyNoticeService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CompanyPrivacyNoticeControllerTest {
|
||||
|
||||
@Mock private CompanyPrivacyNoticeService service;
|
||||
@InjectMocks private CompanyPrivacyNoticeController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
private static CompanyPrivacyNotice notice() {
|
||||
CompanyPrivacyNotice n = new CompanyPrivacyNotice();
|
||||
n.setId(5L);
|
||||
n.setCompanyId(1L);
|
||||
n.setTextEn("en");
|
||||
n.setTextMs("ms");
|
||||
n.setCreatedBy("admin");
|
||||
n.setCreatedAt(LocalDateTime.of(2026, 12, 1, 9, 0));
|
||||
return n;
|
||||
}
|
||||
|
||||
@Test
|
||||
void getReturnsCurrentNotice() throws Exception {
|
||||
when(service.current(1L)).thenReturn(notice());
|
||||
|
||||
mvc.perform(get("/company/1/privacyNotice")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(5)).andExpect(jsonPath("$.companyId").value(1))
|
||||
.andExpect(jsonPath("$.textEn").value("en")).andExpect(jsonPath("$.textMs").value("ms"))
|
||||
.andExpect(jsonPath("$.createdBy").value("admin")).andExpect(jsonPath("$.createdAt").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWithNoneIs404() throws Exception {
|
||||
when(service.current(1L)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "No privacy notice"));
|
||||
|
||||
mvc.perform(get("/company/1/privacyNotice")).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postAddsVersionAndReturnsIt() throws Exception {
|
||||
when(service.add(1L, "en", "ms", "admin")).thenReturn(notice());
|
||||
|
||||
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"textEn\":\"en\",\"textMs\":\"ms\",\"doneBy\":\"admin\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value(5));
|
||||
verify(service).add(1L, "en", "ms", "admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidTextIs400WithMessage() throws Exception {
|
||||
when(service.add(1L, "", "ms", null)).thenThrow(new IllegalArgumentException("English text is required"));
|
||||
|
||||
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"textEn\":\"\",\"textMs\":\"ms\"}"))
|
||||
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("English text is required"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownCompanyIs404() throws Exception {
|
||||
when(service.add(9L, "en", "ms", null)).thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "Company not found"));
|
||||
|
||||
mvc.perform(post("/company/9/privacyNotice").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"textEn\":\"en\",\"textMs\":\"ms\"}"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingBodyIs400() throws Exception {
|
||||
mvc.perform(post("/company/1/privacyNotice").contentType(MediaType.APPLICATION_JSON).content(""))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.solvway.services.system.service;
|
||||
|
||||
import com.solvway.services.system.model.CompanyPrivacyNotice;
|
||||
import com.solvway.services.system.repo.CompanyPrivacyNoticeRepository;
|
||||
import com.solvway.services.system.repo.CompanyRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class CompanyPrivacyNoticeServiceTest {
|
||||
|
||||
@Mock private CompanyPrivacyNoticeRepository notices;
|
||||
@Mock private CompanyRepository companies;
|
||||
private CompanyPrivacyNoticeService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new CompanyPrivacyNoticeService(notices, companies);
|
||||
when(companies.existsById(1L)).thenReturn(true);
|
||||
when(notices.save(any(CompanyPrivacyNotice.class))).thenAnswer(i -> i.getArgument(0));
|
||||
}
|
||||
|
||||
private static CompanyPrivacyNotice notice(Long id) {
|
||||
CompanyPrivacyNotice n = new CompanyPrivacyNotice();
|
||||
n.setId(id);
|
||||
n.setCompanyId(1L);
|
||||
return n;
|
||||
}
|
||||
|
||||
private static void assertStatus(Throwable e, int code) {
|
||||
assertThat(e).isInstanceOfSatisfying(ResponseStatusException.class, r -> assertThat(r.getStatusCode().value()).isEqualTo(code));
|
||||
}
|
||||
|
||||
@Test
|
||||
void currentIsTheNewestVersion() {
|
||||
CompanyPrivacyNotice newest = notice(7L);
|
||||
when(notices.findFirstByCompanyIdOrderByIdDesc(1L)).thenReturn(Optional.of(newest));
|
||||
|
||||
assertThat(service.current(1L)).isSameAs(newest);
|
||||
}
|
||||
|
||||
@Test
|
||||
void currentWithNoNoticeIs404() {
|
||||
when(notices.findFirstByCompanyIdOrderByIdDesc(1L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.current(1L)).satisfies(e -> assertStatus(e, 404));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInsertsANewRowTrimmedAndNeverTouchesOldOnes() {
|
||||
CompanyPrivacyNotice saved = service.add(1L, " English ", " Bahasa ", "admin");
|
||||
|
||||
assertThat(saved.getId()).isNull(); // always a fresh insert, never an update of an existing row
|
||||
assertThat(saved.getCompanyId()).isEqualTo(1L);
|
||||
assertThat(saved.getTextEn()).isEqualTo("English");
|
||||
assertThat(saved.getTextMs()).isEqualTo("Bahasa");
|
||||
assertThat(saved.getCreatedBy()).isEqualTo("admin");
|
||||
assertThat(saved.getCreatedAt()).isNotNull();
|
||||
verify(notices).save(any(CompanyPrivacyNotice.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothTextsAreRequired() {
|
||||
assertThatThrownBy(() -> service.add(1L, " ", "ms", "a")).isInstanceOf(IllegalArgumentException.class);
|
||||
assertThatThrownBy(() -> service.add(1L, "en", null, "a")).isInstanceOf(IllegalArgumentException.class);
|
||||
verify(notices, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void textsAreLimitedTo5000Characters() {
|
||||
assertThat(service.add(1L, "e".repeat(5000), "m".repeat(5000), "a")).isNotNull();
|
||||
assertThatThrownBy(() -> service.add(1L, "e".repeat(5001), "ms", "a")).isInstanceOf(IllegalArgumentException.class);
|
||||
assertThatThrownBy(() -> service.add(1L, "en", "m".repeat(5001), "a")).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addForUnknownCompanyIs404() {
|
||||
assertThatThrownBy(() -> service.add(9L, "en", "ms", "a")).satisfies(e -> assertStatus(e, 404));
|
||||
verify(notices, never()).save(any());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user