def create_or_update_lead_in_marketo(person_id): """ Create or update a lead in Marketo. Update can be performed if the person is already associated to a lead (field marketoid is populated). If the lead is already up-to-date with the associated person, does nothing. Data to set is defined in mappings. :param person_id: The person id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching person data from Pipedrive with id=%s', str(person_id)) person = pipedrive.Person(get_pipedrive_client(), person_id) if person.id is not None: lead = marketo.Lead(get_marketo_client(), person.marketoid) if lead.id is None: app.logger.info('New lead created') status = 'created' else: app.logger.info('Lead data fetched from Marketo with id=%s', str(lead.id)) status = 'updated' data_changed = False for mkto_field in mappings.LEAD_TO_PERSON: data_changed = update_field( person, lead, mkto_field, mappings.LEAD_TO_PERSON[mkto_field]) or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending person data with id=%s to Marketo%s', str(person_id), ' with id=%s' % str(person.id) if person.id is not None else '') lead.save() if not person.marketoid or len( person.marketoid.split(',')) > 1 or int( person.marketoid) != lead.id: app.logger.info( 'Updating marketo_id=%s in Pipedrive%s', lead.id, ' (old=%s)' % person.marketoid if person.marketoid else '') person.marketoid = lead.id person.save() else: app.logger.info('Nothing to do in Marketo for lead with id=%s', lead.id) status = 'skipped' response = {'status': status, 'id': lead.id} else: message = 'No person found with id %s' % str(person_id) app.logger.error(message) response = {'error': message} return response
def create_or_update_person_in_pipedrive(lead_id): """ Create or update a person in Pipedrive. Update can be performed if the lead is already associated to a person (field pipedriveId is populated). If the person is already up-to-date with the associated lead, does nothing. Data to set is defined in mappings. :param lead_id: The lead id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching lead data from Marketo with id=%s', str(lead_id)) lead = marketo.Lead(get_marketo_client(), lead_id) if lead.id is not None: person = pipedrive.Person(get_pipedrive_client(), lead.pipedriveId) if person.id is None: app.logger.info('New person created') status = 'created' else: app.logger.info('Person data fetched from Pipedrive with id=%s', str(person.id)) status = 'updated' data_changed = False for pd_field in mappings.PERSON_TO_LEAD: data_changed = update_field( lead, person, pd_field, mappings.PERSON_TO_LEAD[pd_field]) or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending lead data with id=%s to Pipedrive%s', str(lead_id), ' for person with id=%s' % str(person.id) if person.id is not None else '') person.save() if not lead.pipedriveId or lead.pipedriveId != person.id: app.logger.info( 'Updating pipedrive_id=%s in Marketo%s', person.id, ' (old=%s)' % lead.pipedriveId if lead.pipedriveId else '') lead.pipedriveId = person.id lead.save() else: app.logger.info('Nothing to do in Pipedrive for person with id=%s', person.id) status = 'skipped' response = {'status': status, 'id': person.id} else: message = 'No lead found in Marketo with id=%s' % str(lead_id) app.logger.error(message) response = {'error': message} return response
def delete_person_in_pipedrive(lead_pipedrive_id): """ Delete a person in Pipedrive. :param lead_pipedrive_id: The lead related person id to delete :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Deleting person in Pipedrive with id=%s', str(lead_pipedrive_id)) person = pipedrive.Person(get_pipedrive_client(), lead_pipedrive_id, 'id', False) person_id = person.delete() if person_id: response = {'status': 'deleted', 'id': person_id} else: message = 'Could not delete person in Pipedrive with id=%s' % str( lead_pipedrive_id) app.logger.error(message) response = {'error': message} return response