PlotDirector/PlotLine/Sql/242_CoreImportCompletion.sql

36 lines
2.2 KiB
Transact-SQL

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
-- Final character review is the end of core import, not the start of an optional AI job.
-- Scope completion to the original wizard/book so a stale browser cannot finish another import.
CREATE OR ALTER PROCEDURE dbo.UserOnboarding_CompleteCoreImport
@UserID int,@OnboardingID int,@BookID int,@PreviewID uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;SET XACT_ABORT ON;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT 1 FROM dbo.OnboardingManuscriptBuilds b JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID AND a.UserID=@UserID AND a.IsActive=1 AND a.AccessRole=N'Owner'
JOIN dbo.UserOnboardingState o WITH(UPDLOCK,HOLDLOCK) ON o.UserID=@UserID AND o.UserOnboardingStateID=@OnboardingID AND o.BookID=b.BookID AND o.ProjectID=b.ProjectID
WHERE b.PreviewID=@PreviewID AND b.UserID=@UserID AND b.BookID=@BookID AND b.Status IN ('Built','Complete')
AND EXISTS(SELECT 1 FROM dbo.OnboardingManuscriptBuildSceneMap m WHERE m.OnboardingManuscriptBuildID=b.OnboardingManuscriptBuildID)
AND EXISTS(SELECT 1 FROM dbo.CoreImportBasicLocationReviews r WHERE r.PreviewID=@PreviewID AND r.UserID=@UserID AND r.BookID=@BookID AND r.CompletedUtc IS NOT NULL))
BEGIN ROLLBACK;THROW 51000,'Core import context changed or required review is incomplete',1;END;
UPDATE dbo.UserOnboardingState SET CurrentStep=N'Complete',OnboardingCompletedUtc=COALESCE(OnboardingCompletedUtc,SYSUTCDATETIME()),UpdatedUtc=SYSUTCDATETIME()
WHERE UserID=@UserID AND UserOnboardingStateID=@OnboardingID AND BookID=@BookID;
COMMIT;
EXEC dbo.UserOnboarding_Get @UserID=@UserID;
END;
GO
-- Navigation is not a new import. Explicit setup/SaveBook actions already reset completion.
CREATE OR ALTER PROCEDURE dbo.UserOnboarding_SetCurrentStep @UserID int,@CurrentStep nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.UserOnboardingState WHERE UserID=@UserID) INSERT dbo.UserOnboardingState(UserID) VALUES(@UserID);
UPDATE dbo.UserOnboardingState SET CurrentStep=@CurrentStep,
OnboardingCompletedUtc=CASE WHEN @CurrentStep=N'Complete' THEN COALESCE(OnboardingCompletedUtc,SYSUTCDATETIME()) ELSE OnboardingCompletedUtc END,
UpdatedUtc=SYSUTCDATETIME() WHERE UserID=@UserID;
EXEC dbo.UserOnboarding_Get @UserID=@UserID;
END;
GO