Ejemplo n.º 1
0
        public async Task <IActionResult> Get()
        {
            // only system admins can access this functionality
            if (!await _securityHelper.IsSysAdmin())
            {
                return(new HttpStatusCodeResult(403)); // Forbidden
            }
            List <GroupAdminSummary> rtnView = new List <GroupAdminSummary>();

            // get group managers
            List <ScampUser> mgrList = await _settingsRepository.GetGroupAdmins();

            // build the return set
            foreach (ScampUser usr in mgrList)
            {
                // get groups user controls budget of
                var groupList = await _groupRepository.GetGroupsByBudgetOwner(usr.Id);

                // calculate total amount of user's budget that has been used across all groups
                long totUsed = 0; // initial total used of users allocated budget
                foreach (var group in groupList)
                {
                    // get group's budget state
                    var groupBudget = await _volatileStorageController.GetGroupBudgetState(group.Id);

                    if (groupBudget != null) // if we found budget state
                    {
                        totUsed += groupBudget.UnitsUsed;
                    }
                }

                // build summary view object
                GroupAdminSummary newAdmin = new GroupAdminSummary()
                {
                    Id                = usr.Id,
                    Name              = usr.Name,
                    unitsBudgeted     = usr.budget.unitsBudgeted,
                    endDate           = usr.budget.EndDate,
                    totUnitsUsed      = totUsed,
                    totUnitsAllocated = usr.budget.Allocated,
                    totGroups         = groupList.Count
                };
                rtnView.Add(newAdmin);  // add it to the collection
            }

            // return list
            return(new ObjectResult(rtnView)
            {
                StatusCode = 200
            });
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get(string userId, string view)
        {
            //TODO: authorization check

            // get requested user document
            ScampUser userDoc = await _userRepository.GetUserById(userId);

            if (userDoc == null)
            {
                return(HttpNotFound());
            }

            if (view == "summary")
            {
                UserBudgetSummary tmpBudgetSummary = new UserBudgetSummary();

                foreach (var group in userDoc.GroupMembership)
                {
                    if (group.isManager)
                    {
                        // get this group's current budget
                        var groupBudget = await _volatileStorageController.GetGroupBudgetState(group.Id);

                        tmpBudgetSummary.totGroups++;
                        tmpBudgetSummary.unitsBudgeted     += groupBudget.UnitsBudgetted;
                        tmpBudgetSummary.totUnitsUsed      += groupBudget.UnitsUsed;
                        tmpBudgetSummary.totUnitsAllocated += groupBudget.UnitsAllocated;
                    }
                }
                ;

                // return view
                return(new ObjectResult(tmpBudgetSummary)
                {
                    StatusCode = 200
                });
            }
            else
            {
                return(new ObjectResult(string.Format("view '{0}' not supported", view))
                {
                    StatusCode = 400
                });
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Get(string userId, string view)
        {
            // get requested user document
            ScampUser userDoc = await _userRepository.GetUserById(userId);

            if (userDoc == null)
            {
                return(HttpNotFound());
            }

            //TODO: authorization check

            // build return view
            if (view == "admin") // do admin view
            {
                List <ScampAdminGroupReference> rtnView = new List <ScampAdminGroupReference>();

                // build return view
                foreach (ScampUserGroupMbrship group in userDoc.GroupMembership)
                {
                    if (!(await _securityHelper.CurrentUserCanManageGroup(group.Id)))
                    {
                        continue;
                    }

                    // get group budget
                    var groupBudget = await _volatileStorageController.GetGroupBudgetState(group.Id);

                    // build return list item
                    ScampAdminGroupReference tmpGroupRef = new ScampAdminGroupReference()
                    {
                        Id   = group.Id,
                        Name = group.Name,
                        // be sure to handle user without a budget values
                        totUnitsUsed      = (groupBudget == null ? 0 : groupBudget.UnitsUsed),
                        totUnitsAllocated = (groupBudget == null ? 0 : groupBudget.UnitsAllocated),
                        totUnitsBudgeted  = (groupBudget == null ? 0 : groupBudget.UnitsBudgetted)
                    };
                    // add item to list
                    rtnView.Add(tmpGroupRef);
                }

                // return results
                return(new ObjectResult(rtnView)
                {
                    StatusCode = 200
                });
            }
            else if (view == "user") // do user view
            {
                List <ScampUserGroupReference> rtnView = new List <ScampUserGroupReference>();

                // get user group budgets
                var groupBudgets = await _volatileStorageController.GetUserBudgetStates(userId);

                foreach (ScampUserGroupMbrship group in userDoc.GroupMembership)
                {
                    // get group
                    var groupBudget = groupBudgets.First(g => g.groupId == group.Id);

                    // build return object
                    ScampUserGroupReference tmpGroupRef = new ScampUserGroupReference()
                    {
                        Id   = group.Id,
                        Name = group.Name,
                        // be sure to handle user without a budget values
                        totUnitsUsedByUser       = (groupBudget == null ? 0 : groupBudget.UnitsUsed),
                        totUnitsRemainingForUser = (groupBudget == null ? 0 : (groupBudget.UnitsBudgetted - groupBudget.UnitsUsed))
                    };
                    rtnView.Add(tmpGroupRef);
                }

                // return document
                return(new ObjectResult(rtnView)
                {
                    StatusCode = 200
                });
            }
            else
            {
                //TODO: invalid argument "view"
            }

            return(new ObjectResult(null)
            {
                StatusCode = 200
            });
        }