Exemple #1
0
def saveSfdcContactsToMaster(user_id=None, company_id=None, job_id=None, run_type=None):
    # delete later
    # job_id = ObjectId("569adcfc8afb00205c799f28")
    if run_type == "initial":
        contacts = TempData.objects(
            Q(company_id=company_id) & Q(record_type="contact") & Q(source_system="sfdc") & Q(job_id=job_id)
        ).only(
            "source_record"
        )  # & Q(job_id=job_id)
    else:
        contacts = TempDataDelta.objects(
            Q(company_id=company_id) & Q(record_type="contact") & Q(source_system="sfdc") & Q(job_id=job_id)
        ).only(
            "source_record"
        )  # & Q(job_id=job_id)
    print "co id is " + str(company_id)
    contactListTemp = list(contacts)
    contactList = [i["source_record"] for i in contactListTemp]
    # print 'saving sfdc contacts'
    try:
        # get the custom field for Contact Status, if it exists
        existingIntegration = CompanyIntegration.objects(company_id=company_id).first()
        contact_status = None
        if "sfdc" in existingIntegration["integrations"]:
            contact_status = existingIntegration["mapping"].get("sfdc_contact_status", None)

        for newContact in contactList:  # ['records']:

            # company_id = request.user.company_id
            sfdc_contact_Id = str(newContact["Id"])
            print "contact id is " + sfdc_contact_Id
            # sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
            existingLeadMkto = None
            existingLeadSfdc = None
            existingLeadHspt = None
            existingContact = Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).first()

            if existingContact is not None:  # we found this contact already in the DB
                print "found contact match for " + str(sfdc_contact_Id)
                if "sfdc" in existingContact.contacts:
                    existingContact.source_first_name = newContact["FirstName"]
                    existingContact.source_last_name = newContact["LastName"]
                    existingContact.source_email = newContact["Email"]
                    # existingContact.source_created_date = str(newContact['CreatedDate'])
                    existingContact.source_source = newContact["LeadSource"]
                    if contact_status is not None and contact_status in newContact:
                        existingContact.source_status = newContact[contact_status]
                    existingContact.contacts["sfdc"] = newContact
                    existingContact.save()
                    # Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).update(contacts__sfdc=newContact)
                else:
                    existingContact.contacts["sfdc"] = {}
                    existingContact.contacts["sfdc"] = newContact
                    existingContact.save()
            # elif existingContact is None:  # this lead does not exist
            else:
                existingLeadSfdc = (
                    Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__ConvertedContactId=sfdc_contact_Id))
                    .hint("company_id_1_leads.sfdc.convertedContactId_1")
                    .first()
                )
                if existingLeadSfdc is not None:
                    print "found match for sfdc lead for contact " + str(sfdc_contact_Id)
                    # existingLeadSfdcList = list(existingLeadSfdc)
                    existingLeadSfdc.sfdc_contact_id = sfdc_contact_Id
                    if contact_status is not None and contact_status in newContact:
                        existingLeadSfdc.source_status = newContact[contact_status]
                    existingLeadSfdc.contacts = {}
                    existingLeadSfdc.contacts["sfdc"] = newContact
                    existingLeadSfdc.save()
                    # remove below comments after figuring out how Mkto stored SFDC contact ID
                else:
                    existingLeadMkto = Lead.objects(
                        Q(company_id=company_id) & Q(leads__mkto__sfdcContactId=sfdc_contact_Id)
                    ).first()
                    if (
                        existingLeadMkto is not None
                    ):  # we found a MKto lead record which is matched to this new Sfdc lead
                        print "found mkto lead" + existingLeadMkto.mkto_id
                        existingLeadMkto.sfdc_contact_id = sfdc_contact_Id
                        # existingLeadMkto.contacts = {}
                        existingLeadMkto.contacts["sfdc"] = newContact
                        existingLeadMkto.save()
                    else:
                        existingLeadHspt = Lead.objects(
                            Q(company_id=company_id) & Q(leads__hspt__properties__salesforcecontactid=sfdc_contact_Id)
                        ).first()
                        if (
                            existingLeadHspt is not None
                        ):  # we found a MKto lead record which is matched to this new Sfdc lead
                            existingLeadHspt.sfdc_contact_id = sfdc_contact_Id
                            existingLeadHspt.contacts = {}
                            existingLeadHspt.contacts["sfdc"] = newContact
                            existingLeadHspt.save()
            if (
                existingLeadSfdc is None
                and existingLeadMkto is None
                and existingLeadHspt is None
                and existingContact is None
            ):  # no matches found so save new record
                lead = Lead()
                lead.sfdc_contact_id = sfdc_contact_Id
                lead.company_id = company_id
                lead.source_first_name = newContact["FirstName"]
                lead.source_last_name = newContact["LastName"]
                lead.source_email = newContact["Email"]
                lead.source_created_date = str(newContact["CreatedDate"])
                lead.source_source = newContact["LeadSource"]
                if contact_status is not None and contact_status in newContact:
                    lead.source_status = newContact[contact_status]
                lead.contacts = {}
                lead.contacts["sfdc"] = newContact
                lead.save()
            # lead = Lead()
    #             company_id = request.user.company_id
    #             derived_id = 'sfdc_' + str(newLead['Id'])
    #             Lead.objects(derived_id = derived_id).modify(upsert=True, new=True, set__leads__sfdc = newLead, set_on_insert__derived_id = derived_id, set_on_insert__company_id = company_id)
    #
    #             oldLead = Lead.objects(derived_id = lead.derived_id)
    #             if oldLead.count() == 0:
    #                 lead.leads["sfdc"] = newLead
    #                 lead.save()
    #             else:
    #                 oldLead.leads["sfdc"] = newLead
    #                 Lead.objects(derived_id = lead.derived_id).update(oldLead)
    except Exception as e:
        print "exception while saving SFDC contact " + str(e)
        send_notification(dict(type="error", success=False, message=str(e)))