public async Task <ActionResult <SendRoomTransferRespone> > SendRoomTransfer(SendRoomTransferRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(await _roomTransferService.SendRequest(request)); }
public async Task <SendRoomTransferRespone> SendRequest(SendRoomTransferRequest request) { var roomTypes = await _paramService.FindAllByParamType(GlobalParams.ParamTypeRoomType); if (!roomTypes.Exists(t => t.ParamId == request.TargetRoomType)) { throw new HttpStatusCodeException(HttpStatusCode.NotFound, "RoomTransferService: Room Type is not found"); } //Find student in database var student = await _studentService.FindById(request.StudentId); if (student.RoomId == null) { throw new HttpStatusCodeException(HttpStatusCode.NotFound, "RoomTransferService: Student doesn't have any room'"); } //Check if there's active room transfer request var transfers = (List <RoomTransferRequestForm>) await _repoWrapper.RoomTransfer.FindAllAsyncWithCondition(r => r.StudentId == request.StudentId); if (transfers != null) { if (transfers.Exists(b => b.Status == RequestStatus.Pending || b.Status == RequestStatus.Approved)) { throw new HttpStatusCodeException(HttpStatusCode.Forbidden, "RoomTransferService: There are already active transfer requests for this account"); } } //Check if contract is active next month var thisTimeNextMonth = DateTime.Now.AddHours(GlobalParams.TimeZone).AddMonths(1); var contracts = (List <Contract>) await _repoWrapper.Contract.FindAllAsyncWithCondition(c => c.StudentId == student.StudentId && c.Status == ContractStatus.Active && thisTimeNextMonth <= c.EndDate); if (contracts == null || !contracts.Any()) { throw new HttpStatusCodeException(HttpStatusCode.Forbidden, "RoomTransferService: Contract isn't active next month"); } var maxDayForApproveRoomTransfer = await _repoWrapper.Param.FindByIdAsync(GlobalParams.MaxDayForApproveRoomTransfer); if (maxDayForApproveRoomTransfer?.Value == null) { throw new HttpStatusCodeException(HttpStatusCode.NotFound, "RoomTransferService: MaxDayForApproveRoomTransfer not found"); } //Create new room transfer from request var result = SendRoomTransferRequest.NewEntityFromRequest(request, maxDayForApproveRoomTransfer.Value.Value); //Create in database result = await _repoWrapper.RoomTransfer.CreateAsync(result); return(new SendRoomTransferRespone() { RoomTransferFormId = result.RoomTransferRequestFormId }); }