示例#1
0
        // Embed relations in working-hour resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelationsAsync(WorkingHour workingHour, WorkingHour oldWorkingHour = null)
        {
            try {
                if (oldWorkingHour != null)
                {
                    workingHour.Invoice = oldWorkingHour.Invoice; // invoice cannot be updated. Take invoice of old workingHour on update.
                }
                else
                {
                    workingHour.Invoice = await _invoiceDataProvider.GetByIdAsync(workingHour.Invoice.Id);
                }

                if (oldWorkingHour != null && oldWorkingHour.Employee != null && oldWorkingHour.Employee.Id == oldWorkingHour.Employee.Id)
                {
                    workingHour.Employee = oldWorkingHour.Employee;
                }
                else
                {
                    workingHour.Employee = await _employeeDataProvider.GetByIdAsync(workingHour.Employee.Id);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }
示例#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.");
            }
        }
示例#3
0
        // Embed relations in request resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelations(Order order, Order oldOrder = null)
        {
            try {
                if (order.VatRate != null)
                {
                    if (oldOrder != null && oldOrder.VatRate != null && oldOrder.VatRate.Id == order.VatRate.Id)
                    {
                        order.VatRate = oldOrder.VatRate;
                    }
                    else
                    {
                        order.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(order.VatRate.Id));
                    }
                }
                else
                {
                    // Order must have a VAT rate. Take VAT rate of oldOrder if none is passed.
                    if (oldOrder != null)
                    {
                        order.VatRate = oldOrder.VatRate;
                        _logger.LogDebug("Received an update for order {0} without a VAT rate", order.Id);
                    }
                }

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

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

                // Customer cannot be updated. Take customer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Customer = oldOrder.Customer;
                }
                else
                {
                    order.Customer = await _customerDataProvider.GetByNumberAsync(order.Customer.Id);
                }

                // Offer cannot be updated. Take offer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Offer = oldOrder.Offer;
                }
                else
                {
                    order.Offer = await _offerDataProvider.GetByIdAsync(order.Offer.Id);
                }

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

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

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

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