GuestDocument Versions - Part 1:

- Changed GuestDocument PK from `DocumentType`+`DocumentNumber` to to `ID`+`Version` for future versioning.
- Added DB field "IsLastVersion" to simplify queries where only last/current version is allowed.
- Updated GuestDocument DB fields type for `CreateTime` & `UpdateTime` to add precision.
- Updated usages of GuestDocument PK to new PK.
- Limit delete of a GuestDocument when it's currently used in a StayEntry.

Note: DB changes require recreating db tables, so all data is lost and migration "rollback" is not allowed.
This commit is contained in:
2026-09-01 15:04:38 +02:00
parent a01eed82e1
commit 887c3d787a
21 changed files with 525 additions and 93 deletions
@@ -46,12 +46,13 @@ public class StayEntryEntityService(
//Check and add GuestDocuments
foreach (GuestDocumentDTO guestDto in dto.GuestDocuments!)
{
//Get it from db.
GuestDocument? dbGuest = await dbContext.GuestDocuments.FindAsync(guestDto.DocumentType, guestDto.DocumentNum)
?? throw new InvalidOperationException($"GuestDocument not found! Id: '{guestDto.FullKey}'");
//Get guest from db.
GuestDocument dbGuest = await dbContext.GuestDocuments
.Where(gd => gd.Id == guestDto.Id && gd.Version == guestDto.Version)
.SingleAsync();
//Check if Guest is already in other stay during the same period.
bool isInOtherStay = await this.IsGuestInStayInPeriod(dbGuest.DocumentType, dbGuest.DocumentNum, dbStay.DateIn, dbStay.DateOut);
bool isInOtherStay = await this.IsGuestInStayInPeriod(dbGuest.Id, dbStay.DateIn, dbStay.DateOut);
if (isInOtherStay)
{
string period = $"{dbStay.DateIn}-{dbStay.DateOut}";
@@ -168,24 +169,34 @@ public class StayEntryEntityService(
}
public async Task<bool> IsGuestInStayInPeriod(string docType, string docNum, DateTime startTime, DateTime endTime)
public async Task<bool> IsGuestInStay(long guestDocId)
{
using var dbContext = contextFactory.CreateDbContext();
bool rs = await dbContext.StayEntries
.AnyAsync(s => s.GuestDocuments.Any(g => g.Id == guestDocId));
return rs;
}
public async Task<bool> IsGuestInStayInPeriod(long guestDocId, DateTime startTime, DateTime endTime)
{
using var dbContext = contextFactory.CreateDbContext();
bool rs = await dbContext.StayEntries
.AnyAsync(s => s.DateIn < endTime && s.DateOut > startTime
&& s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum)
&& s.GuestDocuments.Any(g => g.Id == guestDocId)
);
return rs;
}
public async Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(string docType, string docNum, StayEntryQueryConfig? queryConfig = null)
public async Task<IEnumerable<StayEntryDTO>> GetStaysWithGuest(long guestDocId, StayEntryQueryConfig? queryConfig = null)
{
using var dbContext = contextFactory.CreateDbContext();
var rs = await dbContext.StayEntries
.Where(s => s.GuestDocuments.Any(g => g.DocumentType == docType && g.DocumentNum == docNum))
.Where(s => s.GuestDocuments.Any(g => g.Id == guestDocId))
.AddQueryConfig(queryConfig)
.OrderByDescending(e => e.DateIn)
.ToListAsync();