Exemplo n.º 1
0
		public EntityQuery(string entityName, string fields = "*", QueryObject query = null, 
			QuerySortObject[] sort = null, int? skip = null, int? limit = null )
		{
			if (string.IsNullOrWhiteSpace(entityName))
				throw new ArgumentException("Invalid entity name.");

			if (string.IsNullOrWhiteSpace(fields))
                fields = "*";

            EntityName = entityName;
			Fields = fields;
			Query = query;
			Sort = sort;
			Skip = skip;
			Limit = limit;
		}
Exemplo n.º 2
0
        public IActionResult MyMilestones()
        {
            var response = new ResponseModel();
            var resultProjectIdList = new List<Guid>();
            try
            {
                #region << Get Project Ids >>

                #region << Can user read projects >>
                //Get current user
                ErpUser user = SecurityContext.CurrentUser;
                //Get entity meta
                var entity = entMan.ReadEntity("wv_project").Object;
                //check if user role has permissions
                var canRead = user.Roles.Any(x => entity.RecordPermissions.CanRead.Any(z => z == x.Id));
                var canCreate = user.Roles.Any(x => entity.RecordPermissions.CanCreate.Any(z => z == x.Id));
                var canUpdate = user.Roles.Any(x => entity.RecordPermissions.CanUpdate.Any(z => z == x.Id));
                var canDelete = user.Roles.Any(x => entity.RecordPermissions.CanDelete.Any(z => z == x.Id));

                if (!canRead)
                {
                    response.Success = false;
                    response.Message = "You do not have permission to read the projects in this system";
                    response.Timestamp = DateTime.UtcNow;
                    return Json(response); //return empty object
                }
                var milestone = entMan.ReadEntity("wv_milestone").Object;
                //check if user role has permissions
                var canReadMilestone = user.Roles.Any(x => milestone.RecordPermissions.CanRead.Any(z => z == x.Id));
                var canCreateMilestone = user.Roles.Any(x => milestone.RecordPermissions.CanCreate.Any(z => z == x.Id));
                var canUpdateMilestone = user.Roles.Any(x => milestone.RecordPermissions.CanUpdate.Any(z => z == x.Id));
                var canDeleteMilestone = user.Roles.Any(x => milestone.RecordPermissions.CanDelete.Any(z => z == x.Id));

                if (!canReadMilestone)
                {
                    response.Success = false;
                    response.Message = "You do not have permission to read the milestones in this system";
                    response.Timestamp = DateTime.UtcNow;
                    return Json(response); //return empty object
                }

                #endregion

                var requestedFields = "id," +
                "$user_1_n_project_owner.id," +
                "$role_n_n_project_team.id,$role_n_n_project_customer.id";
                #region << Query builder >>
                //QueryObject filterObj = EntityQuery.QueryEQ("id", recordId);
                QueryObject filterObj = null;
                EntityQuery resultQuery = new EntityQuery("wv_project", requestedFields, filterObj, null, null, null, null);
                #endregion

                #region << Execute >>
                QueryResponse result = recMan.Find(resultQuery);
                if (!result.Success)
                {
                    response.Success = false;
                    response.Timestamp = DateTime.UtcNow;
                    response.Message = result.Message;
                    response.Object = null;
                    return Json(response);
                }
                foreach (var record in result.Object.Data)
                {
                    //Check if user can view the object
                    var userIsPM = false;
                    var userIsStaff = false;
                    var userIsCustomer = false;
                    #region << Check user roles >>
                    foreach (var userRole in user.Roles)
                    {
                        if (!userIsPM)
                        {
                            userIsPM = ((List<EntityRecord>)record["$user_1_n_project_owner"]).Any(z => (Guid)z["id"] == user.Id);
                        }
                        if (!userIsStaff)
                        {
                            userIsStaff = ((List<EntityRecord>)record["$role_n_n_project_team"]).Any(z => (Guid)z["id"] == userRole.Id);
                        }
                        if (!userIsCustomer)
                        {
                            userIsCustomer = ((List<EntityRecord>)record["$role_n_n_project_customer"]).Any(z => (Guid)z["id"] == userRole.Id);
                        }
                    }
                    #endregion

                    if (userIsPM || userIsStaff || userIsCustomer)
                    {
                        resultProjectIdList.Add((Guid)record["id"]);
                    }
                }
                #endregion
                #endregion

                if (resultProjectIdList.Count == 0)
                {
                    response.Success = true;
                    response.Timestamp = DateTime.UtcNow;
                    response.Message = "You do not have access to any project or there are no projects yet";
                    response.Object = null;
                    return Json(response);
                }

                #region << Get Milestones >>
                var milestoneFields = "*";

                QueryObject projectIdFilterSection = null;
                #region << project id filters >>
                var projectIdRulesList = new List<QueryObject>();
                foreach (var projectId in resultProjectIdList)
                {
                    var projectIdRule = EntityQuery.QueryEQ("project_id", projectId);
                    projectIdRulesList.Add(projectIdRule);
                }
                projectIdFilterSection = EntityQuery.QueryOR(projectIdRulesList.ToArray());
                #endregion

                var sortRulesList = new List<QuerySortObject>();
                var defaultSortRule = new QuerySortObject("name", QuerySortType.Ascending);
                sortRulesList.Add(defaultSortRule);

                var milestoneQuery = new EntityQuery("wv_milestone", milestoneFields, projectIdFilterSection, sortRulesList.ToArray(), null, null, null);
                var milestoneQueryResponse = recMan.Find(milestoneQuery);
                if (!milestoneQueryResponse.Success)
                {
                    response.Success = false;
                    response.Timestamp = DateTime.UtcNow;
                    response.Message = milestoneQueryResponse.Message;
                    response.Object = null;
                    return Json(response);
                }

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "My milestones successfully read";
                response.Object = milestoneQueryResponse.Object.Data;

                return Json(response);

                #endregion

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Error: " + ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 3
0
        public IActionResult LastUpdatedTasksUserOwns(int page = 1)
        {
            var response = new ResponseModel();
            try
            {
                //var queryString = HttpContext.Request.QueryString;
                #region << Can user read activities >>
                //Get current user
                ErpUser user = SecurityContext.CurrentUser;
                //Get entity meta
                var entity = entMan.ReadEntity("wv_task").Object;
                //check if user role has permissions
                var canRead = user.Roles.Any(x => entity.RecordPermissions.CanRead.Any(z => z == x.Id));
                var canCreate = user.Roles.Any(x => entity.RecordPermissions.CanCreate.Any(z => z == x.Id));
                var canUpdate = user.Roles.Any(x => entity.RecordPermissions.CanUpdate.Any(z => z == x.Id));
                var canDelete = user.Roles.Any(x => entity.RecordPermissions.CanDelete.Any(z => z == x.Id));

                if (!canRead)
                {
                    response.Success = false;
                    response.Message = "You do not have permission to read the tasks in this system";
                    response.Timestamp = DateTime.UtcNow;
                    return Json(response); //return empty object
                }
                #endregion

                var taskQueryResponse = new QueryResponse();

                #region << Get tasks >>
                {
                    var fields = "id,number,subject,priority,last_modified_on,$user_wv_task_modified_by.username";

                    //Add default sort by created_on
                    var sortRulesList = new List<QuerySortObject>();
                    var defaultSortRule = new QuerySortObject("last_modified_on", QuerySortType.Descending);
                    sortRulesList.Add(defaultSortRule);

                    #endregion
                    var ownerFilter = EntityQuery.QueryEQ("owner_id", SecurityContext.CurrentUser.Id);
                    var notClosedFilter = EntityQuery.QueryNOT("status", "completed");

                    var rootFilterSection = EntityQuery.QueryAND(ownerFilter, notClosedFilter);

                    //Calculate page
                    var pageSize = 5;
                    var skipRecords = (page - 1) * pageSize;

                    var activityQuery = new EntityQuery("wv_task", fields, rootFilterSection, sortRulesList.ToArray(), skipRecords, pageSize, null);

                    taskQueryResponse = recMan.Find(activityQuery);
                    if (!taskQueryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = taskQueryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                }

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Successful read";
                response.Object = taskQueryResponse.Object.Data;

                return Json(response);

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 4
0
        public IActionResult GetSprintTasksList(Guid sprintId, string status = "not started", string scope = "user", int page = 1, int pageSize = 1)
        {
            var response = new ResponseModel();
            var taskList = new List<EntityRecord>();
            var processedTaskList = new List<EntityRecord>();
            var userDictionary = new Dictionary<Guid, EntityRecord>();
            var skipPages = (page - 1) * pageSize;
            try
            {

                #region << Get all users >>
                {
                    var query = new EntityQuery("user");
                    var queryResponse = recMan.Find(query);
                    if (!queryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "Error: " + queryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    foreach (var user in queryResponse.Object.Data)
                    {
                        userDictionary[(Guid)user["id"]] = user;
                    }
                }

                #endregion

                #region << Get tasks >>
                {
                    QueryObject queryFilter = null;
                    var queryRulesList = new List<QueryObject>();
                    //Only not completed tasks
                    queryRulesList.Add(EntityQuery.QueryNOT("status", "completed"));
                    if (scope == "user")
                    {
                        queryRulesList.Add(EntityQuery.QueryEQ("owner_id", SecurityContext.CurrentUser.Id));
                    }
                    queryRulesList.Add(EntityQuery.QueryEQ("status", status));

                    queryFilter = EntityQuery.QueryAND(queryRulesList.ToArray());

                    var sortRulesList = new List<QuerySortObject>();
                    var sortRule = new QuerySortObject("created_on", QuerySortType.Descending);
                    sortRulesList.Add(sortRule);
                    var queryFields = "id,code,owner_id, priority,status,subject," +
                        "x_billable_hours,x_nonbillable_hours,estimation";

                    var query = new EntityQuery("wv_task", queryFields, queryFilter, sortRulesList.ToArray(), skipPages, pageSize);
                    var queryResponse = recMan.Find(query);
                    if (!queryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "Error: " + queryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    if (queryResponse.Object.Data.Any())
                    {
                        taskList = queryResponse.Object.Data;
                    }
                    else
                    {
                        response.Success = true;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "No tasks found!";
                        response.Object = new List<EntityRecord>();
                        return Json(response);
                    }
                }
                #endregion

                #region << process Tasks >>
                foreach (var task in taskList)
                {
                    var proccessedTask = new EntityRecord();
                    proccessedTask["id"] = (Guid)task["id"];
                    proccessedTask["code"] = (string)task["code"];
                    proccessedTask["priority"] = (string)task["priority"];
                    proccessedTask["status"] = (string)task["status"];
                    proccessedTask["subject"] = (string)task["subject"];
                    proccessedTask["estimation"] = (decimal)task["estimation"];
                    proccessedTask["logged"] = (decimal)task["x_nonbillable_hours"] + (decimal)task["x_billable_hours"];
                    proccessedTask["owner_username"] = (string)userDictionary[(Guid)task["owner_id"]]["username"];
                    proccessedTask["owner_image"] = (string)userDictionary[(Guid)task["owner_id"]]["image"];
                    processedTaskList.Add(proccessedTask);
                }
                #endregion

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Tasks successfully read";
                response.Object = processedTaskList;

                return Json(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Error: " + ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 5
0
        public IActionResult GetSprintList(int page = 1, int pageSize = 1)
        {
            var response = new ResponseModel();
            var sprintList = new List<EntityRecord>();
            var skipPages = (page - 1) * pageSize;
            try
            {
                QueryObject queryFilter = null;
                var sortRulesList = new List<QuerySortObject>();
                var sortRule = new QuerySortObject("start_date", QuerySortType.Descending);
                sortRulesList.Add(sortRule);
                var query = new EntityQuery("wv_sprint", "*", queryFilter, sortRulesList.ToArray(), skipPages, pageSize);
                var queryResponse = recMan.Find(query);
                if (!queryResponse.Success)
                {
                    response.Success = false;
                    response.Timestamp = DateTime.UtcNow;
                    response.Message = "Error: " + queryResponse.Message;
                    response.Object = null;
                    return Json(response);
                }
                if (queryResponse.Object.Data.Any())
                {
                    sprintList = queryResponse.Object.Data;
                }
                else
                {
                    response.Success = true;
                    response.Timestamp = DateTime.UtcNow;
                    response.Message = "No sprints found!";
                    response.Object = null;
                    return Json(response);
                }

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Sprints successfully read";
                response.Object = sprintList;

                return Json(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Error: " + ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 6
0
        public IActionResult AllActivitiesUserCanSee(string label = "all", int page = 1)
        {
            var response = new ResponseModel();
            try
            {
                //var queryString = HttpContext.Request.QueryString;
                #region << Can user read activities >>
                //Get current user
                ErpUser user = SecurityContext.CurrentUser;
                //Get entity meta
                var entity = entMan.ReadEntity("wv_project_activity").Object;
                //check if user role has permissions
                var canRead = user.Roles.Any(x => entity.RecordPermissions.CanRead.Any(z => z == x.Id));
                var canCreate = user.Roles.Any(x => entity.RecordPermissions.CanCreate.Any(z => z == x.Id));
                var canUpdate = user.Roles.Any(x => entity.RecordPermissions.CanUpdate.Any(z => z == x.Id));
                var canDelete = user.Roles.Any(x => entity.RecordPermissions.CanDelete.Any(z => z == x.Id));

                if (!canRead)
                {
                    response.Success = false;
                    response.Message = "You do not have permission to read the activities in this system";
                    response.Timestamp = DateTime.UtcNow;
                    return Json(response); //return empty object
                }
                #endregion

                var activityQueryResponse = new QueryResponse();
                var userCanSeeProjectIds = new List<Guid>();
                #region << Generate list of projects user can see >>
                {
                    var requestedFields = "id,$user_1_n_project_owner.id,$role_n_n_project_team.id,$role_n_n_project_customer.id";
                    //QueryObject filterObj = EntityQuery.QueryEQ("id", recordId);
                    QueryObject filterObj = null;
                    EntityQuery resultQuery = new EntityQuery("wv_project", requestedFields, filterObj, null, null, null, null);
                    QueryResponse result = recMan.Find(resultQuery);
                    var resultRecordsList = new List<EntityRecord>();
                    if (!result.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = result.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    foreach (var record in result.Object.Data)
                    {
                        //Check if user can view the object
                        var userIsPM = false;
                        var userIsStaff = false;
                        var userIsCustomer = false;
                        #region << Check user roles >>
                        foreach (var userRole in user.Roles)
                        {
                            if (!userIsPM)
                            {
                                userIsPM = ((List<EntityRecord>)record["$user_1_n_project_owner"]).Any(z => (Guid)z["id"] == user.Id);
                            }
                            if (!userIsStaff)
                            {
                                userIsStaff = ((List<EntityRecord>)record["$role_n_n_project_team"]).Any(z => (Guid)z["id"] == userRole.Id);
                            }
                            if (!userIsCustomer)
                            {
                                userIsCustomer = ((List<EntityRecord>)record["$role_n_n_project_customer"]).Any(z => (Guid)z["id"] == userRole.Id);
                            }
                        }
                        #endregion

                        if (userIsPM || userIsStaff || userIsCustomer)
                        {
                            userCanSeeProjectIds.Add((Guid)record["id"]);
                        }
                    }
                }
                #endregion

                #region << Get activities >>
                {
                    var fields = "id,label,created_on,description,subject," +
                    "$user_wv_project_activity_created_by.username,$user_wv_project_activity_created_by.image," +
                    "$project_1_n_activity.name";

                    QueryObject rootFilterSection = null;
                    QueryObject auxFilterSection = null;
                    QueryObject projectIdFilterSection = null;

                    #region << project id filters >>
                    var projectIdRulesList = new List<QueryObject>();
                    foreach (var projectId in userCanSeeProjectIds)
                    {
                        var projectIdRule = EntityQuery.QueryEQ("project_id", projectId);
                        projectIdRulesList.Add(projectIdRule);
                    }
                    projectIdFilterSection = EntityQuery.QueryOR(projectIdRulesList.ToArray());
                    #endregion

                    #region << Aux filters & Sort>>
                    var auxRulesList = new List<QueryObject>();
                    QueryObject auxRule = new QueryObject();
                    if (label != "all")
                    {
                        auxRule = EntityQuery.QueryEQ("label", label);
                        auxRulesList.Add(auxRule);
                    }

                    auxFilterSection = EntityQuery.QueryAND(auxRulesList.ToArray());
                    //Add default sort by created_on
                    var sortRulesList = new List<QuerySortObject>();
                    var defaultSortRule = new QuerySortObject("created_on", QuerySortType.Descending);
                    sortRulesList.Add(defaultSortRule);

                    #endregion

                    rootFilterSection = EntityQuery.QueryAND(projectIdFilterSection, auxFilterSection);

                    //Calculate page
                    var pageSize = 10;
                    var skipRecords = (page - 1) * pageSize;

                    var activityQuery = new EntityQuery("wv_project_activity", fields, rootFilterSection, sortRulesList.ToArray(), skipRecords, pageSize, null);

                    activityQueryResponse = recMan.Find(activityQuery);
                    if (!activityQueryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = activityQueryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                }
                #endregion

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Successful read";
                response.Object = activityQueryResponse.Object.Data;

                return Json(response);

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 7
0
        public IActionResult AllTaskUserCanSee(string listName, int page = 0)
        {
            var response = new ResponseModel();
            try
            {
                //var queryString = HttpContext.Request.QueryString;
                #region << Can user read tasks >>
                //Get current user
                ErpUser user = SecurityContext.CurrentUser;
                //Get entity meta
                var entity = entMan.ReadEntity("wv_task").Object;
                //check if user role has permissions
                var canRead = user.Roles.Any(x => entity.RecordPermissions.CanRead.Any(z => z == x.Id));
                var canCreate = user.Roles.Any(x => entity.RecordPermissions.CanCreate.Any(z => z == x.Id));
                var canUpdate = user.Roles.Any(x => entity.RecordPermissions.CanUpdate.Any(z => z == x.Id));
                var canDelete = user.Roles.Any(x => entity.RecordPermissions.CanDelete.Any(z => z == x.Id));

                if (!canRead)
                {
                    response.Success = false;
                    response.Message = "You do not have permission to read the projects in this system";
                    response.Timestamp = DateTime.UtcNow;
                    return Json(response); //return empty object
                }
                #endregion
                var taskQueryResponse = new QueryResponse();
                var userCanSeeProjectIds = new List<Guid>();
                #region << Generate list of projects user can see >>
                {
                    var requestedFields = "id,$user_1_n_project_owner.id,$role_n_n_project_team.id,$role_n_n_project_customer.id";
                    //QueryObject filterObj = EntityQuery.QueryEQ("id", recordId);
                    QueryObject filterObj = null;
                    EntityQuery resultQuery = new EntityQuery("wv_project", requestedFields, filterObj, null, null, null, null);
                    QueryResponse result = recMan.Find(resultQuery);
                    var resultRecordsList = new List<EntityRecord>();
                    if (!result.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = result.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    foreach (var record in result.Object.Data)
                    {
                        //Check if user can view the object
                        var userIsPM = false;
                        var userIsStaff = false;
                        var userIsCustomer = false;
                        #region << Check user roles >>
                        foreach (var userRole in user.Roles)
                        {
                            if (!userIsPM)
                            {
                                userIsPM = ((List<EntityRecord>)record["$user_1_n_project_owner"]).Any(z => (Guid)z["id"] == user.Id);
                            }
                            if (!userIsStaff)
                            {
                                userIsStaff = ((List<EntityRecord>)record["$role_n_n_project_team"]).Any(z => (Guid)z["id"] == userRole.Id);
                            }
                            if (!userIsCustomer)
                            {
                                userIsCustomer = ((List<EntityRecord>)record["$role_n_n_project_customer"]).Any(z => (Guid)z["id"] == userRole.Id);
                            }
                        }
                        #endregion

                        if (userIsPM || userIsStaff || userIsCustomer)
                        {
                            userCanSeeProjectIds.Add((Guid)record["id"]);
                        }
                    }
                }
                #endregion

                #region << Get tasks >>
                {
                    var fields = "id,code,number,subject,start_date,end_date,status,priority,$user_1_n_task_owner.id,$user_1_n_task_owner.image";

                    QueryObject rootFilterSection = null;
                    QueryObject auxFilterSection = null;
                    QueryObject projectIdFilterSection = null;

                    #region << project id filters >>
                    var projectIdRulesList = new List<QueryObject>();
                    foreach (var projectId in userCanSeeProjectIds)
                    {
                        var projectIdRule = EntityQuery.QueryEQ("project_id", projectId);
                        projectIdRulesList.Add(projectIdRule);
                    }
                    projectIdFilterSection = EntityQuery.QueryOR(projectIdRulesList.ToArray());
                    #endregion

                    #region << Aux filters & Sort>>
                    var sortRulesList = new List<QuerySortObject>();
                    var queryString = HttpContext.Request.QueryString.ToString();
                    var queryKeyValueList = QueryHelpers.ParseQuery(queryString);
                    var auxRulesList = new List<QueryObject>();
                    var getListObject = entMan.ReadRecordList(entity.Name, listName).Object;
                    //Currently we will hardcode the query generation
                    //auxFilterSection = RecordListQuery.ConvertQuery(getListObject.Query);
                    QueryObject auxRule = new QueryObject();
                    foreach (var query in queryKeyValueList)
                    {
                        switch (query.Key.ToLowerInvariant())
                        {
                            case "code":
                                auxRule = new QueryObject();
                                auxRule = EntityQuery.QueryContains("code", (string)query.Value);
                                auxRulesList.Add(auxRule);
                                break;
                            case "subject":
                                auxRule = new QueryObject();
                                auxRule = EntityQuery.QueryContains("subject", (string)query.Value);
                                auxRulesList.Add(auxRule);
                                break;
                            case "status":
                                auxRule = new QueryObject();
                                auxRule = EntityQuery.QueryEQ("status", (string)query.Value);
                                auxRulesList.Add(auxRule);
                                break;
                            case "priority":
                                auxRule = new QueryObject();
                                auxRule = EntityQuery.QueryEQ("priority", (string)query.Value);
                                auxRulesList.Add(auxRule);
                                break;
                            case "sortby":
                                var sortRule = new QuerySortObject((string)query.Value, QuerySortType.Descending);
                                if (!queryKeyValueList.ContainsKey("sortOrder") || (string)queryKeyValueList["sortOrder"] == "ascending")
                                {
                                    sortRule = new QuerySortObject((string)query.Value, QuerySortType.Ascending);
                                }
                                sortRulesList.Add(sortRule);
                                break;
                        }

                    }
                    auxFilterSection = EntityQuery.QueryAND(auxRulesList.ToArray());
                    //Add default sort by created_on
                    var defaultSortRule = new QuerySortObject("created_on", QuerySortType.Ascending);
                    sortRulesList.Add(defaultSortRule);

                    #endregion

                    rootFilterSection = EntityQuery.QueryAND(projectIdFilterSection, auxFilterSection);

                    //Calculate page
                    var pageSize = getListObject.PageSize;
                    var skipRecords = (page - 1) * pageSize;

                    var taskQuery = new EntityQuery("wv_task", fields, rootFilterSection, sortRulesList.ToArray(), skipRecords, pageSize, null);

                    taskQueryResponse = recMan.Find(taskQuery);
                    if (!taskQueryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = taskQueryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                }
                #endregion
                var taskList = new List<EntityRecord>();
                #region << Post-process >>

                foreach (var task in taskQueryResponse.Object.Data)
                {
                    var record = new EntityRecord();
                    record["id"] = (Guid)task["id"];
                    record["code"] = (string)task["code"];
                    record["subject"] = (string)task["subject"];
                    record["start_date"] = (DateTime)task["start_date"];
                    record["end_date"] = (DateTime)task["end_date"];
                    record["status"] = (string)task["status"];
                    record["priority"] = (string)task["priority"];
                    var taskOwnerIdList = new List<Guid>();
                    var taskOwnerImageList = new List<string>();
                    var taskOwnerId = (Guid)((List<EntityRecord>)task["$user_1_n_task_owner"])[0]["id"];
                    var taskOwnerImage = (string)((List<EntityRecord>)task["$user_1_n_task_owner"])[0]["image"];
                    taskOwnerIdList.Add(taskOwnerId);
                    taskOwnerImageList.Add(taskOwnerImage);
                    record["$field$user_1_n_task_owner$id"] = taskOwnerIdList;
                    record["$field$user_1_n_task_owner$image"] = taskOwnerImageList;
                    taskList.Add(record);
                }
                #endregion

                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Successful read";
                response.Object = taskList;

                return Json(response);

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 8
0
        public IActionResult SprintDetails(Guid? sprintId = null, string scope = "user")
        {
            var response = new ResponseModel();
            var runningSprints = new List<EntityRecord>();
            var currentSprint = new EntityRecord();
            var userDictionary = new Dictionary<Guid, EntityRecord>();
            try
            {
                Guid? currentSprintId = null;

                #region << Get all users >>
                {
                    var query = new EntityQuery("user");
                    var queryResponse = recMan.Find(query);
                    if (!queryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "Error: " + queryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    foreach (var user in queryResponse.Object.Data)
                    {
                        userDictionary[(Guid)user["id"]] = user;
                    }
                }

                #endregion

                #region << Init the current sprint id >>
                if (sprintId == null)
                {
                    //Get all current sprints
                    {
                        var sprintFields = "id";
                        var queryFilter = EntityQuery.QueryAND(EntityQuery.QueryLTE("start_date", DateTime.UtcNow), EntityQuery.QueryGTE("end_date", DateTime.UtcNow.AddDays(-1)));
                        var sortRulesList = new List<QuerySortObject>();
                        var sortRule = new QuerySortObject("start_date", QuerySortType.Ascending);
                        sortRulesList.Add(sortRule);
                        var query = new EntityQuery("wv_sprint", sprintFields, queryFilter, sortRulesList.ToArray());
                        var queryResponse = recMan.Find(query);
                        if (!queryResponse.Success)
                        {
                            response.Success = false;
                            response.Timestamp = DateTime.UtcNow;
                            response.Message = "Error: " + queryResponse.Message;
                            response.Object = null;
                            return Json(response);
                        }
                        if (queryResponse.Object.Data.Any())
                        {
                            runningSprints = queryResponse.Object.Data;
                        }
                        else
                        {
                            response.Success = true;
                            response.Timestamp = DateTime.UtcNow;
                            response.Message = "Sprints successfully read, but no current sprints found!";
                            response.Object = null;
                            return Json(response);
                        }
                    }

                    //Find the first sprint that is current to the current data
                    currentSprintId = (Guid)runningSprints[0]["id"];
                }
                else
                {
                    currentSprintId = sprintId;
                }

                #endregion

                #region << Get current sprint details >>
                {
                    var fields = "id,start_date,end_date,name," +
                    "$wv_sprint_n_n_wv_task.id,$wv_sprint_n_n_wv_task.code,$wv_sprint_n_n_wv_task.owner_id, $wv_sprint_n_n_wv_task.priority,$wv_sprint_n_n_wv_task.status,$wv_sprint_n_n_wv_task.subject," +
                    "$wv_sprint_n_n_wv_task.x_billable_hours,$wv_sprint_n_n_wv_task.x_nonbillable_hours,$wv_sprint_n_n_wv_task.estimation," +
                    "$$role_n_n_wv_sprint.id";
                    var queryFilter = EntityQuery.QueryEQ("id", currentSprintId);
                    var query = new EntityQuery("wv_sprint", fields, queryFilter);
                    var queryResponse = recMan.Find(query);
                    if (!queryResponse.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "Error: " + queryResponse.Message;
                        response.Object = null;
                        return Json(response);
                    }
                    if (!queryResponse.Object.Data.Any())
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "There is no sprint with this Id";
                        response.Object = null;
                        return Json(response);
                    }

                    currentSprint = queryResponse.Object.Data[0];
                    var sprintRoleRecords = (List<EntityRecord>)currentSprint["$role_n_n_wv_sprint"];
                    //Check if the current user can see the sprint
                    var userRoles = SecurityContext.CurrentUser.Roles;
                    var userCanAccessSprint = false;
                    foreach(var role in sprintRoleRecords) {
                        if(userRoles.Any(x => x.Id == (Guid)role["id"])) {
                            userCanAccessSprint = true;
                            break;
                        }
                    }
                    if(!userCanAccessSprint) {
                        response.Success = true;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = "no access";
                        response.Object = null;
                        return Json(response);
                    }

                }
                #endregion

                var processedSprintObject = new EntityRecord();
                processedSprintObject["id"] = (Guid)currentSprint["id"];
                processedSprintObject["start_date"] = (DateTime)currentSprint["start_date"];
                processedSprintObject["end_date"] = (DateTime)currentSprint["end_date"];
                processedSprintObject["name"] = (string)currentSprint["name"];
                processedSprintObject["estimation"] = (decimal)0;
                processedSprintObject["logged"] = (decimal)0;
                processedSprintObject["tasks_not_started"] = new List<EntityRecord>();
                processedSprintObject["tasks_in_progress"] = new List<EntityRecord>();
                processedSprintObject["tasks_completed"] = new List<EntityRecord>();
                foreach (var task in (List<EntityRecord>)currentSprint["$wv_sprint_n_n_wv_task"])
                {
                    if ((scope == "user" && (Guid)task["owner_id"] == SecurityContext.CurrentUser.Id) || scope != "user")
                    {
                        var proccessedTask = new EntityRecord();
                        proccessedTask["id"] = (Guid)task["id"];
                        proccessedTask["code"] = (string)task["code"];
                        proccessedTask["priority"] = (string)task["priority"];
                        proccessedTask["status"] = (string)task["status"];
                        proccessedTask["subject"] = (string)task["subject"];
                        proccessedTask["estimation"] = (decimal)task["estimation"];
                        proccessedTask["logged"] = (decimal)task["x_nonbillable_hours"] + (decimal)task["x_billable_hours"];
                        proccessedTask["owner_username"] = (string)userDictionary[(Guid)task["owner_id"]]["username"];
                        proccessedTask["owner_image"] = (string)userDictionary[(Guid)task["owner_id"]]["image"];
                        switch ((string)task["status"])
                        {
                            case "not started":
                                ((List<EntityRecord>)processedSprintObject["tasks_not_started"]).Add(proccessedTask);
                                break;
                            case "in progress":
                                ((List<EntityRecord>)processedSprintObject["tasks_in_progress"]).Add(proccessedTask);
                                break;
                            case "completed":
                                ((List<EntityRecord>)processedSprintObject["tasks_completed"]).Add(proccessedTask);
                                break;
                        }
                        processedSprintObject["estimation"] = (decimal)processedSprintObject["estimation"] + (decimal)task["estimation"];
                        processedSprintObject["logged"] = (decimal)processedSprintObject["logged"] + (decimal)proccessedTask["logged"];
                    }
                }
                processedSprintObject["estimation"] = Math.Round((decimal)processedSprintObject["estimation"],2);
                processedSprintObject["logged"] = Math.Round((decimal)processedSprintObject["logged"],2);
                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "My projects successfully read";
                response.Object = processedSprintObject;

                return Json(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Error: " + ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 9
0
        public IActionResult ProjectTimelogReport(int year = 0, int month = 0)
        {
            var response = new ResponseModel();
            if (year == 0 || month == 0)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Month or year parameter is missing";
                response.Object = null;
                return Json(response);
            }
            try
            {
                var timelogs = new List<EntityRecord>();
                var projects = new List<EntityRecord>();
                var projectsWithTimelogs = new List<EntityRecord>();
                var projectTaskBugsDict = new Dictionary<Guid, List<EntityRecord>>();
                var workedTaskIds = new List<Guid>();
                var workedBugIds = new List<Guid>();
                var workedTasks = new List<EntityRecord>();
                var workedBugs = new List<EntityRecord>();
                var taskTimelogs = new Dictionary<Guid, List<EntityRecord>>();
                var bugTimelogs = new Dictionary<Guid, List<EntityRecord>>();

                #region << Get timelogs from the period >>
                var startDate = new DateTime(year, month, 1);
                var endDate = startDate.AddMonths(1);
                {
                    var requestedFields = "billable,hours," +
                    "$$bug_1_n_time_log.id,$$task_1_n_time_log.id";
                    var filterObj = EntityQuery.QueryAND(EntityQuery.QueryGTE("log_date", startDate), EntityQuery.QueryLT("log_date", endDate));
                    EntityQuery resultQuery = new EntityQuery("wv_timelog", requestedFields, filterObj);
                    QueryResponse result = recMan.Find(resultQuery);
                    if (!result.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = result.Message;
                        response.Object = null;
                        return Json(response);
                    }

                    timelogs = result.Object.Data;
                    var addedTasks = new Dictionary<Guid, bool>();
                    var addedBugs = new Dictionary<Guid, bool>();
                    foreach (var timelog in timelogs)
                    {

                        if (((List<EntityRecord>)timelog["$task_1_n_time_log"]).Any())
                        {
                            var task = ((List<EntityRecord>)timelog["$task_1_n_time_log"])[0];
                            var taskId = (Guid)task["id"];
                            if (!addedTasks.ContainsKey(taskId))
                            {
                                addedTasks[taskId] = true;
                                workedTaskIds.Add(taskId);
                            }
                            var timelogList = new List<EntityRecord>();
                            if (taskTimelogs.ContainsKey(taskId))
                            {
                                timelogList = taskTimelogs[taskId];
                                timelogList.Add(timelog);
                            }
                            else
                            {
                                timelogList.Add(timelog);
                            }
                            taskTimelogs[taskId] = timelogList;
                        }

                        if (((List<EntityRecord>)timelog["$bug_1_n_time_log"]).Any())
                        {
                            var bug = ((List<EntityRecord>)timelog["$bug_1_n_time_log"])[0];
                            var bugId = (Guid)bug["id"];
                            if (!addedBugs.ContainsKey(bugId))
                            {
                                addedBugs[bugId] = true;
                                workedBugIds.Add(bugId);
                            }
                            var timelogList = new List<EntityRecord>();
                            if (bugTimelogs.ContainsKey(bugId))
                            {
                                timelogList = bugTimelogs[bugId];
                                timelogList.Add(timelog);
                            }
                            else
                            {
                                timelogList.Add(timelog);
                            }
                            bugTimelogs[bugId] = timelogList;
                        }
                    }

                }
                #endregion

                #region << Get tasks >>
                {
                    if (workedTaskIds.Count() > 0)
                    {
                        var requestedFields = "id,subject,project_id";
                        var queryList = new List<QueryObject>();
                        foreach (var taskId in workedTaskIds)
                        {
                            var query = EntityQuery.QueryEQ("id", taskId);
                            queryList.Add(query);
                        }

                        var filterObj = EntityQuery.QueryOR(queryList.ToArray());
                        var sortRulesList = new List<QuerySortObject>();
                        var sortRule = new QuerySortObject("created_on", QuerySortType.Ascending);
                        sortRulesList.Add(sortRule);
                        EntityQuery resultQuery = new EntityQuery("wv_task", requestedFields, filterObj, sortRulesList.ToArray());
                        QueryResponse result = recMan.Find(resultQuery);
                        if (!result.Success)
                        {
                            response.Success = false;
                            response.Timestamp = DateTime.UtcNow;
                            response.Message = result.Message;
                            response.Object = null;
                            return Json(response);
                        }

                        workedTasks = result.Object.Data;
                    }
                }
                #endregion

                #region << Get bugs >>
                {
                    if (workedBugIds.Count() > 0)
                    {
                        var requestedFields = "id,subject,project_id";
                        var queryList = new List<QueryObject>();
                        foreach (var bugId in workedBugIds)
                        {
                            var query = EntityQuery.QueryEQ("id", bugId);
                            queryList.Add(query);
                        }
                        var filterObj = EntityQuery.QueryOR(queryList.ToArray());
                        var sortRulesList = new List<QuerySortObject>();
                        var sortRule = new QuerySortObject("created_on", QuerySortType.Ascending);
                        sortRulesList.Add(sortRule);
                        EntityQuery resultQuery = new EntityQuery("wv_bug", requestedFields, filterObj, sortRulesList.ToArray());
                        QueryResponse result = recMan.Find(resultQuery);
                        if (!result.Success)
                        {
                            response.Success = false;
                            response.Timestamp = DateTime.UtcNow;
                            response.Message = result.Message;
                            response.Object = null;
                            return Json(response);
                        }

                        workedBugs = result.Object.Data;
                    }
                }
                #endregion

                #region << Generate project task & bugs dict >>
                foreach (var task in workedTasks)
                {
                    var taskId = (Guid)task["id"];
                    var taskProjectId = (Guid)task["project_id"];
                    var taskBillable = (decimal)0;
                    var taskNotBillable = (decimal)0;
                    var taskTimeLogList = taskTimelogs[taskId];
                    foreach (var timelog in taskTimeLogList)
                    {
                        if ((bool)timelog["billable"])
                        {
                            taskBillable += (decimal)timelog["hours"];
                        }
                        else
                        {
                            taskNotBillable += (decimal)timelog["hours"];
                        }
                    }
                    task["billable"] = taskBillable;
                    task["not_billable"] = taskNotBillable;
                    task["type"] = "task";
                    var projectBugAndTasks = new List<EntityRecord>();
                    if (projectTaskBugsDict.ContainsKey(taskProjectId))
                    {
                        projectBugAndTasks = projectTaskBugsDict[taskProjectId];
                    }
                    projectBugAndTasks.Add(task);
                    projectTaskBugsDict[taskProjectId] = projectBugAndTasks;
                }
                foreach (var bug in workedBugs)
                {
                    var bugId = (Guid)bug["id"];
                    var bugProjectId = (Guid)bug["project_id"];
                    var bugBillable = (decimal)0;
                    var bugNotBillable = (decimal)0;
                    var bugTimeLogList = bugTimelogs[bugId];
                    foreach (var timelog in bugTimeLogList)
                    {
                        if ((bool)timelog["billable"])
                        {
                            bugBillable += (decimal)timelog["hours"];
                        }
                        else
                        {
                            bugNotBillable += (decimal)timelog["hours"];
                        }
                    }
                    bug["billable"] = bugBillable;
                    bug["not_billable"] = bugNotBillable;
                    bug["type"] = "bug";
                    var projectBugAndTasks = new List<EntityRecord>();
                    if (projectTaskBugsDict.ContainsKey(bugProjectId))
                    {
                        projectBugAndTasks = projectTaskBugsDict[bugProjectId];
                    }
                    projectBugAndTasks.Add(bug);
                    projectTaskBugsDict[bugProjectId] = projectBugAndTasks;
                }
                #endregion

                #region << Get all projects >>
                {
                    var requestedFields = "id,name";
                    var queryList = new List<QueryObject>();
                    foreach (var taskId in workedTaskIds)
                    {
                        var query = EntityQuery.QueryEQ("id", taskId);
                        queryList.Add(query);
                    }

                    QueryObject filterObj = null;
                    var sortRulesList = new List<QuerySortObject>();
                    var sortRule = new QuerySortObject("created_on", QuerySortType.Ascending);
                    sortRulesList.Add(sortRule);
                    EntityQuery resultQuery = new EntityQuery("wv_project", requestedFields, filterObj, sortRulesList.ToArray());
                    QueryResponse result = recMan.Find(resultQuery);
                    if (!result.Success)
                    {
                        response.Success = false;
                        response.Timestamp = DateTime.UtcNow;
                        response.Message = result.Message;
                        response.Object = null;
                        return Json(response);
                    }

                    projects = result.Object.Data;

                }
                #endregion

                #region << Generate project with logs >>
                var totalBillable = (decimal)0;
                var totalNotBillable = (decimal)0;
                foreach (var project in projects)
                {
                    if (projectTaskBugsDict.ContainsKey((Guid)project["id"]))
                    {
                        var projectEntries = (List<EntityRecord>)projectTaskBugsDict[(Guid)project["id"]];
                        project["entries"] = projectEntries;
                        project["billable"] = (decimal)0;
                        project["not_billable"] = (decimal)0;
                        foreach (var entry in projectEntries)
                        {
                            project["billable"] = (decimal)project["billable"] + (decimal)entry["billable"];
                            project["not_billable"] = (decimal)project["not_billable"] + (decimal)entry["not_billable"];
                        }
                        totalBillable += (decimal)project["billable"];
                        totalNotBillable += (decimal)project["not_billable"];
                        projectsWithTimelogs.Add(project);
                    }
                }

                #endregion

                #region
                var responseObject = new EntityRecord();
                responseObject["projects"] = projectsWithTimelogs;
                responseObject["billable"] = totalBillable;
                responseObject["not_billable"] = totalNotBillable;
                response.Success = true;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Report successfully generated";
                response.Object = responseObject;

                return Json(response);

                #endregion

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Timestamp = DateTime.UtcNow;
                response.Message = "Error: " + ex.Message;
                response.Object = null;
                return Json(response);
            }
        }
Exemplo n.º 10
0
        public IEnumerable<IEnumerable<KeyValuePair<string, object>>> Find(string entityName, QueryObject query, QuerySortObject[] sort, int? skip, int? limit)
        {
            var mongoCollection = MongoStaticContext.Context.GetBsonCollection(RECORD_COLLECTION_PREFIX + entityName);
            var mongoQuery = ConvertQuery(query);
            var cursor = mongoCollection.Find(mongoQuery);

            if (sort != null && sort.Length > 0)
            {
                SortByBuilder sortBy = null;
                foreach (var s in sort)
                {
                    if (s.SortType == QuerySortType.Ascending)
                        sortBy = sortBy == null ? SortBy.Ascending(s.FieldName) : sortBy.Ascending(s.FieldName);
                    else
                        sortBy = sortBy == null ? SortBy.Descending(s.FieldName) : sortBy.Descending(s.FieldName);
                }
                cursor.SetSortOrder(sortBy);
            }

            if (skip.HasValue)
                cursor.SetSkip(skip.Value);

            if (limit.HasValue)
                cursor.SetLimit(limit.Value);

            List<List<KeyValuePair<string, object>>> result = new List<List<KeyValuePair<string, object>>>();
            foreach (BsonDocument doc in cursor)
            {
                List<KeyValuePair<string, object>> record = new List<KeyValuePair<string, object>>();
                foreach (var fieldName in doc.Names)
                {
                    if (fieldName == "_id")
                        record.Add(new KeyValuePair<string, object>("id", BsonTypeMapper.MapToDotNetValue(doc["_id"])));
                    else
                        record.Add(new KeyValuePair<string, object>(fieldName, ConvertBsonValueToObject(doc[fieldName])));
                }
                result.Add(record);
            }
            return result;
        }