public async Task <IActionResult> Create([Bind("Id,UserId,ActionId,PartnerId,ActionValue")] UsedActionModel usedAction)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    usedAction = _factory.Create(usedAction);
                    return(RedirectToAction(nameof(Index)));
                }
                catch (InvalidOperationException e)
                {
                    // log error here

                    var allowedErrors = new List <string>()
                    {
                        ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_UserDoesNotExist,
                        ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_PartnerDoesNotExist,
                        ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_MapAlreadyExists,
                        ServicesConstants.CreateUsedAction_NotAllowedReasonMessage_ActionDoesNotExist,
                    };

                    if (allowedErrors.Contains(e.Message ?? ""))
                    {
                        return(View("CustomError", new CustomErrorViewModel()
                        {
                            HeaderMessage = "Not allowed",
                            Message = e.Message,
                            ReturnUrls = new Dictionary <string, string>()
                            {
                                { "Back to Create Used Action Record page", Url.Action("Create", "UsedActions", new { area = "Admin" }) },
                                { "Go to Used Action Record List", Url.Action("Index", "UsedActions", new { area = "Admin" }) },
                            }
                        }));
                    }
                    else
                    {
                        return(RedirectToAction(nameof(Index)));
                    }
                }
                catch (Exception e)
                {
                    // log error here

                    return(RedirectToAction(nameof(Index)));
                }
            }
            else
            {
                ViewData["ActionId"]  = new SelectList(_actionFactory.GetAll(), "Id", "Name", usedAction.ActionId);
                ViewData["PartnerId"] = new SelectList(_partnerFactory.GetAll(), "Id", "Name", usedAction.PartnerId);
                ViewData["UserId"]    = new SelectList(_userFactory.GetAllUsers(), "Id", "UserName", usedAction.UserId);
                return(View(usedAction));
            }
        }
示例#2
0
        public IActionResult SubmitActionUse([Bind] LogActionUseModel model)
        {
            int partnerId = -1;
            var user      = _userFactory.GetUser(User.Identity.Name);

            if (User.IsInRole(WebConstants.PartnerRole) || User.IsInRole(WebConstants.AdminRole))
            {
                if (user.PartnerId == null)
                {
                    throw new Exception("User with Partner role must have a partner assigned.");
                }

                partnerId = user.PartnerId.Value;
            }
            else
            {
                throw new Exception("User must be in Partner role to Access this page");
            }

            try
            {
                int userId   = -1;
                int actionId = -1;
                if (model.UserCode != null)
                {
                    var nums = model.UserCode.Split("-");
                    if (nums.Count() == 4)
                    {
                        int.TryParse(nums[2], out userId);
                        int.TryParse(nums[3], out actionId);
                    }
                }

                if (userId == -1 || actionId == -1)
                {
                    userId   = model.UserId ?? -1;
                    actionId = model.ActionId ?? -1;
                }

                if (userId == -1 || actionId == -1)
                {
                    throw new Exception("Invalid code (Step 1a) or User and Action not chosen (Step 1b)");
                }

                if (model.OriginalValue <= 0)
                {
                    throw new Exception("Bad Purchase Amount value (Step 2)");
                }

                var passedUser = _userFactory.GetUser(userId);
                if (passedUser == null)
                {
                    throw new Exception("No such User");
                }
                if (!passedUser.Roles.Contains(WebConstants.UserRole))
                {
                    throw new Exception("The User you are trying to bind the Discount to isn't a regular User or the User Code is invalid");
                }

                var action = _partnerActionMapFactory.GetActionsForPartnerActionsView(partnerId, user.Id).Where(x => x.Id == actionId).FirstOrDefault();
                if (action == null)
                {
                    throw new Exception("No such Discount Action");
                }

                if (action.CashValue == null && action.PercentValue == null)
                {
                    throw new Exception("The Discount Action you are trying to use is badly defined. It may not be used until fixed. Both Percent and Cash value have no value set");
                }

                string percentValueString = "none";
                string cashValueString    = "none";

                decimal value;
                if (action.PercentValue != null)
                {
                    value = action.PercentValue.Value * model.OriginalValue;
                    percentValueString = "%" + action.PercentValue.Value.ToString();

                    // if both CashValue and PercentValue are defined in the DiscountAction
                    // the CashValue acts as the upper limit
                    if (action.CashValue != null)
                    {
                        cashValueString = "$" + action.CashValue.Value.ToString();
                        if (action.CashValue.Value < value)
                        {
                            value = action.CashValue.Value;
                        }
                    }
                }
                else
                {
                    value           = action.CashValue.Value;
                    cashValueString = "$" + action.CashValue.Value.ToString();
                }

                var res = _usedActionFactory.Create(new Services.Models.UsedActionModel()
                {
                    ActionId      = actionId,
                    ActionValue   = value,
                    DateCreated   = DateTime.UtcNow,
                    OriginalValue = model.OriginalValue,
                    PartnerId     = partnerId,
                    UserId        = userId
                });

                TempData["Message"] = $"Successfully added action [{res.ActionName}] to user [{res.UserName}]. Action value = [${res.ActionValue}]. Action value based on the Purchase Amount = [${model.OriginalValue}] and actions Percent Value = [{percentValueString}] and Cash Value = [{cashValueString}].";

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                TempData.Put("LogActionUseModel", model);
                TempData["Errors"] = e.Message;

                return(RedirectToAction(nameof(LogActionUse)));
            }
        }