feat(pages): company logo and accent colour
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
b164110367
commit
dcbe8071b5
5 files changed
+133
-1
No files matched your search
@@ -39,7 +39,7 @@ Verify commands:
|
||||
|
||||
## 3. services-system
|
||||
|
||||
- [ ] 3.1 Company `logoImageId` and `accentColor`:
|
||||
- [x] 3.1 Company `logoImageId` and `accentColor`:
|
||||
- `#RRGGBB` validation;
|
||||
- a read of the branding by company code, with no user data.
|
||||
- Verify: `mvn -T 1C -B -q -pl solvway-services-system -am verify`
|
||||
|
||||
+13
@@ -44,6 +44,19 @@ public class CompanyController {
|
||||
return service.renameCode(id, body.get("code"));
|
||||
}
|
||||
|
||||
/** Body: logoImageId (nullable) and accentColor (#RRGGBB, blank clears). */
|
||||
@PostMapping("/{id}/branding")
|
||||
public Company saveBranding(@PathVariable Long id, @RequestBody Map<String, Object> body) {
|
||||
Object logo = body.get("logoImageId");
|
||||
return service.saveBranding(id, logo instanceof Number n ? n.longValue() : null, (String) body.get("accentColor"));
|
||||
}
|
||||
|
||||
/** Public: what a visitor's landing page shows about an active company. */
|
||||
@GetMapping("/branding/{code}")
|
||||
public ResponseEntity<Map<String, Object>> branding(@PathVariable String code) {
|
||||
return service.branding(code).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@ExceptionHandler({IllegalArgumentException.class, HttpMessageNotReadableException.class})
|
||||
public ResponseEntity<Map<String, String>> badRequest(RuntimeException e) {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", String.valueOf(e.getMessage())));
|
||||
|
||||
@@ -20,4 +20,10 @@ public class Company {
|
||||
|
||||
/** A active, I inactive. */
|
||||
private String status;
|
||||
|
||||
/** Landing-page logo: a services-counter page_image id of this company. */
|
||||
private Long logoImageId;
|
||||
|
||||
/** Landing-page accent colour, #RRGGBB. */
|
||||
private String accentColor;
|
||||
}
|
||||
+31
@@ -11,7 +11,11 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
@@ -19,6 +23,7 @@ public class CompanyService {
|
||||
|
||||
private static final int USERNAME_MAX = 255; // user.username column length
|
||||
private static final Pattern CODE = Pattern.compile("^[A-Z0-9-]{2,20}$");
|
||||
private static final Pattern ACCENT = Pattern.compile("^#[0-9A-F]{6}$");
|
||||
|
||||
private final CompanyRepository companies;
|
||||
private final UserRepository users;
|
||||
@@ -60,6 +65,32 @@ public class CompanyService {
|
||||
return saveUnique(c);
|
||||
}
|
||||
|
||||
/** Landing-page branding; mobile-api checks the logo is an image of this company. Blank clears a value. */
|
||||
@Transactional
|
||||
public Company saveBranding(Long id, Long logoImageId, String accentColor) {
|
||||
String accent = accentColor == null || accentColor.isBlank() ? null : accentColor.trim().toUpperCase(Locale.ROOT);
|
||||
if (accent != null && !ACCENT.matcher(accent).matches()) {
|
||||
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "accentColor|CompanyBadAccent");
|
||||
}
|
||||
Company c = companies.findById(id).orElseThrow(() -> notFound());
|
||||
c.setLogoImageId(logoImageId);
|
||||
c.setAccentColor(accent);
|
||||
return companies.save(c);
|
||||
}
|
||||
|
||||
/** What a visitor's page may know about an active company: no users, no settings. */
|
||||
public Optional<Map<String, Object>> branding(String code) {
|
||||
return companies.findByCodeIgnoreCase(code).filter(c -> "A".equals(c.getStatus())).map(c -> {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", c.getId());
|
||||
m.put("code", c.getCode());
|
||||
m.put("name", c.getName());
|
||||
m.put("logoImageId", c.getLogoImageId());
|
||||
m.put("accentColor", c.getAccentColor());
|
||||
return m;
|
||||
});
|
||||
}
|
||||
|
||||
/** Changes the code and the login name (local@code) of every admin of the company, atomically. */
|
||||
@Transactional
|
||||
public Company renameCode(Long id, String code) {
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.solvway.services.system.service;
|
||||
|
||||
import com.solvway.services.system.model.Company;
|
||||
import com.solvway.services.system.repo.CompanyRepository;
|
||||
import com.solvway.services.system.repo.UserRepository;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Map;
|
||||
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;
|
||||
|
||||
/** Spec landing-pages "Company Branding": logo and accent colour, and the public read that carries nothing else. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class CompanyBrandingTest {
|
||||
|
||||
@Mock private CompanyRepository companies;
|
||||
@Mock private UserRepository users;
|
||||
private CompanyService service;
|
||||
private Company ins;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new CompanyService(companies, users);
|
||||
when(companies.save(any(Company.class))).thenAnswer(i -> i.getArgument(0));
|
||||
ins = new Company();
|
||||
ins.setId(1L);
|
||||
ins.setName("INS");
|
||||
ins.setCode("INS");
|
||||
ins.setStatus("A");
|
||||
when(companies.findById(1L)).thenReturn(Optional.of(ins));
|
||||
when(companies.findByCodeIgnoreCase("ins")).thenReturn(Optional.of(ins));
|
||||
}
|
||||
|
||||
@Test
|
||||
void savesTheLogoAndAnUpperCasedAccentColour() {
|
||||
Company saved = service.saveBranding(1L, 5L, "#0b5fff");
|
||||
assertThat(saved.getLogoImageId()).isEqualTo(5L);
|
||||
assertThat(saved.getAccentColor()).isEqualTo("#0B5FFF");
|
||||
|
||||
saved = service.saveBranding(1L, null, " ");
|
||||
assertThat(saved.getLogoImageId()).isNull();
|
||||
assertThat(saved.getAccentColor()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesAColourThatIsNotHashSixHex() {
|
||||
for (String bad : new String[]{"blue", "#FFF", "0B5FFF", "#0B5FFG"}) {
|
||||
assertThatThrownBy(() -> service.saveBranding(1L, null, bad)).as(bad)
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
assertThat(e.getReason()).isEqualTo("accentColor|CompanyBadAccent");
|
||||
});
|
||||
}
|
||||
verify(companies, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBrandingReadHoldsOnlyPublicFieldsOfAnActiveCompany() {
|
||||
ins.setLogoImageId(5L);
|
||||
ins.setAccentColor("#0B5FFF");
|
||||
assertThat(service.branding("ins")).contains(Map.of("id", 1L, "code", "INS", "name", "INS", "logoImageId", 5L, "accentColor", "#0B5FFF"));
|
||||
|
||||
ins.setStatus("I");
|
||||
assertThat(service.branding("ins")).isEmpty();
|
||||
assertThat(service.branding("nope")).isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user