public async Task <Paged <Request> > GetAllByContactIdAsync(int contactId, QuerySet query) { try { var includeQuery = new QuerySet(); includeQuery.Include.Fields = new string[] { "customer" }; var contact = await _contactDataProvider.GetByIdAsync(contactId, includeQuery); if (query.Sort.Field == null) { query.Sort.Order = SortQuery.ORDER_DESC; query.Sort.Field = "number"; } return(await _requestDataProvider.GetAllByRelativeContactIdAsync(contact.Customer.Id, contact.Number, query)); } catch (EntityNotFoundException) { return(new Paged <Request> { Count = 0, Items = new List <Request>(), PageNumber = 0, PageSize = query.Page.Size }); } }
private async Task EmbedCustomerAndContactTelephonesAsync(ICaseRelated resource) { var telephoneQuery = new QuerySet(); telephoneQuery.Sort.Field = "order"; telephoneQuery.Sort.Order = SortQuery.ORDER_ASC; telephoneQuery.Include.Fields = new string[] { "country", "telephone-type" }; if (resource.Customer != null) { var customerIncludeQuery = new QuerySet(); customerIncludeQuery.Include.Fields = new string[] { "honorific-prefix", "language" }; resource.Customer = await _customerDataProvider.GetByNumberAsync(resource.Customer.Number, customerIncludeQuery); var telephones = await _telephoneDataProvider.GetAllByCustomerIdAsync(resource.Customer.Id, telephoneQuery); resource.Customer.Telephones = telephones.Items; } if (resource.Contact != null) { var contactIncludeQuery = new QuerySet(); contactIncludeQuery.Include.Fields = new string[] { "honorific-prefix", "language" }; resource.Contact = await _contactDataProvider.GetByIdAsync(resource.Contact.Id, contactIncludeQuery); var telephones = await _telephoneDataProvider.GetAllByContactIdAsync(resource.Contact.Id, telephoneQuery); resource.Contact.Telephones = telephones.Items; } }
// Note: contact and building of a Case can only be updated through this method // UpdateAsync() methods of a resource in OfferManager, OrdereManager, etc. prevent the update of the contact/building for an existing resource // That's why we directly call methods of the OfferDataProvider, OrderDataProvider, etc. here public async Task UpdateContactAndBuildingAsync(int?contactId, int?buildingId, int?requestId, int?interventionId, int?offerId, int?orderId, int?invoiceId) { int?relativeContactId = null; int?relativeBuildingId = null; try { if (contactId != null) { var contact = await _contactDataProvider.GetByIdAsync((int)contactId); relativeContactId = contact.Number; } if (buildingId != null) { var building = await _buildingDataProvider.GetByIdAsync((int)buildingId); relativeBuildingId = building.Number; } if (requestId != null) { await _requestDataProvider.UpdateContactAndBuildingAsync((int)requestId, relativeContactId, relativeBuildingId); } if (interventionId != null) { await _interventionDataProvider.UpdateContactAndBuildingAsync((int)interventionId, relativeContactId, relativeBuildingId); } if (offerId != null) { await _offerDataProvider.UpdateContactAndBuildingAsync((int)offerId, relativeContactId, relativeBuildingId); } if (orderId != null) { // updating the offer automatically updates the contact/building of the order too. No need to do that explicitly here. var query = new QuerySet(); query.Page.Size = 1000; // TODO we assume 1 case doesn't have more than 1000 deposit invoices. Ideally, we should query by page. var depositInvoices = await _depositInvoiceDataProvider.GetAllByOrderIdAsync((int)orderId, query); foreach (var depositInvoice in depositInvoices.Items) { await _depositInvoiceDataProvider.UpdateContactAndBuildingAsync(depositInvoice.Id, relativeContactId, relativeBuildingId); } } if (invoiceId != null) { await _invoiceDataProvider.UpdateContactAndBuildingAsync((int)invoiceId, relativeContactId, relativeBuildingId); } } catch (EntityNotFoundException e) { throw new IllegalArgumentException("IllegalEntity", $"Contact and building cannot be updated: ${e.Message}", e); } }
// Embed relations in request resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelations(Offer offer, Offer oldOffer = null) { try { if (offer.VatRate != null) { if (oldOffer != null && oldOffer.VatRate != null && oldOffer.VatRate.Id == offer.VatRate.Id) { offer.VatRate = oldOffer.VatRate; } else { offer.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(offer.VatRate.Id)); } } else { // Offer must have a VAT rate. Take VAT rate of oldOffer if none is passed. if (oldOffer != null) { offer.VatRate = oldOffer.VatRate; _logger.LogDebug("Received an update for offer {0} without a VAT rate", offer.Id); } } // Customer cannot be updated. Take customer of oldOffer on update. if (oldOffer != null) { offer.Customer = oldOffer.Customer; } else { offer.Customer = await _customerDataProvider.GetByNumberAsync(offer.Customer.Id); } // Request cannot be updated. Take request of oldOffer on update. if (oldOffer != null) { offer.Request = oldOffer.Request; } else { offer.Request = await _requestDataProvider.GetByIdAsync(offer.Request.Id); } // Order cannot be updated. Take order of oldOffer on update. if (oldOffer != null) { offer.Order = oldOffer.Order; } else { offer.Order = null; } var includeCustomer = new QuerySet(); includeCustomer.Include.Fields = new string[] { "customer" }; // Contact can only be updated through CaseManager. Take contact of oldOffer on update. if (oldOffer != null) { offer.Contact = oldOffer.Contact; } else if (offer.Contact != null) { offer.Contact = await _contactDataProvider.GetByIdAsync(offer.Contact.Id, includeCustomer); } // Building can only be updated through CaseManager. Take building of oldOffer on update. if (oldOffer != null) { offer.Building = oldOffer.Building; } else if (offer.Building != null) { offer.Building = await _buildingDataProvider.GetByIdAsync(offer.Building.Id, includeCustomer); } } catch (EntityNotFoundException) { _logger.LogDebug("Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
public async Task <Telephone> CreateAsync(Telephone telephone) { // Validations if (telephone.Id != null) { throw new IllegalArgumentException("IllegalAttribute", "Telephone cannot have an id on create."); } if (telephone.Area == null) { throw new IllegalArgumentException("IllegalAttribute", "Area is required on telephone creation."); } if (telephone.Number == null) { throw new IllegalArgumentException("IllegalAttribute", "Number is required on telephone creation."); } if (telephone.Country == null) { throw new IllegalArgumentException("IllegalAttribute", "Country is required on telephone creation."); } if (telephone.TelephoneType == null) { throw new IllegalArgumentException("IllegalAttribute", "Telephone-type is required on telephone creation."); } if (new CustomerEntity[3] { telephone.Customer, telephone.Contact, telephone.Building }.Where(x => x != null).Count() != 1) { throw new IllegalArgumentException("IllegalAttribute", "Just one of customer, contact or building is required on telephone creation."); } if (telephone.Number.Length < 6) { throw new IllegalArgumentException("IllegalAttribute", "Number must at least contain 6 digits."); } if (telephone.Area.Length < 2 || telephone.Area.Length > 4) { throw new IllegalArgumentException("IllegalAttribute", "Area must contain 2-4 digits."); } // Embed existing records try { var id = int.Parse(telephone.Country.Id); telephone.Country = await _countryDataProvider.GetByIdAsync(id); id = int.Parse(telephone.TelephoneType.Id); telephone.TelephoneType = await _telephoneTypeDataProvider.GetByIdAsync(id); if (telephone.Customer != null) { telephone.Customer = await _customerDataProvider.GetByNumberAsync(telephone.Customer.Id); } else if (telephone.Contact != null) { telephone.Contact = await _contactDataProvider.GetByIdAsync(telephone.Contact.Id); } else if (telephone.Building != null) { telephone.Building = await _buildingDataProvider.GetByIdAsync(telephone.Building.Id); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } return(await _telephoneDataProvider.CreateAsync(telephone)); }
// Embed relations in request resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelations(Request request, Request oldRequest = null) { try { if (request.WayOfEntry != null) { if (oldRequest != null && oldRequest.WayOfEntry != null && oldRequest.WayOfEntry.Id == request.WayOfEntry.Id) { request.WayOfEntry = oldRequest.WayOfEntry; } else { request.WayOfEntry = await _wayOfEntryDataProvider.GetByIdAsync(int.Parse(request.WayOfEntry.Id)); } } if (request.Customer != null) { if (oldRequest != null && oldRequest.Customer != null && oldRequest.Customer.Id == request.Customer.Id) { request.Customer = oldRequest.Customer; } else { request.Customer = await _customerDataProvider.GetByNumberAsync(request.Customer.Id); } } var includeCustomer = new QuerySet(); includeCustomer.Include.Fields = new string[] { "customer" }; // Offer cannot be updated. Take offer of oldRequest on update. if (oldRequest != null) { request.Offer = oldRequest.Offer; } else { request.Offer = null; } // Contact can only be updated through CaseManager. Take contact of oldRequest on update. if (oldRequest != null) { request.Contact = oldRequest.Contact; } else if (request.Contact != null) { request.Contact = await _contactDataProvider.GetByIdAsync(request.Contact.Id, includeCustomer); } // Building can only be updated through CaseManager. Take building of oldRequest on update. if (oldRequest != null) { request.Building = oldRequest.Building; } else if (request.Building != null) { request.Building = await _buildingDataProvider.GetByIdAsync(request.Building.Id, includeCustomer); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
// Embed relations in deposit invoice resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelationsAsync(DepositInvoice depositInvoice, DepositInvoice oldDepositInvoice = null) { try { if (depositInvoice.VatRate != null) { if (oldDepositInvoice != null && oldDepositInvoice.VatRate != null && oldDepositInvoice.VatRate.Id == depositInvoice.VatRate.Id) { depositInvoice.VatRate = oldDepositInvoice.VatRate; } else { depositInvoice.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(depositInvoice.VatRate.Id)); } } // Customer cannot be updated. Take customer of oldDepositInvoice on update. if (oldDepositInvoice != null) { depositInvoice.Customer = oldDepositInvoice.Customer; } else { depositInvoice.Customer = await _customerDataProvider.GetByNumberAsync(depositInvoice.Customer.Id); } // Order cannot be updated. Take order of oldDepositInvoice on update. if (oldDepositInvoice != null) { depositInvoice.Order = oldDepositInvoice.Order; } else if (depositInvoice.Order != null) // isolated invoice doesn't have an order attached { depositInvoice.Order = await _orderDataProvider.GetByIdAsync(depositInvoice.Order.Id); } var includeCustomer = new QuerySet(); includeCustomer.Include.Fields = new string[] { "customer" }; // Contact can only be updated through CaseManager. Take contact of oldDepositInvoice on update. if (oldDepositInvoice != null) { depositInvoice.Contact = oldDepositInvoice.Contact; } else if (depositInvoice.Contact != null) { depositInvoice.Contact = await _contactDataProvider.GetByIdAsync(depositInvoice.Contact.Id, includeCustomer); } // Building can only be updated through CaseManager. Take building of oldDepositInvoice on update. if (oldDepositInvoice != null) { depositInvoice.Building = oldDepositInvoice.Building; } else if (depositInvoice.Building != null) { depositInvoice.Building = await _buildingDataProvider.GetByIdAsync(depositInvoice.Building.Id, includeCustomer); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
public async Task <Contact> GetByIdAsync(int id, QuerySet query) { return(await _contactDataProvider.GetByIdAsync(id, query)); }