From 6f76928aba46077a802092a1674732d03fa2f71c Mon Sep 17 00:00:00 2001 From: temi Date: Wed, 5 Jun 2024 09:02:48 +1000 Subject: [PATCH] #3106 - fixes issue with deleting organisation social media link --- .../javascripts/fieldcapture-application.js | 5 +-- grails-app/assets/javascripts/organisation.js | 2 +- .../ala/merit/OrganisationController.groovy | 12 +++++++ .../merit/OrganisationControllerSpec.groovy | 31 ++++++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/grails-app/assets/javascripts/fieldcapture-application.js b/grails-app/assets/javascripts/fieldcapture-application.js index 5eb718e7e..00420732a 100644 --- a/grails-app/assets/javascripts/fieldcapture-application.js +++ b/grails-app/assets/javascripts/fieldcapture-application.js @@ -636,10 +636,11 @@ function Documents(options) { } return null; }; - self.addLink = function(role, url) { + self.addLink = function(role, url, documentId) { self.links.push(new DocumentViewModel({ role: role, - url: url + url: url, + documentId: documentId })); }; self.fixLinkDocumentIds = function(existingLinks) { diff --git a/grails-app/assets/javascripts/organisation.js b/grails-app/assets/javascripts/organisation.js index 85d2ce8de..acf7e2f76 100644 --- a/grails-app/assets/javascripts/organisation.js +++ b/grails-app/assets/javascripts/organisation.js @@ -139,7 +139,7 @@ OrganisationViewModel = function (props, options) { // links if (props.links) { $.each(props.links, function(i, link) { - self.addLink(link.role, link.url); + self.addLink(link.role, link.url, link.documentId); }); } diff --git a/grails-app/controllers/au/org/ala/merit/OrganisationController.groovy b/grails-app/controllers/au/org/ala/merit/OrganisationController.groovy index 1ee5634d3..40c805c62 100644 --- a/grails-app/controllers/au/org/ala/merit/OrganisationController.groovy +++ b/grails-app/controllers/au/org/ala/merit/OrganisationController.groovy @@ -175,6 +175,7 @@ class OrganisationController { } private void createOrUpdateOrganisation(String organisationId, Map organisationDetails) { + def originalOrganisation = organisationService.get(organisationId) def documents = organisationDetails.remove('documents') def links = organisationDetails.remove('links') def result = organisationService.update(organisationId, organisationDetails) @@ -193,6 +194,17 @@ class OrganisationController { } } + List existingLinks = links?.findResults { it.documentId } + List toDeleteLinks = originalOrganisation?.links?.findAll { !existingLinks.contains(it.documentId) } + // delete any links that were removed. + if (toDeleteLinks && !result.error) { + toDeleteLinks.each { link -> + if (link.documentId) { + documentService.delete(link.documentId) + } + } + } + if (result.error) { render result as JSON } else { diff --git a/src/test/groovy/au/org/ala/merit/OrganisationControllerSpec.groovy b/src/test/groovy/au/org/ala/merit/OrganisationControllerSpec.groovy index ab479507a..7e0e7be91 100644 --- a/src/test/groovy/au/org/ala/merit/OrganisationControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/merit/OrganisationControllerSpec.groovy @@ -6,6 +6,8 @@ import au.org.ala.merit.util.ProjectGroupingHelper import grails.converters.JSON import org.apache.http.HttpStatus import org.grails.plugins.excelimport.ExcelImportService +import org.grails.web.converters.marshaller.json.CollectionMarshaller +import org.grails.web.converters.marshaller.json.MapMarshaller import org.springframework.mock.web.MockMultipartFile import spock.lang.Specification import grails.testing.web.controllers.ControllerUnitTest @@ -44,6 +46,9 @@ class OrganisationControllerSpec extends Specification implements ControllerUnit controller.projectService = projectService controller.reportService = reportService controller.settingService = settingService + + grails.converters.JSON.registerObjectMarshaller(new MapMarshaller()) + grails.converters.JSON.registerObjectMarshaller(new CollectionMarshaller()) } def "only the about tab should be viewable anonymously"() { @@ -591,11 +596,35 @@ class OrganisationControllerSpec extends Specification implements ControllerUnit response.json == [success:true] } - private Map testOrganisation(String id="", boolean includeReports) { + def "should be able to delete links"() { + setup: + def testOrg = testOrganisation("id", true, true) + organisationService.get(_,_) >> testOrg + setupFcAdmin() + Map updatedOrg = new HashMap(testOrg) + updatedOrg.links = new ArrayList(updatedOrg.links) + Map linkRemoved = updatedOrg.links.pop() + + when: + request.method = "POST" + request.JSON = (updatedOrg as JSON).toString() + controller.ajaxUpdate('id') + + then: + 1 * documentService.delete(linkRemoved.documentId) >> 200 + 1 * documentService.saveLink([name:'link2', url:'url2', documentId: 'd2', organisationId: "id"]) >> [resp: [documentId: 'd1'], statusCode: 200] + 1 * organisationService.update(*_) >> [resp: [status:"ok"]] + response.json.status == "ok" + } + + private Map testOrganisation(String id="", boolean includeReports, boolean addLinks = false) { Map org = [organisationId:id, name:'name', description:'description', config:[:], inheritedConfig:[:]] if (includeReports) { org.reports = [[type:'report1', reportId:'r1', activityId:'a1', organisationId:id], [type:'report1', reportId:'r2', activityId:'a2', organisationId:id]] } + if (addLinks) { + org.links = [[name:'link1', url:'url1', documentId: 'd1'], [name:'link2', url:'url2', documentId: 'd2']] + } organisationService.get(id) >> org userService.getMembersOfProgram() >> [ [userId:adminUserId, role:RoleService.PROJECT_ADMIN_ROLE],