Fix on update affiliation endpoint

fixed update condition for current affiliation

Change-Id: I35968a03ad164eeee27f3ba79d2539c6b2ccd8af
This commit is contained in:
Sebastian Marcet 2018-09-19 11:53:34 -03:00
parent 9a5942a8fe
commit 6688606abe
2 changed files with 17 additions and 11 deletions

View File

@ -194,6 +194,14 @@ class Affiliation extends SilverstripeBaseModel
$this->job_title = $job_title; $this->job_title = $job_title;
} }
public function clearEndDate(){
$this->end_date = null;
}
public function clearStartDate(){
$this->start_date = null;
}
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();

View File

@ -59,16 +59,20 @@ final class MemberService
if(is_null($affiliation)) if(is_null($affiliation))
throw new EntityNotFoundException(sprintf("affiliation id %s does not belongs to member id %s", $affiliation_id, $member->getId())); throw new EntityNotFoundException(sprintf("affiliation id %s does not belongs to member id %s", $affiliation_id, $member->getId()));
if(isset($data['is_current'])) if(isset($data['is_current'])) {
$affiliation->setIsCurrent(boolval($data['is_current'])); $affiliation->setIsCurrent(boolval($data['is_current']));
}
if(isset($data['start_date'])) { if(isset($data['start_date'])) {
$start_date = intval($data['start_date']); $start_date = intval($data['start_date']);
$affiliation->setStartDate(new DateTime("@$start_date")); $affiliation->setStartDate(new DateTime("@$start_date"));
} }
if(isset($data['end_date'])) {
if(!$affiliation->isCurrent() && isset($data['end_date'])) {
$end_date = intval($data['end_date']); $end_date = intval($data['end_date']);
$affiliation->setEndDate($end_date > 0 ? new DateTime("@$end_date") : null); $affiliation->setEndDate($end_date > 0 ? new DateTime("@$end_date") : null);
} }
if(isset($data['organization_id'])) { if(isset($data['organization_id'])) {
$org = $this->organization_repository->getById(intval($data['organization_id'])); $org = $this->organization_repository->getById(intval($data['organization_id']));
if(is_null($org)) if(is_null($org))
@ -80,15 +84,9 @@ final class MemberService
$affiliation->setJobTitle(trim($data['job_title'])); $affiliation->setJobTitle(trim($data['job_title']));
} }
if($affiliation->isCurrent() && $affiliation->getEndDate() != null) if($affiliation->isCurrent()){
throw new ValidationException $affiliation->clearEndDate();
( }
sprintf
(
"in order to set affiliation id %s as current end_date should be null",
$affiliation_id
)
);
return $affiliation; return $affiliation;
}); });