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); } }