SHA256
StayExport Tracking - StayEntry edit
StayEntries now can be edited: - Implemented required entityService method. - Added UI for edit mode in StayEntry details page.
This commit is contained in:
1 parent
72cd11caf2
commit
b515f18ca9
5 files changed
+223
-48
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using FluentStorage.Utils.Extensions;
|
||||
using FluentValidation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Somnia.GuestLog.Enums.Models;
|
||||
@@ -79,9 +80,91 @@ public class StayEntryEntityService(
|
||||
}
|
||||
}
|
||||
|
||||
public Task<Result> UpdateEntryAsync(StayEntryDTO updatedDto)
|
||||
public async Task<Result> UpdateEntryAsync(StayEntryDTO updatedDto)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var valResult = dtoValidator.Validate(updatedDto);
|
||||
if (!valResult.IsValid)
|
||||
{
|
||||
logger.LogWarning("StayEntryService - UpdateEntryAsync: Dto validation failed. Fix the errors: {errorList}", valResult.Errors);
|
||||
return Result.Fail(ResultCode.FailedValidation);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var dbContext = contextFactory.CreateDbContext();
|
||||
//Get current entity from DB.
|
||||
StayEntry? dbItem = await dbContext.StayEntries
|
||||
.Where(e => e.Id == updatedDto.Id)
|
||||
.Include(e => e.GuestDocuments)
|
||||
.SingleAsync();
|
||||
|
||||
if (dbItem == null)
|
||||
{
|
||||
logger.LogWarning("StayEntryService - UpdateEntryAsync: Error updating StayEntry (ID: '{id}'): StayEntry not found!", updatedDto.Id);
|
||||
return Result.Fail(ResultCode.NotFound);
|
||||
}
|
||||
|
||||
if (dbItem.StayUploadStatus == UploadStatus.Uploaded)
|
||||
{
|
||||
logger.LogWarning("StayEntryService - UpdateEntryAsync: StayEntry (ID: '{id}') can't be updated: StayEntry is already uploaded!", updatedDto.Id);
|
||||
return Result.Fail(ResultCode.DataIsFinal);
|
||||
}
|
||||
|
||||
DateTime updateTime = DateTime.UtcNow;
|
||||
StayEntry updatedItem = new()
|
||||
{
|
||||
Id = dbItem.Id,
|
||||
DateIn = updatedDto.DateIn!.Value.ToUniversalTime(),
|
||||
DateOut = updatedDto.DateOut!.Value.ToUniversalTime(),
|
||||
RoomCount = updatedDto.RoomCount!.Value,
|
||||
RoomNumber = updatedDto.RoomNumber,
|
||||
PaymentMethod = updatedDto.PaymentMethod!,
|
||||
StayUploadStatus = dbItem.StayUploadStatus,
|
||||
StayUploadStatusTime = dbItem.StayUploadStatusTime,
|
||||
CreateTime = dbItem.CreateTime,
|
||||
UpdateTime = updateTime,
|
||||
};
|
||||
dbContext.Entry(dbItem).CurrentValues.SetValues(updatedItem);
|
||||
|
||||
//Update GuestDocuments
|
||||
if (updatedDto.GuestDocuments != null)
|
||||
{
|
||||
var updatedGuestDocIdList = updatedDto.GuestDocuments.Select(e => e.Id);
|
||||
List<GuestDocument> updatedDbGuests = await dbContext.GuestDocuments
|
||||
.Where(e => updatedGuestDocIdList.Contains(e.Id))
|
||||
.ToListAsync();
|
||||
|
||||
foreach (GuestDocument guest in updatedDbGuests)
|
||||
{
|
||||
//Skip check for guests already in the stay.
|
||||
if (dbItem.GuestDocuments.Any(e => e.Id == guest.Id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//Check if Guest is already in other stay during the same period.
|
||||
bool isInOtherStay = await this.IsGuestInStayInPeriod(guest.Id, updatedItem.DateIn, updatedItem.DateOut);
|
||||
if (isInOtherStay)
|
||||
{
|
||||
string period = $"{updatedItem.DateIn}-{updatedItem.DateOut}";
|
||||
return Result.FailWithContext(ResultCode.GuestCurrentlyInStay, guest.FullDocument, period);
|
||||
}
|
||||
}
|
||||
|
||||
dbItem.GuestDocuments.Clear();
|
||||
dbItem.GuestDocuments.AddRange(updatedDbGuests);
|
||||
}
|
||||
|
||||
var result = await dbContext.SaveChangesAsync();
|
||||
logger.LogInformation("StayEntryService - UpdateEntryAsync: StayEntry Updated OK {id}", dbItem.GetFullKey());
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "StayEntryService - UpdateEntryAsync: Error updating StayEntry: '{id}'", updatedDto.Id);
|
||||
return Result.FailUnknownError();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteEntryAsync(StayEntryDTO dto)
|
||||
@@ -260,5 +343,10 @@ public class StayEntryEntityService(
|
||||
return EntityMapping.MapStayEntryEntityToDTO(dbItem, queryConfig.IncludeGuests);
|
||||
}
|
||||
|
||||
private static string? GetValueOrNull(string? dtoValue)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(dtoValue) ? null : dtoValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user