private bool HasNoDataToUpdate(UpdateAssetEvent asset) { return(asset.AssetName == MasterDataConstants.InvalidStringValue && asset.AssetType == MasterDataConstants.InvalidStringValue && asset.EquipmentVIN == MasterDataConstants.InvalidStringValue && asset.IconKey == MasterDataConstants.InvalidIntValue && asset.LegacyAssetID == MasterDataConstants.InvalidIntValue && asset.Model == MasterDataConstants.InvalidStringValue && asset.ModelYear == MasterDataConstants.InvalidIntValue && asset.OwningCustomerUID == Guid.Empty && asset.ObjectType == MasterDataConstants.InvalidStringValue && asset.Category == MasterDataConstants.InvalidStringValue && asset.ProjectStatus == MasterDataConstants.InvalidStringValue && asset.Source == MasterDataConstants.InvalidStringValue && asset.SortField == MasterDataConstants.InvalidStringValue && asset.UserEnteredRuntimeHours == MasterDataConstants.InvalidStringValue && asset.Classification == MasterDataConstants.InvalidStringValue && asset.PlanningGroup == MasterDataConstants.InvalidStringValue); }
public bool UpdateDevice(UpdateDeviceEvent device, DeviceStateEnum deviceState) { var currentUTC = DateTime.UtcNow; var devicePayload = new UpdateDevicePayload { ActionUTC = currentUTC, CellModemIMEI = device.CellModemIMEI, CellularFirmwarePartnumber = device.CellularFirmwarePartnumber, DataLinkType = device.DataLinkType, DeregisteredUTC = device.DeregisteredUTC, DevicePartNumber = device.DevicePartNumber, DeviceSerialNumber = device.DeviceSerialNumber, DeviceState = deviceState == DeviceStateEnum.None?null: deviceState.ToString(), DeviceType = device.DeviceType, DeviceUID = device.DeviceUID.Value, FirmwarePartNumber = device.FirmwarePartNumber, GatewayFirmwarePartNumber = device.GatewayFirmwarePartNumber, MainboardSoftwareVersion = device.MainboardSoftwareVersion, ModuleType = device.ModuleType, NetworkFirmwarePartnumber = device.NetworkFirmwarePartnumber, OwningCustomerUID = device.OwningCustomerUID, RadioFirmwarePartNumber = device.RadioFirmwarePartNumber, ReceivedUTC = device.ReceivedUTC, SatelliteFirmwarePartnumber = device.SatelliteFirmwarePartnumber }; var existingDeviceProp = GetExistingDeviceProperties(device.DeviceUID.Value); if (PLDeviceTypes.Contains(device.DeviceType.ToLower(), StringComparer.InvariantCultureIgnoreCase)) { existingDeviceProp.RadioFirmwarePartNumber = existingDeviceProp.FirmwarePartNumber; existingDeviceProp.FirmwarePartNumber = null; } var existingPersonalities = GetExistingDevicePersonalities(device.DeviceUID.Value); DbDeviceType deviceType = string.IsNullOrEmpty(devicePayload.DeviceType) ? deviceTypesCache.First(x => string.Equals(x.Key, existingDeviceProp.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value : deviceTypesCache.First(x => string.Equals(x.Key, devicePayload.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value; var devicePersonality = GetDevicePersonalities(devicePayload, existingPersonalities, deviceType); devicePayload = AppendExistingDeviceProperties(devicePayload, existingDeviceProp); Guid?currentOwningCustomerUID = GetExistingOwningCustomerUid(device.DeviceUID.Value); devicePayload.OwningCustomerUID = devicePayload.OwningCustomerUID ?? currentOwningCustomerUID; //DeviceSerialNumber will be persisited as NULL in masterdata and send it as Empty to Kafka devicePayload.DeviceSerialNumber = string.IsNullOrEmpty(devicePayload.DeviceSerialNumber) ? string.Empty : devicePayload.DeviceSerialNumber; bool equal = CheckExistingDevicePropertiesForUpdate(devicePayload, (DeviceStateEnum)Enum.Parse(typeof(DeviceStateEnum), devicePayload.DeviceState), currentOwningCustomerUID, existingDeviceProp); if (equal) { logger.LogInformation("No properties to update"); return(false); } else { var kafkaMessageList = new List <KafkaMessage>(); kafkaTopicNames.ForEach(topic => { var kafkaMessage = new KafkaMessage() { Key = device.DeviceUID.ToString(), Message = new { UpdateDeviceEvent = mapper.Map <UpdateDevicePayload, UpdateDeviceEvent>(devicePayload) }, Topic = topic }; kafkaMessageList.Add(kafkaMessage); }); kafkaTopicNamesV2.ForEach(topic => { var kafkaMessage = new KafkaMessage() { Key = device.DeviceUID.ToString(), Message = ToDevicePayloadV2(devicePayload, devicePersonality), Topic = topic }; kafkaMessageList.Add(kafkaMessage); }); List <Action> actions = new List <Action>(); var deviceModel = new DbDevice { CellModemIMEI = devicePayload.CellModemIMEI, CellularFirmwarePartnumber = devicePayload.CellularFirmwarePartnumber, DataLinkType = devicePayload.DataLinkType, DeregisteredUTC = devicePayload.DeregisteredUTC, DevicePartNumber = devicePayload.DevicePartNumber, SerialNumber = devicePayload.DeviceSerialNumber, fk_DeviceStatusID = deviceState.GetHashCode(), fk_DeviceTypeID = deviceType.DeviceTypeID, DeviceUID = devicePayload.DeviceUID, FirmwarePartNumber = devicePayload.RadioFirmwarePartNumber == null ? devicePayload.FirmwarePartNumber : devicePayload.RadioFirmwarePartNumber, GatewayFirmwarePartNumber = devicePayload.GatewayFirmwarePartNumber, MainboardSoftwareVersion = devicePayload.MainboardSoftwareVersion, ModuleType = devicePayload.ModuleType, NetworkFirmwarePartnumber = devicePayload.NetworkFirmwarePartnumber, SatelliteFirmwarePartnumber = devicePayload.SatelliteFirmwarePartnumber, UpdateUTC = currentUTC }; var asset = GetOwningCustomerUID(device.DeviceUID.Value); if (device.OwningCustomerUID.HasValue && asset != null && (asset.OwningCustomerUID == Guid.Empty || !device.OwningCustomerUID.Equals(asset.OwningCustomerUID))) { var updateAsset = new UpdateAssetEvent { AssetName = asset.AssetName, AssetType = asset.AssetType, EquipmentVIN = asset.EquipmentVIN, IconKey = asset.IconKey, Model = asset.Model, ModelYear = asset.ModelYear, LegacyAssetID = asset.LegacyAssetID, AssetUID = asset.AssetUID, OwningCustomerUID = device.OwningCustomerUID.Value, ActionUTC = DateTime.UtcNow, ReceivedUTC = DateTime.UtcNow }; actions.Add(() => assetServices.UpdateAsset(updateAsset)); } actions.Add(() => transactions.Upsert(deviceModel)); actions.Add(() => transactions.Upsert <DbDevicePersonality>(devicePersonality)); actions.Add(() => transactions.Publish(kafkaMessageList)); return(transactions.Execute(actions)); } }
public ActionResult UpdateAssetInfo([FromBody] UpdateAssetEvent assetEvent) { try { if (HasNoDataToUpdate(assetEvent)) { return(BadRequest("Update Request should have atleast one data to update")); } if (!Request.Headers.TryGetValue("X-JWT-Assertion", out StringValues headerValues)) { return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request")); } TPaaSJWT jwt; try { jwt = new TPaaSJWT(headerValues); } catch { jwt = null; } if (jwt != null) { _logger.LogInformation("Creating asset for user: {0}", jwt.UserUid); } else { return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request")); } _logger.LogDebug($"UpdateAsset - Calling Application Name: {jwt.ApplicationName}, UserType: {jwt.UserType}"); ClientModel.Asset assetDetails = _assetService.GetAsset(assetEvent.AssetUID.Value); if (assetDetails == null || assetDetails.StatusInd == 0) { return(BadRequest("Asset does not exist")); } bool isCGIntegrator = jwt.UserType == "APPLICATION" && jwt.ApplicationName == _configuration["CGIntegratorAppName"]; if (isCGIntegrator) { if (assetEvent.OwningCustomerUID == null || assetEvent.OwningCustomerUID == Guid.Empty) { if (!string.IsNullOrEmpty(assetDetails.OwningCustomerUID) && Guid.Parse(assetDetails.OwningCustomerUID) == kiewitAMPCustomerUID) { // get current AssetId from DB and update assetEvent.AssetName = assetDetails.AssetName; } } else if (assetEvent.OwningCustomerUID == kiewitAMPCustomerUID) { // get current AssetId from DB and update assetEvent.AssetName = assetDetails.AssetName; } } if (assetEvent.LegacyAssetID == 0) // retain it { assetEvent.LegacyAssetID = assetDetails.LegacyAssetID; } assetEvent.ReceivedUTC = DateTime.UtcNow; if (_assetService.UpdateAsset(assetEvent)) { return(Ok(assetEvent.AssetUID.ToString())); } return(BadRequest("Unable to save to db. Make sure request is not duplicated and all keys exist")); } catch (Exception ex) { _logger.LogError("Update Asset Exception: " + ex.Message); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public ActionResult CreateAsset([FromBody] CreateAssetEvent asset) { try { //var jwt = GetSampleJwt(); //use this for testing locally if (!Request.Headers.TryGetValue("X-JWT-Assertion", out StringValues headerValues)) { return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request")); } TPaaSJWT jwt; try { jwt = new TPaaSJWT(headerValues); } catch (Exception ex) { jwt = null; } if (jwt != null) { _logger.LogInformation("Creating asset for user: {0}", jwt.UserUid); } else { return(BadRequest("no jwt token")); } asset.ReceivedUTC = DateTime.UtcNow; _logger.LogDebug($"CreateAsset - Calling Application Name: {jwt.ApplicationName}, UserType: {jwt.UserType}"); bool isCGIntegrator = jwt.UserType == "APPLICATION" && jwt.ApplicationName == _configuration["CGIntegratorAppName"]; if (isCGIntegrator) { if (asset.AssetUID == null || asset.AssetUID.Equals(Guid.Empty) || !Guid.TryParse(asset.AssetUID.ToString(), out Guid g)) { return(BadRequest("AssetUID must be given for VisionLink integrator")); } if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = new Guid(); } } else if (!string.IsNullOrEmpty(jwt.ApplicationName)) { asset.AssetUID = Guid.NewGuid(); var customers = _assetService.GetCustomersForApplication(jwt.ApplicationName); if (customers?.Count > 0) { if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = customers.First(); } } else { return(BadRequest("Application does not have any customers mapped. Please contact your API administrator.")); } Guid?existingAssetGuid = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAssetGuid.HasValue) { return(StatusCode((int)HttpStatusCode.Conflict, (existingAssetGuid.Value.ToString()))); } } else { return(BadRequest("jwt application name is empty")); } Guid?existingAsset = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAsset.HasValue) { if (isCGIntegrator) { var updatePayload = new UpdateAssetEvent { AssetUID = existingAsset.Value, OwningCustomerUID = asset.OwningCustomerUID, LegacyAssetID = asset.LegacyAssetID, AssetName = asset.AssetName, Model = asset.Model, AssetType = asset.AssetType, IconKey = asset.IconKey, EquipmentVIN = asset.EquipmentVIN, ModelYear = asset.ModelYear, ActionUTC = DateTime.UtcNow, ObjectType = asset.ObjectType, Category = asset.Category, ProjectStatus = asset.ProjectStatus, SortField = asset.SortField, Source = asset.Source, UserEnteredRuntimeHours = asset.UserEnteredRuntimeHours, Classification = asset.Classification, PlanningGroup = asset.PlanningGroup }; var updateResult = UpdateAssetInfo(updatePayload); return(updateResult.GetType().Name == "OkObjectResult" ? StatusCode((int)HttpStatusCode.Conflict, (existingAsset.Value.ToString())) : updateResult); } return(Conflict(new { message = $"Asset already exists" })); } if (!_assetService.IsValidMakeCode(asset.MakeCode.ToUpper())) { return(BadRequest($"Asset make code '{asset.MakeCode.ToUpper()}' is not valid.")); } if (_assetService.CreateAsset(asset)) { return(Ok(asset.AssetUID.ToString())); } return(BadRequest("Unable to save to db. Make sure request is not duplicated and all keys exist")); } catch (Exception ex) { _logger.LogError("Create Asset Exception: " + ex.ToString()); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public void Update_Asset() { //Arrange for Create var upadteAssetObject = new UpdateAssetEvent() { AssetName = "TestAssetName", AssetType = "loader", AssetUID = Guid.NewGuid(), Model = "model", EquipmentVIN = "equipmentVIN", IconKey = 1, LegacyAssetID = 1, ActionUTC = DateTime.UtcNow, ModelYear = 2016, ReceivedUTC = DateTime.UtcNow, OwningCustomerUID = Guid.NewGuid(), ObjectType = "test", Category = "test", ProjectStatus = "test", SortField = "test", Source = "test", Classification = "test", PlanningGroup = "test", UserEnteredRuntimeHours = "1234" }; VSS.MasterData.WebAPI.ClientModel.Asset getAssetObject = new VSS.MasterData.WebAPI.ClientModel.Asset { AssetUID = upadteAssetObject.AssetUID.ToString(), OwningCustomerUID = upadteAssetObject.OwningCustomerUID.ToString(), AssetName = "TestAssetName", AssetType = "loader", SerialNumber = "TestSerialNumber", MakeCode = "TestMake", Model = "model", EquipmentVIN = "equipmentVIN", IconKey = 1, LegacyAssetID = 1, InsertUTC = DateTime.UtcNow, ModelYear = 2016, UpdateUTC = DateTime.UtcNow, ObjectType = "test", Category = "test", ProjectStatus = "test", SortField = "test", Source = "test", Classification = "test", PlanningGroup = "test", UserEnteredRuntimeHours = "1234", StatusInd = 1 }; _transaction.Get <VSS.MasterData.WebAPI.ClientModel.Asset>(Arg.Any <string>()).Returns(x => { return(new List <VSS.MasterData.WebAPI.ClientModel.Asset>() { getAssetObject }); }); _transaction.Execute(Arg.Any <List <Action> >()).Returns(x => { foreach (var action in x.Arg <List <Action> >()) { action(); } return(true); }); //Act Assert.True(_assetServices.UpdateAsset(upadteAssetObject)); }
public virtual bool UpdateAsset(UpdateAssetEvent asset) { try { //Db Object var _asset = GetAsset(asset.AssetUID.Value); if (_asset == null || _asset.AssetUID == null) { return(false); // Asset Not Found } if (_asset.OwningCustomerUID == null && asset.OwningCustomerUID == null) { asset.OwningCustomerUID = new Guid(); } else if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = new Guid(_asset.OwningCustomerUID); } DateTime actionUTC = DateTime.UtcNow; var assetUpdate = new AssetPayload { //Un-updatable columns: Added an ignored columns SerialNumber = _asset.SerialNumber, MakeCode = _asset.MakeCode, InsertUTC = _asset.InsertUTC, StatusInd = _asset.StatusInd == 1 ? true : false, UpdateUTC = actionUTC, //updatable columns AssetUID = asset.AssetUID.Value, OwningCustomerUID = asset.OwningCustomerUID, LegacyAssetID = asset.LegacyAssetID == null ? (long)_asset.LegacyAssetID : (long)asset.LegacyAssetID, ModelYear = asset.ModelYear == -9999999 ? null : (asset.ModelYear == null ? _asset.ModelYear : asset.ModelYear), IconKey = asset.IconKey == -9999999 ? null : (asset.IconKey == null ? _asset.IconKey : asset.IconKey), AssetName = asset.AssetName == null ? _asset.AssetName : (string.IsNullOrWhiteSpace(asset.AssetName) ? null : asset.AssetName), Model = asset.Model == null ? _asset.Model : (string.IsNullOrWhiteSpace(asset.Model) ? null : asset.Model), AssetTypeName = asset.AssetType == null ? _asset.AssetType : (string.IsNullOrWhiteSpace(asset.AssetType) ? null : asset.AssetType), EquipmentVIN = asset.EquipmentVIN == null ? _asset.EquipmentVIN : (string.IsNullOrWhiteSpace(asset.EquipmentVIN) ? null : asset.EquipmentVIN), ObjectType = asset.ObjectType == null ? _asset.ObjectType : (string.IsNullOrWhiteSpace(asset.ObjectType) ? null : asset.ObjectType), Category = asset.Category == null ? _asset.Category : (string.IsNullOrWhiteSpace(asset.Category) ? null : asset.Category), ProjectStatus = asset.ProjectStatus == null ? _asset.ProjectStatus : (string.IsNullOrWhiteSpace(asset.ProjectStatus) ? null : asset.ProjectStatus), SortField = asset.SortField == null ? _asset.SortField : (string.IsNullOrWhiteSpace(asset.SortField) ? null : asset.SortField), Source = asset.Source == null ? _asset.Source : (string.IsNullOrWhiteSpace(asset.Source) ? null : asset.Source), UserEnteredRuntimeHours = asset.UserEnteredRuntimeHours == null ? _asset.UserEnteredRuntimeHours : (string.IsNullOrWhiteSpace(asset.UserEnteredRuntimeHours) ? null : asset.UserEnteredRuntimeHours), Classification = asset.Classification == null ? _asset.Classification : (string.IsNullOrWhiteSpace(asset.Classification) ? null : asset.Classification), PlanningGroup = asset.PlanningGroup == null ? _asset.PlanningGroup : (string.IsNullOrWhiteSpace(asset.PlanningGroup) ? null : asset.PlanningGroup), }; var actions = new List <Action>() { () => _transaction.Upsert(assetUpdate), () => _transaction.Publish(GetGetAssetSourceSnapshotTopublish(assetUpdate, asset.ReceivedUTC.Value, true)) }; return(_transaction.Execute(actions)); } catch (Exception ex) { _logger.LogError("error while Upadting asset in db: ", ex); throw ex; } }