x
1
declare
2
internalid integer;
3
begin
4
--EMAIL NULO
5
if email is null then
6
RAISE EXCEPTION 'email = %', email
7
USING HINT = 'Field email must be not null';
8
end if;
9
10
--IDNPADRAO NULO OU FORA DO PADRAO
11
if idnpadrao is null or idnpadrao not in (0,1) then
12
RAISE EXCEPTION 'idnpadrao = %', idnpadrao
13
USING HINT = 'Field must not be null or different from 1 or 0.';
14
end if;
15
16
--LOOKUP IDUSUARIO
17
IF PostUsuarioEmail.idusuario IS NULL AND PostUsuarioEmail.idusuario_externalid IS NOT NULL
18
THEN PostUsuarioEmail.idusuario = (SELECT C2.idusuario FROM usuario C2 WHERE C2.CODIGOERP = PostUsuarioEmail.idusuario_externalid);
19
END IF;
20
21
--IDUSUARIO NULO
22
if PostUsuarioEmail.idusuario is null then
23
RAISE EXCEPTION 'idusuario = %', PostUsuarioEmail.idusuario
24
USING HINT = 'Field idusuario must be not null';
25
end if;
26
27
--CODIGOERP NULO
28
if externalid is null or externalid = '' then
29
RAISE EXCEPTION 'externalid = %', externalid
30
USING HINT = 'Field externalid must be not null';
31
end if;
32
33
--VALIDAÇÃO DE CODIGOERP JÁ EXISTENTE
34
IF (select distinct 1 FROM USUARIOEMAIL WHERE CODIGOERP = PostUsuarioEmail.externalid) = 1
35
THEN RAISE EXCEPTION 'externalid = %', PostUsuarioEmail.externalid
36
USING HINT = 'The record (externalid = '|| PostUsuarioEmail.externalid ||') already exists, it was not possible to insert it.';
37
ELSE insert into usuarioemail (idusuarioemail,email,idnpadrao,idusuario,codigoerp) values (nextval('seqpkusuarioemail'),email,idnpadrao,idusuario,externalid) returning idusuarioemail into internalid;
38
end if;
39
40
return query select internalid;
41
END;