Skip to content

Commit ee22d7e

Browse files
committed
Fixes #718
1 parent 460210b commit ee22d7e

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.commons.logging.LogFactory;
4040
import org.springframework.beans.factory.annotation.Value;
4141
import org.springframework.core.io.Resource;
42+
import org.springframework.http.HttpStatus;
4243
import org.springframework.http.MediaType;
4344
import org.springframework.http.ResponseEntity;
4445
import org.springframework.security.access.prepost.PreAuthorize;
@@ -391,10 +392,15 @@ public ResponseEntity<List<CRMOrganisation>> organisations() {
391392
@Operation(summary = "Delete CRM organisation")
392393
@SecurityRequirement(name = API_HEADER_SCHEME_NAME)
393394
@PreAuthorize("hasRole('CRM')")
394-
public ResponseEntity<Void> deleteOrganisation(@RequestBody CRMOrganisation crmOrganisation) {
395+
public ResponseEntity<Map<String, String>> deleteOrganisation(@RequestBody CRMOrganisation crmOrganisation) {
395396
Optional<Organisation> optionalOrganisation = organisationRepository.findByCrmOrganisationId(crmOrganisation.getOrganisationId());
396-
optionalOrganisation.ifPresent(organisation -> organisationRepository.delete(organisation));
397-
return ResponseEntity.status(204).build();
397+
return optionalOrganisation
398+
.map(organisation -> {
399+
organisationRepository.delete(organisation);
400+
return ResponseEntity.ok(Map.of("status", "deleted"));
401+
}).orElseGet(() -> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(
402+
Map.of("status",
403+
String.format("Organisation with crmOrganisationId %s not found", crmOrganisation.getOrganisationId()))));
398404
}
399405

400406
@PostMapping(value = "/crm/api/v1/invite/send", produces = MediaType.APPLICATION_JSON_VALUE)

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.github.tomakehurst.wiremock.client.WireMock.getAllServeEvents;
3131
import static invite.security.SecurityConfig.API_KEY_HEADER;
3232
import static io.restassured.RestAssured.given;
33+
import static org.hamcrest.core.IsEqual.equalTo;
3334
import static org.junit.jupiter.api.Assertions.*;
3435

3536
class CRMControllerTest extends AbstractMailTest {
@@ -872,11 +873,26 @@ void deleteOrganisation() {
872873
.body(new CRMOrganisation(CRM_ORGANIZATION_ID, "abbr", "name"))
873874
.delete("/crm/api/v1/organisations")
874875
.then()
875-
.statusCode(204);
876+
.statusCode(200)
877+
.body("status", equalTo("deleted"));
876878
Optional<Organisation> optionalOrganisation = organisationRepository.findByCrmOrganisationId(CRM_ORGANIZATION_ID);
877879
assertTrue(optionalOrganisation.isEmpty());
878880
}
879881

882+
@Test
883+
void deleteOrganisationNotFound() {
884+
given()
885+
.when()
886+
.accept(ContentType.JSON)
887+
.header(API_KEY_HEADER, "secret")
888+
.contentType(ContentType.JSON)
889+
.body(new CRMOrganisation("nope", "abbr", "name"))
890+
.delete("/crm/api/v1/organisations")
891+
.then()
892+
.statusCode(400)
893+
.body("status", equalTo("Organisation with crmOrganisationId nope not found"));
894+
}
895+
880896
@Test
881897
void removeCRMRoles() throws JsonProcessingException {
882898
this.seedCRMData();

0 commit comments

Comments
 (0)