Skip to content

Commit 67b80d1

Browse files
Nethmin DulsaraRajithaKumara
Nethmin Dulsara
authored andcommitted
OHRM5X-1959: Develop claim - request API getAll operation (orangehrm#1656)
1 parent 2a34d45 commit 67b80d1

19 files changed

+1702
-23
lines changed

src/plugins/orangehrmClaimPlugin/Api/ClaimRequestActionAPI.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use OrangeHRM\Core\Api\V2\Validator\Rules;
3939
use OrangeHRM\Core\Traits\Auth\AuthUserTrait;
4040
use OrangeHRM\Core\Traits\ORM\EntityManagerHelperTrait;
41+
use OrangeHRM\Core\Traits\Service\DateTimeHelperTrait;
4142
use OrangeHRM\Entity\ClaimRequest;
4243
use OrangeHRM\Entity\Employee;
4344
use OrangeHRM\Entity\WorkflowStateMachine;
@@ -49,6 +50,7 @@ class ClaimRequestActionAPI extends Endpoint implements ResourceEndpoint
4950
use ClaimRequestAPIHelperTrait;
5051
use EntityManagerHelperTrait;
5152
use ClaimServiceTrait;
53+
use DateTimeHelperTrait;
5254

5355
public const PARAMETER_REQUEST_ID = 'requestId';
5456
public const PARAMETER_ACTION = 'action';
@@ -109,9 +111,13 @@ public function update(): EndpointResult
109111

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

114+
if ($actionIndex == WorkflowStateMachine::CLAIM_ACTION_SUBMIT) {
115+
$claimRequest->setSubmittedDate($this->getDateTimeHelper()->getNow());
116+
}
117+
112118
$this->getClaimService()->getClaimDao()->saveClaimRequest($claimRequest);
113119
$this->commitTransaction();
114-
} catch (ForbiddenException | InvalidParamException | RecordNotFoundException $e) {
120+
} catch (ForbiddenException|InvalidParamException|RecordNotFoundException $e) {
115121
$this->rollBackTransaction();
116122
throw $e;
117123
} catch (Exception $e) {

src/plugins/orangehrmClaimPlugin/Api/EmployeeClaimRequestAPI.php

+231-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
use Exception;
2424
use OpenApi\Annotations as OA;
2525
use OrangeHRM\Claim\Api\Model\ClaimRequestModel;
26+
use OrangeHRM\Claim\Api\Model\EmployeeClaimRequestModel;
27+
use OrangeHRM\Claim\Dto\ClaimRequestSearchFilterParams;
28+
use OrangeHRM\Claim\Dto\EmployeeClaimRequestSearchFilterParams;
2629
use OrangeHRM\Claim\Traits\Service\ClaimServiceTrait;
2730
use OrangeHRM\Core\Api\CommonParams;
2831
use OrangeHRM\Core\Api\V2\CrudEndpoint;
2932
use OrangeHRM\Core\Api\V2\Endpoint;
33+
use OrangeHRM\Core\Api\V2\EndpointCollectionResult;
3034
use OrangeHRM\Core\Api\V2\EndpointResourceResult;
3135
use OrangeHRM\Core\Api\V2\EndpointResult;
3236
use OrangeHRM\Core\Api\V2\Exception\BadRequestException;
@@ -47,6 +51,7 @@
4751
use OrangeHRM\Entity\Employee;
4852
use OrangeHRM\Entity\WorkflowStateMachine;
4953
use OrangeHRM\ORM\Exception\TransactionException;
54+
use OrangeHRM\Pim\Traits\Service\EmployeeServiceTrait;
5055

5156
class EmployeeClaimRequestAPI extends Endpoint implements CrudEndpoint
5257
{
@@ -56,13 +61,19 @@ class EmployeeClaimRequestAPI extends Endpoint implements CrudEndpoint
5661
use AuthUserTrait;
5762
use NormalizerServiceTrait;
5863
use UserRoleManagerTrait;
64+
use EmployeeServiceTrait;
5965

6066
public const PARAMETER_CLAIM_EVENT_ID = 'claimEventId';
6167
public const PARAMETER_CURRENCY_ID = 'currencyId';
6268
public const PARAMETER_REMARKS = 'remarks';
6369
public const REMARKS_MAX_LENGTH = 1000;
6470
public const PARAMETER_ALLOWED_ACTIONS = 'allowedActions';
6571
public const PARAMETER_EMPLOYEE_NUMBER = 'empNumber';
72+
public const PARAMETER_REFERENCE_ID = 'referenceId';
73+
public const PARAMETER_EVENT_ID = 'eventId';
74+
public const PARAMETER_STATUS = 'status';
75+
public const PARAMETER_FROM_DATE = 'fromDate';
76+
public const PARAMETER_TO_DATE = 'toDate';
6677
public const ACTIONABLE_STATES_MAP = [
6778
WorkflowStateMachine::CLAIM_ACTION_SUBMIT => 'SUBMIT',
6879
WorkflowStateMachine::CLAIM_ACTION_APPROVE => 'APPROVE',
@@ -220,19 +231,237 @@ protected function setClaimRequest(ClaimRequest $claimRequest, int $empNumber):
220231
}
221232

222233
/**
234+
* @OA\Get(
235+
* path="/api/v2/claim/employees/requests",
236+
* tags={"Claim/Requests"},
237+
* @OA\Parameter(
238+
* name="sortField",
239+
* in="query",
240+
* required=false,
241+
* @OA\Schema(type="string", enum=EmployeeClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS)
242+
* ),
243+
* @OA\Parameter(
244+
* name="referenceId",
245+
* in="query",
246+
* required=false,
247+
* @OA\Schema(type="string")
248+
* ),
249+
* @OA\Parameter(
250+
* name="status",
251+
* in="query",
252+
* required=false,
253+
* @OA\Schema(type="string", enum={"INITIATED", "SUBMITTED", "APPROVED", "REJECTED", "CANCELLED", "PAID"})
254+
* ),
255+
* @OA\Parameter(
256+
* name="eventId",
257+
* in="query",
258+
* required=false,
259+
* @OA\Schema(type="integer")
260+
* ),
261+
* @OA\Parameter(
262+
* name="empNumber",
263+
* in="query",
264+
* required=false,
265+
* @OA\Schema(type="integer")
266+
* ),
267+
* @OA\Parameter(
268+
* name="fromDate",
269+
* in="query",
270+
* required=false,
271+
* @OA\Schema(type="DateTime")
272+
* ),
273+
* @OA\Parameter(
274+
* name="toDate",
275+
* in="query",
276+
* required=false,
277+
* @OA\Schema(type="DateTime")
278+
* ),
279+
* @OA\Parameter(ref="#/components/parameters/sortOrder"),
280+
* @OA\Parameter(ref="#/components/parameters/limit"),
281+
* @OA\Parameter(ref="#/components/parameters/offset"),
282+
* @OA\Response(
283+
* response="200",
284+
* description="Success",
285+
* @OA\JsonContent(
286+
* @OA\Property(
287+
* property="data",
288+
* type="array",
289+
* @OA\Items(ref="#/components/schemas/Claim-EmployeeClaimRequestModel")
290+
* ),
291+
* @OA\Property(
292+
* property="meta",
293+
* type="object",
294+
* @OA\Property(property="total", type="integer")
295+
* )
296+
* )
297+
* )
298+
* )
223299
* @inheritDoc
224300
*/
225301
public function getAll(): EndpointResult
226302
{
227-
throw $this->getNotImplementedException();
303+
$employeeClaimRequestSearchFilterParams = $this->getClaimRequestSearchFilterParams();
304+
305+
$this->setSortingAndPaginationParams($employeeClaimRequestSearchFilterParams);
306+
$this->getCommonFilterParams($employeeClaimRequestSearchFilterParams);
307+
$this->setEmpNumbers($employeeClaimRequestSearchFilterParams);
308+
309+
$claimRequests = $this->getClaimService()->getClaimDao()
310+
->getClaimRequestList($employeeClaimRequestSearchFilterParams);
311+
312+
$count = $this->getClaimService()->getClaimDao()
313+
->getClaimRequestCount($employeeClaimRequestSearchFilterParams);
314+
315+
return $this->getEndPointCollectionResult($claimRequests, $count);
316+
}
317+
318+
/**
319+
* @return ClaimRequestSearchFilterParams
320+
*/
321+
protected function getClaimRequestSearchFilterParams(): ClaimRequestSearchFilterParams
322+
{
323+
return new EmployeeClaimRequestSearchFilterParams();
324+
}
325+
326+
/**
327+
* @param array $claimRequests
328+
* @param int $count
329+
* @return EndpointCollectionResult
330+
*/
331+
protected function getEndPointCollectionResult(array $claimRequests, int $count): EndpointCollectionResult
332+
{
333+
return new EndpointCollectionResult(
334+
EmployeeClaimRequestModel::class,
335+
$claimRequests,
336+
new ParameterBag([CommonParams::PARAMETER_TOTAL => $count])
337+
);
338+
}
339+
340+
/**
341+
* @param ClaimRequestSearchFilterParams $claimRequestSearchFilterParams
342+
*/
343+
protected function setEmpNumbers(ClaimRequestSearchFilterParams $claimRequestSearchFilterParams): void
344+
{
345+
$empNumber = $this->getRequestParams()->getIntOrNull(
346+
RequestParams::PARAM_TYPE_QUERY,
347+
self::PARAMETER_EMPLOYEE_NUMBER
348+
);
349+
350+
if (!is_null($empNumber)) {
351+
if (!$this->getEmployeeService()->getEmployeeDao()->getEmployeeByEmpNumber($empNumber) instanceof Employee) {
352+
throw $this->getRecordNotFoundException();
353+
}
354+
if (!$this->isSelfByEmpNumber($empNumber)) {
355+
throw $this->getForbiddenException();
356+
}
357+
if (!$this->getUserRoleManagerHelper()->isEmployeeAccessible($empNumber)) {
358+
throw $this->getForbiddenException();
359+
}
360+
$claimRequestSearchFilterParams->setEmpNumbers([$empNumber]);
361+
} else {
362+
$empNumbers = $this->getUserRoleManager()->getAccessibleEntityIds(Employee::class);
363+
$claimRequestSearchFilterParams->setEmpNumbers($empNumbers);
364+
}
365+
}
366+
367+
/**
368+
* @param ClaimRequestSearchFilterParams $claimRequestSearchFilterParams
369+
*/
370+
private function getCommonFilterParams(ClaimRequestSearchFilterParams $claimRequestSearchFilterParams): void
371+
{
372+
$claimRequestSearchFilterParams->setReferenceId(
373+
$this->getRequestParams()->getStringOrNull(
374+
RequestParams::PARAM_TYPE_QUERY,
375+
self::PARAMETER_REFERENCE_ID
376+
)
377+
);
378+
$claimRequestSearchFilterParams->setEventId(
379+
$this->getRequestParams()->getIntOrNull(
380+
RequestParams::PARAM_TYPE_QUERY,
381+
self::PARAMETER_EVENT_ID
382+
)
383+
);
384+
$claimRequestSearchFilterParams->setStatus(
385+
$this->getRequestParams()->getStringOrNull(
386+
RequestParams::PARAM_TYPE_QUERY,
387+
self::PARAMETER_STATUS
388+
)
389+
);
390+
$claimRequestSearchFilterParams->setFromDate(
391+
$this->getRequestParams()->getDateTimeOrNull(
392+
RequestParams::PARAM_TYPE_QUERY,
393+
self::PARAMETER_FROM_DATE
394+
)
395+
);
396+
$claimRequestSearchFilterParams->setToDate(
397+
$this->getRequestParams()->getDateTimeOrNull(
398+
RequestParams::PARAM_TYPE_QUERY,
399+
self::PARAMETER_TO_DATE
400+
)
401+
);
402+
}
403+
404+
/**
405+
* @return ParamRuleCollection
406+
*/
407+
protected function getCommonParamRuleCollectionGetAll(): ParamRuleCollection
408+
{
409+
return new ParamRuleCollection(
410+
$this->getValidationDecorator()->notRequiredParamRule(
411+
new ParamRule(
412+
self::PARAMETER_REFERENCE_ID,
413+
new Rule(Rules::STRING_TYPE)
414+
)
415+
),
416+
$this->getValidationDecorator()->notRequiredParamRule(
417+
new ParamRule(
418+
self::PARAMETER_EVENT_ID,
419+
new Rule(Rules::POSITIVE)
420+
)
421+
),
422+
$this->getValidationDecorator()->notRequiredParamRule(
423+
new ParamRule(
424+
self::PARAMETER_STATUS,
425+
new Rule(Rules::STRING_TYPE)
426+
)
427+
),
428+
$this->getValidationDecorator()->notRequiredParamRule(
429+
new ParamRule(
430+
self::PARAMETER_FROM_DATE,
431+
new Rule(Rules::DATE_TIME)
432+
)
433+
),
434+
$this->getValidationDecorator()->notRequiredParamRule(
435+
new ParamRule(
436+
self::PARAMETER_TO_DATE,
437+
new Rule(Rules::DATE_TIME)
438+
)
439+
)
440+
);
228441
}
229442

230443
/**
231444
* @inheritDoc
232445
*/
233446
public function getValidationRuleForGetAll(): ParamRuleCollection
234447
{
235-
throw $this->getNotImplementedException();
448+
$paramRuleCollection = $this->getCommonParamRuleCollectionGetAll();
449+
$paramRuleCollection->addParamValidation(
450+
$this->getValidationDecorator()->notRequiredParamRule(
451+
new ParamRule(
452+
self::PARAMETER_EMPLOYEE_NUMBER,
453+
new Rule(Rules::POSITIVE)
454+
)
455+
)
456+
);
457+
$sortFieldParamRules = $this->getSortingAndPaginationParamsRules(
458+
EmployeeClaimRequestSearchFilterParams::ALLOWED_SORT_FIELDS
459+
);
460+
foreach ($sortFieldParamRules as $sortFieldParamRule) {
461+
$paramRuleCollection->addParamValidation($sortFieldParamRule);
462+
}
463+
464+
return $paramRuleCollection;
236465
}
237466

238467
/**

0 commit comments

Comments
 (0)