// The internal methods are only used by the tests. internal async Task CopyOrganisationFromPoweroffice( long powerofficeOrganisationId) { var powerofficeOrganisation = await PowerofficeClient.GetCustomer(powerofficeOrganisationId); await CopyOrganisationFromPoweroffice(powerofficeOrganisation); }
/// <summary>Returns the PowerOffice customer code of the organisation that this delivery is associated with. Returns `null` if the code couldn't be found. This can happen if the organisation doesn't exist in PowerOffice.</summary> private async Task <long?> GetPowerofficeCustomerCode( DeliveryDto webcrmDelivery) { if (!webcrmDelivery.DeliveryOrganisationId.HasValue) { throw new ApplicationException("Cannot get PowerOffice organisation ID since DeliveryOrganisationId is null."); } var webcrmOrganisation = await WebcrmClient.GetOrganisationById(webcrmDelivery.DeliveryOrganisationId.Value); long?powerofficeOrganisationId = webcrmOrganisation.GetPowerofficeOrganisationId(Configuration.OrganisationIdFieldName); if (!powerofficeOrganisationId.HasValue) { return(null); } var powerofficeOrganisation = await PowerofficeClient.GetCustomer(powerofficeOrganisationId.Value); if (powerofficeOrganisation == null) { throw new ApplicationException($"Could not find a PowerOffice organisation with ID {powerofficeOrganisationId.Value}."); } if (!powerofficeOrganisation.Code.HasValue) { throw new ApplicationException("PowerOffice organisation code is null."); } return(powerofficeOrganisation.Code.Value); }
public async Task CopyOrganisationToPoweroffice( OrganisationDto webcrmOrganisation) { long?powerofficeOrganisationId = webcrmOrganisation.GetPowerofficeOrganisationId(Configuration.OrganisationIdFieldName); if (powerofficeOrganisationId == null) { Logger.LogTrace("Copying organisation to PowerOffice, creating a new one."); var newPowerofficeOrganisation = new NewCustomer(webcrmOrganisation); var createdPowerofficeOrganisation = await PowerofficeClient.CreateCustomer(newPowerofficeOrganisation); webcrmOrganisation.SetPowerofficeOrganisationId(Configuration.OrganisationIdFieldName, createdPowerofficeOrganisation.Id); await WebcrmClient.UpdateOrganisation(webcrmOrganisation); } else { var matchingPowerofficeOrganisation = await PowerofficeClient.GetCustomer(powerofficeOrganisationId.Value); if (matchingPowerofficeOrganisation == null) { Logger.LogWarning($"Could not find PowerOffice organisation (customer) with id {powerofficeOrganisationId.Value}."); return; } if (!webcrmOrganisation.HasChangesRelevantToPoweroffice(matchingPowerofficeOrganisation, Configuration)) { Logger.LogTrace("Not copying organisation to PowerOffice because it does not have any relevant changes."); return; } Logger.LogTrace("Copying organisation to PowerOffice, updating an existing one."); matchingPowerofficeOrganisation.Update(webcrmOrganisation); await PowerofficeClient.UpdateCustomer(matchingPowerofficeOrganisation); } }