@RequestMapping(method = RequestMethod.PUT, consumes = "application/json")
 @ResponseStatus(HttpStatus.OK)
 public AgreementRuleDTO update(@RequestBody AgreementRuleDTO dto) {
   AgreementRule rule =
       service.update(
           dto.getDestinationCode(),
           dto.getGoodsCode(),
           dto.isAllowed(),
           createVisasForService(dto),
           TEMP_USER_UID);
   return AgreementRuleDTO.build(rule);
 }
  private List<Pair<Department, Seniority>> createVisasForService(AgreementRuleDTO dto) {
    List<AgreementRuleVisaDTO> visaDTOs = dto.getVisas();

    if (visaDTOs == null || visaDTOs.isEmpty()) return new ArrayList<>();

    // AgreementRuleDTO provides only department code
    List<Department> departments = departmentRepository.findAll();
    // Key = department code, Value = department
    Map<String, Department> mapDepartment =
        departments.stream().collect(Collectors.toMap(Department::getCode, d -> d));

    return visaDTOs
        .stream()
        .map(v -> Pair.of(mapDepartment.get(v.getDepartmentCode()), Seniority.of(v.getSeniority())))
        .collect(Collectors.toList());
  }
  @RequestMapping(
      value = "/{destinationCode}/{goodsCode}",
      method = RequestMethod.GET,
      produces = "application/json")
  @ResponseStatus(HttpStatus.OK)
  public AgreementRuleDTO findByDestinationCodeAndGoodsCode(
      @PathVariable String destinationCode, @PathVariable String goodsCode) {
    AgreementRule rule =
        repository.findByDestinationCodeAndGoodsCode(
            destinationCode.trim().toUpperCase(), goodsCode.trim().toUpperCase());

    if (rule == null) {
      throw SpTranspBizError.AGR_RULE_DOESNT_EXIST.exception(destinationCode, goodsCode);
    }

    return AgreementRuleDTO.build(rule);
  }
 private List<AgreementRuleDTO> findByGoodsCode(String goodsCode) {
   List<AgreementRule> rules = repository.findByGoodsCode(goodsCode.trim().toUpperCase());
   return AgreementRuleDTO.build(rules);
 }
 private List<AgreementRuleDTO> findByDestinationCode(String destinationCode) {
   List<AgreementRule> rules =
       repository.findByDestinationCode(destinationCode.trim().toUpperCase());
   return AgreementRuleDTO.build(rules);
 }
 private List<AgreementRuleDTO> findAll() {
   List<AgreementRule> rules = repository.findAll();
   return AgreementRuleDTO.build(rules);
 }