public static async Task Run([EventHubTrigger("pack2schoolhub", Connection = "myConnection")] EventData[] events, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages,
                                     ILogger log)
        {
            var exceptions = new List <Exception>();

            foreach (EventData eventData in events)
            {
                List <string>          missingSubjects = null;
                DataBaseAndScanUpdates dataBaseAndScanUpdates;

                eventData.SystemProperties.TryGetValue(deviceIdProperty, out var deviceIdObj);
                string userId = (string)deviceIdObj;

                string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");

                JObject obj          = JObject.Parse(messageBody);
                var     errorMessage = obj[error]?.ToString();

                if (errorMessage != null)
                {
                    dataBaseAndScanUpdates = new DataBaseAndScanUpdates(errorMessage);
                }
                else
                {
                    var subjects       = obj[stickers].Select(x => x.ToString()).ToList();
                    var subjectsUpdate = await SubjectsTableUtilities.UpdateBagContent(userId, subjects);

                    var extraSubjects = subjectsUpdate[0];
                    missingSubjects        = subjectsUpdate[1];
                    dataBaseAndScanUpdates = new DataBaseAndScanUpdates(userId, missingSubjects: missingSubjects, extraSubjects: extraSubjects);
                }

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    UserId    = userId,
                    Target    = ProjectConsts.SignalRTarget,
                    Arguments = new object[] { dataBaseAndScanUpdates }
                });


                var parentsEntities = UsersTableUtilities.GetParentsEntitiesFromChildId(userId);

                foreach (var parentEntity in parentsEntities)
                {
                    if (parentEntity.UserEmail != null && dataBaseAndScanUpdates.missingSubjects != null && dataBaseAndScanUpdates.missingSubjects.Count > 0)
                    {
                        await EmailSender.sendEmailAsync(userId, parentEntity.UserEmail, missingSubjects);
                    }
                }
            }
        }
        public static async Task RunAsync([TimerTrigger("0 00 14 * * *")] TimerInfo myTimer, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            var TodayDateTime   = DateTime.Today;
            var classesTable    = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var classesEntities = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.ScheduleTableName == null)
                {
                    continue;
                }

                if (classEntity.LastTeacherUpdate.Date != TodayDateTime)
                {
                    var day            = System.DateTime.Now.DayOfWeek.ToString();
                    var scheduleTable  = CloudTableUtilities.OpenTable(classEntity.ScheduleTableName);
                    var subjectsEntity = CloudTableUtilities.getTableEntityAsync <ScheduleTable>(scheduleTable, day).Result.First();
                    var neededSubjects = typeof(ScheduleTable).GetProperties().Where(propInfo => propInfo.Name.Contains(SubjectsTableUtilities.SubjectPropertyPrefix) && propInfo.GetValue(subjectsEntity) != null)
                                         .Select(propInfo => propInfo.GetValue(subjectsEntity)?.ToString()).ToList();

                    var subjectsTable     = CloudTableUtilities.OpenTable(classEntity.subjectsTableName);
                    var subjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
                    var subjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();
                    SubjectsTableUtilities.UpdateSubjectNecessity(subjectsNames, subjectsNecessity, neededSubjects);
                    await CloudTableUtilities.AddTableEntity(subjectsTable, subjectsNecessity);

                    neededSubjects = SubjectsTableUtilities.GetNeededSubject(classEntity.subjectsTableName);
                    var studentsIds = SubjectsTableUtilities.GetAllStudentsIds(classEntity.subjectsTableName);

                    foreach (var studentId in studentsIds)
                    {
                        var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(classEntity.subjectsTableName, studentId);

                        var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects);

                        await signalRMessages.AddAsync(
                            new SignalRMessage
                        {
                            UserId    = studentId,
                            Target    = ProjectConsts.SignalRTarget,
                            Arguments = new object[] { dataBaseAndScanUpdates }
                        });
                    }
                }
            }
        }
        public static async Task <string> UpdateSubjectNecessity(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            var            operationResult = new OperationResult();
            SubjectRequest subjectRequest  = await Utilities.ExtractContent <SubjectRequest>(request);

            var subjectsTable          = CloudTableUtilities.OpenTable(subjectRequest.tableName);
            var classSubjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
            var classSubjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();

            SubjectsTableUtilities.UpdateSubjectNecessity(classSubjectsNames, classSubjectsNecessity, subjectRequest.neededForTomorrow);
            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNecessity);

            var neededSubjects = SubjectsTableUtilities.GetNeededSubject(subjectRequest.tableName);
            var studentsIds    = SubjectsTableUtilities.GetAllStudentsIds(subjectRequest.tableName);

            foreach (var studentId in studentsIds)
            {
                var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(subjectRequest.tableName, studentId);

                var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects);

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    UserId    = studentId,
                    Target    = ProjectConsts.SignalRTarget,
                    Arguments = new object[] { dataBaseAndScanUpdates }
                });
            }

            var classesTable    = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var classesEntities = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.subjectsTableName == subjectRequest.tableName)
                {
                    classEntity.LastTeacherUpdate = DateTime.Now;
                    await CloudTableUtilities.AddTableEntity(classesTable, classEntity);
                }
            }

            return(JsonConvert.SerializeObject(operationResult));
        }
        public static async Task <string> EditSubject(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request, [SignalR(HubName = "Pack2SchoolSignalR1")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log)
        {
            OperationResult operationResult    = new OperationResult();
            SubjectRequest  editSubjectRequest = await Utilities.ExtractContent <SubjectRequest>(request);

            var    subjectsTable          = CloudTableUtilities.OpenTable(editSubjectRequest.tableName);
            var    classSubjectsNames     = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.ClassSubjects).Result.First();
            var    classSubjectsNecessity = CloudTableUtilities.getTableEntityAsync <SubjectsTable>(subjectsTable, SubjectsTableUtilities.Necessity).Result.First();
            var    classesTable           = CloudTableUtilities.OpenTable(ProjectConsts.classesTableName);
            var    classesEntities        = CloudTableUtilities.getTableEntityAsync <ClassesTable>(classesTable).Result;
            string scheduleTableName      = null;

            foreach (var classEntity in classesEntities)
            {
                if (classEntity.subjectsTableName == editSubjectRequest.tableName)
                {
                    scheduleTableName = classEntity.ScheduleTableName;
                    break;
                }
            }

            if (editSubjectRequest.requestType == ProjectConsts.AddSubjectOperation)
            {
                SubjectsTableUtilities.AddNewSubject(editSubjectRequest, classSubjectsNames, classSubjectsNecessity, operationResult);
            }

            if (editSubjectRequest.requestType == ProjectConsts.RenameSubjectOperation)
            {
                await SubjectsTableUtilities.RenameSubjectAsync(editSubjectRequest, classSubjectsNames, scheduleTableName, classSubjectsNecessity, operationResult);
            }

            if (editSubjectRequest.requestType == ProjectConsts.DeleteSubjectOperation)
            {
                await SubjectsTableUtilities.DeleteSubjectAsync(editSubjectRequest, classSubjectsNames, scheduleTableName, classSubjectsNecessity, operationResult);
            }

            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNames);

            await CloudTableUtilities.AddTableEntity(subjectsTable, classSubjectsNecessity);

            var neededSubjects = SubjectsTableUtilities.GetNeededSubject(editSubjectRequest.tableName);
            var allSubjects    = SubjectsTableUtilities.GetAllSubjects(editSubjectRequest.tableName);
            var studentsIds    = SubjectsTableUtilities.GetAllStudentsIds(editSubjectRequest.tableName);

            foreach (var studentId in studentsIds)
            {
                var missingSubjects = SubjectsTableUtilities.GetMissingSubejcts(editSubjectRequest.tableName, studentId);

                var dataBaseAndScanUpdates = new DataBaseAndScanUpdates(studentId, neededSubjects, missingSubjects, allSubjects);

                await signalRMessages.AddAsync(
                    new SignalRMessage
                {
                    UserId    = studentId,
                    Target    = ProjectConsts.SignalRTarget,
                    Arguments = new object[] { dataBaseAndScanUpdates }
                });
            }

            return(JsonConvert.SerializeObject(operationResult));
        }