Exemplo n.º 1
0
        public async Task <int> ConfirmBooking([FromBody] BookingRequest request)
        {
            try
            {
                //if (id < 100000 || id > 1000000) throw new ValidationException(nameof(id));
                if (request == null || !request.Participants.Any())
                {
                    throw new ArgumentNullException(nameof(request));
                }

                //var jsonBookingRequest = JsonConvert.SerializeObject(request);

                var bookingId = await _bookingService.AddBooking(request);

                var listingDetail = _listingService.GetListingViewById(request.ListingId).Result;

                var bookingParticipants = request.Participants.Select(item => new BookingParticipant
                {
                    Fullname         = $"{item.FirstName} {item.LastName}",
                    DateOfBirth      = item.Birthday.ToString(),
                    Email            = item.Email,
                    Phone            = item.Phone,
                    EmergencyContact = item.EmergencyContact
                }).ToList();

                var listingUrl       = $"{Request.Headers["Access-Control-Allow-Origin"]}/listing/{StringHelper.SeorizeListingName(listingDetail.Header, listingDetail.Id)}";
                var bookingUrl       = $"{Request.Headers["Access-Control-Allow-Origin"]}/booking/{StringHelper.SeorizeListingName(listingDetail.Header, bookingId)}";
                var manageBookingUrl = $"{Request.Headers["Access-Control-Allow-Origin"]}/booking/manage/{StringHelper.SeorizeListingName(listingDetail.Header, listingDetail.Id)}";;

                var bookingEmailViewModel = new BookingEmailViewModel
                {
                    ListingId           = listingDetail.Id,
                    ListingUrl          = listingUrl,
                    BookingUrl          = bookingUrl,
                    ManageBookingUrl    = manageBookingUrl,
                    MainImageUrl        = _imageStorageService.GetCloudinaryImageUrl(ImageType.Listing, listingDetail.ImageUrls.Split(";").FirstOrDefault()),
                    ListingHeader       = listingDetail.Header,
                    ListingDescription  = listingDetail.Description,
                    BookingDate         = request.BookingDate.ToString("dddd dd-MMM-yyyy", CultureInfo.DefaultThreadCurrentUICulture),
                    BookingTime         = request.Time.ToString(@"hh\:mm", CultureInfo.DefaultThreadCurrentUICulture),
                    BookingParticipants = bookingParticipants
                };

                return(await _emailService.SendBookingConfirmEmail(bookingEmailViewModel, listingDetail.OwnerEmail, listingDetail.OwnerEmail));

                //return ;
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <int> UpdateBooking(int bookingId, [FromBody] BookingRequest request)
        {
            try
            {
                //if (id < 100000 || id > 1000000) throw new ValidationException(nameof(id));
                if (request == null || !request.Participants.Any())
                {
                    throw new ArgumentNullException(nameof(request));
                }

                var booking = await _bookingService.GetBooking(bookingId);

                if (!(await _authorizationService.AuthorizeAsync(User, booking, Operations.Update)).Succeeded)
                {
                    throw new UnauthorizedAccessException();
                }

                await _bookingService.UpdateBooking(bookingId, request);

                var listingDetail = _listingService.GetListingViewById(request.ListingId).Result;

                var bookingParticipants = request.Participants.Select(item => new BookingParticipant
                {
                    Fullname    = $"{item.FirstName} {item.LastName}",
                    DateOfBirth = item.Birthday.ToString(),
                    Email       = item.Email,
                    Phone       = item.Phone
                }).ToList();

                var bookingEmailViewModel = new BookingEmailViewModel
                {
                    ListingId           = listingDetail.Id,
                    ListingUrl          = StringHelper.SeorizeListingName(listingDetail.Header, listingDetail.Id),
                    ListingHeader       = listingDetail.Header,
                    ListingDescription  = listingDetail.Description,
                    BookingDate         = request.BookingDate.ToString("dddd dd-MMM-yyyy", CultureInfo.DefaultThreadCurrentUICulture),
                    BookingTime         = request.Time.ToString(@"hh\:mm", CultureInfo.DefaultThreadCurrentUICulture),
                    BookingParticipants = bookingParticipants
                };

                return(await _emailService.SendBookingConfirmEmail(bookingEmailViewModel, listingDetail.OwnerEmail, listingDetail.OwnerEmail));

                //return ;
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                throw;
            }
        }
        // GET: Bookings/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var userdb = new ApplicationDbContext();
            //Create a new bookingEmailViewModel
            BookingEmailViewModel bevm = new BookingEmailViewModel();

            bevm.Booking = db.Bookings.Find(id);
            //find the user email according to the user id in the booking record.
            bevm.User = userdb.Users.Find(bevm.Booking.UserId);
            //Booking booking = db.Bookings.Find(id);
            if (bevm == null)
            {
                return(HttpNotFound());
            }
            return(View(bevm));
        }
Exemplo n.º 4
0
        public async Task <int> SendBookingConfirmEmail(BookingEmailViewModel emailViewModel, string hostEmail, string hostName)
        {
            //var engine = EngineFactory.CreatePhysical(_appSettings.EmailTemplateFolder);
            var engine = EngineFactory.CreatePhysical($"{Directory.GetCurrentDirectory()}\\Content\\EmailTemplates");

            emailViewModel.EmailHeader = $"Hi, thanks for your booking";
            var emailContentForGuests = engine.Parse(_appSettings.BookingGuestsEmailTemplate, emailViewModel);

            emailViewModel.EmailHeader = $"Hi, you have a new booking";
            emailViewModel.IsHost      = true;
            var emailContentForHost = engine.Parse(_appSettings.BookingGuestsEmailTemplate, emailViewModel);

            var emailLogs = new List <EmailLog>();

            var particiants = string.Join(",", emailViewModel.BookingParticipants.Select(x => x.Email));

            if (_appSettings.Environment != "prod")
            {
                particiants = "*****@*****.**";

                emailLogs.Add(new EmailLog
                {
                    ListingId   = emailViewModel.ListingId,
                    FromAddress = _appSettings.DefaultEmailAddress,
                    ToAddress   = string.Join(",", particiants),
                    Subject     = _appSettings.BookingEmailSubject,
                    Content     = emailContentForGuests,
                    CreatedDate = DateTime.Now,
                    //MessageId = x.Result.MessageID,
                    //TransactionId = x.Result.TransactionID,
                    Status = true
                });

                emailLogs.Add(new EmailLog
                {
                    ListingId   = emailViewModel.ListingId,
                    FromAddress = _appSettings.DefaultEmailAddress,
                    ToAddress   = hostEmail,
                    Subject     = _appSettings.BookingEmailSubject,
                    Content     = emailContentForHost,
                    CreatedDate = DateTime.Now,
                    //MessageId = x.Result.MessageID,
                    //TransactionId = x.Result.TransactionID,
                    Status = true
                });
            }
            else
            {
                var sendList = new List <Task>
                {
                    ElasticEmailClient.Send(_appSettings.BookingEmailSubject, _appSettings.DefaultEmailAddress, _appSettings.DefaultEmailName, to: particiants, bodyHtml: emailContentForGuests, isTransactional: true)
                    .ContinueWith(x => emailLogs.Add(new EmailLog
                    {
                        ListingId     = emailViewModel.ListingId,
                        FromAddress   = _appSettings.DefaultEmailAddress,
                        ToAddress     = string.Join(",", particiants),
                        Subject       = _appSettings.BookingEmailSubject,
                        Content       = emailContentForGuests,
                        CreatedDate   = DateTime.Now,
                        MessageId     = x.Result.MessageID,
                        TransactionId = x.Result.TransactionID,
                        Status        = true
                    })),
                    ElasticEmailClient.Send(_appSettings.BookingEmailSubject, _appSettings.DefaultEmailAddress, _appSettings.DefaultEmailName, to: hostEmail, bodyHtml: emailContentForHost, isTransactional: true)
                    .ContinueWith(x => emailLogs.Add(new EmailLog
                    {
                        ListingId     = emailViewModel.ListingId,
                        FromAddress   = _appSettings.DefaultEmailAddress,
                        ToAddress     = hostEmail,
                        Subject       = _appSettings.BookingEmailSubject,
                        Content       = emailContentForHost,
                        CreatedDate   = DateTime.Now,
                        MessageId     = x.Result.MessageID,
                        TransactionId = x.Result.TransactionID,
                        Status        = true
                    }))
                };

                await Task.WhenAll(sendList);
            }

            return(await _emailLogRepository.LogEmail(emailLogs));

            //return sendList.Select(x => x.Result).ToList();
        }