aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_save_precheckin_legal_document
Parameters
Name
Type
Mode
data
jsonb
IN
Definition
DECLARE v_state TEXT; v_message TEXT; v_detail TEXT; v_hint TEXT; v_context TEXT; v_d record; v_id int; BEGIN SELECT * INTO v_d FROM jsonb_to_record(data) AS d( id int, doc_type text, lang_code text, version text, title text, content_html text, is_translated bool, is_active bool ); IF v_d.doc_type NOT IN ('terms', 'privacy') THEN RETURN fn_result_error('60201', sys_msg('60201', 'doc_type must be terms or privacy')); END IF; IF v_d.lang_code IS NULL OR NOT EXISTS (SELECT 1 FROM public.precheckin_language WHERE lang_code = v_d.lang_code) THEN RETURN fn_result_error('60202', sys_msg('60202', 'lang_code does not exist in precheckin_language')); END IF; IF v_d.content_html IS NULL OR btrim(v_d.content_html) = '' THEN RETURN fn_result_error('60203', sys_msg('60203', 'content_html is required')); END IF; IF v_d.id IS NULL OR v_d.id < 1 THEN INSERT INTO public.precheckin_legal_document (doc_type, lang_code, version, title, content_html, is_translated, is_active, create_at) VALUES (v_d.doc_type, v_d.lang_code, COALESCE(v_d.version, '1.0'), v_d.title, v_d.content_html, COALESCE(v_d.is_translated, true), COALESCE(v_d.is_active, true), CURRENT_TIMESTAMP) RETURNING id INTO v_id; ELSE UPDATE public.precheckin_legal_document SET doc_type = v_d.doc_type, lang_code = v_d.lang_code, version = COALESCE(v_d.version, version), title = v_d.title, content_html = v_d.content_html, is_translated = COALESCE(v_d.is_translated, is_translated), is_active = COALESCE(v_d.is_active, is_active), update_at = CURRENT_TIMESTAMP WHERE id = v_d.id RETURNING id INTO v_id; IF v_id IS NULL THEN RETURN fn_result_error('60204', sys_msg('60204', 'Legal document not found')); END IF; END IF; RETURN fn_result_success( (SELECT row_to_json(d) FROM public.precheckin_legal_document d WHERE d.id = v_id) ); EXCEPTION WHEN unique_violation THEN RETURN fn_result_error('60205', sys_msg('60205', 'This doc_type + lang_code + version combination already exists')); WHEN OTHERS THEN GET STACKED DIAGNOSTICS v_state = RETURNED_SQLSTATE, v_message = MESSAGE_TEXT, v_detail = PG_EXCEPTION_DETAIL, v_hint = PG_EXCEPTION_HINT, v_context = PG_EXCEPTION_CONTEXT; RETURN fn_handle_error(v_state, v_message, v_detail, v_hint, v_context, 'sp_save_precheckin_legal_document', data); END