/// <summary> /// /// </summary> /// <param name="profiles"></param> /// <returns></returns> private List <Profile> _getProfileDetails(dynamic profiles) { List <Profile> lst = new List <Profile>(); foreach (var oProfile in profiles) { Profile o = new Profile(); o.idProfile = oProfile.IdProfile; o.name = oProfile.Name; o.description = oProfile.Description; o.active = oProfile.Active; o.tagName = oProfile.TagName; o.paid = oProfile.Paid; o.dimensions = new List <Dimension>(); // Get all profiles dimensions var lstProfileDimensions = this.profilesDimensionsRepository.GetProfileDimensionsByIdProfile(o.idProfile); if (lstProfileDimensions.Count > 0) { // If we find profiles dimensions, travel to get dimensions 1 per 1 foreach (var oProfileDimension in lstProfileDimensions) { var dimension = this.dimensionsRepository.GetDimension(oProfileDimension.IdDimension.Value); Dimension oDimension = new Dimension(); oDimension.idDimension = dimension.IdDimension; oDimension.idDimensionCategory = dimension.IdDimensionCategory.Value; oDimension.idDimensionType = dimension.IdDimensionType.Value; oDimension.nameDimension = dimension.Description; oDimension.tagDimension = dimension.TagName; DimensionTypeEnum dimensionTypeEnum = (DimensionTypeEnum)oDimension.idDimensionType; oDimension.nameDimensionType = dimensionTypeEnum.ToString(); oDimension.tagDimensionCategory = dimension.DimensionsCategories.TagName; if (dimension.IdDimensionType == (int)DimensionTypeEnum.SWITCH) { oDimension.originalValue = oProfileDimension.SwitchValue; } else { oDimension.originalValue = oProfileDimension.Value; } o.dimensions.Add(oDimension); } } lst.Add(o); } return(lst); }
/// <summary> /// Update dimension value, it will check dimension type for /// </summary> /// <param name="idProduct">ID Product</param> /// <param name="idClient">ID Client (external code)</param> /// <param name="idProfile">ID Profile</param> /// <param name="idDimension">ID Dimension (must to be consumible)</param> /// <param name="value">New value for dimension</param> /// <returns></returns> public ActionResponse UpdateValueDimensionAction(int idProduct, int idClient, int idProfile, int idDimension, decimal value) { try { // Check if product exists var oProduct = this.productsRepository.GetProduct(idProduct); if (oProduct == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe el producto", null)); } // Check if client (user) exists or it's linked to product var oUser = this.usersRepository.GetUserv2(idClient.ToString(), idProduct); if (oUser == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El usuario no existe o no está relacionado con el producto", null)); } if (!oUser.Active.Value) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "El usuario indicado no se encuentra activo en la plataforma", null)); } int idUser = oUser.IdUser; // ID USER FREEMIUM // Check if profile exists var oProfile = profilesRepository.GetProfile(idProfile); if (oProfile == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El perfil no existe en el sistema", null)); } // Check if product has profiles associated and idProfile is part of it var profiles = oProduct.Profiles; if (profiles.Count <= 0) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El producto no posee perfiles configurados", null)); } bool isValidProfile = false; foreach (var oProf in profiles) { if (oProf.IdProfile == idProfile) { isValidProfile = true; break; } } if (!isValidProfile) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "El perfil no tiene relación con el producto", null)); } // Check if dimension exists var oDimension = this.dimensionsRepository.GetDimension(idDimension); if (oDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe ninguna dimensión con el identificador proporcionado", null)); } // Check dimension is related to profile across dimensioncategories var dimensionCategories = oProduct.DimensionsCategories; if (dimensionCategories.Count <= 0) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existen categorías de dimensión asociadas al producto", null)); } bool okDimension = false; foreach (var oDimensionCategory in dimensionCategories) { // Will find dimension if (oDimensionCategory.IdDimensionCategory == oDimension.IdDimensionCategory) { okDimension = true; break; } } if (!okDimension) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "La dimensión no está relacionada a ninguna categoría", null)); } // Update value dimension // If dimension is consumible will update it if (oDimension.IdDimensionType == (int)DimensionTypeEnum.CONSUMIBLE) { // Find subscription var subscription = subscriptionsRepository.GetUserCurrentSubscription(idUser); if (subscription == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar la subscripción del usuario", null)); } // Update value var userDimension = usersDimensionsRepository.GetUserDimension(idDimension, subscription.IdSubscription); if (userDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar la dimensión del usuario", null)); } if (userDimension.CurrentValue == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar el valor de la dimensión del usuario", null)); } decimal oldValueX = userDimension.CurrentValue.Value; bool action = usersDimensionsRepository.UpdateUserDimensionValue(userDimension.IdUserDimension, value); DimensionTypeEnum dimensionTypeEnum = (DimensionTypeEnum)oDimension.IdDimensionType.Value; UpdateValueDimensionResponse response = new UpdateValueDimensionResponse { idDimension = idDimension, nameDimension = oDimension.Description, tagName = oDimension.TagName, idDimensionType = oDimension.IdDimensionType.Value, nameDimensionType = dimensionTypeEnum.ToString(), oldValue = oldValueX, currentValue = value }; return(utilities.Response((int)CodeStatusEnum.OK, "OK", response)); } else { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "La dimensión no es del tipo consumible", null)); } } catch (Exception e) { return(utilities.Response((int)CodeStatusEnum.INTERNAL_ERROR, "Error desconocido en el sistema: " + e.Message, null)); } }
/// <summary> /// Get dimension's attributes. Service will check kind of dimension, then it will find its data associated /// for example: if dimension is consumible, need user ID to get subscription and check UserDimension /// </summary> /// <param name="idProduct">ID Product</param> /// <param name="idClient">ID Client (ID user master)</param> /// <param name="idProfile">ID profile</param> /// <param name="idDimension">ID dimension</param> /// <returns>ActionResponse object (200 OK - <> 200 NOK)</returns> public ActionResponse GetDimensionAction(int idProduct, int idClient, int idProfile, int idDimension) { try { // Check if product exists var oProduct = this.productsRepository.GetProduct(idProduct); if (oProduct == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe el producto", null)); } // Check if client (user) exists or it's linked to product var oUser = this.usersRepository.GetUserv2(idClient.ToString(), idProduct); if (oUser == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El usuario no existe o no está relacionado con el producto", null)); } if (!oUser.Active.Value) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "El usuario indicado no se encuentra activo en la plataforma", null)); } int idUser = oUser.IdUser; // ID USER FREEMIUM // Looking for dimension var oDimension = this.dimensionsRepository.GetDimension(idDimension); if (oDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe ninguna dimensión con el identificador proporcionado", null)); } // Finding dimensioncategories (needed for get dimension) var dimensionCategories = oProduct.DimensionsCategories; if (dimensionCategories.Count <= 0) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existen categorías de dimensión asociadas al producto", null)); } bool okDimension = false; foreach (var oDimensionCategory in dimensionCategories) { // Will find dimension if (oDimensionCategory.IdDimensionCategory == oDimension.IdDimensionCategory) { okDimension = true; break; } } if (!okDimension) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "La dimensión no está relacionada a ninguna categoría", null)); } // Finding profile // Check if profile provided is part of product sent var profiles = oProduct.Profiles; if (profiles.Count <= 0) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El producto no posee perfiles configurados", null)); } foreach (var o in profiles) { if (o.IdProfile == idProfile) { string msgProfileNonActive = "El perfil no se encuentra activo en el sistema"; if (o.Active == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, msgProfileNonActive, null)); } else { if (!o.Active.Value) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, msgProfileNonActive, null)); } } // Profile is found, will be used it in function to dimension type // Check dimension type: // If dimension is numeric (static) or switch, only we need its value // If dimension is consumible, we need to get more data from userdimension entity Object value = null; Object originalValue = null; // ProfileDimensions var profileDimension = this.profilesDimensionsRepository.GetProfileDimensionPD(idProfile, idDimension); if (profileDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar la relación del perfil con la dimensión", null)); } switch (oDimension.IdDimensionType.Value) { case (int)DimensionTypeEnum.NUMERIC: value = profileDimension.Value; // decimal break; case (int)DimensionTypeEnum.SWITCH: value = profileDimension.SwitchValue; // bool break; case (int)DimensionTypeEnum.CONSUMIBLE: // Subscription var oSubscription = this.subscriptionsRepository.GetUserCurrentSubscription(idUser); if (oSubscription == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar la suscripción del cliente", null)); } // UserDimension var oUserDimension = this.usersDimensionsRepository.GetUserDimension(idDimension, oSubscription.IdSubscription); if (oUserDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se pudo determinar el valor actual de la dimensión/perfil", null)); } value = oUserDimension.CurrentValue; originalValue = profileDimension.Value; break; default: return(utilities.Response((int)CodeStatusEnum.CONFLICT, "No se pudo determinar el tipo de dimensión", null)); } GetDimensionResponse response = new GetDimensionResponse(); response.nameDimension = oDimension.Description; response.tagName = oDimension.TagName; response.idDimensionType = oDimension.IdDimensionType.Value; DimensionTypeEnum dimensionTypeEnum = (DimensionTypeEnum)oDimension.IdDimensionType.Value; response.nameDimensionType = dimensionTypeEnum.ToString(); response.currentValue = value; response.originalValue = originalValue; return(utilities.Response((int)CodeStatusEnum.OK, "OK", response)); } } // At this point, profile doesn't exist, then need to stop action return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe ningún perfil asociado con el producto", null)); } catch (Exception e) { return(utilities.Response((int)CodeStatusEnum.INTERNAL_ERROR, "Error desconocido en el sistema: " + e.Message, null)); } }
/// <summary> /// Get all data related to user (profiles + dimensions) /// </summary> /// <param name="idProduct"></param> /// <param name="idClient"></param> /// <returns></returns> public ActionResponse GetDataUserAction(int idProduct, int idClient) { try { // Check for product var oProduct = productsRepository.GetProduct(idProduct); if (oProduct == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe el producto", null)); } // Get user (will check against idUserExternal) var oUser = this.usersRepository.GetUserv2(idClient.ToString(), idProduct); if (oUser == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "El usuario no existe o no está relacionado con el producto", null)); } if (!oUser.Active.Value) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "El usuario indicado no se encuentra activo en la plataforma", null)); } int idUser = oUser.IdUser; // ID USER FREEMIUM // Get subscription data var subscription = subscriptionsRepository.GetUserCurrentSubscription(idUser); if (subscription == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se ha podido determinar la suscripción del usuario", null)); } // Profile about subscription var profile = profilesRepository.GetProfile(subscription.IdProfile.Value); if (profile == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se ha podido identificar el perfil asociado al usuario", null)); } int idProfile = profile.IdProfile; // Get dimensions by profile var dimensions = dimensionsRepository.GetProfileDimensions(idProfile); // Travel dimensions, if type is consumible, will get user dimensions vinculated List <Dimension> newDimensions = new List <Dimension>(); if (dimensions.Count > 0) { foreach (var item in dimensions) // item = dimension object (from repository) { Dimension dimensionOut = new Dimension() { idDimension = (int)item.IdDimension, idDimensionType = item.IdDimensionType.Value, idDimensionCategory = item.DimensionsCategories.IdDimensionCategory, nameDimension = item.Description, tagDimension = item.TagName, tagDimensionCategory = item.DimensionsCategories.TagName }; // ProfileDimension associated var profileDimension = profilesDimensionsRepository.GetProfileDimensionPD(idProfile, dimensionOut.idDimension); if (profileDimension == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se se pudo obtener la relación del perfil con la dimensión", null)); } switch ((int)item.IdDimensionType) { case (int)DimensionTypeEnum.NUMERIC: case (int)DimensionTypeEnum.SWITCH: if ((int)item.IdDimensionType == (int)DimensionTypeEnum.NUMERIC) { if (profileDimension.Value == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "No se ha detectado ningún valor para el atributo 'Value' en Dimensión " + dimensionOut.idDimension + ", por favor corregir", null)); } dimensionOut.currentValue = profileDimension.Value.Value; } else { if (profileDimension.SwitchValue == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "No se ha detectado ningún valor para el atributo 'SwitchValue' en Dimensión " + dimensionOut.idDimension + ", por favor corregir", null)); } dimensionOut.currentValue = profileDimension.SwitchValue.Value; } break; case (int)DimensionTypeEnum.CONSUMIBLE: // Get UserDimension var userDimension = usersDimensionsRepository.GetUserDimension(item.IdDimension, subscription.IdSubscription); bool valSetWithNewUD = false; if (userDimension == null) { // If user dimension is not set, we will create it in order to normalize user configuration var newDimension = usersDimensionsRepository.NewUserDimension(item.IdDimension, subscription.IdSubscription); if (newDimension.CurrentValue == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "Imposible determinar 'CurrentValue' para Dimensión " + item.IdDimension + " y Suscripción " + subscription.IdSubscription + ", por favor corregir", null)); } dimensionOut.currentValue = newDimension.CurrentValue.Value; valSetWithNewUD = true; //return utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No se se pudo obtener la relación de la dimensión con el usuario", null); } if (!valSetWithNewUD) { if (userDimension.CurrentValue == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "Imposible determinar 'CurrentValue' para Dimensión " + item.IdDimension + " y Suscripción " + subscription.IdSubscription + ", por favor corregir", null)); } dimensionOut.currentValue = userDimension.CurrentValue.Value; } if (profileDimension.Value == null) { return(utilities.Response((int)CodeStatusEnum.CONFLICT, "No se ha detectado ningún valor para el atributo 'Value' en Dimensión " + dimensionOut.idDimension + ", por favor corregir", null)); } dimensionOut.originalValue = profileDimension.Value.Value; break; } DimensionTypeEnum dimensionTypeEnum = (DimensionTypeEnum)dimensionOut.idDimensionType; dimensionOut.nameDimensionType = dimensionTypeEnum.ToString(); // Add object newDimensions.Add(dimensionOut); } } // Response object GetDataUserResponse response = new GetDataUserResponse { idSubscription = subscription.IdSubscription, dateCreated = subscription.DateCreated.Value, profile = new Profile { idProfile = profile.IdProfile, name = profile.Name, description = profile.Description, tagName = profile.TagName, active = profile.Active, paid = profile.Paid }, dimensions = newDimensions }; return(utilities.Response((int)CodeStatusEnum.OK, "OK", response)); } catch (Exception e) { return(utilities.Response((int)CodeStatusEnum.INTERNAL_ERROR, "Error desconocido en el sistema: " + e.Message, null)); } }