Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch update ID requires all required model properties to be passed in request #3541

Closed
bhupesh-sf opened this issue Aug 13, 2019 · 1 comment
Labels

Comments

@bhupesh-sf
Copy link

Steps to reproduce

  1. Have a model with some required properties
  2. Create a record
  3. try to partial update the record with Patch

Current Behavior

Screenshot from 2019-08-13 20-25-47

Expected Behavior

It should allow the record to be updated.

Additional information

Controller and model code below:

async updateById(
    @param.path.number('id') id: number,
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(EmployeeSalaryPackage, { partial: true }),
        },
      },
    })
    employeeSalaryPackage: EmployeeSalaryPackage,
  ): Promise<void> {
    const existingSalaryPackage: EmployeeSalaryPackage = await this.employeeSalaryPackageRepository.findById(id);

    let effective_date: string = '';
    let end_date: string | undefined = '';
    if (employeeSalaryPackage.effective_date || employeeSalaryPackage.end_date) {
      effective_date = (employeeSalaryPackage.effective_date) ? employeeSalaryPackage.effective_date : existingSalaryPackage.effective_date;

      end_date = (employeeSalaryPackage.end_date) ? employeeSalaryPackage.end_date : existingSalaryPackage.end_date

      if (end_date && end_date < effective_date) {
        throw new HttpErrors.BadRequest('End date cannot be less than effective date')
      }

      const overlappingSalaryPackage = await this.employeeSalaryPackageRepository.findOverlappingPackage(existingSalaryPackage.employee_id, effective_date, end_date, id)
      console.log(overlappingSalaryPackage);
      if (overlappingSalaryPackage) {
        throw new HttpErrors.Conflict('Overlapping salary package exists')
      }
    }

    if (employeeSalaryPackage.current_package) {
      employeeSalaryPackage.monthly_cost = employeeSalaryPackage.current_package / 12;
      employeeSalaryPackage.hourly_cost = employeeSalaryPackage.monthly_cost / 160;
    }
    await this.employeeSalaryPackageRepository.replaceById(id, employeeSalaryPackage);
  }

import { model, property } from '@loopback/repository';
import { UserModifiableEntity } from '.';
import { Currency } from '../modules/employee/currency.enum';

@model({
  name: 'employee_salary_package',
})
export class EmployeeSalaryPackage extends UserModifiableEntity {
  @property({
    type: 'number',
    id: true,
  })
  id?: number;

  @property({
    type: 'number',
    required: true,
  })
  employee_id: number;

  @property({
    type: 'number',
    required: true,
  })
  current_package: number;

  @property({
    type: 'string',
    required: true,
  })
  currency: Currency;

  @property({
    type: 'string',
    required: true,
  })
  effective_date: string;

  @property({
    type: 'string',
  })
  end_date?: string;

  @property({
    type: 'number'
  })
  monthly_cost: number;

  @property({
    type: 'number'
  })
  hourly_cost: number;

  constructor(data?: Partial<EmployeeSalaryPackage>) {
    super(data);
  }
}

export interface EmployeeSalaryPackageRelations {
  // describe navigational properties here
}

export type EmployeeSalaryPackageWithRelations = EmployeeSalaryPackage & EmployeeSalaryPackageRelations;

linux x64 10.13.0
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Related Issues

#2652
#1722

@bhupesh-sf bhupesh-sf added the bug label Aug 13, 2019
@bhupesh-sf
Copy link
Author

My bad, I mistakenly replace the repository function from updateById to replaceById and that was causing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant