PlotDirector/PlotLine/Sql/234_NativeImportedSessionGuard.sql

36 lines
3.0 KiB
Transact-SQL

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
IF COL_LENGTH(N'dbo.NativeReviewImports',N'ObservationSession') IS NULL ALTER TABLE dbo.NativeReviewImports ADD ObservationSession uniqueidentifier NULL;
GO
CREATE OR ALTER PROCEDURE dbo.NativeReviewSource_Save @UserID int,@ReviewID uniqueidentifier,@EncryptedSource nvarchar(max),@ObservedRevision bigint=NULL,@SessionID uniqueidentifier=NULL
AS
BEGIN
SET NOCOUNT ON;
IF DATALENGTH(@EncryptedSource)>8388608 THROW 51000,'Source capture too large',1;
UPDATE dbo.NativeChapterReviews SET EncryptedSource=@EncryptedSource,ResultJson=CASE WHEN @ObservedRevision IS NOT NULL THEN JSON_MODIFY(JSON_MODIFY(ResultJson,'$.DocumentRevision',@ObservedRevision),'$.CaptureSession',CONVERT(varchar(36),@SessionID)) ELSE ResultJson END WHERE ReviewID=@ReviewID AND UserID=@UserID AND EncryptedSource IS NULL;
END;
GO
CREATE OR ALTER TRIGGER dbo.NativeReviewImports_CapturePositions ON dbo.NativeReviewImports AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE n SET DocumentGuid=r.ManuscriptID,ObservationSession=TRY_CONVERT(uniqueidentifier,JSON_VALUE(r.ResultJson,'$.CaptureSession')),ObservationRevision=TRY_CONVERT(bigint,JSON_VALUE(r.ResultJson,'$.DocumentRevision')),ChapterStart=r.ChapterStart,ChapterEnd=r.ChapterEnd,
ScenePositionsJson=(SELECT p.Start,p.[End],m.SceneID,n.ChapterID FROM OPENJSON(r.ResultJson,'$.Scenes') WITH(Start int,[End] int) p JOIN dbo.OnboardingManuscriptBuildSceneMap m ON m.OnboardingManuscriptBuildID=n.BuildID AND m.TemporarySceneKey=CONCAT(n.ChapterKey,':',p.Start) FOR JSON PATH)
FROM dbo.NativeReviewImports n JOIN inserted i ON i.BookID=n.BookID AND i.ChapterKey=n.ChapterKey JOIN dbo.NativeChapterReviews r ON r.ReviewID=n.ReviewID;
IF EXISTS(SELECT 1 FROM dbo.NativeReviewImports n JOIN inserted i ON i.BookID=n.BookID AND i.ChapterKey=n.ChapterKey JOIN dbo.NativeReviewImports other ON other.BookID=n.BookID AND other.DocumentGuid=n.DocumentGuid AND other.ChapterKey<>n.ChapterKey AND other.ChapterStart<n.ChapterEnd AND n.ChapterStart<other.ChapterEnd)
THROW 51000,'An imported chapter overlaps this capture. Reconcile before importing.',1;
END;
GO
CREATE OR ALTER PROCEDURE dbo.NativeImportedPosition @UserID int,@BookID int,@DocumentGuid uniqueidentifier,@Revision bigint,@Position int,@SessionID uniqueidentifier=NULL
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID WHERE b.BookID=@BookID AND b.IsArchived=0 AND a.UserID=@UserID AND a.IsActive=1) RETURN;
SELECT TOP(1) n.ChapterID,p.SceneID FROM dbo.NativeReviewImports n
JOIN dbo.Chapters c ON c.ChapterID=n.ChapterID AND c.BookID=@BookID AND c.IsArchived=0
OUTER APPLY(SELECT TOP(1) s.SceneID FROM OPENJSON(n.ScenePositionsJson) WITH(Start int,[End] int,SceneID int) s JOIN dbo.Scenes sc ON sc.SceneID=s.SceneID AND sc.ChapterID=n.ChapterID AND sc.IsArchived=0 WHERE @Position>=s.Start AND @Position<s.[End]) p
WHERE n.BookID=@BookID AND n.DocumentGuid=@DocumentGuid AND n.ObservationSession=@SessionID AND n.ObservationRevision=@Revision AND @Position>=n.ChapterStart AND @Position<n.ChapterEnd;
END;
GO