Example #1
0
 public Task <Result <Guid> > Add(CreateMessageModel model)
 => _messageBiz.Add(model);
        public Task <Result <UserSurveyModel> > Answer(SurveyAnswerModel model)
        => Result <UserSurveyModel> .TryAsync(async() =>
        {
            var results = await _repository.ListAsync <Answer>(a => model.AnswerIds.Contains(a.Id),
                                                               a => a.Question.Survey.Question.Select(q => q.Answer.Select(aa => aa.Action)),
                                                               a => a.Question.MustAnswered, a => a.Question.Survey.Question.Select(q => q.MustAnswered),
                                                               a => a.Question.Survey.Question.Select(q => q.MustAnswered)
                                                               , a => a.Action);


            if (!results.Success || results.Data == null || !results.Data.Any() ||
                results.Data.Count != model.AnswerIds.Count)
            {
                return(Result <UserSurveyModel> .Failed(Error.WithData(1000, new[] { "answer not found" })));
            }
            if (results.Data.GroupBy(a => a.Question).ToList().Count() != 1)
            {
                return(Result <UserSurveyModel> .Failed(Error.WithData(1000,
                                                                       new[] { "answers are not from one question" })));
            }


            var answers = results.Data;

            var resultUserSurvey = await _repository.FirstOrDefaultAsync <UserSurvey>(us =>
                                                                                      us.SurveyId == answers.FirstOrDefault().Question.Survey.Id&&
                                                                                      us.UserId == generalDataService.User.Id,
                                                                                      us => us.UserAnswer.Select(ua => ua.Answer.Question.Survey.Question.Select(q => q.Answer)),
                                                                                      us => us.Survey.Question.Select(q => q.MustAnswered),
                                                                                      us => us.Survey.Question.Select(question => question.Answer), us => us.UserDependentSurvey);

//                var resultUserSurvey = await _repository.FirstOrDefaultAsync<UserSurvey>(us =>
//                        us.SurveyId == answers.FirstOrDefault().Question.Survey.Id && us.UserId == generalDataService.User.Id,
//                    us => us.UserAnswer);

            UserSurvey userSurvey = null;
            if (resultUserSurvey.Data == null)     // first answer
            {
                userSurvey = new UserSurvey
                {
                    Id         = Guid.NewGuid(),
                    UserId     = generalDataService.User.Id,
                    Survey     = answers.FirstOrDefault().Question.Survey,
                    IsFinsihed = answers.FirstOrDefault().Question.Survey.Question.Count == 1,
                    UserAnswer = answers.Select(answer => new UserAnswer
                    {
                        Id = Guid.NewGuid(), Answer = answer, Text = model.UserAnswer
                    }).ToList()
                };
                _repository.Add(userSurvey);
            }
            else     // not the first answer
            {
                userSurvey = resultUserSurvey.Data;

                var questionsAnswered = userSurvey.UserAnswer.Select(ua => ua.Answer.Question).ToList();
                if (questionsAnswered.Select(qa => qa.Id).Contains(answers.FirstOrDefault().Question.Id)
                    ) // change answer
                {
                    var previousAnswers = userSurvey.UserAnswer.Where(ua =>
                                                                      answers.Any(aa => aa.Question.Id == ua.Answer.Question.Id)).ToList();
                    previousAnswers.AddRange(userSurvey.UserAnswer.Where(ua =>
                                                                         ua.Answer.Question.Number > answers.FirstOrDefault().Question.Number));
                    var actions = previousAnswers.Where(ua =>
                                                        ua.Answer.Question.Number > answers.FirstOrDefault().Question.Number).ToList()
                                  .SelectMany(ua => ua.Answer.Action);
                    if (actions.Any(a => a.Type == (int)ActionType.AddDependent))
                    {
                        if (previousAnswers.Any(ua => ua.Answer.Question.Text.ToLower().Contains("spouse")))
                        {
                            _repository.RemoveRange(userSurvey.UserDependentSurvey);
                            if (userSurvey.UserAssessmentSurvey != null && userSurvey.UserAssessmentSurvey.Any() &&
                                userSurvey.UserAssessmentSurvey
                                .Select(ua => ua.UserAssessmentBlob).Any())
                            {
                                _repository.RemoveRange(userSurvey.UserAssessmentSurvey
                                                        .Select(ua => ua.UserAssessmentBlob).ToList());
                            }
                            if (userSurvey.UserAssessmentSurvey != null && userSurvey.UserAssessmentSurvey.Any())
                            {
                                _repository.RemoveRange(userSurvey.UserAssessmentSurvey);
                            }
                        }
                        else
                        {
                            var allDependents =
                                await _membershipServiceApi.MembershipLinkedUserApiService.ListByUser(
                                    new MembershipService.ApiClient.Models.BaseModel
                            {
                                Id = generalDataService.User.Id
                            });
                            var dependentIds = allDependents.Data.Where(ad => ad.RelationType.ToLower() != "spouse")
                                               .ToList()
                                               .Select(ad => ad.FirstUser.Id).ToList();
                            _repository.RemoveRange(
                                userSurvey.UserDependentSurvey.Where(ud => dependentIds.Contains(ud.UserId))
                                .ToList());
                        }
                    }

                    if (actions.Any(a => a.Type == (int)ActionType.AddRelative))
                    {
                        _repository.RemoveRange(userSurvey.UserRelativeSurvey);
                    }


                    previousAnswers.ForEach(previousAnswer => userSurvey.UserAnswer.Remove(previousAnswer));
                    _repository.RemoveRange(previousAnswers);
                }

                answers.ToList().ForEach(
                    answer => userSurvey.UserAnswer.Add(new UserAnswer
                {
                    Id = Guid.NewGuid(), Answer = answer, Text = model.UserAnswer
                }));
            }

            QuestionCoreModel nextQuestionCoreModel = CalculateNextQuestion(answers.FirstOrDefault(), userSurvey);

            var userResult = await _membershipServiceApi.AuthAuthApiService.Setting(
                new MembershipService.ApiClient.Models.BaseModel
            {
                Id = generalDataService.User.Id
            });

            var profile = await _membershipServiceApi.AuthAuthApiService.Profile(
                new MembershipService.ApiClient.Models.BaseModel
            {
                Id = generalDataService.User.Id
            });

            var isFinished = SurveyIsFinished(userSurvey).Data;

            var makeAppointment = false;
            var userCartUpdated = false;
            Guid?invoiceId      = null;
            if (isFinished)     // Perform actions after survey is finished
            {
                var result      = (await PerformActions(userSurvey)).Data;
                invoiceId       = result.Item1;
                makeAppointment = result.Item2;

                await _messageBiz.Add(new CreateMessageModel
                {
                    Title = "Survey Finished",
                    Body  =
                        "you have successfully finished your survey , we will check and contact you about the results as soon as possible ",
                    Priority = MessagePriority.High
                });

                _coreSmtpClient.SendSurveySubmittedNotif(profile.Data.Email, profile.Data.Mobile,
                                                         profile.Data.Username,
                                                         DateTime.Now.ToString("F"));


//                    _coreSmtpClient.Send(userResult.Data.Email,
//                        $"Dear {userResult.Data.Firstname} {userResult.Data.Lastname} ,\n you have finished our survey , we will check and contact you about the results as soon as possible. \n Best Regards",
//                        "Finished Survey ");

//                    _smsHttpClient.Send(userResult.Data.Mobile,
//                        $"Dear {userResult.Data.Firstname} {userResult.Data.Lastname} , You have finished doing a survey, we will check and contact you about the results as soon as possible. \n Best Regards ");
            }

            userSurvey.IsFinsihed = isFinished;
            await _repository.CommitAsync();

            return(Result <UserSurveyModel> .Successful(new UserSurveyModel
            {
                NextQuestion = nextQuestionCoreModel,
                SurveyId = answers.FirstOrDefault().Question.Survey.Id,
                MakeAnAppointment = makeAppointment,
                UserCartUpdated = userCartUpdated,
                SurveyName = answers.FirstOrDefault().Question.Survey.Name,
                IsFinished = isFinished,
                IsStarted = true,
                QuestionCount = userSurvey.Survey.Question.Count,
                InvoiceId = invoiceId,
                User = userResult.Data
            }));
        });
        public async Task <Result <InvoiceCoreModel> > CaptureOrder(CaptureOrderModel model)
        {
            var invoice = await _repository.FirstOrDefaultAsync <Invoice>(i => i.Id == model.InvoiceId,
                                                                          i => i.PaypalOrder, i => i.Appointment, i => i.UserSurvey.Select(us => us.Tax));

            if (!invoice.Success || invoice.Data == null)
            {
                return(Result <InvoiceCoreModel> .Failed(Error.WithData(1000, new[] { "Invoice not found" })));
            }

            var capture = await _paypalHttpClient.Capture(invoice.Data.PaypalOrder.OrderId);

            if (!capture.Success)
            {
                return(Result <InvoiceCoreModel> .Failed(Error.WithData(1000, new[] { "Could not capture order" })));
            }

            invoice.Data.IsPaid  = true;
            invoice.Data.Enabled = true;
            invoice.Data.Status  = (byte)InvoiceStatus.Paid;


            await _repository.CommitAsync();

            var profile =
                await _membershipServiceApi.AuthAuthApiService.Profile(new BaseModel { Id = invoice.Data.UserId });

            try
            {
                if (invoice.Data.UserSurvey != null && invoice.Data.UserSurvey.Any())
                {
                    invoice.Data.UserSurvey.FirstOrDefault().Tax.ToList()
                    .ForEach(t => t.Status = (byte)TaxStatus.TaxProcessPending);
                    await _repository.CommitAsync();

                    _coreSmtpClient.SendPaypalPayment(profile.Data.Email,
                                                      profile.Data.Firstname + " " + profile.Data.Lastname);
                }

                if (invoice.Data.Appointment != null && invoice.Data.Appointment.Any())
                {
                    _coreSmtpClient.SendVipConsultation(profile.Data.Email,
                                                        profile.Data.Firstname + " " + profile.Data.Lastname, invoice.Data.Appointment.FirstOrDefault().Date.ToString("F"));
                    _coreSmtpClient.SendAppointmentBookNotif("*****@*****.**",
                                                             profile.Data.Firstname + " " + profile.Data.Lastname,
                                                             invoice.Data.Appointment.FirstOrDefault().Date.ToString("F"));


                    await _messageBiz.Add(new CreateMessageModel
                    {
                        Title = "Your Appointment",
                        Body  =
                            $"Your appointment has been confirmed. One of our AccNet representatives will reach out to you at the scheduled time : {invoice.Data.Appointment.FirstOrDefault().Date.ToString("F")} (PACIFIC STANDARD TIME)",
                        ToUser   = profile.Data.Id,
                        Priority = MessagePriority.High
                    });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            _coreSmtpClient.SendPaysForInvoicesNotif("*****@*****.**", profile.Data.Username,
                                                     DateTime.Now.ToString("F"));

//            var result =
//                await _membershipServiceApi.SystemUserApiService.Get(new MembershipService.ApiClient.Models.BaseModel
//                    {Id = generalDataService.User.Id});
//            _coreSmtpClient.SendInvoice(invoice.Data, result.Data);


            return(Result <InvoiceCoreModel> .Successful(new InvoiceCoreModel
            {
                Id = invoice.Data.Id,
                Amount = invoice.Data.Amount,
                Description = invoice.Data.Description,
                Title = invoice.Data.Title,
                Status = (InvoiceStatus)invoice.Data.Status,
                CreationDate = invoice.Data.CreationDate,
                Enabled = invoice.Data.Enabled,
            }));
        }
        public Task <Result <object> > Add(CreateAppointmentModel model)
        => Result <object> .TryAsync(async() =>
        {
            var isAdmin = generalDataService.User.Permissions.Any(p => p == (int)PermissionType.Admin);

            if (model.UserId == Guid.Empty)
            {
                model.UserId = generalDataService.User.Id;
            }
            var user = (await _membershipServiceApi.SystemUserApiService.Get(
                            new MembershipService.ApiClient.Models.BaseModel {
                Id = model.UserId
            })).Data;
            if (user == null)
            {
                return(Result <object> .Failed(Error.WithData(1000, new[] { "User not found" })));
            }

            var validateAppointment = await _appointmentExceptionBiz.ValidateAppointment(model.Date);
            if (validateAppointment.Success && validateAppointment.Data)
            {
                return(Result <object> .Failed(Error.WithData(1000,
                                                              new[] { "Sorry , there are no available representative in the requested time" })));
            }

            var duplicateTime =
                await _repository.FirstOrDefaultAsNoTrackingAsync <Appointment>(a => a.Date == model.Date);
            if (duplicateTime.Success && duplicateTime.Data != null)
            {
                return(Result <object> .Failed(Error.WithData(1000,
                                                              new[] { "Sorry , there are no available representative in the requested time" })));
            }

            Guid?invoiceId = null;
            if (model.Duration > 20)
            {
                invoiceId = (await _invoiceBiz.Add(new CreateInvoiceModel
                {
                    Amount = 200,
                    Description = @"1xVIP appointment $200 
Sub total = $200
Taxes = $10
Total = $210",
                    Status = InvoiceStatus.Pending,
                    Title = "VIP Appointment",
                    Enabled = true,
                    UserId = model.UserId
                })).Data;
            }

            var appointment = new Appointment
            {
                Id               = Guid.NewGuid(),
                InvoiceId        = invoiceId,
                Description      = model.Description,
                CreationDate     = DateTime.Now,
                Date             = model.Date,
                Duration         = model.Duration,
                Type             = model.Type,
                UserId           = !isAdmin ? generalDataService.User.Id : user.Id,
                RepresentativeId = isAdmin ? generalDataService.User.Id : Guid.Empty,
                Title            = model.Title,
                Approved         = true
            };

            _repository.Add(appointment);

            var usersurvey = await _repository.ListAsync <UserSurvey>(us => us.UserId == generalDataService.User.Id);
            if (usersurvey.Success && usersurvey.Data != null && usersurvey.Data.Any())
            {
                var userSurveyIds = usersurvey.Data.Select(us => us.Id).ToList();
                var taxes         = await _repository.ListAsync <Tax>(t =>
                                                                      t.UserSurveyId != null && userSurveyIds.Contains(t.UserSurveyId.Value) &&
                                                                      t.Status == (int)TaxStatus.SetConsultation);

                if (taxes.Success && taxes.Data != null && taxes.Data.Any())
                {
                    taxes.Data.ToList().ForEach(t => t.Status = (byte)TaxStatus.PendingConsultation);
                }
            }

            await _repository.CommitAsync();


            if (appointment.InvoiceId == null)
            {
                _coreSmtpClient.SendComplimentryConsultation(user.Email,
                                                             user.Firstname + " " + user.Lastname, appointment.Date.ToString("F"));


                await _messageBiz.Add(new CreateMessageModel
                {
                    Title = "Your Appointment",
                    Body  =
                        $"Your appointment has been confirmed. One of our AccNet representatives will reach out to you at the scheduled time : {appointment.Date.ToString("F")} (PACIFIC STANDARD TIME)",
                    ToUser   = user.Id,
                    Priority = MessagePriority.High
                });

                _coreSmtpClient.SendAppointmentBookNotif("*****@*****.**",
                                                         user.Firstname + " " + user.Lastname,
                                                         appointment.Date.ToString("F"));
            }

            return(Result <object> .Successful(new
            {
                message = invoiceId.HasValue
                        ? $"To confirm your VIP consultation on {appointment.Date.ToString("F")} (PACIFIC STANDARD TIME), please click OK to kindly pay the fee for this call. Once paid you will receive a confirmation email detailing the selected date and time. Thank you"
                        : $"Your appointment has been confirmed. One of our AccNet representatives will reach out to you at the scheduled time : {appointment.Date.ToString("F")} (PACIFIC STANDARD TIME)",
                vip = invoiceId.HasValue
            }));
        });