The core infrastructure backend (API, database, Docker, etc).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 lines
1.1 KiB

CREATE OR ALTER PROCEDURE [dbo].[User_ReadByIdsWithCalculatedPremium]
@Ids NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
-- Declare a table variable to hold the parsed JSON data
DECLARE @ParsedIds TABLE (Id UNIQUEIDENTIFIER);
-- Parse the JSON input into the table variable
INSERT INTO @ParsedIds (Id)
SELECT value
FROM OPENJSON(@Ids);
-- Check if the input table is empty
IF (SELECT COUNT(1) FROM @ParsedIds) < 1
BEGIN
RETURN(-1);
END
-- Main query to fetch user details and calculate premium access
SELECT
U.*,
CASE
WHEN U.[Premium] = 1
OR EXISTS (
SELECT 1
FROM [dbo].[OrganizationUser] OU
JOIN [dbo].[Organization] O ON OU.[OrganizationId] = O.[Id]
WHERE OU.[UserId] = U.[Id]
AND O.[UsersGetPremium] = 1
AND O.[Enabled] = 1
)
THEN 1
ELSE 0
END AS HasPremiumAccess
FROM
[dbo].[UserView] U
WHERE
U.[Id] IN (SELECT [Id] FROM @ParsedIds);
END;