vsp_fixes #15

Merged
Raykov-MS merged 3 commits from vsp_fixes into test 2026-06-30 09:56:55 +03:00
Showing only changes of commit 0652ba4aee - Show all commits

View File

@ -0,0 +1,335 @@
"""vsp remove p_numbers param
Revision ID: 0004
Revises: 0003
Create Date: 2026-06-29 17:20:08.998832
"""
from typing import Sequence, Union
from alembic import op
# revision identifiers, used by Alembic.
revision: str = '0004'
down_revision: Union[str, None] = '0003'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
op.execute(
"""
CREATE OR REPLACE FUNCTION v3.add_vsp(
p_branch_id integer,
p_reg_number character varying DEFAULT NULL::character varying,
p_address character varying DEFAULT NULL::character varying,
p_format character varying DEFAULT NULL::character varying,
p_opened_at date DEFAULT NULL::date,
p_placement_type character varying DEFAULT NULL::character varying,
p_staff_count integer DEFAULT NULL::integer,
p_total_area numeric DEFAULT NULL::numeric,
p_closed_at date DEFAULT NULL::date,
p_is_active boolean DEFAULT true,
p_is_deleted boolean DEFAULT false,
p_system_code character varying DEFAULT NULL::character varying,
p_created_by integer DEFAULT NULL::integer,
p_vsp_type character varying DEFAULT NULL::character varying,
p_notes text DEFAULT NULL::text,
p_location_form character varying DEFAULT NULL::character varying,
p_rent_contract_num character varying DEFAULT NULL::character varying,
p_rent_end_date date DEFAULT NULL::date
)
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE
v_id INT;
v_branch_name VARCHAR;
v_actor_id INT;
v_actor_email VARCHAR;
v_actor_full_name VARCHAR;
BEGIN
SELECT ou.title INTO v_branch_name
FROM v3.org_unit ou
WHERE ou.id = p_branch_id;
v_actor_id := COALESCE(p_created_by, v3.current_user_id());
IF v_actor_id IS NOT NULL THEN
SELECT u.email, u.full_name
INTO v_actor_email, v_actor_full_name
FROM v3.app_user u
WHERE u.id = v_actor_id;
END IF;
INSERT INTO v3.vsp
(branch_id, reg_number, address, format, opened_at,
placement_type, staff_count, total_area, closed_at,
is_active, is_deleted, system_code, created_by,
vsp_type, notes, location_form, rent_contract_num, rent_end_date)
VALUES
(p_branch_id, p_reg_number, p_address, p_format, p_opened_at,
p_placement_type, p_staff_count, p_total_area, p_closed_at,
p_is_active, p_is_deleted, p_system_code, p_created_by,
p_vsp_type, p_notes, p_location_form, p_rent_contract_num, p_rent_end_date)
RETURNING id INTO v_id;
PERFORM v3.log_event(
'VSP_CREATE', 'VSP',
jsonb_build_object(
'vsp_id', v_id,
'entity_id', v_id,
'branch_id', p_branch_id,
'branch_name', v_branch_name,
'reg_number', p_reg_number,
'address', p_address,
'format', p_format,
'opened_at', v3._diff_val(p_opened_at),
'placement_type', p_placement_type,
'staff_count', p_staff_count,
'total_area', v3._diff_val(p_total_area),
'closed_at', v3._diff_val(p_closed_at),
'is_active', p_is_active,
'is_deleted', p_is_deleted,
'system_code', p_system_code,
'created_by', p_created_by,
'vsp_type', p_vsp_type,
'notes', p_notes,
'location_form', p_location_form,
'rent_contract_num', p_rent_contract_num,
'rent_end_date', v3._diff_val(p_rent_end_date),
'user_email', v_actor_email,
'user_full_name', v_actor_full_name
), null, null, p_branch_id
);
RETURN v_id;
END;
$function$;
"""
)
op.execute(
"""
CREATE OR REPLACE FUNCTION v3.upd_vsp(
p_vsp_id integer,
p_branch_id integer DEFAULT NULL::integer,
p_reg_number character varying DEFAULT NULL::character varying,
p_address character varying DEFAULT NULL::character varying,
p_format character varying DEFAULT NULL::character varying,
p_opened_at date DEFAULT NULL::date,
p_placement_type character varying DEFAULT NULL::character varying,
p_staff_count integer DEFAULT NULL::integer,
p_total_area numeric DEFAULT NULL::numeric,
p_closed_at date DEFAULT NULL::date,
p_is_active boolean DEFAULT NULL::boolean,
p_is_deleted boolean DEFAULT NULL::boolean,
p_system_code character varying DEFAULT NULL::character varying,
p_updated_by integer DEFAULT NULL::integer,
p_vsp_type character varying DEFAULT NULL::character varying,
p_notes text DEFAULT NULL::text,
p_location_form character varying DEFAULT NULL::character varying,
p_rent_contract_num character varying DEFAULT NULL::character varying,
p_rent_end_date date DEFAULT NULL::date
)
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
v_old RECORD;
v_changes JSONB := '{}'::jsonb;
v_branch_name_before VARCHAR;
v_branch_name_after VARCHAR;
v_actor_id INT;
v_actor_email VARCHAR;
v_actor_full_name VARCHAR;
BEGIN
SELECT v.branch_id, v.reg_number, v.address, v.format, v.opened_at,
v.placement_type, v.staff_count, v.total_area, v.closed_at,
v.is_active, v.is_deleted, v.system_code,
v.vsp_type, v.notes, v.location_form, v.rent_contract_num, v.rent_end_date
INTO v_old
FROM v3.vsp v
WHERE v.id = p_vsp_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'vsp #% не существует', p_vsp_id;
END IF;
SELECT ou.title
INTO v_branch_name_before
FROM v3.org_unit ou
WHERE ou.id = v_old.branch_id;
v_branch_name_after := v_branch_name_before;
v_actor_id := COALESCE(p_updated_by, v3.current_user_id());
IF v_actor_id IS NOT NULL THEN
SELECT u.email, u.full_name
INTO v_actor_email, v_actor_full_name
FROM v3.app_user u
WHERE u.id = v_actor_id;
END IF;
IF p_branch_id IS NOT NULL AND p_branch_id IS DISTINCT FROM v_old.branch_id THEN
SELECT ou.title INTO v_branch_name_after
FROM v3.org_unit ou
WHERE ou.id = p_branch_id;
UPDATE v3.vsp SET branch_id = p_branch_id WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object(
'branch_id', jsonb_build_object(
'before', v3._diff_val(v_old.branch_id),
'after', v3._diff_val(p_branch_id)
),
'branch_name', jsonb_build_object(
'before', v3._diff_val(v_branch_name_before),
'after', v3._diff_val(v_branch_name_after)
)
);
END IF;
IF p_reg_number IS NOT NULL AND p_reg_number IS DISTINCT FROM v_old.reg_number THEN
UPDATE v3.vsp SET reg_number = p_reg_number WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('reg_number',
jsonb_build_object('before', v3._diff_val(v_old.reg_number),
'after', v3._diff_val(p_reg_number)));
END IF;
IF p_address IS NOT NULL AND p_address IS DISTINCT FROM v_old.address THEN
UPDATE v3.vsp SET address = p_address WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('address',
jsonb_build_object('before', v3._diff_val(v_old.address),
'after', v3._diff_val(p_address)));
END IF;
IF p_format IS NOT NULL AND p_format IS DISTINCT FROM v_old.format THEN
UPDATE v3.vsp SET format = p_format WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('format',
jsonb_build_object('before', v3._diff_val(v_old.format),
'after', v3._diff_val(p_format)));
END IF;
IF p_opened_at IS NOT NULL AND p_opened_at IS DISTINCT FROM v_old.opened_at THEN
UPDATE v3.vsp SET opened_at = p_opened_at WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('opened_at',
jsonb_build_object('before', v3._diff_val(v_old.opened_at),
'after', v3._diff_val(p_opened_at)));
END IF;
IF p_placement_type IS NOT NULL AND p_placement_type IS DISTINCT FROM v_old.placement_type THEN
UPDATE v3.vsp SET placement_type = p_placement_type WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('placement_type',
jsonb_build_object('before', v3._diff_val(v_old.placement_type),
'after', v3._diff_val(p_placement_type)));
END IF;
IF p_staff_count IS NOT NULL AND p_staff_count IS DISTINCT FROM v_old.staff_count THEN
UPDATE v3.vsp SET staff_count = p_staff_count WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('staff_count',
jsonb_build_object('before', v3._diff_val(v_old.staff_count),
'after', v3._diff_val(p_staff_count)));
END IF;
IF p_total_area IS NOT NULL AND p_total_area IS DISTINCT FROM v_old.total_area THEN
UPDATE v3.vsp SET total_area = p_total_area WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('total_area',
jsonb_build_object('before', v3._diff_val(v_old.total_area),
'after', v3._diff_val(p_total_area)));
END IF;
IF p_closed_at IS NOT NULL AND p_closed_at IS DISTINCT FROM v_old.closed_at THEN
UPDATE v3.vsp SET closed_at = p_closed_at WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('closed_at',
jsonb_build_object('before', v3._diff_val(v_old.closed_at),
'after', v3._diff_val(p_closed_at)));
END IF;
IF p_is_active IS NOT NULL AND p_is_active IS DISTINCT FROM v_old.is_active THEN
UPDATE v3.vsp SET is_active = p_is_active WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('is_active',
jsonb_build_object('before', v3._diff_val(v_old.is_active),
'after', v3._diff_val(p_is_active)));
END IF;
IF p_is_deleted IS NOT NULL AND p_is_deleted IS DISTINCT FROM v_old.is_deleted THEN
UPDATE v3.vsp SET is_deleted = p_is_deleted WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('is_deleted',
jsonb_build_object('before', v3._diff_val(v_old.is_deleted),
'after', v3._diff_val(p_is_deleted)));
END IF;
IF p_system_code IS NOT NULL AND p_system_code IS DISTINCT FROM v_old.system_code THEN
UPDATE v3.vsp SET system_code = p_system_code WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('system_code',
jsonb_build_object('before', v3._diff_val(v_old.system_code),
'after', v3._diff_val(p_system_code)));
END IF;
IF p_vsp_type IS NOT NULL AND p_vsp_type IS DISTINCT FROM v_old.vsp_type THEN
UPDATE v3.vsp SET vsp_type = p_vsp_type WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('vsp_type',
jsonb_build_object('before', v3._diff_val(v_old.vsp_type),
'after', v3._diff_val(p_vsp_type)));
END IF;
IF p_notes IS NOT NULL AND p_notes IS DISTINCT FROM v_old.notes THEN
UPDATE v3.vsp SET notes = p_notes WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('notes',
jsonb_build_object('before', v3._diff_val(v_old.notes),
'after', v3._diff_val(p_notes)));
END IF;
IF p_location_form IS NOT NULL AND p_location_form IS DISTINCT FROM v_old.location_form THEN
UPDATE v3.vsp SET location_form = p_location_form WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('location_form',
jsonb_build_object('before', v3._diff_val(v_old.location_form),
'after', v3._diff_val(p_location_form)));
END IF;
IF p_rent_contract_num IS NOT NULL AND p_rent_contract_num IS DISTINCT FROM v_old.rent_contract_num THEN
UPDATE v3.vsp SET rent_contract_num = p_rent_contract_num WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('rent_contract_num',
jsonb_build_object('before', v3._diff_val(v_old.rent_contract_num),
'after', v3._diff_val(p_rent_contract_num)));
END IF;
IF p_rent_end_date IS NOT NULL AND p_rent_end_date IS DISTINCT FROM v_old.rent_end_date THEN
UPDATE v3.vsp SET rent_end_date = p_rent_end_date WHERE id = p_vsp_id;
v_changes := v_changes || jsonb_build_object('rent_end_date',
jsonb_build_object('before', v3._diff_val(v_old.rent_end_date),
'after', v3._diff_val(p_rent_end_date)));
END IF;
IF v_changes <> '{}'::jsonb THEN
UPDATE v3.vsp
SET updated_at = now(),
updated_by = p_updated_by
WHERE id = p_vsp_id;
PERFORM v3.log_event(
'VSP_UPDATE', 'VSP',
jsonb_build_object(
'vsp_id', p_vsp_id,
'entity_id', p_vsp_id,
'branch_id', COALESCE(p_branch_id, v_old.branch_id),
'branch_name', COALESCE(v_branch_name_after, v_branch_name_before),
'changes', v_changes,
'user_email', v_actor_email,
'user_full_name', v_actor_full_name
), null, null, COALESCE(p_branch_id, v_old.branch_id)
);
END IF;
END;
$function$;
"""
)
def downgrade() -> None:
"""Downgrade schema."""
pass