"""Update subaccount mapping in art_relations.""" from src.connectors import sql_connection from src.constants import queries def db_execution(query, params, update=False): """Execute queries.""" db_connection = sql_connection.database_connection() cursor = db_connection.cursor() cursor.execute(query, params) if update: db_connection.commit() db_connection.close() else: result = cursor.fetchall() db_connection.close() return result def update_mapping_in_art_relations(subaccount_ids, logger): """Update subaccount_id mapping in art_relations.""" for subaccount, projects in subaccount_ids.items(): logger.info(f'subaccount: {subaccount}, projects: {projects}') db_execution( queries.UPDATE_PROJECT_AND_PRODUCT_SUBACCOUNT, {'subaccount': subaccount, 'projects': tuple(projects)}, True ) logger.info('Subaccount mapping completed') def validate_vendor(subaccount_ids, vendor_id, logger): """Check if subaccount and project belong to same vendor.""" for subaccount, projects in subaccount_ids.items(): subaccount_vendor = db_execution(queries.GET_VENDOR_OF_SUBACCOUNT, {'subaccount': subaccount}) project_vendor = db_execution(queries.GET_VENDOR_OF_PROJECTS, {'projects': tuple(projects)}) if len(project_vendor) != len(subaccount_vendor) or project_vendor[0] != subaccount_vendor[0]: logger.info('Vendor of subaccount and project do not match') return False if vendor_id != str(subaccount_vendor[0][0]): logger.info('Vendor of sheet and subaccount vendor do not match') return False return True