using System.Globalization; namespace GuestOps.Web; public sealed class PmsWork(IStore store,OhipClient ohip,IConfiguration config) { public OhipProfile Profile(string hotel)=>OhipProfile.Read(config,hotel)??throw new PmsInvalid("Your administrator has not configured OHIP for this hotel."); async Task WritesAllowed(string hotel,OhipProfile profile) { if(!profile.WritesEnabled||(await store.Get(hotel,hotel))?.PmsUpdatesEnabled!=true)throw new PmsInvalid("PMS updates are disabled. Enable them only after sandbox acceptance."); } public async Task Lookup(string hotel,string confirmation,CancellationToken ct) { var snapshot=await ohip.Lookup(hotel,Profile(hotel),confirmation,ct);await store.Insert(snapshot);return snapshot; } public async Task Propose(string hotel,string user,PmsSnapshot snapshot,PmsProposeInput input) { var profile=Profile(hotel); if(snapshot.HotelId!=hotel||snapshot.ConnectionRevision!=profile.Revision||snapshot.FetchedAt365)throw new PmsInvalid("Enter valid arrival and departure dates for a stay of at most 365 nights."); if(input.Arrival==snapshot.Arrival&&input.Departure==snapshot.Departure)throw new PmsInvalid("The proposed dates are unchanged."); change.Arrival=input.Arrival;change.Departure=input.Departure; } else { if(!Input.Text(input.Note,1,2000))throw new PmsInvalid("Enter a reservation note of at most 2,000 characters."); change.Note=input.Note.Trim(); } _=OhipClient.Payload(change,profile); if(!await store.TryInsertPmsChange(change))throw new PmsConflict("A change is already being reviewed or needs reconciliation for this reservation. Resolve it in the change history first."); return change; } async Task Save(PmsChange change,string state,string detail) { var version=change.Version;change.Version++;change.State=state;change.Detail=detail;change.UpdatedAt=DateTime.UtcNow; if(!await store.Replace(change.HotelId,change.Id,version,change))throw new PmsConflict("This change was updated elsewhere. Refresh its status."); } public async Task Apply(PmsChange change,long version,string approver,bool priceChecked,CancellationToken requestToken) { if(change.State!="Review"||change.Version!=version)throw new PmsConflict("Only the current reviewed proposal can be applied once."); if(change.ExpiresAt Verify(PmsChange change,long version,CancellationToken ct) { if(change.Version!=version || !(change.State=="NeedsReview" || change.State=="Applying"&&change.UpdatedAt Cancel(PmsChange change,long version) { if(change.State!="Review"||change.Version!=version)throw new PmsConflict("Only a proposal that has not started can be cancelled."); await Save(change,"Cancelled","The proposal was cancelled before any PMS update.");return change; } }