public async Task CopyEventPropertyValuesToTheNewEvent()
        {
            var duplicateEventModel = new DuplicateEventViewModel()
            {
                Id = EVENT_TO_DUPLICATE_ID,
                Name = "Name",
                Description = "Description",
                StartDateTime = new DateTimeOffset(2016, 1, 1, 0, 0, 0, new TimeSpan()),
                EndDateTime = new DateTimeOffset(2016, 1, 31, 0, 0, 0, new TimeSpan())
            };

            var eventId = await DuplicateEvent(duplicateEventModel);
            var sut = await GetEvent(eventId);

            Assert.Equal(1, sut.CampaignId);
            Assert.Equal(1, sut.Campaign.Id);
            Assert.Equal("Name", sut.Name);
            Assert.Equal("Description", sut.Description);
            Assert.Equal(EventType.Itinerary, sut.EventType);
            Assert.Equal(10, sut.NumberOfVolunteersRequired);
            Assert.Equal(new DateTimeOffset(2016, 1, 1, 0, 0, 0, new TimeSpan()), sut.StartDateTime);
            Assert.Equal(new DateTimeOffset(2016, 1, 31, 0, 0, 0, new TimeSpan()), sut.EndDateTime);
            Assert.Equal("Organizer", sut.Organizer.Id);
            Assert.Equal("ImageUrl", sut.ImageUrl);
            Assert.Equal(false, sut.IsLimitVolunteers);
            Assert.Equal(true, sut.IsAllowWaitList);
        }
 private static EventEditViewModel BuildNewEventDetailsModel(EventEditViewModel existingEvent, DuplicateEventViewModel newEventDetails)
 {
     existingEvent.Id = 0;
     existingEvent.Name = newEventDetails.Name;
     existingEvent.Description = newEventDetails.Description;
     existingEvent.StartDateTime = newEventDetails.StartDateTime;
     existingEvent.EndDateTime = newEventDetails.EndDateTime;
     return existingEvent;
 }
        public async Task<IActionResult> Duplicate(DuplicateEventViewModel viewModel)
        {
            if (viewModel == null)
            {
                return BadRequest();
            }

            if (!viewModel.UserIsOrgAdmin)
            {
                return Unauthorized();
            }

            var existingEvent = await _mediator.SendAsync(new EventEditQuery { EventId = viewModel.Id });
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = existingEvent.CampaignId });
            var newEvent = BuildNewEventDetailsModel(existingEvent, viewModel);

            //mgmccarthy: why are we validating here? We don't need to validate as the event that is being duplicated was already validated before it was created
            var errors = _eventEditViewModelValidator.Validate(newEvent, campaign);
            errors.ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                var id = await _mediator.SendAsync(new DuplicateEventCommand { DuplicateEventModel = viewModel });
                return RedirectToAction(nameof(Details), new { area = "Admin", id });
            }

            return View(viewModel);
        }
        public async Task MaintainOffsetBetweenTaskStartTimeAndEventStartTimeInNewTask()
        {
            var duplicateEventModel = new DuplicateEventViewModel()
            {
                Id = EVENT_TO_DUPLICATE_ID,
                Name = "Name",
                Description = "Description",
                StartDateTime = new DateTimeOffset(2016, 2, 1, 0, 0, 0, new TimeSpan()),
                EndDateTime = new DateTimeOffset(2016, 2, 28, 0, 0, 0, new TimeSpan())
            };

            var eventId = await DuplicateEvent(duplicateEventModel);
            var @event = await GetEvent(eventId);
            var sut = @event.Tasks.OrderBy(t => t.StartDateTime).ToList();

            Assert.Equal(new DateTimeOffset(2016, 2, 1, 9, 0, 0, new TimeSpan()), sut[0].StartDateTime);
            Assert.Equal(new DateTimeOffset(2016, 2, 2, 10, 0, 0, new TimeSpan()), sut[1].StartDateTime);
        }
 async Task<int> DuplicateEvent(DuplicateEventViewModel duplicateEventModel)
 {
     var command = new DuplicateEventCommand() { DuplicateEventModel = duplicateEventModel };
     var handler = new DuplicateEventCommandHandler(Context);
     return await handler.Handle(command);
 }
        public async Task CreateNewTasksWithoutCopyingAssignedVolunteers()
        {
            var duplicateEventModel = new DuplicateEventViewModel()
            {
                Id = EVENT_TO_DUPLICATE_ID,
                Name = "Name",
                Description = "Description",
                StartDateTime = new DateTimeOffset(2016, 2, 1, 0, 0, 0, new TimeSpan()),
                EndDateTime = new DateTimeOffset(2016, 2, 28, 0, 0, 0, new TimeSpan())
            };

            var eventId = await DuplicateEvent(duplicateEventModel);
            var @event = await GetEvent(eventId);
            var sut = @event.Tasks.OrderBy(t => t.StartDateTime).ToList();

            Assert.Equal(0, sut[0].AssignedVolunteers.Count());
            Assert.Equal(0, sut[1].AssignedVolunteers.Count());
        }
        public async Task<IActionResult> Duplicate(DuplicateEventViewModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }
        
            var organizationId = await _mediator.SendAsync(new ManagingOrganizationIdByEventIdQuery { EventId = model.Id });
            if (!User.IsOrganizationAdmin(organizationId))
                return Unauthorized();

            var existingEvent = await _mediator.SendAsync(new EventEditQuery { EventId = model.Id });
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = existingEvent.CampaignId });
            var newEvent = BuildNewEventDetailsModel(existingEvent, model);

            var errors = _eventDetailModelValidator.Validate(newEvent, campaign);
            errors.ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                var id = await _mediator.SendAsync(new DuplicateEventCommand { DuplicateEventModel = model });
                return RedirectToAction(nameof(Details), new { area = "Admin", id });
            }

            return View(model);
        }