def test_delete_contact_org_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if it's still being used by an entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = entity._model # pylint:disable=protected-access contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() OrgService.delete_contact(org_id=org_id) org = OrgService.find_by_org_id(org_id) response = OrgService.get_contacts(org_id) assert len(response['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert not exist_contact_link
def test_delete_contact_entity_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted without entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = entity._model # pylint:disable=protected-access contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() updated_entity = entity.delete_contact() dictionary = None dictionary = updated_entity.as_dict() assert len(dictionary['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert not delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert exist_contact_link
def update_contact(self, contact_info: dict): """Update a business contact for this entity.""" # find the contact link object for this entity contact_link = ContactLinkModel.find_by_entity_id(self._model.id) if contact_link is None or contact_link.contact is None: raise BusinessException(Error.DATA_NOT_FOUND, None) contact = contact_link.contact contact.update_from_dict(**camelback2snake(contact_info)) contact.commit() return self
def delete_contact(self): """Delete a business contact for this entity.""" contact_link = ContactLinkModel.find_by_entity_id(self._model.id) if contact_link is None: raise BusinessException(Error.DATA_NOT_FOUND, None) del contact_link.entity contact_link.commit() if not contact_link.has_links(): contact = contact_link.contact contact_link.delete() contact.delete() return self
def add_contact(self, contact_info: dict): """Add a business contact to this entity.""" # check for existing contact (we only want one contact per user) contact_link = ContactLinkModel.find_by_entity_id(self._model.id) if contact_link is not None: raise BusinessException(Error.DATA_ALREADY_EXISTS, None) contact = ContactModel(**camelback2snake(contact_info)) contact.commit() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = self._model contact_link.commit() return self
def get_contact(self): """Get the contact for this business.""" contact_link = ContactLinkModel.find_by_entity_id(self._model.id) if contact_link is None: return None return contact_link.contact