Example #1
0
        public async Task CopyPersonToPoweroffice(
            PersonDto webcrmPerson)
        {
            var personKey = webcrmPerson.GetPowerofficePersonKey(Logger, Configuration.PersonIdFieldName);

            if (personKey == null)
            {
                long?powerofficeOrganisationId = await GetPowerofficeOrganisationId(webcrmPerson);

                if (powerofficeOrganisationId == null)
                {
                    throw new ApplicationException("Cannot create person in PowerOffice without an associated organisation.");
                }

                Logger.LogTrace("Copying person to PowerOffice, creating a new one.");
                var newPowerofficePerson = new NewContactPerson();
                newPowerofficePerson.Update(webcrmPerson);
                var createdPowerofficePerson = await PowerofficeClient.CreateContactPerson(powerofficeOrganisationId.Value, newPowerofficePerson);

                personKey = new PowerofficePersonKey(powerofficeOrganisationId.Value, createdPowerofficePerson.Id);
                webcrmPerson.SetPowerofficePersonKey(Configuration.PersonIdFieldName, personKey);
                await WebcrmClient.UpdatePerson(webcrmPerson);
            }
            else
            {
                var matchingPowerofficePerson = await PowerofficeClient.GetContactPerson(personKey.PowerofficeOrganisationId, personKey.PowerofficePersonId);

                if (matchingPowerofficePerson == null)
                {
                    Logger.LogWarning($"Could not find PowerOffice person (contact person) with key {personKey}.");
                    return;
                }

                if (!matchingPowerofficePerson.HasRelevantChanges(webcrmPerson))
                {
                    Logger.LogTrace("Not copying person to PowerOffice because it does not have any relevant changes.");
                    return;
                }

                Logger.LogTrace("Copying person to PowerOffice, updating an existing one.");
                matchingPowerofficePerson.Update(webcrmPerson);
                await PowerofficeClient.UpdateContactPerson(personKey.PowerofficeOrganisationId, matchingPowerofficePerson);
            }
        }
Example #2
0
        public async Task CopyPersonFromPoweroffice(
            ContactPerson powerofficeContactPerson,
            long powerofficeOrganisationId)
        {
            var webcrmOrganisation = await WebcrmClient.GetOrganisationByField(Configuration.OrganisationIdFieldName, powerofficeOrganisationId.ToString());

            if (webcrmOrganisation == null)
            {
                // We don't know if the missing organisation is not being synchronized or simply hasn't been synchronized yet. Throwing an exception to put this person message back on the queue. This might result in false positives on the poison queue, but will make the system handle the out-of-order execution that might happen when multiple messages are dequeued simultaneously.
                throw new ApplicationException($"Could not find matching organisation in webCRM where field name '{Configuration.OrganisationIdFieldName}' has the value '{powerofficeOrganisationId}'. Cannot update upserted person with PowerOffice id {powerofficeOrganisationId}/{powerofficeContactPerson.Id}.");
            }

            if (!webcrmOrganisation.ShouldSynchronise(Configuration))
            {
                Logger.LogTrace("Not copying person from PowerOffice, because the associated organisation should not be synchronised.");
                return;
            }

            var personKey            = new PowerofficePersonKey(powerofficeOrganisationId, powerofficeContactPerson.Id);
            var matchingWebcrmPerson = await WebcrmClient.GetPersonByField(Configuration.PersonIdFieldName, personKey.ToString());

            if (matchingWebcrmPerson == null)
            {
                Logger.LogTrace("Copying person from PowerOffice, creating a new one.");
                var newWebcrmPerson = new PersonDto();
                newWebcrmPerson.Update(Logger, powerofficeContactPerson, powerofficeOrganisationId, webcrmOrganisation.OrganisationId, Configuration);
                await WebcrmClient.CreatePerson(newWebcrmPerson);
            }
            else
            {
                if (!powerofficeContactPerson.HasRelevantChanges(matchingWebcrmPerson))
                {
                    Logger.LogTrace("Not copying person from PowerOffice because it does not have any relevant changes.");
                    return;
                }

                Logger.LogTrace("Copying person from PowerOffice, updating an existing one.");
                matchingWebcrmPerson.Update(Logger, powerofficeContactPerson, powerofficeOrganisationId, webcrmOrganisation.OrganisationId, Configuration);
                await WebcrmClient.UpdatePerson(matchingWebcrmPerson);
            }
        }