-- =============================================
-- Author:		Charlene van Heerden
-- Create date: 2016-05-16
-- Description:	Gets all calendars where user has access
-- =============================================
CREATE PROCEDURE [dbo].[sp_CalendarAccess] 
	@userId INT 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    SELECT recId, name, [description], interval, firstDayOfWeek, defaultView, isDoubleBook, isAccessRestricted, dateSaved, 
	userIdSaved, dateUpdated, userIdUpdated, [sequence], isActive 
	FROM pal_Calendar 
	WHERE isActive = 1 
	AND isAccessRestricted = 0 
	UNION
	SELECT c.recId, c.name, c.[description], c.interval, c.firstDayOfWeek, c.defaultView, c.isDoubleBook, c.isAccessRestricted, c.dateSaved, 
	c.userIdSaved, c.dateUpdated, c.userIdUpdated, c.[sequence], c.isActive
	FROM pal_Calendar c 
	LEFT OUTER JOIN pal_CalendarUser u 
	ON u.calendarId = c.recId 
	WHERE c.isActive = 1 
	AND c.isAccessRestricted = 1
	AND u.userId = @userId
	ORDER BY [sequence]

END
