Skip to main content

Criação de APIKey em hml (HARD)

declare @unidadeId INT,
        @UsuarioId INT,
        @correspondenteId INT,
        @hierarquiaNivelDetalhe INT,
        @senha VARCHAR(32),
        @pessoaid INT,
        @output_data varchar(max),
            @idApiKey int,
            @apikey uniqueidentifier,
            @put_data varchar(max) = '{
    "Id": null,
    "CorrespondenteId": _correspondenteId_,
    "CanalId": 3,
    "todasUnidades": 0,
    "operacao": "I"
    }'


select @correspondenteId = correspondenteid from unidade with(nolock) where cpfcnpj = '13054592000176' and Ativo = 1

-- Criando unidade do vendedor
    INSERT INTO Unidade(
        CorrespondenteId,
        UnidadeId,
        Tipo,
        CEP,
        CidadeId,
        Bairro,
        Logradouro,
        Numero,
        Complemento,
        Nome,
        Cadastro,
        CPFCNPJ,
        IndicadorId,
        Ativo,
        TipoModalidade
    )VALUES(
        @correspondenteId,
        NULL,
        4,
        '60430730',
        1749, -- SELECT * from Cidade WITH(nolock) where Nome like'%FORTALEZA%' 
        'RODOLFO TEOFILO',
        'RUA TEODORETO SOUTO',
        null,
        null,
        'EMILIANO LUCIUS LIMA VIANA ',
        dbo.getdateBR(),
        '64190005304',
        NULL,
        1,
        0
    )
    SET @unidadeId = @@IDENTITY

    SET @senha = CONVERT(VARCHAR(32), HashBytes('MD5', '641900'), 2) -- 6 primeiros difitos do cpf
    
    --Criando o usuario
    INSERT INTO Seguranca.Usuario(
        PessoaId,
        [Login],
        Senha,
        Ramal,
        CorrespondenteId,
        UnidadeId,
        Nome,
        CPFCNPJ,
        Ativo,
        Bloqueado,
        DepartamentoId,
        UsuarioTipoId,
        Cadastro
    )VALUES(
        NULL,
        'CC030177289',
        @senha,
        NULL,
        @correspondenteId,
        @unidadeId,
        'EMILIANO LUCIUS LIMA VIANA ',
        '64190005304',
        1,
        0,
        15,
        5,
        dbo.getdateBR()
    )
    SET @UsuarioId = @@IDENTITY

    INSERT INTO seguranca.UsuarioSenhaHistorico(
        UsuarioId,
        Senha,
        DataExpiracao,
        SofreExpiracao
        )VALUES(
            @usuarioId,
            @senha,
            dbo.getdateBR(),
            0
        )

 
-- Setando perfil
    INSERT INTO Seguranca.PerfilUsuario(
        PerfilId,
        UsuarioId
    )VALUES(
        18,
        @UsuarioId
    )
 
--Pegando posição do supervisor
    set  @hierarquiaNivelDetalhe =  57

-- Vinculando unidade a hierarquia/correspondente
        INSERT INTO CorrespondenteHierarquiaNivelDetalhe(
        CorrespondenteId,
        UnidadeId,
        HierarquiaNivelDetalheId,
        DataInicio,
        DataFim
    )VALUES(
        @correspondenteId,
        @unidadeId,
        @hierarquiaNivelDetalhe,
        dbo.getdateBR(),
        null
    )

   --Criando API Key

    set @put_data = replace(@put_data, '_correspondenteId_', @correspondenteId)
 
    exec Suporte.stpAPIKey @put_data, @output_data output
 
    select
        @idApiKey = id
    from openjson(@output_data) with (id int)
 
    set @output_data = null
    set @put_data = '{
    "id": null,
    "unidadeId": _unidadeID_,
    "apiKeyId": _apiKeyId_,
    "operacao": "I"
    }'
 
    set @put_data = replace(replace(@put_data, '_unidadeID_', @unidadeId), '_apiKeyId_', @idApiKey)
 
    exec Suporte.stpUnidadeAPIKey @put_data, @output_data output

    select @apikey = apikey from seguranca.apikey where Id = @idApiKey

    insert into 
        seguranca.ApikeyUnidade (Apikey, UnidadeId, DataHoraVigenciaInicio, DataHoraVigenciaFim, CanalId, VersaoApiParceirosIdMinima, VersaoApiParceirosIdMaxima)
    values
        (@apikey, @unidadeId, dbo.getdatebr(), null, 3, 1, 1)