authen
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
fn_gen_services_token
Parameters
Name
Type
Mode
service_code
text
IN
prop_code
text
IN
expire_day
integer
IN (DEFAULT NULL)
Definition
DECLARE v_service_id int; v_prop_id int; v_id int; v_token text; v_jti uuid := gen_random_uuid(); v_expire date; BEGIN -- หา id SELECT id INTO v_service_id FROM public.service WHERE code = service_code LIMIT 1; SELECT id INTO v_prop_id FROM public.property WHERE code = prop_code LIMIT 1; IF v_service_id IS NULL THEN RAISE EXCEPTION 'service_code not found: %', service_code; END IF; IF v_prop_id IS NULL THEN RAISE EXCEPTION 'prop_code not found: %', prop_code; END IF; -- expire IF expire_day IS NOT NULL THEN v_expire := CURRENT_DATE + expire_day; END IF; -- insert + get id INSERT INTO public.service_token ( prop_id, service_id, token_expire, active, name, create_time ) VALUES ( v_prop_id, v_service_id, v_expire, true, 'token_' || prop_code || '_' || service_code || '_' || now(), now() ) RETURNING id INTO v_id; -- เอา token ที่ trigger generate ไว้แล้วกลับมา SELECT token INTO v_token FROM public.service_token WHERE id = v_id; RETURN v_token; END;