public async Task <Approval> UpsertVacationApproval( CalendarEvent @event, DateTimeOffset timestamp, string approvedBy) { this.logger.Debug($"Starting to upsert vacation approvals for event with id {@event.EventId} for employee {@event.EmployeeId}"); using (var context = this.contextFactory()) { var existingVacations = await this.GetVacationsInternal( context, @event.EmployeeId, @event.EventId); var existingVacation = existingVacations.FirstOrDefault(); if (existingVacation == null) { throw new Exception($"Vacation with id {@event.EventId} is not found"); } var approvedById = int.Parse(approvedBy); this.EnsureVacationIsNotProcessed(existingVacation); VacationApprovals newApproval = null; var existingApproval = existingVacation.VacationApprovals .FirstOrDefault(va => va.ApproverId == approvedById); if (existingApproval == null) { this.logger.Debug($"New approval created for employee {approvedById} for vacation with id {existingVacation.Id}"); newApproval = new VacationApprovals { TimeStamp = timestamp, ApproverId = approvedById, Status = (int)VacationApprovalStatus.Approved }; existingVacation.VacationApprovals.Add(newApproval); } context.Vacations.Update(existingVacation); await context.SaveChangesAsync(); return(newApproval != null ? new Approval(newApproval.TimeStamp ?? DateTimeOffset.Now, newApproval.ApproverId.ToString()) : null); } }
private Vacations CreateVacationFromCalendarEvent( CalendarEvent @event, DateTimeOffset timestamp, string updatedBy) { var vacation = new Vacations { EmployeeId = int.Parse(@event.EmployeeId), RaisedAt = timestamp, Start = @event.Dates.StartDate.Date, End = @event.Dates.EndDate.Date, Type = (int)VacationType.Regular }; var updatedById = int.Parse(updatedBy); if (@event.Status == VacationStatuses.Cancelled) { var vacationCancellation = new VacationCancellations { CancelledAt = timestamp, CancelledById = updatedById }; vacation.VacationCancellations.Add(vacationCancellation); } else if (@event.Status == VacationStatuses.Rejected) { var rejectedApproval = new VacationApprovals { ApproverId = updatedById, TimeStamp = timestamp, Status = (int)VacationApprovalStatus.Declined }; vacation.VacationApprovals.Add(rejectedApproval); } else if (@event.Status == VacationStatuses.Approved) { var finalApproval = new VacationApprovals { ApproverId = updatedById, TimeStamp = timestamp, Status = (int)VacationApprovalStatus.Approved, IsFinal = true }; vacation.VacationApprovals.Add(finalApproval); } return(vacation); }