diff --git a/packages/apps/hr/src/api/index.ts b/packages/apps/hr/src/api/index.ts index e260fafc4b..f132db5d49 100644 --- a/packages/apps/hr/src/api/index.ts +++ b/packages/apps/hr/src/api/index.ts @@ -45,6 +45,7 @@ export {searchExpenseType as searchExpenseTypeApi} from './expense-type-api'; export {searchKilometricAllowParam as searchKilometricAllowParamApi} from './kilometric-allow-param-api'; export { cancelLeave as cancelLeaveApi, + deleteLeave as deleteLeaveApi, fetchLeave as fetchLeaveApi, fetchLeaveById as fetchLeaveByIdApi, fetchLeaveReason as fetchLeaveReasonApi, diff --git a/packages/apps/hr/src/api/leave-api.js b/packages/apps/hr/src/api/leave-api.js index 1c37d9fd3a..e8c048ed2d 100644 --- a/packages/apps/hr/src/api/leave-api.js +++ b/packages/apps/hr/src/api/leave-api.js @@ -180,3 +180,15 @@ export async function rejectLeave({leaveRequestId, version, groundForRefusal}) { description: 'reject leave', }); } + +export async function deleteLeave({leaveRequestId}) { + return getActionApi().send({ + url: `ws/rest/com.axelor.apps.hr.db.LeaveRequest/${leaveRequestId}`, + method: 'delete', + description: 'delete leave request', + matchers: { + modelName: 'com.axelor.apps.hr.db.LeaveRequest', + id: leaveRequestId, + }, + }); +} diff --git a/packages/apps/hr/src/components/molecules/LeaveActionCard/LeaveActionCard.tsx b/packages/apps/hr/src/components/molecules/LeaveActionCard/LeaveActionCard.tsx index ed9cd1e418..22aae584c6 100644 --- a/packages/apps/hr/src/components/molecules/LeaveActionCard/LeaveActionCard.tsx +++ b/packages/apps/hr/src/components/molecules/LeaveActionCard/LeaveActionCard.tsx @@ -16,11 +16,15 @@ * along with this program. If not, see . */ -import React from 'react'; -import {useTranslator, useTypes} from '@axelor/aos-mobile-core'; +import React, {useMemo} from 'react'; +import { + usePermitted, + useSelector, + useTranslator, + useTypes, +} from '@axelor/aos-mobile-core'; import {ActionCard} from '@axelor/aos-mobile-ui'; import {LeaveCard} from '../../atoms'; -import {Leave} from '../../../types'; interface LeaveActionCardProps { mode: number; @@ -39,26 +43,47 @@ const LeaveActionCard = ({ }: LeaveActionCardProps) => { const I18n = useTranslator(); const {LeaveRequest} = useTypes(); + const {readonly} = usePermitted({ + modelName: 'com.axelor.apps.hr.db.LeaveRequest', + }); + + const {user} = useSelector((state: any) => state.user); + + const userCanValidate = useMemo(() => { + return ( + (user.employee?.hrManager || + leave.employee?.managerUser?.id === user.id) && + leave.statusSelect === LeaveRequest?.statusSelect.WaitingValidation + ); + }, [LeaveRequest?.statusSelect.WaitingValidation, leave, user]); + + const isDefaultDisplay = useMemo(() => { + return ( + readonly || + (!userCanValidate && + leave.statusSelect !== LeaveRequest?.statusSelect.Draft) + ); + }, [LeaveRequest?.statusSelect, readonly, leave, userCanValidate]); return ( + actionList={ + !isDefaultDisplay && [ + { + iconName: 'send-fill', + helper: I18n.t('Hr_Send'), + onPress: onSend, + hidden: leave.statusSelect !== LeaveRequest?.statusSelect.Draft, + }, + { + iconName: 'check-lg', + helper: I18n.t('Hr_Validate'), + onPress: onValidate, + hidden: leave.statusSelect === LeaveRequest?.statusSelect.Draft, + }, + ] + }> { const I18n = useTranslator(); const Colors = useThemeColor(); + const navigation = useNavigation(); const dispatch = useDispatch(); const {LeaveRequest} = useTypes(); + const {canDelete, readonly} = usePermitted({ + modelName: 'com.axelor.apps.hr.db.LeaveRequest', + }); const [refusalPopupIsOpen, setRefusalPopupIsOpen] = useState(false); @@ -55,49 +60,47 @@ const LeaveDetailsButtons = ({ const leaveRequestParams = useMemo( () => ({ - leaveRequestId: leaveId, - version: leaveVersion, + leaveRequestId: leaveRequest.id, + version: leaveRequest.version, + user: user, + userId: user.id, }), - [leaveId, leaveVersion], + [leaveRequest, user], ); const sendLeaveAPI = useCallback(() => { - dispatch( - (sendLeave as any)({ - ...leaveRequestParams, - userId: user?.id, - }), - ); - }, [dispatch, leaveRequestParams, user?.id]); + dispatch((sendLeave as any)(leaveRequestParams)); + }, [dispatch, leaveRequestParams]); const validateLeaveAPI = useCallback(() => { - dispatch( - (validateLeave as any)({ - ...leaveRequestParams, - user: user, - }), - ); - }, [dispatch, leaveRequestParams, user]); + dispatch((validateLeave as any)(leaveRequestParams)); + }, [dispatch, leaveRequestParams]); const cancelLeaveAPI = useCallback(() => { - dispatch( - (cancelLeave as any)({ - ...leaveRequestParams, - userId: user?.id, - }), - ); - }, [dispatch, leaveRequestParams, user?.id]); + dispatch((cancelLeave as any)(leaveRequestParams)); + }, [dispatch, leaveRequestParams]); + + const deleteLeaveAPI = useCallback(() => { + dispatch((deleteLeave as any)(leaveRequestParams)); + navigation.pop(); + }, [dispatch, leaveRequestParams, navigation]); + + if (readonly) { + return null; + } if (statusSelect === LeaveRequest?.statusSelect.Draft) { return ( -