Skip to content

Commit

Permalink
Merge pull request #763 from bcgov/yj
Browse files Browse the repository at this point in the history
feat(dss-953)
  • Loading branch information
ychung-mot authored Oct 30, 2024
2 parents 047b1fb + 56a124e commit 6b38744
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/StrDss.Api/Controllers/OrganizationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,21 @@ public async Task<ActionResult<LocalGovViewDto>> GetJurisdiction(long id)

return Ok(jurisdiction);
}

[ApiAuthorize(Permissions.JurisdictionWrite)]
[HttpPut("localgovs/jurisdictions/{id}", Name = "UpdateJurisdiction")]
public async Task<ActionResult> UpdateJurisdiction(JurisdictionUpdateDto dto, long id)
{
dto.OrganizationId = id;

var errors = await _orgService.UpdateJurisdictionAsync(dto);

if (errors.Any())
{
return ValidationUtils.GetValidationErrorResult(errors, ControllerContext);
}

return Ok();
}
}
}
1 change: 1 addition & 0 deletions server/StrDss.Service/FieldValidatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public FieldValidatorService()
BizLicenceValidationRule.LoadBizLicenceValidationRules(_rules);
PlatformValidationRule.LoadPlatformUpdateValidationRules(_rules);
LocalGovValidationRules.LoadLocalGovValidationRules(_rules);
JurisdictionValidationRules.LoadJurisdictionValidationRules(_rules);
}

public IEnumerable<FieldValidationRule> GetFieldValidationRules(string entityName)
Expand Down
53 changes: 53 additions & 0 deletions server/StrDss.Service/OrganizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IOrganizationService
Task<LocalGovViewDto?> GetLocalGov(long id);
Task<Dictionary<string, List<string>>> UpdateLocalGovAsync(LocalGovUpdateDto dto);
Task<JurisdictionsViewDto?> GetJurisdiction(long id);
Task<Dictionary<string, List<string>>> UpdateJurisdictionAsync(JurisdictionUpdateDto dto);
}
public class OrganizationService : ServiceBase, IOrganizationService
{
Expand Down Expand Up @@ -301,5 +302,57 @@ private async Task<Dictionary<string, List<string>>> ValidateLocalGovUpdateDto(L
{
return await _orgRepo.GetJurisdiction(id);
}

public async Task<Dictionary<string, List<string>>> UpdateJurisdictionAsync(JurisdictionUpdateDto dto)
{
var errors = new Dictionary<string, List<string>>();

await ValidateJurisdictionUpdateDto(dto, errors);

if (errors.Any())
{
return errors;
}

await _orgRepo.UpdateJurisdictionAsync(dto);

_unitOfWork.Commit();

return errors;
}

private async Task<Dictionary<string, List<string>>> ValidateJurisdictionUpdateDto(JurisdictionUpdateDto dto, Dictionary<string, List<string>> 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;
}
}
}

0 comments on commit 6b38744

Please sign in to comment.