Esempio n. 1
0
        /// <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));
            }
        }
Esempio n. 2
0
        /// <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));
            }
        }