Exemplo n.º 1
0
        public override async Task <long> SubmitUpdateChangeRequestAsync <TId>(Vehicle entity, TId id, string requestedBy,
                                                                               ChangeType changeType = ChangeType.None,
                                                                               List <ChangeRequestItemStaging> changeRequestItemStagings = null, CommentsStagingModel comment = null, List <AttachmentsModel> attachments = null, string changeContent = null)
        {
            if (entity.SubModelId.Equals(default(int)) ||
                entity.RegionId.Equals(default(int)))
            {
                throw new ArgumentException(nameof(entity));
            }

            if (id.Equals(default(TId)))
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (!entity.Id.Equals(id))
            {
                throw new ArgumentException("Invalid Vehicle Id");
            }

            var vehicleFromDb = await FindAsync(id);

            if (vehicleFromDb == null)
            {
                throw new NoRecordFound("No Vehicle exist");
            }

            var existingEntry = new Vehicle()
            {
                Id                            = vehicleFromDb.Id,
                BaseVehicleId                 = vehicleFromDb.BaseVehicleId,
                SubModelId                    = vehicleFromDb.SubModelId,
                SourceName                    = vehicleFromDb.SourceName,
                SourceId                      = vehicleFromDb.SourceId,
                RegionId                      = vehicleFromDb.RegionId,
                PublicationStageId            = vehicleFromDb.PublicationStageId,
                PublicationStageSource        = vehicleFromDb.PublicationStageSource,
                PublicationStageDate          = vehicleFromDb.PublicationStageDate,
                PublicationEnvironment        = vehicleFromDb.PublicationEnvironment,
                ChangeRequestId               = vehicleFromDb.ChangeRequestId,
                InsertDate                    = vehicleFromDb.InsertDate,
                DeleteDate                    = vehicleFromDb.DeleteDate,
                LastUpdateDate                = vehicleFromDb.LastUpdateDate,
                VehicleToBrakeConfigCount     = vehicleFromDb.VehicleToBrakeConfigCount,
                VehicleToBedConfigCount       = vehicleFromDb.VehicleToBedConfigCount,
                VehicleToBodyStyleConfigCount = vehicleFromDb.VehicleToBodyStyleConfigCount
            };

            changeType = vehicleFromDb.BaseVehicleId == entity.BaseVehicleId ? ChangeType.Modify : ChangeType.Replace;
            changeRequestItemStagings = changeRequestItemStagings ?? new List <ChangeRequestItemStaging>();

            // Validation check for insert of new vehicle
            await ValidateVehicleHasNoChangeRequest(entity);
            await ValidateVehicleLookupsHasNoChangeRequest(entity);
            await ValidateBaseVehicleHasNoChangeRequest(entity);

            // 5. validate there is no open CR for vehicle to brake system configuration
            // brake system configuration
            var changerequestid = await
                                  ChangeRequestBusinessService.ChangeRequestExist <VehicleToBrakeConfig>(
                x => x.VehicleId == entity.Id);

            if (changerequestid > 0)
            {
                throw new ChangeRequestExistException($"There is already an open CR ID {changerequestid} for Vehicle to Brake System Configuration.");
            }
            // 6. validate there is no open CR to replace vehicle that rely on this vehicle
            {
                // NOTE: No vehicle replacement functionality implemented, yet.
            }
            //<Raja>: I don't see the difference between the above base vehicle validation and below base vehicle validation
            // 7. Validate that there are no open CR to the existing base vehicle.
            // Applicable only during Base vehicle replace
            if (changeType.Equals(ChangeType.Replace))
            {
                await ValidateBaseVehicleHasNoChangeRequest(vehicleFromDb);
            }

            //Fill in the existing values to avoid being overwritten when final approve in change review screen.
            entity.InsertDate             = vehicleFromDb.InsertDate;
            entity.PublicationEnvironment = vehicleFromDb.PublicationEnvironment;
            entity.PublicationStageId     = vehicleFromDb.PublicationStageId;
            entity.PublicationStageDate   = vehicleFromDb.PublicationStageDate;
            entity.PublicationStageSource = vehicleFromDb.PublicationStageSource;
            entity.SourceId   = vehicleFromDb.SourceId;
            entity.SourceName = vehicleFromDb.SourceName;
            changeRequestItemStagings.Add(new ChangeRequestItemStaging()
            {
                ChangeType      = changeType,
                EntityId        = entity.Id.ToString(),
                CreatedDateTime = DateTime.UtcNow,
                Entity          = typeof(Vehicle).Name,
                Payload         = base.Serializer.Serialize(entity),
                ExistingPayload = base.Serializer.Serialize(existingEntry)
            });

            changeContent = string.Format("{0} / {1} / {2} / {3} / {4}"
                                          , vehicleFromDb.BaseVehicle.YearId
                                          , vehicleFromDb.BaseVehicle.Make.Name
                                          , vehicleFromDb.BaseVehicle.Model.Name
                                          , vehicleFromDb.SubModel.Name
                                          , vehicleFromDb.Region.Name);

            // NOTE: change-request-comments-staging perfomed on base

            var changeRequestId = await base.SubmitUpdateChangeRequestAsync(entity, id, requestedBy, changeType, changeRequestItemStagings, comment, attachments, changeContent);

            vehicleFromDb.ChangeRequestId = changeRequestId;
            _vehicleRepositoryService.Update(vehicleFromDb);
            Repositories.SaveChanges();

            await _vehicleIndexingService.UpdateVehicleChangeRequestIdAsync(entity.Id, changeRequestId);

            return(changeRequestId);
        }