From ce32cbbc458ace32e8c80995d22107b1e9f8985d Mon Sep 17 00:00:00 2001 From: Samir Ladoui Date: Tue, 22 Oct 2024 13:46:15 +0100 Subject: [PATCH] [UPD] odex_mobile: add app close timeout --- .../controllers/rest_api_v2/attendance.py | 61 ++++++++++++++++++- odex25_mobile/odex_mobile/i18n/ar_001.po | 7 +++ .../models/attendence_zone_config.py | 1 + .../odex_mobile/models/hr_employee.py | 4 ++ .../views/attendance_zone_config_view.xml | 1 + 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py b/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py index c7b626425..0636d0f49 100644 --- a/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py +++ b/odex25_mobile/odex_mobile/controllers/rest_api_v2/attendance.py @@ -46,7 +46,7 @@ class AttendanceController(http.Controller): general_zoons = True if general else False for z in specific: value = {'id':z.id, 'name':z.zone, 'latitude':z.latitude, 'longitude':z.longitude, 'allowed_range':z.allowed_range, - 'loc_ch_intv': z.loc_ch_intv, 'loc_ch_dist':z.loc_ch_dist, 'srv_ch_tmout':z.srv_ch_tmout} + 'loc_ch_intv': z.loc_ch_intv, 'loc_ch_dist':z.loc_ch_dist, 'srv_ch_tmout':z.srv_ch_tmout, 'app_cl_tmout': z.app_cl_tmout} li.append(value) return http_helper.response(message="Data Found", data={'general_zoons':general_zoons, 'specific_zoons':li}) except (UserError, AccessError, ValidationError, Exception, Warning) as e: @@ -479,6 +479,65 @@ class AttendanceController(http.Controller): _logger.error(str(e)) message = validator.get_server_error(e, user) return http_helper.errcode(code=403, message=message) + + @http.route('/rest_api/v2/data-sync', type='http', auth='user', methods=['GET'], csrf=False) + def data_sync(self, **kwargs): + # Assuming that http_method, body, headers, and token are obtained via helper methods + http_method, body, headers, token = http_helper.parse_request() + + # Validate token + result = validator.verify_token(token) + if not result['status']: + return http_helper.errcode(code=result['code'], message=result['message']) + + # Verify user from token + user = validator.verify(token) + if not user: + return http_helper.response( + code=400, + message="You are not allowed to perform this operation. Please check with one of your team admins.", + success=False + ) + + # Find employee linked to the user + employee = request.env['hr.employee'].search([('user_id', '=', user.id)], limit=1) + if not employee: + return http_helper.response( + code=400, + message="You are not allowed to perform this operation. Please check with one of your team admins.", + success=False + ) + + # Get the attendance zone for the employee or use general zone + zone = request.env['attendance.zone'].search([('employee_ids', 'in', employee.id)], limit=1) + if not zone: + zone = request.env['attendance.zone'].search([('general', '=', True)], limit=1) + + # Fetch the timeout value from the zone, default to 5 minutes if zone is not found or no value + app_cl_tmout = zone.app_cl_tmout if zone else 5 + + # Get the current datetime and compare with last_active_time + now = fields.Datetime.now() + last_active_time = employee.last_active_time or now + time_difference = now - last_active_time + + # Check if the time difference exceeds the zone's timeout using timedelta + if time_difference > timedelta(minutes=app_cl_tmout): + return http_helper.response( + code=200, + message=f"Time difference is more than {app_cl_tmout} minutes", + success=True, + data={'force_checkout': True} + ) + else: + # Update last_active_time if force_checkout is False + employee.write({'last_active_time': now}) + return http_helper.response( + code=200, + message=f"Time difference is less than {app_cl_tmout} minutes", + success=True, + data={'force_checkout': False} + ) def send_msg(self, emp, msg, subject): if emp.user_id.partner_id: diff --git a/odex25_mobile/odex_mobile/i18n/ar_001.po b/odex25_mobile/odex_mobile/i18n/ar_001.po index f366b28d4..a0ffcf20e 100644 --- a/odex25_mobile/odex_mobile/i18n/ar_001.po +++ b/odex25_mobile/odex_mobile/i18n/ar_001.po @@ -140,6 +140,11 @@ msgid "" " Expert Ltd Co cannot accept responsibility." msgstr "" +#. module: odex_mobile +#: model:ir.model.fields,field_description:odex_mobile.field_attendance_zone__app_cl_tmout +msgid "App Close Timeout - Minutes" +msgstr "مهلة إغلاق التطبيق - دقائق" + #. module: odex_mobile #: code:addons/odex_mobile/controllers/project_timesheet.py:0 #, python-format @@ -2319,3 +2324,5 @@ msgstr "الساعات الإضافية" #, python-format msgid "Attendance hours" msgstr "ساعات الحضور" + + diff --git a/odex25_mobile/odex_mobile/models/attendence_zone_config.py b/odex25_mobile/odex_mobile/models/attendence_zone_config.py index cd49a0332..c33f36079 100644 --- a/odex25_mobile/odex_mobile/models/attendence_zone_config.py +++ b/odex25_mobile/odex_mobile/models/attendence_zone_config.py @@ -25,6 +25,7 @@ class AttendanceZone(models.Model): loc_ch_intv = fields.Integer('Location Change Interval - Minutes', default=1) loc_ch_dist = fields.Integer('Location Change Distance - Meter', default=100) srv_ch_tmout = fields.Integer('Services Change Timeout - Minutes', default=5) + app_cl_tmout = fields.Integer('App Close Timeout - Minutes', default=5) auto_checkout = fields.Integer(string="Auto Checkout After" ,default=10) diff --git a/odex25_mobile/odex_mobile/models/hr_employee.py b/odex25_mobile/odex_mobile/models/hr_employee.py index 3426ba880..7294c207c 100644 --- a/odex25_mobile/odex_mobile/models/hr_employee.py +++ b/odex25_mobile/odex_mobile/models/hr_employee.py @@ -22,6 +22,10 @@ class HrEmployee(models.Model): fcm_token = fields.Char(string='FCM Token') attendance_log_ids = fields.One2many('attendance.log','employee_id',string="Attendance Log") message_sent = fields.Boolean(string="Message Sent", default=False) + # Used internally by the API + last_active_time = fields.Datetime(readonly=True) + + def user_push_notification(self, notification): def _get_access_token(json_file): """Retrieve a valid access token that can be used to authorize requests. diff --git a/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml b/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml index 91dd23fd8..e457c5054 100644 --- a/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml +++ b/odex25_mobile/odex_mobile/views/attendance_zone_config_view.xml @@ -31,6 +31,7 @@ +