"""Ledger Vat Summary logic.""" from typing import Iterable, Type from abacus_common_logic.models.base import db from owsresponse import response from sqlalchemy import exc from ledger.constants import error from ledger.logic.exceptions import LogicError from ledger.models import LedgerVatSummary def bulk_create( event_id: int, statement_period_id: int, create_params: Iterable[dict], ) -> Type[response.Response]: """Create one or more ledger_vat_summary records. Args: event_id (int): id of the event statement_period_id (int): id of the statement period create_params (List[dict]): list of POST parameters """ base_params = { 'statement_period_id': statement_period_id, 'abacus_event_id': event_id, } instances = [] for params in create_params: params.update(base_params) # NOTE: If an `activity_statement_period_id` is not provided, default to # using the `statement_period_id`. if 'activity_statement_period_id' not in params: params.update({'activity_statement_period_id': statement_period_id}) record = LedgerVatSummary(**params) instances.append(record) if instances: try: db.session.bulk_save_objects(instances) db.session.commit() except exc.IntegrityError: db.session.rollback() raise LogicError(error.ERROR_INTEGRITY) return response.Response(message='ok', status=201)