예제 #1
0
        private void sendNotifyTopicOfTheDay(DateTime now)
        {
            var requiredNotifyTOTD = _classCalendarRepo.GetRequireNotifyTopicOfTheDay(now).ToList();

            if (!requiredNotifyTOTD.Any())
            {
                return;
            }

            var classIds = requiredNotifyTOTD.Select(it => it.ClassRoomId).Distinct();
            var students = _userProfileRepo.GetUserProfilesByClassRoomId(classIds).ToList();

            var qry = from classCalendar in requiredNotifyTOTD
                      where !classCalendar.CloseDate.HasValue
                      from lessonCalendar in classCalendar.LessonCalendars
                      where !lessonCalendar.DeletedDate.HasValue
                      where !lessonCalendar.SendTopicOfTheDayDate.HasValue
                      where lessonCalendar.RequiredSendTopicOfTheDayDate.Date <= now.Date
                      from student in students
                      where !student.DeletedDate.HasValue
                      from subscription in student.Subscriptions
                      where !subscription.DeletedDate.HasValue
                      where subscription.ClassRoomId == classCalendar.ClassRoomId
                      where subscription.Role != UserProfile.AccountRole.Teacher
                      select new Notification
            {
                id = Guid.NewGuid().ToString(),
                ByUserProfileId = Enumerable.Empty <string>(),
                ClassRoomId     = classCalendar.ClassRoomId,
                CreatedDate     = now,
                LastUpdateDate  = now,
                LessonId        = lessonCalendar.LessonId,
                Message         = lessonCalendar.TopicOfTheDayMessage,
                Tag             = Notification.NotificationTag.TopicOfTheDay,
                ToUserProfileId = student.id
            };

            _notificationRepo.Insert(qry);

            var reqUpdateLessons = from classCalendar in requiredNotifyTOTD
                                   where !classCalendar.CloseDate.HasValue
                                   from lessonCalendar in classCalendar.LessonCalendars
                                   where !lessonCalendar.DeletedDate.HasValue
                                   select lessonCalendar;

            foreach (var item in reqUpdateLessons)
            {
                item.SendTopicOfTheDayDate = now;
            }
            requiredNotifyTOTD.ForEach(it => _classCalendarRepo.UpsertClassCalendar(it));
        }
예제 #2
0
        public async Task <IActionResult> ChargeACreditCard(PurchaseCourseViewModel model)
        {
            try
            {
                var selectedUserProfile = _userprofileRepo.GetUserProfileById(User.Identity.Name);
                var isUserProfileValid  = selectedUserProfile != null && !selectedUserProfile.DeletedDate.HasValue;
                if (!isUserProfileValid)
                {
                    _logger.LogCritical($"User profile { User.Identity.Name } not found.");
                    ViewBag.ErrorMessage = _errorMsgs.AccountNotFound;
                    return(View("Error"));
                }

                var selectedCourse = _courseCtrl.GetCourseDetail(model.CourseId);
                if (selectedCourse == null)
                {
                    ViewBag.ErrorMessage = _errorMsgs.CourseNotFound;
                    return(View("Error"));
                }

                var selectedClassRoom = _classRoomRepo.GetPublicClassRoomByCourseCatalogId(model.CourseId);
                var isClassRoomValid  = selectedClassRoom != null && !selectedClassRoom.DeletedDate.HasValue;
                if (!isClassRoomValid)
                {
                    _logger.LogCritical($"ClassRoom of CourseId: { model.CourseId } not found.");
                    ViewBag.ErrorMessage = _errorMsgs.SelectedCourseIsNotAvailableForPurchase;
                    return(View("Error"));
                }

                var isAlreadyHaveTheSelectedCourse = !_myCourseCtrl.CanAddNewCourseCatalog(User.Identity.Name, model.CourseId);
                if (isAlreadyHaveTheSelectedCourse)
                {
                    return(RedirectToAction("entercourse", "my", new { @id = model.CourseId }));
                }

                if (ModelState.IsValid)
                {
                    var paymentResult      = Engines.Models.PaymentResult.Unknow;
                    var isPaymentSuccessed = false;
                    var newSubscriptionId  = Guid.NewGuid().ToString();
                    var newPaymentId       = string.Empty;
                    var now = _dateTime.GetCurrentTime();

                    try
                    {
                        // Pay with Paypal
                        paymentResult = _payment.ChargeCreditCard(new Engines.Models.PaymentInformation
                        {
                            Address             = model.PrimaryAddress.Address,
                            City                = model.PrimaryAddress.City,
                            Country             = model.PrimaryAddress.Country.ToString(),
                            PostalCode          = model.PrimaryAddress.ZipCode,
                            State               = model.PrimaryAddress.State,
                            TotalPrice          = selectedCourse.PriceUSD,
                            UserProfileId       = User.Identity.Name,
                            PurchaseForCourseId = model.CourseId,
                            FirstName           = model.CreditCardInfo.FirstName,
                            LastName            = model.CreditCardInfo.LastName,
                            ExpiredYear         = model.CreditCardInfo.ExpiredYear,
                            ExpiredMonth        = model.CreditCardInfo.ExpiredMonth,
                            CVV = model.CreditCardInfo.CVV.ToString(),
                            CreditCardNumber = model.CreditCardInfo.CardNumber,
                            CardType         = model.CreditCardInfo.CardType.ToString()
                        });
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"Paypal payment error, from user: { User.Identity.Name }, course id: { model.CourseId }, Error: { e.ToString() }");
                        ViewBag.ErrorMessage = _errorMsgs.CanNotChargeACreditCard;
                        return(View("Error"));
                    }
                    finally
                    {
                        isPaymentSuccessed = paymentResult == Engines.Models.PaymentResult.approved;
                        newSubscriptionId  = isPaymentSuccessed ? newSubscriptionId : "None";
                        var payment = createNewPayment(selectedCourse.id, selectedCourse.SideName, newSubscriptionId, model, selectedCourse.PriceUSD, now, isPaymentSuccessed);
                        await _paymentRepo.CreateNewPayment(payment);

                        newPaymentId = payment.id;
                    }

                    if (!isPaymentSuccessed)
                    {
                        ViewBag.ErrorMessage = _errorMsgs.CanNotChargeACreditCard;
                        return(View("Error"));
                    }

                    try
                    {
                        var requestLessonCatalogIds = selectedClassRoom.Lessons.Select(it => it.LessonCatalogId);
                        var lessonCatalogs          = _lessonCatalogRepo.GetLessonCatalogById(requestLessonCatalogIds).ToList();
                        var newClassCalendar        = createClassCalendar(selectedClassRoom, lessonCatalogs, now);
                        newClassCalendar.CalculateCourseSchedule();
                        newClassCalendar.ExpiredDate      = null;
                        selectedUserProfile.Subscriptions = addNewSelfPurchaseSubscription(selectedUserProfile.Subscriptions, selectedClassRoom, newClassCalendar.id, model.CourseId, now, newSubscriptionId);
                        var userActivity = selectedUserProfile.CreateNewUserActivity(selectedClassRoom, newClassCalendar, lessonCatalogs, now);

                        _classCalendarRepo.UpsertClassCalendar(newClassCalendar);
                        _userprofileRepo.UpsertUserProfile(selectedUserProfile);
                        _userActivityRepo.UpsertUserActivity(userActivity);

                        return(RedirectToAction("Finished", new { @id = newPaymentId }));
                    }
                    catch (Exception e)
                    {
                        _logger.LogCritical($"User: '******' already purchased course id: '{ model.CourseId }' with payment id: '{ newPaymentId }' but the system can't create new course.");
                        throw e;
                    }
                }
                return(View(model));
            }
            catch (Exception e)
            {
                _logger.LogError($"MongoDB: { e.ToString() }");
                ViewBag.ErrorMessage = _errorMsgs.CanNotConnectToTheDatabase;
                return(View("Error"));
            }
        }
