52 lines
4.0 KiB
C#
52 lines
4.0 KiB
C#
using System.Security.Claims;
|
|
namespace GuestOps.Web;
|
|
|
|
public static class PmsEndpoints
|
|
{
|
|
public static void Map(RouteGroupBuilder api,bool preview)
|
|
{
|
|
var group=api.MapGroup("/pms").RequireRateLimiting("pms");
|
|
group.MapGet("/status",(HttpContext c,IConfiguration config)=>
|
|
{
|
|
var profile=preview?null:OhipProfile.Read(config,Session.Hotel(c));
|
|
return Results.Ok(new{configured=profile!=null,writesConfigured=profile?.WritesEnabled==true,hotelCode=profile?.HotelCode??"",preview});
|
|
});
|
|
group.MapPost("/lookup",async(PmsLookupInput input,HttpContext c,PmsWork work)=>
|
|
{
|
|
if(preview)return Results.BadRequest(new{error="Real PMS connections are unavailable in preview."});
|
|
var snapshot=await work.Lookup(Session.Hotel(c),input.Confirmation,c.RequestAborted);return Results.Ok(snapshot.View());
|
|
});
|
|
group.MapGet("/changes",async(HttpContext c,IStore store)=>Results.Ok((await store.List<PmsChange>(Session.Hotel(c))).OrderByDescending(x=>x.UpdatedAt).Select(x=>x.View())));
|
|
group.MapPost("/changes",async(PmsProposeInput input,HttpContext c,PmsWork work,IStore store)=>
|
|
{
|
|
var snapshot=await store.Get<PmsSnapshot>(Session.Hotel(c),input.SnapshotId);if(snapshot==null)return Results.NotFound();
|
|
if(preview)return Results.BadRequest();
|
|
var change=await work.Propose(Session.Hotel(c),c.User.FindFirstValue(ClaimTypes.NameIdentifier)!,snapshot,input);
|
|
await Session.Audit(store,c,"Prepared a PMS change for review");return Results.Ok(change.View());
|
|
}).RequireAuthorization("Owner");
|
|
group.MapPost("/changes/{id}/apply",async(string id,PmsApproveInput input,HttpContext c,PmsWork work,IStore store)=>
|
|
{
|
|
var change=await store.Get<PmsChange>(Session.Hotel(c),id);if(change==null)return Results.NotFound();if(preview)return Results.BadRequest();
|
|
var result=await work.Apply(change,input.Version,c.User.FindFirstValue(ClaimTypes.NameIdentifier)!,input.AvailabilityAndPriceChecked,c.RequestAborted);
|
|
await Session.Audit(store,c,"PMS change result: "+result.State);return Results.Ok(result.View());
|
|
}).RequireAuthorization("Owner");
|
|
group.MapPost("/changes/{id}/verify",async(string id,VersionInput input,HttpContext c,PmsWork work,IStore store)=>
|
|
{
|
|
var change=await store.Get<PmsChange>(Session.Hotel(c),id);if(change==null)return Results.NotFound();if(preview)return Results.BadRequest();
|
|
var result=await work.Verify(change,input.Version,c.RequestAborted);await Session.Audit(store,c,"Verified PMS state: "+result.State);return Results.Ok(result.View());
|
|
}).RequireAuthorization("Owner");
|
|
group.MapPost("/changes/{id}/cancel",async(string id,VersionInput input,HttpContext c,PmsWork work,IStore store)=>
|
|
{
|
|
var change=await store.Get<PmsChange>(Session.Hotel(c),id);if(change==null)return Results.NotFound();if(preview)return Results.BadRequest();
|
|
var result=await work.Cancel(change,input.Version);await Session.Audit(store,c,"Cancelled an unapplied PMS proposal");return Results.Ok(result.View());
|
|
}).RequireAuthorization("Owner");
|
|
group.MapPut("/controls",async(PmsControlsInput input,HttpContext c,IStore store,IConfiguration config)=>
|
|
{
|
|
if(input.Enabled&&(preview||OhipProfile.Read(config,Session.Hotel(c))?.WritesEnabled!=true))return Results.BadRequest(new{error="Server-side PMS writes must be enabled after sandbox acceptance first."});
|
|
var hotel=await store.Get<Hotel>(Session.Hotel(c),Session.Hotel(c));if(hotel==null)return Results.NotFound();hotel.PmsUpdatesEnabled=input.Enabled;hotel.Version=input.Version+1;
|
|
if(!await store.Replace(hotel.HotelId,hotel.Id,input.Version,hotel))return Input.Conflict();
|
|
await Session.Audit(store,c,"Updated PMS write controls");return Results.Ok(hotel);
|
|
}).RequireAuthorization("Owner");
|
|
}
|
|
}
|