"""Logic for account note.""" from owsresponse import response from owsresponse import status as owsresponse_status from product_review.constants.error import ERROR_CODE_NOT_FOUND, ERROR_MESSAGE_NOT_FOUND from product_review.models import account_note as account_note_model from product_review.models import subaccount_note as subaccount_note_model from product_review.util.db import db_transaction from product_review.util.exception import raise_exception_for_ows_response def get_account_note(vendor_id): """Get an account note.""" result = account_note_model.get_account_note(vendor_id) if not result: raise_exception_for_ows_response( status=owsresponse_status.NOT_FOUND, code=ERROR_CODE_NOT_FOUND, message=ERROR_MESSAGE_NOT_FOUND, ) return response.Response(result) def get_subaccount_note(subaccount_id): """Get a subaccout note.""" result = subaccount_note_model.get_subaccount_note(subaccount_id) if not result: raise_exception_for_ows_response( status=owsresponse_status.NOT_FOUND, code=ERROR_CODE_NOT_FOUND, message=ERROR_MESSAGE_NOT_FOUND, ) return response.Response(result) def add_account_note(vendor_id, note, updated_by): """Add/ update account note for a vendor.""" with db_transaction: existing_note = account_note_model.get_account_note(vendor_id) if existing_note: account_note_model.update_account_note(vendor_id, note, updated_by) return response.Response("Successfully updated account note.") else: account_note_model.create_account_note(vendor_id, note, updated_by) return response.Response("Successfully created account note.") def add_subaccount_note(subaccount_id, note, updated_by): """Add/ update a subaccount note.""" with db_transaction: existing_note = subaccount_note_model.get_subaccount_note(subaccount_id) if existing_note: subaccount_note_model.update_subaccount_note( subaccount_id, note, updated_by ) return response.Response("Successfully updated subaccount note.") else: subaccount_note_model.create_subaccount_note( subaccount_id, note, updated_by ) return response.Response("Successfully created subaccount note.") def dataload_account_notes(account_ids): """Dataload account notes.""" account_notes = account_note_model.get_account_notes_by_vendor_ids(account_ids) mapped_response = {int(an.vendor_id): an.note for an in account_notes} return [ {"vendor_id": account_id, "note": mapped_response.get(account_id)} for account_id in account_ids ] def dataload_subaccount_notes(subaccount_ids): """Dataload subaccount notes.""" subaccount_notes = subaccount_note_model.get_subaccount_notes_by_subaccount_ids( subaccount_ids ) mapped_response = {int(sn.subaccount_id): sn.note for sn in subaccount_notes} return [ {"subaccount_id": subaccount_id, "note": mapped_response.get(subaccount_id)} for subaccount_id in subaccount_ids ]