Skip to content

Commit

Permalink
OHRM5X-1959: ClaimRequests getAll API
Browse files Browse the repository at this point in the history
  • Loading branch information
DulsaraNethmin committed Mar 28, 2023
1 parent edb1ec0 commit 00ce9da
Show file tree
Hide file tree
Showing 10 changed files with 618 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OrangeHRM\Core\Api\V2\Validator\Rules;
use OrangeHRM\Core\Traits\Auth\AuthUserTrait;
use OrangeHRM\Core\Traits\ORM\EntityManagerHelperTrait;
use OrangeHRM\Core\Traits\Service\DateTimeHelperTrait;
use OrangeHRM\Entity\ClaimRequest;
use OrangeHRM\Entity\Employee;
use OrangeHRM\Entity\WorkflowStateMachine;
Expand All @@ -49,6 +50,7 @@ class ClaimRequestActionAPI extends Endpoint implements ResourceEndpoint
use ClaimRequestAPIHelperTrait;
use EntityManagerHelperTrait;
use ClaimServiceTrait;
use DateTimeHelperTrait;

public const PARAMETER_REQUEST_ID = 'requestId';
public const PARAMETER_ACTION = 'action';
Expand Down Expand Up @@ -109,6 +111,10 @@ public function update(): EndpointResult

$claimRequest->setStatus($this->getResultingState($claimRequest, $actionIndex));

if ($actionIndex == WorkflowStateMachine::CLAIM_ACTION_SUBMIT) {
$claimRequest->setSubmittedDate($this->getDateTimeHelper()->getNow());
}

$this->getClaimService()->getClaimDao()->saveClaimRequest($claimRequest);
$this->commitTransaction();
} catch (ForbiddenException | InvalidParamException | RecordNotFoundException $e) {
Expand Down
171 changes: 169 additions & 2 deletions src/plugins/orangehrmClaimPlugin/Api/EmployeeClaimRequestAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
use Exception;
use OpenApi\Annotations as OA;
use OrangeHRM\Claim\Api\Model\ClaimRequestModel;
use OrangeHRM\Claim\Api\Model\EmployeeClaimRequestModel;
use OrangeHRM\Claim\Dto\ClaimRequestSearchFilterParams;
use OrangeHRM\Claim\Traits\Service\ClaimServiceTrait;
use OrangeHRM\Core\Api\CommonParams;
use OrangeHRM\Core\Api\V2\CrudEndpoint;
use OrangeHRM\Core\Api\V2\Endpoint;
use OrangeHRM\Core\Api\V2\EndpointCollectionResult;
use OrangeHRM\Core\Api\V2\EndpointResourceResult;
use OrangeHRM\Core\Api\V2\EndpointResult;
use OrangeHRM\Core\Api\V2\Exception\BadRequestException;
Expand Down Expand Up @@ -63,6 +66,11 @@ class EmployeeClaimRequestAPI extends Endpoint implements CrudEndpoint
public const REMARKS_MAX_LENGTH = 1000;
public const PARAMETER_ALLOWED_ACTIONS = 'allowedActions';
public const PARAMETER_EMPLOYEE_NUMBER = 'empNumber';
public const PARAMETER_REFERENCE_ID = 'referenceId';
public const PARAMETER_EVENT_ID = 'eventId';
public const PARAMETER_STATUS = 'status';
public const PARAMETER_FROM_DATE = 'fromDate';
public const PARAMETER_TO_DATE = 'toDate';
public const ACTIONABLE_STATES_MAP = [
WorkflowStateMachine::CLAIM_ACTION_SUBMIT => 'SUBMIT',
WorkflowStateMachine::CLAIM_ACTION_APPROVE => 'APPROVE',
Expand Down Expand Up @@ -220,19 +228,178 @@ protected function setClaimRequest(ClaimRequest $claimRequest, int $empNumber):
}

/**
* @OA\Get(
* path="/api/v2/claim/employees/{empNumber}/requests",
* tags={"Claim/Requests"},
* @OA\PathParameter(
* name="empNumber",
* @OA\Schema(type="integer")
* ),
* @OA\Parameter(
* name="sortField",
* in="query",
* required=false,
* @OA\Schema(type="string", enum=ClaimAttachmentSearchFilterParams::ALLOWED_SORT_FIELDS)
* ),
* @OA\Parameter(ref="#/components/parameters/sortOrder"),
* @OA\Parameter(ref="#/components/parameters/limit"),
* @OA\Parameter(ref="#/components/parameters/offset"),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(ref="#/components/schemas/Claim-AttachmentModel")
* ),
* @OA\Property(
* property="meta",
* type="object",
* @OA\Property(property="total", type="integer")
* )
* )
* )
* )
* @inheritDoc
*/
public function getAll(): EndpointResult
{
throw $this->getNotImplementedException();
$myClaimRequestSearchFilterParams = new ClaimRequestSearchFilterParams();

$this->setSortingAndPaginationParams($myClaimRequestSearchFilterParams);
$this->getCommonFilterParams($myClaimRequestSearchFilterParams);
$this->setEmpNumbers($myClaimRequestSearchFilterParams);

$claimRequests = $this->getClaimService()->getClaimDao()
->getClaimRequestList($myClaimRequestSearchFilterParams);

$count = $this->getClaimService()->getClaimDao()
->getClaimRequestCount($myClaimRequestSearchFilterParams);

return $this->getEndPointCollectionResult($claimRequests, $count);
}

/**
* @param array $claimRequests
* @param int $count
* @return EndpointCollectionResult
*/
protected function getEndPointCollectionResult(array $claimRequests, int $count): EndpointCollectionResult
{
return new EndpointCollectionResult(
EmployeeClaimRequestModel::class,
$claimRequests,
new ParameterBag([CommonParams::PARAMETER_TOTAL => $count])
);
}

protected function setEmpNumbers(ClaimRequestSearchFilterParams $claimRequestSearchFilterParams): void
{
$empNumber = $this->getRequestParams()->getIntOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_EMPLOYEE_NUMBER
);

if (!is_null($empNumber)) {
if (!$this->isSelfByEmpNumber($empNumber)) {
throw $this->getForbiddenException();
}
if (!$this->getUserRoleManagerHelper()->isEmployeeAccessible($empNumber)) {
throw $this->getForbiddenException();
}
$claimRequestSearchFilterParams->setEmpNumbers([$empNumber]);
} else {
$empNumbers = $this->getUserRoleManager()->getAccessibleEntityIds(Employee::class);
$claimRequestSearchFilterParams->setEmpNumbers($empNumbers);
}
}

