Skip to content

Commit db12bfe

Browse files
committed
Fixes #684
1 parent a52c344 commit db12bfe

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

server/src/main/java/invite/crm/CRMController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,30 @@ public ResponseEntity<ResendInvitationResponse> resendInvitation(@RequestBody Re
318318
return ResponseEntity.ok(new ResendInvitationResponse(timestamp, 200, "ok", "Resend invitation"));
319319
}
320320

321+
@GetMapping(value = "/crm/api/v1/organisations", produces = MediaType.APPLICATION_JSON_VALUE)
322+
@Operation(summary = "Get all CRM organisations")
323+
@SecurityRequirement(name = API_HEADER_SCHEME_NAME)
324+
@PreAuthorize("hasRole('CRM')")
325+
public ResponseEntity<List<CRMOrganisation>> organisations() {
326+
List<Organisation> organisations = organisationRepository.findAll();
327+
List<CRMOrganisation> crmOrganisations = organisations.stream().map(organisation -> new CRMOrganisation(
328+
organisation.getCrmOrganisationId(),
329+
organisation.getCrmOrganisationAbbrevation(),
330+
organisation.getCrmOrganisationName()
331+
)).toList();
332+
return ResponseEntity.ok(crmOrganisations);
333+
}
334+
335+
@DeleteMapping(value = "/crm/api/v1/organisations", produces = MediaType.APPLICATION_JSON_VALUE)
336+
@Operation(summary = "Delete CRM organisation")
337+
@SecurityRequirement(name = API_HEADER_SCHEME_NAME)
338+
@PreAuthorize("hasRole('CRM')")
339+
public ResponseEntity<Void> deleteOrganisation(@RequestBody CRMOrganisation crmOrganisation) {
340+
Optional<Organisation> optionalOrganisation = organisationRepository.findByCrmOrganisationId(crmOrganisation.getOrganisationId());
341+
optionalOrganisation.ifPresent(organisation -> organisationRepository.delete(organisation));
342+
return ResponseEntity.status(204).build();
343+
}
344+
321345
private ProfileResponse crmUserNotFoundOrNoRoles() {
322346
return new ProfileResponse("Could not find any profiles with the given search parameters", 50, List.of());
323347
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ALTER TABLE roles
2+
DROP CONSTRAINT fk_roles_organisation;
3+
ALTER TABLE roles ADD CONSTRAINT fk_roles_organisation
4+
FOREIGN KEY (organisation_id) REFERENCES organisations (id)
5+
ON DELETE CASCADE
6+
ON UPDATE CASCADE;
7+
8+
ALTER TABLE users
9+
DROP CONSTRAINT fk_users_organisation;
10+
ALTER TABLE users
11+
ADD CONSTRAINT fk_users_organisation
12+
FOREIGN KEY (organisation_id) REFERENCES organisations (id)
13+
ON DELETE CASCADE
14+
ON UPDATE CASCADE;

server/src/test/java/invite/crm/CRMControllerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,35 @@ void emptyProfile() {
649649
assertEquals(50, profileResponse.code());
650650
}
651651

652+
@Test
653+
void organisations() {
654+
List<CRMOrganisation> organisations = given()
655+
.when()
656+
.accept(ContentType.JSON)
657+
.header(API_KEY_HEADER, "secret")
658+
.contentType(ContentType.JSON)
659+
.get("/crm/api/v1/organisations")
660+
.as(new TypeRef<>() {
661+
});
662+
assertEquals(1, organisations.size());
663+
assertEquals(CRM_ORGANIZATION_ID, organisations.getFirst().getOrganisationId());
664+
}
665+
666+
@Test
667+
void deleteOrganisation() {
668+
given()
669+
.when()
670+
.accept(ContentType.JSON)
671+
.header(API_KEY_HEADER, "secret")
672+
.contentType(ContentType.JSON)
673+
.body(new CRMOrganisation(CRM_ORGANIZATION_ID, "abbr", "name"))
674+
.delete("/crm/api/v1/organisations")
675+
.then()
676+
.statusCode(204);
677+
Optional<Organisation> optionalOrganisation = organisationRepository.findByCrmOrganisationId(CRM_ORGANIZATION_ID);
678+
assertTrue(optionalOrganisation.isEmpty());
679+
}
680+
652681
private void seedCRMData() {
653682
Organisation organisation = organisationRepository.findByCrmOrganisationId(CRM_ORGANIZATION_ID).get();
654683
Role role = new Role();

0 commit comments

Comments
 (0)