36
36
use OrangeHRM \Core \Traits \Service \DateTimeHelperTrait ;
37
37
use OrangeHRM \Entity \LeaveEntitlement ;
38
38
use OrangeHRM \Leave \Api \Model \LeaveEntitlementModel ;
39
+ use OrangeHRM \Leave \Api \Model \LeaveEntitlementSummaryModel ;
39
40
use OrangeHRM \Leave \Api \Traits \LeaveEntitlementPermissionTrait ;
40
41
use OrangeHRM \Leave \Api \ValidationRules \LeaveTypeIdRule ;
41
42
use OrangeHRM \Leave \Dto \LeaveEntitlementSearchFilterParams ;
@@ -57,10 +58,19 @@ class LeaveEntitlementAPI extends Endpoint implements CrudEndpoint
57
58
public const PARAMETER_ENTITLEMENT = 'entitlement ' ;
58
59
public const PARAMETER_LOCATION_ID = 'locationId ' ;
59
60
public const PARAMETER_SUBUNIT_ID = 'subunitId ' ;
61
+ public const FILTER_MODEL = 'model ' ;
60
62
61
63
public const META_PARAMETER_SUM = 'sum ' ;
62
64
public const META_PARAMETER_COUNT = 'count ' ;
63
65
66
+ public const MODEL_DEFAULT = 'default ' ;
67
+ public const MODEL_SUMMARY = 'summary ' ;
68
+
69
+ public const MODEL_MAP = [
70
+ self ::MODEL_DEFAULT => LeaveEntitlementModel::class,
71
+ self ::MODEL_SUMMARY => LeaveEntitlementSummaryModel::class,
72
+ ];
73
+
64
74
/**
65
75
* @OA\Get(
66
76
* path="/api/v2/leave/leave-entitlements/{id}",
@@ -69,13 +79,26 @@ class LeaveEntitlementAPI extends Endpoint implements CrudEndpoint
69
79
* name="id",
70
80
* @OA\Schema(type="integer")
71
81
* ),
82
+ * @OA\Parameter(
83
+ * name="model",
84
+ * in="query",
85
+ * required=false,
86
+ * @OA\Schema(
87
+ * type="string",
88
+ * enum={\OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_DEFAULT, \OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_SUMMARY},
89
+ * default=\OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_DEFAULT
90
+ * )
91
+ * ),
72
92
* @OA\Response(
73
93
* response="200",
74
94
* description="Success",
75
95
* @OA\JsonContent(
76
96
* @OA\Property(
77
97
* property="data",
78
- * ref="#/components/schemas/Leave-LeaveEntitlementModel"
98
+ * oneOf={
99
+ * @OA\Schema(ref="#/components/schemas/Leave-LeaveEntitlementModel"),
100
+ * @OA\Schema(ref="#/components/schemas/Leave-LeaveEntitlementSummaryModel"),
101
+ * }
79
102
* ),
80
103
* @OA\Property(property="meta", type="object")
81
104
* )
@@ -92,7 +115,11 @@ public function getOne(): EndpointResult
92
115
->getLeaveEntitlement ($ this ->getIdUrlAttribute ());
93
116
$ this ->throwRecordNotFoundExceptionIfNotExist ($ leaveEntitlement , LeaveEntitlement::class);
94
117
$ this ->checkLeaveEntitlementAccessible ($ leaveEntitlement );
95
- return new EndpointResourceResult (LeaveEntitlementModel::class, $ leaveEntitlement );
118
+ return new EndpointResourceResult (
119
+ $ this ->getModelClass (),
120
+ $ leaveEntitlement ,
121
+ new ParameterBag ([CommonParams::PARAMETER_EMP_NUMBER => $ leaveEntitlement ->getEmployee ()->getEmpNumber ()])
122
+ );
96
123
}
97
124
98
125
/**
@@ -112,7 +139,8 @@ private function getIdUrlAttribute(): int
112
139
public function getValidationRuleForGetOne (): ParamRuleCollection
113
140
{
114
141
return new ParamRuleCollection (
115
- $ this ->getIdParamRule ()
142
+ $ this ->getIdParamRule (),
143
+ $ this ->getModelClassParamRule (),
116
144
);
117
145
}
118
146
@@ -128,27 +156,29 @@ private function getIdParamRule(): ParamRule
128
156
* @OA\Get(
129
157
* path="/api/v2/leave/leave-entitlements",
130
158
* tags={"Leave/Entitlements"},
131
- * @OA\PathParameter (
159
+ * @OA\Parameter (
132
160
* name="empNumber",
161
+ * in="query",
162
+ * required=false,
133
163
* @OA\Schema(type="integer")
134
164
* ),
135
165
* @OA\Parameter(
136
166
* name="leaveTypeId",
137
167
* in="query",
138
- * required=true ,
168
+ * required=false ,
139
169
* @OA\Schema(type="integer")
140
170
* ),
141
171
* @OA\Parameter(
142
172
* name="fromDate",
143
173
* in="query",
144
- * required=true ,
145
- * @OA\Schema(type="number ")
174
+ * required=false ,
175
+ * @OA\Schema(type="string ")
146
176
* ),
147
177
* @OA\Parameter(
148
178
* name="toDate",
149
179
* in="query",
150
- * required=true ,
151
- * @OA\Schema(type="number ")
180
+ * required=false ,
181
+ * @OA\Schema(type="string ")
152
182
* ),
153
183
* @OA\Parameter(
154
184
* name="sortField",
@@ -159,19 +189,35 @@ private function getIdParamRule(): ParamRule
159
189
* @OA\Parameter(ref="#/components/parameters/sortOrder"),
160
190
* @OA\Parameter(ref="#/components/parameters/limit"),
161
191
* @OA\Parameter(ref="#/components/parameters/offset"),
192
+ * @OA\Parameter(
193
+ * name="model",
194
+ * in="query",
195
+ * required=false,
196
+ * @OA\Schema(
197
+ * type="string",
198
+ * enum={\OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_DEFAULT, \OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_SUMMARY},
199
+ * default=\OrangeHRM\Leave\Api\LeaveEntitlementAPI::MODEL_DEFAULT
200
+ * )
201
+ * ),
162
202
* @OA\Response(
163
203
* response="200",
164
204
* description="Success",
165
205
* @OA\JsonContent(
166
206
* @OA\Property(
167
207
* property="data",
168
- * ref="#/components/schemas/Leave-LeaveEntitlementModel"
208
+ * type="array",
209
+ * @OA\Items(oneOf={
210
+ * @OA\Schema(ref="#/components/schemas/Leave-LeaveEntitlementModel"),
211
+ * @OA\Schema(ref="#/components/schemas/Leave-LeaveEntitlementSummaryModel"),
212
+ * })
169
213
* ),
170
214
* @OA\Property(property="meta",
171
215
* type="object",
172
- * @OA\Property(property="total", type="integer"),
173
- * @OA\Property(property="fromDate", type="number"),
174
- * @OA\Property(property="toDate", type="number")
216
+ * @OA\Property(property="total", type="number"),
217
+ * @OA\Property(property="sum", type="number"),
218
+ * @OA\Property(property="fromDate", type="string"),
219
+ * @OA\Property(property="toDate", type="string"),
220
+ * @OA\Property(property="empNumber", type="integer"),
175
221
* )
176
222
* )
177
223
* ),
@@ -223,7 +269,7 @@ public function getAll(): EndpointResult
223
269
->getLeaveEntitlementsSum ($ entitlementSearchFilterParams );
224
270
225
271
return new EndpointCollectionResult (
226
- LeaveEntitlementModel::class ,
272
+ $ this -> getModelClass () ,
227
273
$ entitlements ,
228
274
new ParameterBag (
229
275
[
@@ -233,11 +279,38 @@ public function getAll(): EndpointResult
233
279
->formatDateTimeToYmd ($ fromDate ),
234
280
LeaveCommonParams::PARAMETER_TO_DATE => $ this ->getDateTimeHelper ()
235
281
->formatDateTimeToYmd ($ toDate ),
282
+ CommonParams::PARAMETER_EMP_NUMBER => $ empNumber ,
236
283
]
237
284
)
238
285
);
239
286
}
240
287
288
+ /**
289
+ * @return string
290
+ */
291
+ protected function getModelClass (): string
292
+ {
293
+ $ model = $ this ->getRequestParams ()->getString (
294
+ RequestParams::PARAM_TYPE_QUERY ,
295
+ self ::FILTER_MODEL ,
296
+ self ::MODEL_DEFAULT ,
297
+ );
298
+ return self ::MODEL_MAP [$ model ];
299
+ }
300
+
301
+ /**
302
+ * @return ParamRule
303
+ */
304
+ protected function getModelClassParamRule (): ParamRule
305
+ {
306
+ return $ this ->getValidationDecorator ()->notRequiredParamRule (
307
+ new ParamRule (
308
+ self ::FILTER_MODEL ,
309
+ new Rule (Rules::IN , [array_keys (self ::MODEL_MAP )])
310
+ ),
311
+ );
312
+ }
313
+
241
314
/**
242
315
* @inheritDoc
243
316
*/
@@ -249,6 +322,7 @@ public function getValidationRuleForGetAll(): ParamRuleCollection
249
322
$ this ->getValidationDecorator ()->notRequiredParamRule ($ this ->getLeaveTypeIdParamRule ()),
250
323
$ this ->getValidationDecorator ()->notRequiredParamRule ($ fromDateRule ),
251
324
$ this ->getValidationDecorator ()->notRequiredParamRule ($ toDateRule ),
325
+ $ this ->getModelClassParamRule (),
252
326
...$ this ->getSortingAndPaginationParamsRules (LeaveEntitlementSearchFilterParams::ALLOWED_SORT_FIELDS )
253
327
);
254
328
}
0 commit comments