"""Vendor Agreement information DB layer module. Contains functions that create/retrieve agreements. """ from owsresponse import response from sqlalchemy import Column, DATETIME, func, Integer, String from users import constants as const from users.connectors import mysql class VendorAgreement(mysql.BaseModel): """Vendor Agreement Model.""" __tablename__ = 'vendor_agreement' vendor_agreement_id = Column(Integer, primary_key=True, nullable=False, autoincrement=True) opt_in_preference_id = Column(Integer, nullable=False) vendor_id = Column(Integer, nullable=False) user_id = Column(Integer, nullable=True) date_accepted = Column(DATETIME, default=func.now()) impersonator_user_id = Column(String, nullable=True, default=None) def to_dict(self): """Get a dict representation of Vendor Agreement.""" return { 'vendor_agreement_id': self.vendor_agreement_id, 'opt_in_preference_id': self.opt_in_preference_id, 'vendor_id': self.vendor_id, 'user_id': self.user_id, 'date_accepted': str(self.date_accepted), 'impersonator_user_id': self.impersonator_user_id, } @mysql.wrap_db_errors def create_vendor_agreement(data): """Create an entity to the vendor_agreement table. Args: data (dict): The vendor agreement data. This dictionary should contain the vendor_id and and the opt_in_preference_id Returns: response.Response: containing the created vendor_agreement. """ if not data: return response.create_error_response( code=const.BAD_PARAMS_ERROR_CODE, message=const.BAD_PARAMS_ERROR_message, status=400 ) with mysql.db_session() as session: vendorAgreement = VendorAgreement(**data) session.add(vendorAgreement) session.commit() return response.Response(vendorAgreement.to_dict()) @mysql.wrap_db_errors def get_vendor_agreement(vendor_id, permission_type_id, exclude_impersonator): """Get a vendor agreement based on vendor_id and permission_type_id. Args: vendor_id (int): The vendor unique identifier permission_type_id: the opt_in_preference_id exclude_impersonator (bool): If True results with an impersonator user ID will be excluded. Returns: response.Response: containing the vendor permission agreement. """ if not vendor_id or not permission_type_id: return response.create_error_response( code=const.BAD_PARAMS_ERROR_CODE, message=const.BAD_PARAMS_ERROR_message, status=400 ) with mysql.db_session() as session: filters = [ VendorAgreement.vendor_id == vendor_id, VendorAgreement.opt_in_preference_id == permission_type_id, ] if exclude_impersonator is True: filters.append(VendorAgreement.impersonator_user_id.is_(None)) query = session.query(VendorAgreement).filter(*filters) result = query.one_or_none() if not result: return response.create_not_found_response() return response.Response(result.to_dict()) @mysql.wrap_db_errors def delete_vendor_agreement(vendor_agreement_id): """Delete a vendor agreement. Args: vendor_agreement_id (int): The id of the agreement to delete Returns: response.Response: 204 status code if successful """ with mysql.db_session() as session: affected_rows = ( session.query(VendorAgreement) .filter(VendorAgreement.vendor_agreement_id == vendor_agreement_id) .delete() ) if affected_rows == 0: return response.create_error_response( code=const.ERROR_CODE_VENDOR_AGREEMENT_NOT_FOUND, message=const.ERROR_MESSAGE_VENDOR_AGREEMENT_NOT_FOUND, status=404, ) session.commit() return response.Response(status=204)