Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
feat: found a lost document#389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106 src/Application/Borrows/Commands/ReportFoundDocument.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using Application.Common.Exceptions; | ||
| using Application.Common.Interfaces; | ||
| using Application.Common.Models.Dtos.Physical; | ||
| using AutoMapper; | ||
| using Domain.Entities; | ||
| using Domain.Statuses; | ||
| using MediatR; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using NodaTime; | ||
| namespace Application.Borrows.Commands; | ||
| public class ReportFoundDocument | ||
| { | ||
| public record Command : IRequest<BorrowDto> | ||
| { | ||
| public User CurrentUser { get; init; } = null!; | ||
| public Guid BorrowId { get; init; } | ||
| } | ||
| public class CommandHandler : IRequestHandler<Command, BorrowDto> | ||
| { | ||
| private readonly IApplicationDbContext _context; | ||
| private readonly IMapper _mapper; | ||
| private readonly IDateTimeProvider _dateTimeProvider; | ||
| public CommandHandler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider) | ||
| { | ||
| _context = context; | ||
| _mapper = mapper; | ||
| _dateTimeProvider = dateTimeProvider; | ||
| } | ||
| public async Task<BorrowDto> Handle(Command request, CancellationToken cancellationToken) | ||
| { | ||
| var borrowRequest = await _context.Borrows | ||
| .Include(x => x.Borrower) | ||
| .Include(x => x.Document) | ||
| .ThenInclude(x => x.Folder!) | ||
| .ThenInclude(x => x.Locker) | ||
| .ThenInclude(x => x.Room) | ||
| .FirstOrDefaultAsync(x => x.Id == request.BorrowId, cancellationToken); | ||
| if (borrowRequest is null) | ||
| { | ||
| throw new KeyNotFoundException("Borrow request does not exist."); | ||
| } | ||
| var staff = await _context.Staffs | ||
| .Include(x => x.Room) | ||
| .FirstOrDefaultAsync(x => x.Id == request.CurrentUser.Id, cancellationToken); | ||
| if (staff is null) | ||
| { | ||
| throw new KeyNotFoundException("Staff does not exist."); | ||
| } | ||
| if (staff.Room is null) | ||
| { | ||
| throw new ConflictException("Staff does not have a room."); | ||
| } | ||
| // Staff cant report lost documents from other department | ||
| if (staff.Room.Id != borrowRequest.Document.Folder!.Locker.Room.Id) | ||
| { | ||
| throw new ConflictException("Request cannot be checked out due to different room."); | ||
| } | ||
| if (borrowRequest.Document.Status is not DocumentStatus.Lost) | ||
| { | ||
| throw new ConflictException("Document is not lost."); | ||
| } | ||
| if (borrowRequest.Status is not BorrowRequestStatus.Lost) | ||
| { | ||
| throw new ConflictException("Request is not lost."); | ||
| } | ||
| // Get borrows for the lost document which are not processable | ||
| var borrowsForDocument = _context.Borrows | ||
| .Include(x => x.Document) | ||
| .Where( x => x.Id != request.BorrowId | ||
| && x.Document.Id == borrowRequest.Document.Id | ||
| && x.Status == BorrowRequestStatus.NotProcessable); | ||
| var localDateTimeNow = LocalDateTime.FromDateTime(_dateTimeProvider.DateTimeNow); | ||
| foreach (var borrow in borrowsForDocument) | ||
| { | ||
| if (borrow.DueTime < localDateTimeNow) | ||
| { | ||
| borrow.Status = BorrowRequestStatus.Pending; | ||
| } | ||
| } | ||
| borrowRequest.Status = BorrowRequestStatus.Returned; | ||
| borrowRequest.Document.Status = DocumentStatus.Available; | ||
| borrowRequest.ActualReturnTime = localDateTimeNow; | ||
| var result = _context.Borrows.Update(borrowRequest); | ||
| await _context.SaveChangesAsync(cancellationToken); | ||
| return _mapper.Map<BorrowDto>(result.Entity); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we found a lost document which some of it's borrow requests due time is your grandma's birthday?