#nullable enable annotations using System.Security.Claims; using Microsoft.EntityFrameworkCore; using Yavsc.Models; using Yavsc.Models.Billing; namespace Yavsc.Server.Helpers; /// /// Shared signature-persistence logic for estimates, extracted from /// BillingController.Sign. Stages the /// row (find-or-add by (EstimateId, Type)) for the PostIt /// wire-format payload — but does not call /// SaveChanges. The caller owns the transaction so it can /// combine the signature write with its own changes (e.g. setting a /// validation date or a query status) in a single /// SaveChanges. /// /// /// The column is the source of /// truth; there is no on-disk copy, so no disk-quota accounting is /// involved. /// /// public static class EstimateSignaturePersister { /// /// Stage a signature for signed by /// as . Upserts /// the row for the /// (EstimateId, Type) pair, writing the wire payload /// (Strokes, CoordinateMax, CapturedAtUtc) /// and . Returns the staged /// ; the caller persists with /// SaveChanges. /// public static async Task StageAsync( ApplicationDbContext db, long estimateId, SignatureType type, string signerId, SignaturePadPayload payload, CancellationToken token = default) { if (db is null) throw new ArgumentNullException(nameof(db)); if (payload is null) throw new ArgumentNullException(nameof(payload)); if (estimateId <= 0) throw new ArgumentOutOfRangeException(nameof(estimateId)); // Find-or-add: the (EstimateId, Type) pair is unique, so a // second submission for the same side of the estimate // replaces the previous signature. EF translates this into a // single UPDATE when the row exists and an INSERT otherwise; // the unique index in ApplicationDbContext is the // database-level guarantee that the contract holds if two // requests race. var signature = await db.Signatures .FirstOrDefaultAsync(s => s.EstimateId == estimateId && s.Type == type, token) .ConfigureAwait(false); if (signature is null) { signature = new Signature { EstimateId = estimateId, SignerId = signerId, Type = type, }; db.Signatures.Add(signature); } signature.SignerId = signerId; signature.CoordinateMax = payload.CoordinateMax; signature.Strokes = payload.Strokes; signature.CapturedAtUtc = payload.CapturedAtUtc; return signature; } } /// /// Wire payload accepted by the signature path and staged by /// . Mirrors /// PostIt.Models.SignaturePadData's JSON shape (without the /// disk-only envelope fields) so the two sides stay trivially /// compatible. /// public class SignaturePadPayload { /// /// Normalised coordinate upper bound. Must be /// PostIt.Models.SignaturePadData.CoordinateMax /// (10_000) today; declared as a property so a future /// resolution change can be replayed against the same /// wire format. /// public int CoordinateMax { get; set; } = 10_000; /// /// Client-reported capture time. The server may ignore /// this for ordering (UTC now is the truth) but keeps it /// for round-trip display. /// public DateTime CapturedAtUtc { get; set; } /// /// Wire strokes. See /// PostIt.Models.SignaturePadData for the format. /// public int[] Strokes { get; set; } = Array.Empty(); }