CREATE PROCEDURE [REG_CreateOperator]

	@OperatorName	VARCHAR(50),
	@OperatorPassword	VARCHAR(100),
	@ApiConsumerId	int,
	@OperatorCreationDate	DATETIME2 = NULL,
	@FirstName	NVARCHAR(50) = NULL,
	@LastName	NVARCHAR(50) = NULL,
	@Phone	VARCHAR(50) = NULL,
	@Email	NVARCHAR(250) = NULL,
	@CountryId	INT = NULL,
	@IsActive	BIT = 1,
	@IsReadOnly	BIT = 0,
	@IsTest	BIT = 0,
	@HttpRequestInfo	NVARCHAR(MAX) = NULL
	
	AS

	-- Save HttpRequestId --
	DECLARE @HttpRequestId	BIGINT = NULL
	IF @HttpRequestInfo IS NOT NULL
	BEGIN
	INSERT INTO HttpRequests (HttpRequestInfo) VALUES (@HttpRequestInfo)
	SET @HttpRequestId = SCOPE_IDENTITY()
	END

	-- Save new Operator --
	SET @OperatorCreationDate = ISNULL(@OperatorCreationDate, [dbo].[FN_GetSystemTime]())

	INSERT INTO BO_Operators (ApiConsumerId, OperatorName,  OperatorPassword,  OperatorCreationDate,  FirstName,  LastName,  Phone,  Email,  CountryId,  IsActive,  IsReadOnly,  IsTest)
	VALUES               (@ApiConsumerId, @OperatorName, @OperatorPassword, @OperatorCreationDate, @FirstName, @LastName, @Phone, @Email, @CountryId, @IsActive, @IsReadOnly, @IsTest)
	
	DECLARE  @OperatorId BIGINT = SCOPE_IDENTITY();
	EXEC REG_GetOperators @OperatorId;

RETURN 0
