Beispiel #1
0
    def mutate(self, _, type, start_date, end_date, workspace_id, user_id,
               comment):

        user_id = user_id or current_user_in_workspace_or_error(
            ws_id=workspace_id).id
        user = User.find_by_id(user_id)

        day_off = DayOff(leave_type=LeaveTypes[type],
                         start_date=start_date,
                         end_date=end_date,
                         workspace_id=workspace_id,
                         comment=comment,
                         user_id=user.id)

        rule_result = execute_day_off_validation_rule(day_off=day_off)

        response = CreateDayOff(errors=rule_result.errors,
                                warnings=rule_result.warnings,
                                notes=rule_result.notes)

        if rule_result.is_rejected:
            return response

        try:
            day_off.save_and_persist()

            try:
                ws_owner = WorkspaceUserRole.find(
                    ws_id=workspace_id, role=WorkspaceUserRoles.OWNER)
                if ws_owner is not None and ws_owner.user_id != user.id:
                    profile = UserProfile.find(user_id=user.id)
                    uds = UserDevice.find_all(user_id=ws_owner.user_id)
                    for ud in uds:
                        if ud is not None and ud.device_token is not None:
                            push_body = 'Someone needs off ;)'
                            if profile is not None:
                                push_body = '{} {} requested {}'.format(
                                    profile.first_name, profile.last_name,
                                    LeaveTypesLabels[type])
                            send_push(
                                to=ud.device_token,
                                title='Leave request',
                                body=push_body,
                                custom_data={'type': 'new_leave_requested'})
            except Exception as e:
                LOG.error(
                    f'Failed to send push notification about new leave. Error: {e}'
                )
            response.day_off = day_off
        except Exception as e:
            LOG.error(f'Could not add a day off. Error: {e}')
            raise GraphQLError('Could not add a day off')

        return response