Skip to content

Commit

Permalink
Merge pull request #3210 from AtlasOfLivingAustralia/feature/issue3106
Browse files Browse the repository at this point in the history
fixes issue with deleting organisation social media link
  • Loading branch information
chrisala authored Jun 5, 2024
2 parents 8fe4ad2 + 6f76928 commit a3fb904
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
5 changes: 3 additions & 2 deletions grails-app/assets/javascripts/fieldcapture-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion grails-app/assets/javascripts/organisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"() {
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit a3fb904

Please sign in to comment.