Exemplo n.º 1
0
        public async Task <IActionResult> Save([FromBody] SaveEventCommand cmd)
        {
            Tuple <Event, ErrorViewModel> result = await mediatr.Send(cmd);

            if (result.Item2 != null)
            {
                return(StatusCode(500, result.Item2));
            }

            return(Json(result.Item1));
        }
Exemplo n.º 2
0
        public static void SetData(EventTranslation ev, SaveEventCommand cmd)
        {
            if (ev == null)
            {
                return;
            }

            ev.LanguageId       = cmd.LanguageId;
            ev.Title            = !string.IsNullOrWhiteSpace(cmd.Title) ? cmd.Title.Trim() : ev.Title;
            ev.ShortDescription = !string.IsNullOrWhiteSpace(cmd.ShortDescription) ? cmd.ShortDescription.Trim() : ev.ShortDescription;
            ev.Text             = !string.IsNullOrWhiteSpace(cmd.Text) ? cmd.Text.Trim() : ev.Text;
            ev.PriceDetails     = cmd.PriceDetails != null ? cmd.PriceDetails : ev.PriceDetails;
        }
Exemplo n.º 3
0
        public async Task <IActionResult> RespondToConsultationRequestAsync(ConsultationRequestResponse request)
        {
            var getConferenceByIdQuery = new GetConferenceByIdQuery(request.ConferenceId);
            var conference             = await _queryHandler.Handle <GetConferenceByIdQuery, Conference>(getConferenceByIdQuery);

            if (conference == null)
            {
                _logger.LogWarning("Unable to find conference");
                return(NotFound());
            }

            var requestedBy = conference.GetParticipants().SingleOrDefault(x => x.Id == request.RequestedBy);

            if (request.RequestedBy != Guid.Empty && requestedBy == null)
            {
                _logger.LogWarning("Unable to find participant request by with id {RequestedBy}", request.RequestedBy);
                return(NotFound());
            }

            var requestedFor = conference.GetParticipants().SingleOrDefault(x => x.Id == request.RequestedFor);

            if (requestedFor == null)
            {
                _logger.LogWarning("Unable to find participant request for with id {RequestedFor}", request.RequestedFor);
                return(NotFound());
            }

            if (string.IsNullOrEmpty(request.RoomLabel))
            {
                _logger.LogWarning("Please provide a room label");
                return(NotFound());
            }

            if (request.Answer != ConsultationAnswer.Accepted)
            {
                _logger.LogWarning($"Answered {request.Answer}");
                return(NoContent());
            }

            var displayName = requestedFor.GetParticipantRoom()?.Label ?? requestedFor.DisplayName;
            var command     = new SaveEventCommand(conference.Id, Guid.NewGuid().ToString(), EventType.Consultation,
                                                   DateTime.UtcNow, null, null, $"Adding {displayName} to {request.RoomLabel}", null)
            {
                ParticipantId = request.RequestedBy
            };
            await _commandHandler.Handle(command);

            await _consultationService.ParticipantTransferToRoomAsync(request.ConferenceId, requestedFor.Id, request.RoomLabel);

            return(NoContent());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> HandleConsultationRequestAsync(ConsultationRequest request)
        {
            _logger.LogDebug("HandleConsultationRequest");
            var getConferenceByIdQuery = new GetConferenceByIdQuery(request.ConferenceId);
            var conference             = await _queryHandler.Handle <GetConferenceByIdQuery, Conference>(getConferenceByIdQuery);

            if (conference == null)
            {
                _logger.LogWarning("Unable to find conference");
                return(NotFound());
            }

            var requestedBy = conference.GetParticipants().SingleOrDefault(x => x.Id == request.RequestedBy);

            if (requestedBy == null)
            {
                _logger.LogWarning("Unable to find participant request by with id {RequestedBy}", request.RequestedBy);
                return(NotFound());
            }

            var requestedFor = conference.GetParticipants().SingleOrDefault(x => x.Id == request.RequestedFor);

            if (requestedFor == null)
            {
                _logger.LogWarning("Unable to find participant request for with id {RequestedFor}", request.RequestedFor);
                return(NotFound());
            }

            var command = new SaveEventCommand(conference.Id, Guid.NewGuid().ToString(), EventType.Consultation,
                                               DateTime.UtcNow, null, null, $"Consultation with {requestedFor.DisplayName}", null)
            {
                ParticipantId = requestedBy.Id
            };
            await _commandHandler.Handle(command);

            try
            {
                await InitiateStartConsultationAsync(conference, requestedBy, requestedFor, request.Answer.GetValueOrDefault());
            }
            catch (DomainRuleException ex)
            {
                _logger.LogError(ex, "No consultation room available for conference");
                ModelState.AddModelError("ConsultationRoom", "No consultation room available");
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }
Exemplo n.º 5
0
        public async Task should_set_endpoint_flag_to_true_when_endpoint_event()
        {
            var conference = new ConferenceBuilder()
                             .WithEndpoint("Display1", "*****@*****.**")
                             .WithEndpoint("Display2", "*****@*****.**").Build();

            var seededConference = await TestDataManager.SeedConference(conference);

            TestContext.WriteLine($"New seeded conference id: {seededConference.Id}");
            _newConferenceId = seededConference.Id;

            var externalEventId   = "AutomatedEventTestIdSuccessfulSave";
            var externalTimeStamp = DateTime.UtcNow.AddMinutes(-10);
            var participantId     = seededConference.GetEndpoints().First().Id;
            var reason            = "Automated";
            var eventType         = EventType.EndpointJoined;

            var command = new SaveEventCommand(_newConferenceId, externalEventId, eventType, externalTimeStamp,
                                               null, null, reason, null)
            {
                ParticipantId = participantId
            };
            await _handler.Handle(command);

            Event savedEvent;

            await using (var db = new VideoApiDbContext(VideoBookingsDbContextOptions))
            {
                savedEvent = await db.Events.FirstOrDefaultAsync(x =>
                                                                 x.ExternalEventId == externalEventId && x.ParticipantId == participantId);
            }

            savedEvent.Should().NotBeNull();
            savedEvent.ExternalEventId.Should().Be(externalEventId);
            savedEvent.EventType.Should().Be(eventType);
            savedEvent.ExternalTimestamp.Should().Be(externalTimeStamp);
            savedEvent.TransferredFrom.Should().BeNull();
            savedEvent.TransferredTo.Should().BeNull();
            savedEvent.Reason.Should().Be(reason);
            savedEvent.ParticipantId.Should().Be(participantId);
            savedEvent.EndpointFlag.Should().BeTrue();
        }
Exemplo n.º 6
0
        public async Task Should_save_event_with_room_id()
        {
            var seededConference = await TestDataManager.SeedConference();

            TestContext.WriteLine($"New seeded conference id: {seededConference.Id}");
            _newConferenceId = seededConference.Id;

            var      externalEventId   = "AutomatedEventTestRoomIdIsSaved";
            var      externalTimeStamp = DateTime.UtcNow.AddMinutes(-10);
            var      participantId     = seededConference.GetParticipants().First().Id;
            RoomType?transferredFrom   = RoomType.WaitingRoom;
            RoomType?transferredTo     = RoomType.ConsultationRoom;
            var      reason            = "Automated";
            var      eventType         = EventType.Disconnected;
            var      roomId            = new Random().Next();

            var command = new SaveEventCommand(_newConferenceId, externalEventId, eventType, externalTimeStamp,
                                               transferredFrom, transferredTo, reason, null)
            {
                ParticipantId = participantId, ParticipantRoomId = roomId
            };
            await _handler.Handle(command);

            Event savedEvent;

            await using (var db = new VideoApiDbContext(VideoBookingsDbContextOptions))
            {
                savedEvent = await db.Events.FirstOrDefaultAsync(x =>
                                                                 x.ExternalEventId == externalEventId && x.ParticipantId == participantId);
            }

            savedEvent.Should().NotBeNull();
            savedEvent.ExternalEventId.Should().Be(externalEventId);
            savedEvent.EventType.Should().Be(eventType);
            savedEvent.ExternalTimestamp.Should().Be(externalTimeStamp);
            savedEvent.TransferredFrom.Should().Be(transferredFrom);
            savedEvent.TransferredTo.Should().Be(transferredTo);
            savedEvent.Reason.Should().Be(reason);
            savedEvent.EndpointFlag.Should().BeFalse();
            savedEvent.Phone.Should().Be(command.Phone);
            savedEvent.ParticipantRoomId.Should().Be(command.ParticipantRoomId);
        }
        public static SaveEventCommand MapEventRequestToEventCommand(Guid conferenceId, ConferenceEventRequest request)
        {
            GetRoomTypeEnums(request, out var transferTo, out var transferFrom);
            var command = new SaveEventCommand(conferenceId, request.EventId, request.EventType.MapToDomainEnum(),
                                               request.TimeStampUtc, transferFrom, transferTo, request.Reason, request.Phone);

            if (Guid.TryParse(request.ParticipantId, out var participantId))
            {
                command.ParticipantId = participantId;
            }

            command.TransferredFromRoomLabel = request.TransferFrom;
            command.TransferredToRoomLabel   = request.TransferTo;

            if (long.TryParse(request.ParticipantRoomId, out var roomParticipantId))
            {
                command.ParticipantRoomId = roomParticipantId;
            }

            return(command);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> PostEventAsync(ConferenceEventRequest request)
        {
            Guid.TryParse(request.ConferenceId, out var conferenceId);

            var command = new SaveEventCommand(conferenceId, request.EventId, request.EventType,
                                               request.TimeStampUtc, request.TransferFrom, request.TransferTo, request.Reason, request.Phone);

            if (Guid.TryParse(request.ParticipantId, out var participantId))
            {
                command.ParticipantId = participantId;
            }

            _logger.LogWarning("Handling {ConferenceEventRequest}", nameof(ConferenceEventRequest));

            await _commandHandler.Handle(command);

            if (request.ShouldSkipEventHandler())
            {
                _logger.LogDebug("Handling CallbackEvent skipped due to result of ShouldHandleEvent.");
                return(NoContent());
            }

            var callbackEvent = new CallbackEvent
            {
                EventId       = request.EventId,
                EventType     = request.EventType,
                ConferenceId  = conferenceId,
                Reason        = request.Reason,
                TransferTo    = request.TransferTo,
                TransferFrom  = request.TransferFrom,
                TimeStampUtc  = request.TimeStampUtc,
                ParticipantId = participantId,
                Phone         = request.Phone
            };
            await _eventHandlerFactory.Get(request.EventType).HandleAsync(callbackEvent);

            return(NoContent());
        }
Exemplo n.º 9
0
        public static void SetData(Event ev, SaveEventCommand cmd)
        {
            if (ev == null)
            {
                return;
            }

            ev.Name         = !string.IsNullOrWhiteSpace(cmd.Name) ? cmd.Name.Trim() : ev.Name;
            ev.DateTimeFrom = cmd.DateTimeFrom;
            ev.DateTimeTo   = cmd.DateTimeTo != null ? cmd.DateTimeTo : ev.DateTimeTo;
            ev.Website      = !string.IsNullOrWhiteSpace(cmd.Website) ? cmd.Website.Trim() : ev.Website;
            ev.Address      = !string.IsNullOrWhiteSpace(cmd.Address) ? cmd.Address.Trim() : ev.Address;
            ev.ImageId      = cmd.ImageId != null ? cmd.ImageId : ev.ImageId;

            if (cmd.Price != null)
            {
                double priceParced = Double.Parse(cmd.Price.Replace(',', '.'), CultureInfo.InvariantCulture);
                ev.Price = Math.Round(priceParced, 2);
            }
            else
            {
                ev.Price = 0;
            }
        }
Exemplo n.º 10
0
        public async Task <Tuple <Event, ErrorViewModel> > Handle(SaveEventCommand cmd, CancellationToken cancellationToken)
        {
            Tuple <Event, ErrorViewModel> result    = null;
            Event            resultEvent            = null;
            EventTranslation resultEventTranslation = null;

            if (!cmd.EventId.HasValue)
            {
                resultEvent = new Event();
                EventDataHandler.SetData(resultEvent, cmd);
                db.Events.Add(resultEvent);

                resultEventTranslation = new EventTranslation();
                EventTranslationDataHandler.SetData(resultEventTranslation, cmd);
                resultEventTranslation.Event = resultEvent;
                db.EventTranslations.Add(resultEventTranslation);
            }
            else
            {
                resultEvent = db.Events.Where(e => e.EventId == cmd.EventId).FirstOrDefault();

                if (resultEvent == null)
                {
                    logger.LogError("Existing event was not found when tried to update it. EventId: " + cmd.EventId);
                    result = new Tuple <Event, ErrorViewModel>(null, new ErrorViewModel()
                    {
                        ErrorCode = (int)AppError.DbRecordNotFound
                    });
                    return(result);
                }

                EventDataHandler.SetData(resultEvent, cmd);
                db.Events.Update(resultEvent);

                if (!cmd.EventTranslationId.HasValue)
                {
                    resultEventTranslation = new EventTranslation();
                    EventTranslationDataHandler.SetData(resultEventTranslation, cmd);
                    resultEventTranslation.Event = resultEvent;
                    db.EventTranslations.Add(resultEventTranslation);
                }
                else
                {
                    resultEventTranslation = db.EventTranslations.Where(x => x.EventTranslationId == cmd.EventTranslationId).FirstOrDefault();

                    if (resultEventTranslation == null)
                    {
                        logger.LogError("Existing event translation was not found when trying to udpate it. EventTranslationId: " + cmd.EventTranslationId);
                        result = new Tuple <Event, ErrorViewModel>(null, new ErrorViewModel()
                        {
                            ErrorCode = (int)AppError.DbRecordNotFound
                        });
                        return(null);
                    }

                    EventTranslationDataHandler.SetData(resultEventTranslation, cmd);
                    db.EventTranslations.Update(resultEventTranslation);
                }
            }

            await db.SaveChangesAsync();

            result = new Tuple <Event, ErrorViewModel>(resultEvent, null);
            return(result);
        }
Exemplo n.º 11
0
        public async Task Save()
        {
            var logger = testConfig.GetLoggerFactory().CreateLogger <EventSaveHandler>();

            EventSaveHandler eventSaveHandler = new EventSaveHandler(testConfig.GetDbContext(), logger);

            SaveEventCommand newEventcmd = new SaveEventCommand()
            {
                EventId            = null,
                EventTranslationId = null,
                Name             = "New event",
                DateTimeFrom     = DateTime.UtcNow,
                Website          = "www",
                Address          = "address",
                Price            = "100.0",
                Title            = "New title",
                Text             = "Text",
                ShortDescription = "New short descr",
                LanguageId       = 1
            };

            Tuple <Event, ErrorViewModel> addNewEventResult = await eventSaveHandler.Handle(newEventcmd, new CancellationToken());

            Assert.AreEqual("New event", addNewEventResult.Item1.Name);
            Assert.AreEqual(100, addNewEventResult.Item1.Price);
            Assert.AreEqual("New title", addNewEventResult.Item1.EventTranslations.First().Title);

            SaveEventCommand updateEventNewTranslationcmd = new SaveEventCommand()
            {
                EventId            = 3,
                EventTranslationId = null,
                Name             = "Update event",
                DateTimeFrom     = DateTime.UtcNow,
                Website          = "www",
                Address          = "address",
                Price            = "200,5",
                LanguageId       = 1,
                Title            = "New title",
                Text             = "Text",
                ShortDescription = "New short descr"
            };

            Tuple <Event, ErrorViewModel> updateEventNewTranslationResult = await eventSaveHandler.Handle(updateEventNewTranslationcmd, new CancellationToken());

            Assert.AreEqual("Update event", updateEventNewTranslationResult.Item1.Name);
            Assert.AreEqual(200.5, updateEventNewTranslationResult.Item1.Price);
            Assert.AreEqual("New title", updateEventNewTranslationResult.Item1.EventTranslations.First().Title);
            Assert.AreEqual(1, updateEventNewTranslationResult.Item1.EventTranslations.Count);

            SaveEventCommand updateEventWithExistingTranslationCmd = new SaveEventCommand()
            {
                EventId            = 3,
                EventTranslationId = updateEventNewTranslationResult.Item1.EventTranslations.First().EventTranslationId,
                Name             = "Update event",
                DateTimeFrom     = DateTime.UtcNow,
                Website          = "www",
                Address          = "address",
                Price            = "200,65",
                LanguageId       = 1,
                Title            = "Update title",
                Text             = "Update Text",
                ShortDescription = "New short descr"
            };

            Tuple <Event, ErrorViewModel> updateEventWithExistingTranslationResult = await eventSaveHandler.Handle(updateEventWithExistingTranslationCmd, new CancellationToken());

            Assert.AreEqual("Update event", updateEventWithExistingTranslationResult.Item1.Name);
            Assert.AreEqual(200.65, updateEventWithExistingTranslationResult.Item1.Price);
            Assert.AreEqual("Update title", updateEventWithExistingTranslationResult.Item1.EventTranslations.First().Title);
            Assert.AreEqual(1, updateEventWithExistingTranslationResult.Item1.EventTranslations.Count);

            SaveEventCommand updateNotExistingEventCmd = new SaveEventCommand()
            {
                EventId          = 100,
                Name             = "Update event",
                DateTimeFrom     = DateTime.UtcNow,
                Address          = "address",
                Price            = "200",
                LanguageId       = 1,
                Title            = "Update title",
                Text             = "Update Text",
                ShortDescription = "New short descr"
            };

            Tuple <Event, ErrorViewModel> updateNotExistingEventResult = await eventSaveHandler.Handle(updateNotExistingEventCmd, new CancellationToken());

            Assert.AreEqual((int)AppError.DbRecordNotFound, updateNotExistingEventResult.Item2.ErrorCode);
        }