예제 #3
0
        public void Leave(LeaveCourseRequest body)
        {
            var areArgumentsValid = body != null &&
                                    !string.IsNullOrEmpty(body.ClassRoomId) &&
                                    !string.IsNullOrEmpty(body.UserProfileId);

            if (!areArgumentsValid)
            {
                return;
            }

            UserProfile userprofile;
            var         canAccessToTheClassRoom = _userprofileRepo.CheckAccessPermissionToSelectedClassRoom(body.UserProfileId, body.ClassRoomId, out userprofile);

            if (!canAccessToTheClassRoom)
            {
                return;
            }

            var isTeacherAccount = userprofile.Subscriptions.First(it => it.ClassRoomId == body.ClassRoomId).Role == UserProfile.AccountRole.Teacher;

            if (!isTeacherAccount)
            {
                return;
            }

            var selectedClassRoom = _classRoomRepo.GetClassRoomById(body.ClassRoomId);

            if (selectedClassRoom == null)
            {
                return;
            }

            var students = _userprofileRepo.GetUserProfilesByClassRoomId(body.ClassRoomId).ToList();

            if (students == null)
            {
                return;
            }

            var selectedClassCalendar = _classCalendarRepo.GetClassCalendarByClassRoomId(body.ClassRoomId);

            if (selectedClassCalendar == null)
            {
                return;
            }

            var now = _dateTime.GetCurrentTime();

            selectedClassRoom.DeletedDate = now;
            _classRoomRepo.UpsertClassRoom(selectedClassRoom);

            selectedClassCalendar.DeletedDate = now;
            _classCalendarRepo.UpsertClassCalendar(selectedClassCalendar);

            var subscriptions = students.SelectMany(it => it.Subscriptions)
                                .Where(it => it.ClassRoomId.Equals(body.ClassRoomId))
                                .Where(it => !it.DeletedDate.HasValue)
                                .ToList();

            subscriptions.ForEach(it => it.DeletedDate = now);
            students.ForEach(it => _userprofileRepo.UpsertUserProfile(it));

            var activities = students.Select(it => _userActivityRepo.GetUserActivityByUserProfileIdAndClassRoomId(it.id, body.ClassRoomId))
                             .Where(it => it != null)
                             .Where(it => !it.DeletedDate.HasValue)
                             .ToList();

            activities.ForEach(it =>
            {
                it.DeletedDate = now;
                _userActivityRepo.UpsertUserActivity(it);
            });

            var selectedStudentKey = _studentKeyRepo.GetStudentKeyByClassRoomId(body.ClassRoomId);

            if (selectedStudentKey == null)
            {
                return;
            }

            selectedStudentKey.DeletedDate = now;
            _studentKeyRepo.UpsertStudentKey(selectedStudentKey);
        }