Exemple #1
0
        // 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));
                    }
                }

                // Origin cannot be updated. Take origin of oldRequest on update.
                if (oldRequest != null)
                {
                    request.Origin = oldRequest.Origin;
                }
                else if (request.Origin != null)
                {
                    request.Origin = await _interventionDataProvider.GetByIdAsync(request.Origin.Id);
                }

                // Offer cannot be updated. Take offer of oldRequest on update.
                if (oldRequest != null)
                {
                    request.Offer = oldRequest.Offer;
                }
                else
                {
                    request.Offer = null;
                }

                if (request.Offer != null && request.Customer == null)
                {
                    request.Customer = oldRequest.Customer;  // Request already has an offer, so it must have a customer
                }
                else
                {
                    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" };

                // 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.");
            }
        }
Exemple #2
0
        // Embed relations in intervention resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelations(Intervention intervention, Intervention oldIntervention = null)
        {
            try {
                if (intervention.WayOfEntry != null)
                {
                    if (oldIntervention != null && oldIntervention.WayOfEntry != null && oldIntervention.WayOfEntry.Id == intervention.WayOfEntry.Id)
                    {
                        intervention.WayOfEntry = oldIntervention.WayOfEntry;
                    }
                    else
                    {
                        intervention.WayOfEntry = await _wayOfEntryDataProvider.GetByIdAsync(int.Parse(intervention.WayOfEntry.Id));
                    }
                }

                if (intervention.Employee != null)
                {
                    if (oldIntervention != null && oldIntervention.Employee != null && oldIntervention.Employee.Id == intervention.Employee.Id)
                    {
                        intervention.Employee = oldIntervention.Employee;
                    }
                    else
                    {
                        intervention.Employee = await _employeeDataProvider.GetByIdAsync(intervention.Employee.Id);
                    }
                }

                if (intervention.Origin != null)
                {
                    if (oldIntervention != null && oldIntervention.Origin != null && oldIntervention.Origin.Id == intervention.Origin.Id)
                    {
                        intervention.Origin = oldIntervention.Origin;
                    }
                    else
                    {
                        intervention.Origin = await _orderDataProvider.GetByIdAsync(intervention.Origin.Id);
                    }
                }

                var technicians = new List <Employee>();
                foreach (var technicianRelation in intervention.Technicians)
                {
                    var technician = await _employeeDataProvider.GetByIdAsync(technicianRelation.Id);

                    technicians.Add(technician);
                }
                intervention.Technicians = technicians;

                // Invoice cannot be updated. Take invoice of oldRequest on update.
                if (oldIntervention != null)
                {
                    intervention.Invoice = oldIntervention.Invoice;
                }
                else
                {
                    intervention.Invoice = null;
                }

                if (intervention.Invoice != null && intervention.Customer == null)
                {
                    intervention.Customer = oldIntervention.Customer;  // Intervention already has an invoice, so it must have a customer
                }
                else
                {
                    if (intervention.Customer != null)
                    {
                        if (oldIntervention != null && oldIntervention.Customer != null && oldIntervention.Customer.Id == intervention.Customer.Id)
                        {
                            intervention.Customer = oldIntervention.Customer;
                        }
                        else
                        {
                            intervention.Customer = await _customerDataProvider.GetByNumberAsync(intervention.Customer.Id);
                        }
                    }
                }

                var includeCustomer = new QuerySet();
                includeCustomer.Include.Fields = new string[] { "customer" };

                // Contact can only be updated through CaseManager. Take contact of oldRequest on update.
                if (oldIntervention != null)
                {
                    intervention.Contact = oldIntervention.Contact;
                }
                else if (intervention.Contact != null)
                {
                    intervention.Contact = await _contactDataProvider.GetByIdAsync(intervention.Contact.Id, includeCustomer);
                }

                // Building can only be updated through CaseManager. Take building of oldRequest on update.
                if (oldIntervention != null)
                {
                    intervention.Building = oldIntervention.Building;
                }
                else if (intervention.Building != null)
                {
                    intervention.Building = await _buildingDataProvider.GetByIdAsync(intervention.Building.Id, includeCustomer);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }