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

OHRM5X-1959: Develop ClaimRequest getAll API #1656

Merged
merged 7 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use from common params

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',
Comment on lines 66 to 79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    public const PARAMETER_CLAIM_EVENT_ID = 'claimEventId';
    public const PARAMETER_CURRENCY_ID = 'currencyId';
    public const PARAMETER_REMARKS = 'remarks';
    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 REMARKS_MAX_LENGTH = 1000;

    public const ACTIONABLE_STATES_MAP = [
        WorkflowStateMachine::CLAIM_ACTION_SUBMIT => 'SUBMIT',
        WorkflowStateMachine::CLAIM_ACTION_APPROVE => 'APPROVE',
        WorkflowStateMachine::CLAIM_ACTION_PAY => 'PAY',
        WorkflowStateMachine::CLAIM_ACTION_CANCEL => 'CANCEL',
        WorkflowStateMachine::CLAIM_ACTION_REJECT => 'REJECT'
];

prefer if you can organize

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)
DulsaraNethmin marked this conversation as resolved.
Show resolved Hide resolved
* ),
* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array $claimRequests
* @param ClaimRequest[] $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)) {
DulsaraNethmin marked this conversation as resolved.
Show resolved Hide resolved
if (!$this->getEmployeeService()->getEmployeeDao()->getEmployeeByEmpNumber($empNumber) instanceof Employee) {
throw $this->getRecordNotFoundException();
}
if (!$this->isSelfByEmpNumber($empNumber)) {
throw $this->getForbiddenException();
}
Comment on lines +354 to +356
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit confusing

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
)
);
Comment on lines +390 to +401
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any idea to validate toDate > fromDate

}

/**
* @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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new Rule(Rules::STRING_TYPE)
new Rule(Rules::IN, [array_values(self::ACTIONABLE_STATES_MAP)]),

)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_FROM_DATE,
new Rule(Rules::DATE_TIME)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new Rule(Rules::DATE_TIME)
new Rule(Rules::API_DATE)

)
),
$this->getValidationDecorator()->notRequiredParamRule(
new ParamRule(
self::PARAMETER_TO_DATE,
new Rule(Rules::DATE_TIME)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new Rule(Rules::DATE_TIME)
new Rule(Rules::API_DATE)

)
)
);
}

/**
* @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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new Rule(Rules::POSITIVE)
new Rule(Rules::IN_ACCESSIBLE_ENTITY_ID, [Employee::class])

🤔

)
)
);
$sortFieldParamRules = $this->getSortingAndPaginationParamsRules(
EmployeeClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS
);
foreach ($sortFieldParamRules as $sortFieldParamRule) {
$paramRuleCollection->addParamValidation($sortFieldParamRule);
}

return $paramRuleCollection;
}

/**
Expand Down
Loading