PlotDirector/PlotLine/Sql/272_WriterReviewObservations.sql

71 lines
6.8 KiB
Transact-SQL

-- Author-private review observations: accepted-version/paragraph references are immutable.
-- Existing scene/session notes have neither chapter-level scope nor accepted-version anchors.
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
IF OBJECT_ID('dbo.WriterReviewObservations','U') IS NULL
BEGIN
CREATE TABLE dbo.WriterReviewObservations(
ID uniqueidentifier NOT NULL PRIMARY KEY,UserID int NOT NULL,BookID int NOT NULL,ChapterID int NOT NULL,
ManuscriptID uniqueidentifier NULL,SnapshotRevision bigint NOT NULL,ContentVersion char(64) NOT NULL,
StartParagraph uniqueidentifier NULL,EndParagraph uniqueidentifier NULL,StartOffset int NULL,EndOffset int NULL,
AnchorHash char(64) NULL,SceneID int NULL,Observation nvarchar(max) NOT NULL,Decision nvarchar(max) NULL,
Status varchar(16) NOT NULL DEFAULT 'Open',Version int NOT NULL DEFAULT 1,
CreatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),UpdatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),
CONSTRAINT FK_WriterReview_User FOREIGN KEY(UserID) REFERENCES dbo.AppUser(UserID) ON DELETE CASCADE,
CONSTRAINT FK_WriterReview_Chapter FOREIGN KEY(ChapterID) REFERENCES dbo.Chapters(ChapterID) ON DELETE CASCADE,
CONSTRAINT FK_WriterReview_Book FOREIGN KEY(BookID) REFERENCES dbo.Books(BookID),
CONSTRAINT CK_WriterReview_Status CHECK(Status IN('Open','Working','Resolved')),
CONSTRAINT CK_WriterReview_Anchor CHECK((StartParagraph IS NULL AND EndParagraph IS NULL AND StartOffset IS NULL AND EndOffset IS NULL AND AnchorHash IS NULL) OR (StartParagraph IS NOT NULL AND EndParagraph IS NOT NULL AND StartOffset>=0 AND EndOffset>=0 AND AnchorHash IS NOT NULL))
);
CREATE INDEX IX_WriterReview_Scope ON dbo.WriterReviewObservations(UserID,BookID,ChapterID,CreatedUtc DESC,ID);
END;
GO
CREATE OR ALTER PROCEDURE dbo.WriterReview_Observations @Action varchar(12),@UserID int,@BookID int,@ChapterID int,@ID uniqueidentifier=NULL,@Version int=0,
@ManuscriptID uniqueidentifier=NULL,@SnapshotRevision bigint=NULL,@ContentVersion char(64)=NULL,
@StartParagraph uniqueidentifier=NULL,@EndParagraph uniqueidentifier=NULL,@StartOffset int=NULL,@EndOffset int=NULL,@AnchorHash char(64)=NULL,@SceneID int=NULL,
@Observation nvarchar(max)=NULL,@Decision nvarchar(max)=NULL,@Status varchar(16)='Open',@Page int=1
AS
BEGIN
SET NOCOUNT ON;SET XACT_ABORT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Chapters c ON c.BookID=b.BookID AND c.ChapterID=@ChapterID AND c.IsArchived=0 JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.IsArchived=0) THROW 51000,'Review scope unavailable',1;
IF @Action='list' BEGIN
IF @Page<1 OR @Page>100000 THROW 51000,'Invalid review page',1;
SELECT * FROM dbo.WriterReviewObservations WHERE UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID ORDER BY CreatedUtc DESC,ID OFFSET ((@Page-1)*50) ROWS FETCH NEXT 51 ROWS ONLY;RETURN;
END;
IF @Action NOT IN('create','update') OR @ID IS NULL OR @Status NOT IN('Open','Working','Resolved') OR LEN(TRIM(COALESCE(@Observation,N'')))=0 OR LEN(@Observation)>10000 OR LEN(COALESCE(@Decision,N''))>10000 THROW 51000,'Invalid observation',1;
BEGIN TRANSACTION;
IF @Action='create'
BEGIN
IF EXISTS(SELECT 1 FROM dbo.WriterReviewObservations WITH(UPDLOCK,HOLDLOCK) WHERE ID=@ID)
BEGIN
IF NOT EXISTS(SELECT 1 FROM dbo.WriterReviewObservations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND Observation=@Observation AND ISNULL(Decision,N'')=ISNULL(@Decision,N'') AND ContentVersion=@ContentVersion AND Status=@Status AND ISNULL(CONVERT(varchar(36),StartParagraph),'')=ISNULL(CONVERT(varchar(36),@StartParagraph),'') AND ISNULL(CONVERT(varchar(36),EndParagraph),'')=ISNULL(CONVERT(varchar(36),@EndParagraph),'') AND ISNULL(StartOffset,-1)=ISNULL(@StartOffset,-1) AND ISNULL(EndOffset,-1)=ISNULL(@EndOffset,-1)) THROW 51002,'This observation changed; reload before retrying',1;
END ELSE BEGIN
DECLARE @CurrentRevision bigint,@CurrentManuscript uniqueidentifier;
SELECT @CurrentRevision=Revision,@CurrentManuscript=ManuscriptID FROM dbo.BookParagraphManuscripts WITH(UPDLOCK,HOLDLOCK) WHERE BookID=@BookID;
IF ISNULL(@CurrentRevision,-1)<>@SnapshotRevision OR ISNULL(@CurrentManuscript,'00000000-0000-0000-0000-000000000000')<>ISNULL(@ManuscriptID,'00000000-0000-0000-0000-000000000000') THROW 51002,'Accepted manuscript changed; your text has been retained',1;
INSERT dbo.WriterReviewObservations(ID,UserID,BookID,ChapterID,ManuscriptID,SnapshotRevision,ContentVersion,StartParagraph,EndParagraph,StartOffset,EndOffset,AnchorHash,SceneID,Observation,Decision,Status)
VALUES(@ID,@UserID,@BookID,@ChapterID,@ManuscriptID,@SnapshotRevision,@ContentVersion,@StartParagraph,@EndParagraph,@StartOffset,@EndOffset,@AnchorHash,@SceneID,@Observation,@Decision,@Status);
END;
END ELSE BEGIN
UPDATE dbo.WriterReviewObservations SET Observation=@Observation,Decision=@Decision,Status=@Status,Version=Version+1,UpdatedUtc=SYSUTCDATETIME() WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND Version=@Version;
IF @@ROWCOUNT<>1 THROW 51002,'Another window changed this observation; your text has been retained',1;
END;
SELECT * FROM dbo.WriterReviewObservations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID;
COMMIT;
END;
GO
-- The existing verified scene-start join, scoped to any accepted paragraph (including chapter-only prose).
CREATE OR ALTER PROCEDURE dbo.WriterReview_ParagraphStart @UserID int,@BookID int,@ChapterID int,@ParagraphID uniqueidentifier,@ManuscriptID uniqueidentifier,@SessionID uniqueidentifier,@WordRevision bigint,@AcceptedRevision bigint
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.IsArchived=0) THROW 51000,'Book access denied',1;
SELECT m.Revision,pos.ChapterID,pos.StartPosition
FROM dbo.BookParagraphManuscripts m JOIN dbo.BookChapterValidations v ON v.BookID=m.BookID
CROSS APPLY OPENJSON(m.Payload,'$.ChapterCheckpoints') WITH(ChapterID int '$.ChapterId',ContentHash char(64),IdentityHash char(64),MappingHash char(64)) c
CROSS APPLY OPENJSON(v.Positions) WITH(ParagraphID uniqueidentifier '$.ParagraphId',StartPosition int '$.Start',ChapterID int '$.ChapterId',Verified bit) pos
JOIN dbo.Chapters ch ON ch.ChapterID=pos.ChapterID AND ch.BookID=m.BookID AND ch.IsArchived=0
WHERE m.BookID=@BookID AND m.ManuscriptID=@ManuscriptID AND m.Revision=@AcceptedRevision AND v.ChapterID=c.ChapterID AND pos.ChapterID=c.ChapterID AND pos.ChapterID=@ChapterID AND v.UserID=@UserID AND v.SessionID=@SessionID AND v.ManuscriptID=@ManuscriptID AND v.WordRevision=@WordRevision AND v.ExpiresUtc>SYSUTCDATETIME() AND v.ContentHash=c.ContentHash AND v.IdentityHash=c.IdentityHash AND v.MappingHash=c.MappingHash AND pos.Verified=1 AND pos.ParagraphID=@ParagraphID;
END;