"""Check statement attachments statuses.""" import logging import sys sys.path.append('') import httpx # noqa: E402 from scripts.utils import get_env # noqa: E402 from scripts.utils import ScriptException # noqa: E402 from moneyhub.models.report_custom import ReportCustom # noqa: E402 from moneyhub.models.statement_attachment import StatementAttachment # noqa: E402 logging.basicConfig(level=logging.INFO) def post_message_to_slack(report_type: str, reports_list: list, webhook_url: str | None): """Post message body to slack. Args: report_type (str): Type of report reports_list (list): List of stuck reports to post webhook_url (str): URL to post message to """ text = f'*⚠️ The following {report_type} are stuck:*\n' for report in reports_list: report_id = ( report.report_custom_id if isinstance(report, ReportCustom) else report.statement_attachment_id ) text += f'* `{report_id}` (created: {report.created_at})\n' if not webhook_url: print(text) return payload = { 'blocks': [ { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': text } } ] } response = httpx.post( webhook_url, json=payload, ) if response.status_code != httpx.codes.OK: raise ScriptException(f'Slack message failed ({response.status_code}): {response.text}') logging.info(f'Message sent to Slack: {response.text}') if __name__ == '__main__': SLACK_WEBHOOK_URL = get_env('SLACK_WEBHOOK_URL') custom_reports = ReportCustom.get_stuck_in_progress() if custom_reports: logging.warning(f'Found {len(custom_reports)} stuck custom reports') post_message_to_slack('custom reports', custom_reports, SLACK_WEBHOOK_URL) else: logging.info('No stuck custom reports') statement_attachments = StatementAttachment.get_stuck_in_progress() if statement_attachments: logging.warning(f'Found {len(statement_attachments)} stuck statement attachments') post_message_to_slack('statement attachments', statement_attachments, SLACK_WEBHOOK_URL) else: logging.info('No stuck statement attachments')