"""Archive existing uploaded approval file if one exists.""" import os from lib import config from lib.constants import ( PAYMENT_GROUP_PAYMENT_REPORT_TYPES, ) from lib.utils import aws from lib.utils import ows from tasks.payments_upload_approval import helpers def archive_upload_approval_file_task(dag_run: dict, **kwargs): """Check if any reports exist in S3 bucket and copy them to an archive directory. Args: dag_run (dict): a dag's config """ event = helpers.get_event_from_params(dag_run, **kwargs) reports = ows.get_payment_group_payment_reports(event.target_id) payment_group_payment = ows.get_payment_group_payment_details(event.target_id) if not reports: return keep_upload_approval_file = next( ( report for report in reports if report['report_type'] == PAYMENT_GROUP_PAYMENT_REPORT_TYPES.APPROVAL ), None ) # This is the object to keep. Find/archive anything else keep_object = aws.split_path(keep_upload_approval_file.get('report_export_url')) prefix = os.path.dirname(keep_object.key) # Ensure operating on the correct bucket assert keep_object.bucket == config.S3_PAYMENTS_BUCKET_NAME, \ f'Bucket mismatch. Got {keep_object.bucket}.' # Ensure operating on the correct prefix assert prefix == helpers.build_upload_approval_path( event.target_id, payment_group_payment['payment_name']), \ f'Path mismatch. Got {prefix}.' archive_path = helpers.build_upload_approval_archive_path( event.target_id, payment_group_payment['payment_name']) keys = aws.list_bucket_keys(keep_object.bucket, prefix) for key in keys: if key == keep_object.key: # Don't touch the new upload approval file continue if archive_path in key: # Don't touch anything already in the archive directory continue # We've found an object to archive original_filename = os.path.basename(key) destination_object = helpers.build_upload_approval_archive_location( event.target_id, payment_group_payment['payment_name'], original_filename, ) # Move the object via copy+delete operations aws.copy_file( source_bucket=config.S3_PAYMENTS_BUCKET_NAME, source_key=key, dest_key=destination_object.key ) aws.delete_files(bucket=config.S3_PAYMENTS_BUCKET_NAME, key=key)