diff --git a/server/StrDss.Api/Controllers/OrganizationsController.cs b/server/StrDss.Api/Controllers/OrganizationsController.cs index 24708fe8..d4d61863 100644 --- a/server/StrDss.Api/Controllers/OrganizationsController.cs +++ b/server/StrDss.Api/Controllers/OrganizationsController.cs @@ -234,5 +234,21 @@ public async Task> GetJurisdiction(long id) return Ok(jurisdiction); } + + [ApiAuthorize(Permissions.JurisdictionWrite)] + [HttpPut("localgovs/jurisdictions/{id}", Name = "UpdateJurisdiction")] + public async Task UpdateJurisdiction(JurisdictionUpdateDto dto, long id) + { + dto.OrganizationId = id; + + var errors = await _orgService.UpdateJurisdictionAsync(dto); + + if (errors.Any()) + { + return ValidationUtils.GetValidationErrorResult(errors, ControllerContext); + } + + return Ok(); + } } } diff --git a/server/StrDss.Service/FieldValidatorService.cs b/server/StrDss.Service/FieldValidatorService.cs index 3726a345..592ec949 100644 --- a/server/StrDss.Service/FieldValidatorService.cs +++ b/server/StrDss.Service/FieldValidatorService.cs @@ -23,6 +23,7 @@ public FieldValidatorService() BizLicenceValidationRule.LoadBizLicenceValidationRules(_rules); PlatformValidationRule.LoadPlatformUpdateValidationRules(_rules); LocalGovValidationRules.LoadLocalGovValidationRules(_rules); + JurisdictionValidationRules.LoadJurisdictionValidationRules(_rules); } public IEnumerable GetFieldValidationRules(string entityName) diff --git a/server/StrDss.Service/OrganizationService.cs b/server/StrDss.Service/OrganizationService.cs index 03b992cd..c9a28e8f 100644 --- a/server/StrDss.Service/OrganizationService.cs +++ b/server/StrDss.Service/OrganizationService.cs @@ -31,6 +31,7 @@ public interface IOrganizationService Task GetLocalGov(long id); Task>> UpdateLocalGovAsync(LocalGovUpdateDto dto); Task GetJurisdiction(long id); + Task>> UpdateJurisdictionAsync(JurisdictionUpdateDto dto); } public class OrganizationService : ServiceBase, IOrganizationService { @@ -301,5 +302,57 @@ private async Task>> ValidateLocalGovUpdateDto(L { return await _orgRepo.GetJurisdiction(id); } + + public async Task>> UpdateJurisdictionAsync(JurisdictionUpdateDto dto) + { + var errors = new Dictionary>(); + + await ValidateJurisdictionUpdateDto(dto, errors); + + if (errors.Any()) + { + return errors; + } + + await _orgRepo.UpdateJurisdictionAsync(dto); + + _unitOfWork.Commit(); + + return errors; + } + + private async Task>> ValidateJurisdictionUpdateDto(JurisdictionUpdateDto dto, Dictionary> errors) + { + var jurisdiction = await _orgRepo.GetJurisdiction(dto.OrganizationId); + + if (jurisdiction == null) + { + errors.AddItem("OrganizationId", $"Jurisdiction with ID {dto.OrganizationId} does not exist"); + return errors; + } + + if (dto.ManagingOrganizationId == null) + { + errors.AddItem("ManagingOrganizationId", $"The local government name field is required."); + return errors; + } + + var localGov = await _orgRepo.GetLocalGov(dto.ManagingOrganizationId.Value); + + if (localGov == null) + { + errors.AddItem("ManagingOrganizationId", $"Local government with ID {dto.ManagingOrganizationId} does not exist"); + return errors; + } + + if (!_validator.CommonCodes.Any()) + { + _validator.CommonCodes = await _codeSetRepo.LoadCodeSetAsync(); + } + + _validator.Validate(Entities.Jurisdiction, dto, errors); + + return errors; + } } }