private function getCommonFilterParams(ClaimRequestSearchFilterParams $myClaimRequestSearchFilterParams): void
{
$myClaimRequestSearchFilterParams->setReferenceId(
$this->getRequestParams()->getStringOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_REFERENCE_ID
)
);
$myClaimRequestSearchFilterParams->setEventId(
$this->getRequestParams()->getIntOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_EVENT_ID
)
);
$myClaimRequestSearchFilterParams->setStatus(
$this->getRequestParams()->getStringOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_STATUS
)
);
$myClaimRequestSearchFilterParams->setFromDate(
$this->getRequestParams()->getDateTimeOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_FROM_DATE
)
);
$myClaimRequestSearchFilterParams->setToDate(
$this->getRequestParams()->getDateTimeOrNull(
RequestParams::PARAM_TYPE_QUERY,
self::PARAMETER_TO_DATE
)
);
}

protected function getCommonParamRuleCollectionGetAll(): ParamRuleCollection
{
return new ParamRuleCollection(
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_REFERENCE_ID,
new Rule(Rules::STRING_TYPE)
)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_EVENT_ID,
new Rule(Rules::POSITIVE)
)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_STATUS,
new Rule(Rules::STRING_TYPE)
)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_FROM_DATE,
new Rule(Rules::DATE_TIME)
)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_TO_DATE,
new Rule(Rules::DATE_TIME)
)
),
...$this->getSortingAndPaginationParamsRules(ClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS)
);
}

/**
* @inheritDoc
*/
public function getValidationRuleForGetAll(): ParamRuleCollection
{
throw $this->getNotImplementedException();
$paramRuleCollection = $this->getCommonParamRuleCollectionGetAll();
$paramRuleCollection->addParamValidation(
new ParamRule(
self::PARAMETER_EMPLOYEE_NUMBER,
new Rule(Rules::POSITIVE)
)
);

return $paramRuleCollection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
* all the essential functionalities required for any enterprise.
* Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
*
* OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/

namespace OrangeHRM\Claim\Api\Model;

use OrangeHRM\Core\Api\V2\Serializer\ModelTrait;
use OrangeHRM\Core\Api\V2\Serializer\Normalizable;
use OrangeHRM\Entity\ClaimRequest;

class EmployeeClaimRequestModel implements Normalizable
{
use ModelTrait;

public function __construct(ClaimRequest $claimRequest)
{
$this->setEntity($claimRequest);
$this->setFilters(
[
'id',
'referenceId',
['getClaimEvent', 'getId'],
['getClaimEvent', 'getName'],
['getCurrencyType', 'getId'],
['getCurrencyType', 'getName'],
'description',
'status',
['getEmployee', 'getEmpNumber'],
['getEmployee', 'getFirstName'],
['getEmployee', 'getLastName'],
['getEmployee', 'getMiddleName'],
['getEmployee', 'getEmployeeId'],
['getEmployee', 'getEmployeeTerminationRecord', 'getId'],
['getDecorator', 'getTotalExpense'],
['getDecorator', 'getSubmittedDate']
]
);
$this->setAttributeNames(
[
'id',
'referenceId',
['claimEvent', 'id'],
['claimEvent', 'name'],
['currencyType', 'id'],
['currencyType', 'name'],
'remarks',
'status',
['employee', 'empNumber'],
['employee', 'firstName'],
['employee', 'lastName'],
['employee', 'middleName'],
['employee', 'employeeId'],
['employee', 'terminationId'],
'total',
'submittedDate'
]
);
}
}
62 changes: 62 additions & 0 deletions src/plugins/orangehrmClaimPlugin/Api/Model/MyClaimRequestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
* all the essential functionalities required for any enterprise.
* Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
*
* OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/

namespace OrangeHRM\Claim\Api\Model;

use OrangeHRM\Core\Api\V2\Serializer\ModelTrait;
use OrangeHRM\Core\Api\V2\Serializer\Normalizable;
use OrangeHRM\Entity\ClaimRequest;

class MyClaimRequestModel implements Normalizable
{
use ModelTrait;

public function __construct(ClaimRequest $claimRequest)
{
$this->setEntity($claimRequest);
$this->setFilters(
[
'id',
'referenceId',
['getClaimEvent', 'getId'],
['getClaimEvent', 'getName'],
['getCurrencyType', 'getId'],
['getCurrencyType', 'getName'],
'description',
'status',
['getDecorator', 'getTotalExpense'],
['getDecorator', 'getSubmittedDate']
]
);
$this->setAttributeNames(
[
'id',
'referenceId',
['claimEvent', 'id'],
['claimEvent', 'name'],
['currencyType', 'id'],
['currencyType', 'name'],
'remarks',
'status',
'total',
'submittedDate'
]
);
}
}
Loading

0 comments on commit 00ce9da

Please sign in to comment.