Skip to content

Commit

Permalink
OHRM5X-1959: Develop claim - request API getAll operation (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nethmin Dulsara authored Apr 10, 2023
1 parent 52f76f6 commit b9518be
Show file tree
Hide file tree
Showing 19 changed files with 1,702 additions and 23 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,9 +111,13 @@ 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) {
} catch (ForbiddenException|InvalidParamException|RecordNotFoundException $e) {
$this->rollBackTransaction();
throw $e;
} catch (Exception $e) {
Expand Down
233 changes: 231 additions & 2 deletions src/plugins/orangehrmClaimPlugin/Api/EmployeeClaimRequestAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
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\Dto\EmployeeClaimRequestSearchFilterParams;
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 All @@ -47,6 +51,7 @@
use OrangeHRM\Entity\Employee;
use OrangeHRM\Entity\WorkflowStateMachine;
use OrangeHRM\ORM\Exception\TransactionException;
use OrangeHRM\Pim\Traits\Service\EmployeeServiceTrait;

class EmployeeClaimRequestAPI extends Endpoint implements CrudEndpoint
{
Expand All @@ -56,13 +61,19 @@ class EmployeeClaimRequestAPI extends Endpoint implements CrudEndpoint
use AuthUserTrait;
use NormalizerServiceTrait;
use UserRoleManagerTrait;
use EmployeeServiceTrait;

public const PARAMETER_CLAIM_EVENT_ID = 'claimEventId';
public const PARAMETER_CURRENCY_ID = 'currencyId';
public const PARAMETER_REMARKS = 'remarks';
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 +231,237 @@ protected function setClaimRequest(ClaimRequest $claimRequest, int $empNumber):
}

/**
* @OA\Get(
* path="/api/v2/claim/employees/requests",
* tags={"Claim/Requests"},
* @OA\Parameter(
* name="sortField",
* in="query",
* required=false,
* @OA\Schema(type="string", enum=EmployeeClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS)
* ),
* @OA\Parameter(
* name="referenceId",
* in="query",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="status",
* in="query",
* required=false,
* @OA\Schema(type="string", enum={"INITIATED", "SUBMITTED", "APPROVED", "REJECTED", "CANCELLED", "PAID"})
* ),
* @OA\Parameter(
* name="eventId",
* in="query",
* required=false,
* @OA\Schema(type="integer")
* ),
* @OA\Parameter(
* name="empNumber",
* in="query",
* required=false,
* @OA\Schema(type="integer")
* ),
* @OA\Parameter(
* name="fromDate",
* in="query",
* required=false,
* @OA\Schema(type="DateTime")
* ),
* @OA\Parameter(
* name="toDate",
* in="query",
* required=false,
* @OA\Schema(type="DateTime")
* ),
* @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-EmployeeClaimRequestModel")
* ),
* @OA\Property(
* property="meta",
* type="object",
* @OA\Property(property="total", type="integer")
* )
* )
* )
* )
* @inheritDoc
*/
public function getAll(): EndpointResult
{
throw $this->getNotImplementedException();
$employeeClaimRequestSearchFilterParams = $this->getClaimRequestSearchFilterParams();

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

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

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

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

/**
* @return ClaimRequestSearchFilterParams
*/
protected function getClaimRequestSearchFilterParams(): ClaimRequestSearchFilterParams
{
return new EmployeeClaimRequestSearchFilterParams();
}

/**
* @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])
);
}

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

if (!is_null($empNumber)) {
if (!$this->getEmployeeService()->getEmployeeDao()->getEmployeeByEmpNumber($empNumber) instanceof Employee) {
throw $this->getRecordNotFoundException();
}
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);
}
}

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

/**
* @return ParamRuleCollection
*/
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)
)
)
);
}

/**
* @inheritDoc
*/
public function getValidationRuleForGetAll(): ParamRuleCollection
{
throw $this->getNotImplementedException();
$paramRuleCollection = $this->getCommonParamRuleCollectionGetAll();
$paramRuleCollection->addParamValidation(
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_EMPLOYEE_NUMBER,
new Rule(Rules::POSITIVE)
)
)
);
$sortFieldParamRules = $this->getSortingAndPaginationParamsRules(
EmployeeClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS
);
foreach ($sortFieldParamRules as $sortFieldParamRule) {
$paramRuleCollection->addParamValidation($sortFieldParamRule);
}

return $paramRuleCollection;
}

/**
Expand Down
Loading

0 comments on commit b9518be

Please sign in to comment.