Example #1
0
        public async Task <bool> UserParser(List <UserList> studentList, IStObjMap stObjMap, IAuthenticationInfo authenticationInfo, string type)
        {
            using (var ctx = new SqlStandardCallContext())
            {
                var sqlDatabase    = stObjMap.StObjs.Obtain <SqlDefaultDatabase>();
                var userTable      = stObjMap.StObjs.Obtain <CustomUserTable>();
                var userTimedTable = stObjMap.StObjs.Obtain <TimedUserTable>();
                var groupTable     = stObjMap.StObjs.Obtain <CustomGroupTable>();

                GroupQueries groupQueries = new GroupQueries(ctx, sqlDatabase);
                UserQueries  userQueries  = new UserQueries(ctx, sqlDatabase);

                int       currentUserId = authenticationInfo.ActualUser.UserId;
                GroupData groupData     = await groupQueries.GetIdSchoolByConnectUser(currentUserId);

                int zoneId = groupData.ZoneId;


                foreach (var element in studentList)
                {
                    string   userName      = Guid.NewGuid().ToString();
                    string   firstName     = element.FirstName;
                    string   lastName      = element.Name;
                    int      timedUserType = 0;
                    string[] groupName     = new string[1];
                    string   mail          = element.Mail;
                    int      idUser        = await CheckIfUserExists(stObjMap, authenticationInfo, mail, userName, firstName, lastName);

                    timedUserType = GetTimedUserType(type);
                    if (type == "student")
                    {
                        groupName = element.GroupName.Split('-');
                    }
                    else if (type == "staffMember")
                    {
                        groupName[0] = element.GroupName;
                    }
                    int groupId = await groupQueries.GetIdGroupByNameAndPeriod(groupName[0], zoneId);


                    TimedUserData timedUserData = await userQueries.CheckIfTimedUserExists(idUser, zoneId);

                    if (timedUserData == null)
                    {
                        await userTimedTable.CreateOrUpdateTimedUserAsync(ctx, timedUserType, zoneId, idUser);

                        groupTable.AddUser(ctx, currentUserId, groupId, idUser, true);
                        if (type == "student" && groupName[1] != "A")
                        {
                            int sector = await groupQueries.GetIdGroupByNameAndPeriod(groupName[1], zoneId);

                            groupTable.AddUser(ctx, currentUserId, sector, idUser, true);
                        }
                    }
                    else
                    {
                        int isUserInGroup = await groupQueries.GetGroupByNamePeriodUser(groupName[0], idUser, zoneId);

                        if (isUserInGroup == 0)
                        {
                            groupTable.AddUser(ctx, currentUserId, groupId, idUser, true);
                            if (type == "student" && groupName[1] != "A")
                            {
                                int sector = await groupQueries.GetIdGroupByNameAndPeriod(groupName[1], zoneId);

                                groupTable.AddUser(ctx, currentUserId, sector, idUser, true);
                            }
                        }
                    }
                }
            }
            return(true);
        }