from anydi import singleton from dmp.adapters.db import ReportingDB, transactional from dmp.adapters.ows_notifications import ( OwsNotificationsClient, ShopifyStoreSyncCompletedNotificationArgs, ) from dmp.shopify.models import ShopifyStoreAssociation from dmp.shopify.repositories import StoreAssociationRepository from dmp.shopify.services import StoreAssociationService @singleton class NotifyStoreSyncCompletedHandler: def __init__( self, db: ReportingDB, ows_notifications_client: OwsNotificationsClient, store_association_repository: StoreAssociationRepository, store_association_service: StoreAssociationService, ) -> None: self.db = db self.ows_notifications_client = ows_notifications_client self.store_association_repository = store_association_repository self.store_association_service = store_association_service @transactional def handle(self) -> list[ShopifyStoreAssociation]: associations_to_update = [] associations_to_notify = self.store_association_repository.find_to_notify_on_initial_sync_completion() for association in associations_to_notify: is_notified = ( self.ows_notifications_client.notify_shopify_store_sync_completed( ShopifyStoreSyncCompletedNotificationArgs( identity_id=association.created_by, store_id=association.id, store_domain=association.shop_domain, is_multiartist_store=association.is_multi_artist_store, ) ) ) if is_notified: association.initial_email_sent = True association = self.store_association_service.refresh_store(association) associations_to_update.append(association) self.store_association_repository.add_all(associations_to_update) return associations_to_update