/// <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)); } }
/// <summary> /// Will set profile to user /// </summary> /// <param name="idProduct">Product ID</param> /// <param name="idClient">User master ID</param> /// <param name="idProfile">Profile ID</param> /// <param name="idGuide">Guide ID (optional), thought for traceability</param> /// <returns></returns> public ActionResponse SetProfileUserAction(int idProduct, int idClient, int idProfile, int idGuide) { try { // Check if products exists var oProduct = this.productsRepository.GetProduct(idProduct); if (oProduct == null) { return(utilities.Response((int)CodeStatusEnum.NO_CONTENT, "No existe el producto", null)); } // Check for user var oUser = this.usersRepository.GetUserv2(idClient.ToString(), idProduct); // If user doesn't exist, need to create everything from beggining // Will send 0 to process this action int idUser = (oUser == null) ? 0 : 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)); } // Set or update all related to profile bool action = subscriptionsRepository.SetProfileUser(idProfile, idUser, idClient, idProduct); if (action) { // OK SetProfileUserResponse response = new SetProfileUserResponse { profile = new Profile { idProfile = oProfile.IdProfile, name = oProfile.Name, description = oProfile.Description, tagName = oProfile.TagName, active = oProfile.Active, paid = oProfile.Paid } }; // After set user, need to call to user's service to update // Will get user data for getting dimension related to id_lista which will be setted SubscriptionsService subscriptionService = new SubscriptionsService(); ActionResponse subscriptionServiceAction = subscriptionService.GetDataUserAction(idProduct, idClient); if (subscriptionServiceAction.code == (int)CodeStatusEnum.OK) { GetDataUserResponse resp = (GetDataUserResponse)subscriptionServiceAction.data; int l = resp.dimensions.Count; if (l > 0) { // Check dimensions to get id list (if it applies) foreach (Dimension obj in resp.dimensions) { if (obj.tagDimension == System.Configuration.ConfigurationManager.AppSettings["TAG_DIMENSION"]) { DimensionsService dimensionService = new DimensionsService(); ActionResponse dimensionServiceAction = dimensionService.GetDimensionAction(idProduct, idClient, idProfile, obj.idDimension); if (dimensionServiceAction.code == (int)CodeStatusEnum.OK) { GetDimensionResponse respB = (GetDimensionResponse)dimensionServiceAction.data; int idList = Convert.ToInt32(respB.currentValue); // Call for external service var test = CallExternal(idClient, idList, idGuide); } break; } } } } return(utilities.Response((int)CodeStatusEnum.OK, "OK", response)); } else { // Check if model responded some error string msj = "No se pudo procesar la solicitud"; if (subscriptionsRepository.getLastError() != String.Empty) { msj = subscriptionsRepository.getLastError(); } return(utilities.Response((int)CodeStatusEnum.CONFLICT, msj, null)); } } catch (Exception e) { return(utilities.Response((int)CodeStatusEnum.INTERNAL_ERROR, "Error desconocido en el sistema: " + e.Message, null)); } }