def schedule_sync(workspace_id, schedule, user): """ Schedule sync :param workspace_id: workspace_id :param schedule: schedule object :param user: user email """ fyle_sdk_connection = connect_to_fyle(workspace_id) jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) created_job = jobs.trigger_interval( callback_url='{0}{1}'.format( settings.API_BASE_URL, '/workspace_jobs/{0}/settings/schedule/trigger/'.format( workspace_id ) ), callback_method='POST', object_id=schedule.id, job_description='Fetch expenses: Workspace id - {0}, user - {1}'.format( workspace_id, user ), start_datetime=schedule.start_datetime.strftime('%Y-%m-%d %H:%M:00.00'), hours=int(schedule.interval_hours) ) schedule.fyle_job_id = created_job['id'] schedule.save()
def schedule_expense_group_creation(workspace_id: int, user: str): """ Schedule Expense group creation :param workspace_id: Workspace id :param user: User email :return: None """ fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token, workspace_id) fyle_sdk_connection = fyle_connector.connection jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) task_log = TaskLog.objects.create(workspace_id=workspace_id, type='FETCHING_EXPENSES', status='IN_PROGRESS') created_job = jobs.trigger_now( callback_url='{0}{1}'.format( settings.API_URL, '/workspaces/{0}/fyle/expense_groups/'.format(workspace_id)), callback_method='POST', object_id=task_log.id, payload={'task_log_id': task_log.id}, job_description='Fetch expenses: Workspace id - {0}, user - {1}'. format(workspace_id, user)) task_log.task_id = created_job['id'] task_log.save()
def schedule_invoice_creation(workspace_id, expense_group_ids, user): """ Schedule Invoice creation :param workspace_id: :param expense_group_ids: :param user: :return: """ expense_groups = ExpenseGroup.objects.filter( workspace_id=workspace_id, id__in=expense_group_ids).all() fyle_sdk_connection = connect_to_fyle(workspace_id) jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) for expense_group in expense_groups: task_log = TaskLog.objects.create( workspace_id=expense_group.workspace.id, expense_group=expense_group, type='CREATING INVOICE', status='IN_PROGRESS') created_job = jobs.trigger_now( callback_url='{0}{1}'.format( settings.API_BASE_URL, '/workspace_jobs/{0}/expense_group/{1}/invoice/trigger/'. format(workspace_id, expense_group.id)), callback_method='POST', object_id=task_log.id, payload={'task_log_id': task_log.id}, job_description=f'Create invoice: Workspace id - {workspace_id}, \ user - {user}, expense group id - {expense_group.id}') task_log.task_id = created_job['id'] task_log.save()
def schedule_expense_group_creation(workspace_id, user): """ Schedule Expense Group creation :param workspace_id: :param user: :return: """ task_log = TaskLog.objects.create(workspace_id=workspace_id, type="FETCHING EXPENSES", status="IN_PROGRESS") try: fyle_sdk_connection = connect_to_fyle(workspace_id) jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) created_job = jobs.trigger_now( callback_url='{0}{1}'.format( settings.API_BASE_URL, '/workspace_jobs/{0}/expense_group/trigger/'.format( workspace_id)), callback_method='POST', object_id=task_log.id, payload={'task_log_id': task_log.id}, job_description= f'Fetch expenses: Workspace id - {workspace_id}, user - {user}') task_log.task_id = created_job['id'] task_log.save() except FyleCredential.DoesNotExist: LOGGER.error('Error: Fyle Credentials not found for this workspace.') task_log.detail = { 'error': 'Please connect your Source (Fyle) Account' } task_log.status = 'FYLE CONNECTION ERROR' task_log.save()
def schedule_sync(workspace_id: int, schedule_enabled: bool, hours: int, next_run: str, user: str): ws_settings, _ = WorkspaceSettings.objects.get_or_create( workspace_id=workspace_id) start_datetime = datetime.strptime(next_run, '%Y-%m-%dT%H:%M:00.000Z') if not ws_settings.schedule and schedule_enabled: schedule = WorkspaceSchedule.objects.create( enabled=schedule_enabled, interval_hours=hours, start_datetime=start_datetime) ws_settings.schedule = schedule created_job = create_schedule_job(workspace_id=workspace_id, schedule=schedule, user=user, start_datetime=start_datetime, hours=hours) ws_settings.schedule.fyle_job_id = created_job['id'] ws_settings.schedule.save() ws_settings.save(update_fields=['schedule']) else: ws_settings.schedule.enabled = schedule_enabled ws_settings.schedule.start_datetime = start_datetime ws_settings.schedule.interval_hours = hours fyle_credentials = FyleCredential.objects.get( workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token, workspace_id) fyle_sdk_connection = fyle_connector.connection jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) if ws_settings.schedule.fyle_job_id: jobs.delete_job(ws_settings.schedule.fyle_job_id) if schedule_enabled: created_job = create_schedule_job(workspace_id=workspace_id, schedule=ws_settings.schedule, user=user, start_datetime=start_datetime, hours=hours) ws_settings.schedule.fyle_job_id = created_job['id'] else: ws_settings.schedule.fyle_job_id = None ws_settings.schedule.save() return ws_settings
def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], user): """ Schedule bills creation :param expense_group_ids: List of expense group ids :param workspace_id: workspace id :param user: user email :return: None """ if expense_group_ids: expense_groups = ExpenseGroup.objects.filter( workspace_id=workspace_id, id__in=expense_group_ids, bill__id__isnull=True).all() else: expense_groups = ExpenseGroup.objects.filter( workspace_id=workspace_id, bill__id__isnull=True).all() fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token, workspace_id) fyle_sdk_connection = fyle_connector.connection jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) for expense_group in expense_groups: task_log, _ = TaskLog.objects.update_or_create( workspace_id=expense_group.workspace_id, expense_group=expense_group, defaults={ 'status': 'IN_PROGRESS', 'type': 'CREATING_BILL' }) created_job = jobs.trigger_now( callback_url='{0}{1}'.format( settings.API_URL, '/workspaces/{0}/qbo/bills/'.format(workspace_id)), callback_method='POST', object_id=task_log.id, payload={ 'expense_group_id': expense_group.id, 'task_log_id': task_log.id }, job_description= 'Create Bill: Workspace id - {0}, user - {1}, expense group id - {2}' .format(workspace_id, user, expense_group.id)) task_log.task_id = created_job['id'] task_log.save()
def create_schedule_job(workspace_id: int, schedule: WorkspaceSchedule, user: str, start_datetime: datetime, hours: int): fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token) fyle_sdk_connection = fyle_connector.connection jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) created_job = jobs.trigger_interval( callback_url='{0}{1}'.format( settings.API_URL, '/workspaces/{0}/schedule/trigger/'.format(workspace_id)), callback_method='POST', object_id=schedule.id, job_description='Fetch expenses: Workspace id - {0}, user - {1}'. format(workspace_id, user), start_datetime=start_datetime.strftime('%Y-%m-%d %H:%M:00.00'), hours=hours) return created_job