public async Task <IActionResult> Put(WorkScheduleDetail scheduleDetail) { var user = await GetCurrentUser(); scheduleDetail.OrganiztionId = user.OrganizationId; var response = await _sessionService.UpdateWorkScheduleAsync(scheduleDetail, user.Id); if (!response.IsSuccess) { return(BadRequest(scheduleDetail)); } return(Ok(response)); }
public async Task <IActionResult> Post(WorkScheduleDetail scheduleDetail) { if (!ModelState.IsValid) { return(ModelError()); } var user = await GetCurrentUser(); scheduleDetail.OrganiztionId = user.OrganizationId; var response = await _sessionService.CreateWorkScheduleAsync(scheduleDetail, user.Id); if (!response.IsSuccess) { return(BadRequest(response)); } return(Ok(response)); }
public async Task <EntityApiResponse <WorkScheduleDetail> > CreateWorkScheduleAsync(WorkScheduleDetail scheduleDetail, string currentUserId) { if (scheduleDetail is null) { throw new ArgumentNullException(nameof(scheduleDetail)); } if (scheduleDetail.Sessions is null || scheduleDetail.Sessions.Count < 1) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Sessions can't be empty")); } // if there exists a session with startDate DOW different that endDate DOW if (scheduleDetail.Sessions.Any(s => s.StartDate.DayOfWeek != s.EndDate.DayOfWeek)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Sessions start and end date should be on the same day")); } // Check if there is a session with end date earlier than start date if (scheduleDetail.Sessions.Any(s => s.EndDate <= s.StartDate)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Schedule has incorrect time ranges")); } var tomorrowDate = DateTime.UtcNow.AddDays(1).NormalizedDate(); if (scheduleDetail.Sessions.Any(s => s.StartDate < tomorrowDate)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Sessions can only be added for tomorrow and beyond")); } if (scheduleDetail.Sessions.Any(s => s.User is null)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "A session doesn't have a user specified for it")); } var sessionsByDayOfWeek = scheduleDetail.Sessions.GroupBy(s => s.StartDate.DayOfWeek); // Check if any session has conflicts wit any other session if (HasTimeConflicts(sessionsByDayOfWeek)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Schedule has some time conflicts")); } var org = await _orgRepository.GetByIdAsync(scheduleDetail.OrganiztionId); if (org is null) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Organization does not exist")); } var usersIdsRetrieved = new List <string>(); var sessionsAdded = new Session[scheduleDetail.Sessions.Count]; int i = 0; foreach (var sessionDetail in scheduleDetail.Sessions) { if (!usersIdsRetrieved.Contains(sessionDetail.User.Id)) { var user = await _userManager.FindByIdAsync(sessionDetail.User.Id); if (user is null) { return(new EntityApiResponse <WorkScheduleDetail> (error: $"Session with name: {sessionDetail.Name} has a selected user that does not exist")); } usersIdsRetrieved.Add(user.Id); } var session = new Session { Name = sessionDetail.Name?.Trim(), Description = sessionDetail.Description?.Trim(), StartDate = sessionDetail.StartDate.ToUniversalTime(), EndDate = sessionDetail.EndDate.ToUniversalTime(), CreatedById = currentUserId, ModifiedById = currentUserId, OrganizationId = org.Id, UserId = sessionDetail.User.Id }; await _sessionRepository.InsertAsync(session); sessionsAdded[i] = session; i++; } var endDate = sessionsAdded.Max(s => s.EndDate); return(new EntityApiResponse <WorkScheduleDetail>(entity: new WorkScheduleDetail(sessionsAdded, tomorrowDate, endDate))); }
public async Task <EntityApiResponse <WorkScheduleDetail> > UpdateWorkScheduleAsync(WorkScheduleDetail scheduleDetail, string currentUserId) { if (scheduleDetail is null) { throw new ArgumentNullException(nameof(scheduleDetail)); } if (scheduleDetail.Sessions is null || scheduleDetail.Sessions.Count < 1) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Sessions can't be empty")); } // if there exists a session with startDate DOW different that endDate DOW if (scheduleDetail.Sessions.Any(s => s.StartDate.DayOfWeek != s.EndDate.DayOfWeek)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Sessions start and end date should be on the same day")); } // Check if there is a session with end date earlier than start date if (scheduleDetail.Sessions.Any(s => s.EndDate <= s.StartDate)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Schedule has incorrect time ranges")); } var tomorrorwDate = DateTime.UtcNow.AddDays(1).NormalizedDate(); if (scheduleDetail.Sessions.Any(s => s.StartDate < tomorrorwDate)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Can't update sessions earlier than tomorrow")); } if (scheduleDetail.Sessions.Any(s => s.User is null)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "A session doesn't have a user specified for it")); } var newSessionsByDayOfWeek = scheduleDetail.Sessions.GroupBy(s => s.StartDate.DayOfWeek); // Check if any session has conflicts wit any other session if (HasTimeConflicts(newSessionsByDayOfWeek)) { return(new EntityApiResponse <WorkScheduleDetail>(error: "Schedule has some time conflicts")); } var endDate = scheduleDetail.Sessions.Max(s => s.EndDate); var oldSessions = await SessionsBetweenDates(tomorrorwDate, endDate, scheduleDetail.OrganiztionId); // Get from the old session the ones where they do not exist in the new sessions // Those would be the ones to delete var sessionsToDelete = oldSessions.Where(session => !scheduleDetail.Sessions.Any(sessionDetail => sessionDetail.Id == session.Id)); var usersIdsRetrieved = new List <string>(); var sessionsToReturn = new Session[scheduleDetail.Sessions.Count]; int i = 0; foreach (var sessionDetail in scheduleDetail.Sessions) { if (!usersIdsRetrieved.Contains(sessionDetail.User.Id)) { var user = await _userManager.FindByIdAsync(sessionDetail.User.Id); if (user is null) { return(new EntityApiResponse <WorkScheduleDetail> (error: $"Session with name: {sessionDetail.Name} has a selected user that does not exist")); } usersIdsRetrieved.Add(user.Id); } Session session; // If the id is null then it is a new session to add if (string.IsNullOrEmpty(sessionDetail.Id)) { session = new Session { Name = sessionDetail.Name?.Trim(), Description = sessionDetail.Description?.Trim(), StartDate = sessionDetail.StartDate.ToUniversalTime(), EndDate = sessionDetail.EndDate.ToUniversalTime(), CreatedById = currentUserId, ModifiedById = currentUserId, OrganizationId = scheduleDetail.OrganiztionId, UserId = sessionDetail.User.Id }; await _sessionRepository.InsertAsync(session); } // It is an already added session that needs to be updated else { session = oldSessions.FirstOrDefault(s => s.Id == sessionDetail.Id); if (session is null) { continue; } session.StartDate = sessionDetail.StartDate.ToUniversalTime(); session.EndDate = sessionDetail.EndDate.ToUniversalTime(); session.Name = sessionDetail.Name?.Trim(); session.Description = sessionDetail.Description.Trim(); session.UserId = sessionDetail.User.Id; session.LastModifiedDate = DateTime.UtcNow; session.ModifiedById = currentUserId; await _sessionRepository.UpdateAsync(session); } sessionsToReturn[i] = session; i++; } // Delete the sessions that have been excluded from the schedule await _sessionRepository.DeleteAsync(sessionsToDelete); return(new EntityApiResponse <WorkScheduleDetail>(entity: new WorkScheduleDetail(sessionsToReturn, tomorrorwDate, endDate))); }