<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizenship_foreign_flow</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/08/05 14:31:22.799</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/05 14:31:22.799</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>create_job_execution_record_CF</from>
      <to>table_input_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>filter_last_row_CF</from>
      <to>change_job_status_on_success_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>detect_empty_stream_CF</from>
      <to>change_job_status_on_success_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>change_job_status_on_error_CF</from>
      <to>abort_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_output_CF</from>
      <to>change_job_status_on_error_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_CF</from>
      <to>identify_last_row_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>identify_last_row_CF</from>
      <to>table_output_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>identify_last_row_CF</from>
      <to>detect_empty_stream_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_output_CF</from>
      <to>filter_last_row_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>filter_true_citizenship</from>
      <to>update_citizen_has_foreign_citizenship</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>filter_true_citizenship</from>
      <to>update_citizen_has_foreign_residence</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_citizen_has_foreign_citizenship</from>
      <to>change_job_status_on_error_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_citizen_has_foreign_residence</from>
      <to>change_job_status_on_error_CF</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>identify_last_row_CF</from>
      <to>filter_true_citizenship</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>abort_CF</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT_WITH_ERROR</abort_option>
    <always_log_rows>Y</always_log_rows>
    <message/>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1456</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_error_CF</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>error_description</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1200</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_success_CF</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1456</xloc>
      <yloc>176</yloc>
    </GUI>
  </transform>
  <transform>
    <name>create_job_execution_record_CF</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>INSERT INTO etl.job_execution (id, job_name, status, execution_datetime, error_description, recruitment_id)
VALUES (DEFAULT, '${JOB_NAME}', 'PROCESSING', DEFAULT, null, '${IDM_ID}');

</sql>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>detect_empty_stream_CF</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>176</yloc>
    </GUI>
  </transform>
  <transform>
    <name>filter_last_row_CF</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>last_row</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_true_to>change_job_status_on_success_CF</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1456</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>filter_true_citizenship</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>has_foreign_citizenship</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_false_to>update_citizen_has_foreign_residence</send_false_to>
    <send_true_to>update_citizen_has_foreign_citizenship</send_true_to>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>identify_last_row_CF</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_input_CF</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <sql>WITH filtered_data AS (
    SELECT
        ri.recruit_id,
        jsonb_array_elements(ri.info -> 'svedFL' -> 'svedIGPP' -> 'svedIG') AS data,
        'citizenship' AS type
    FROM ervu_dashboard.recruits_info ri
    WHERE jsonb_typeof(ri.info -> 'svedFL' -> 'svedIGPP' -> 'svedIG') = 'array'
      AND coalesce(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'

    UNION ALL

    SELECT
        ri.recruit_id,
        jsonb_array_elements(ri.info -> 'svedFL' -> 'svedIGPP' -> 'svedPP') AS data,
        'residence' AS type
    FROM ervu_dashboard.recruits_info ri
    WHERE jsonb_typeof(ri.info -> 'svedFL' -> 'svedIGPP' -> 'svedPP') = 'array'
      AND coalesce(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'
)
SELECT
    recruit_id,
    coalesce(to_date(NULLIF(data ->> 'dataSved', ''), 'YYYY-MM-DD'), current_date) AS source_update_date,  -- дата обновления сведений
    data ->> CASE WHEN type = 'residence' THEN 'osnovPriobrPP' ELSE 'osnovPriobrIG' END AS basis,
    to_date(data ->> CASE WHEN type = 'residence' THEN 'dataPriobrPP' ELSE 'dataPriobrIG' END, 'YYYY-MM-DD') AS acquisition_date,
    data -> 'oksm' ->> 'kodOKSM' AS oksm_code,
    type,
    (type = 'residence')  AS has_foreign_residence,
    (type = 'citizenship') AS has_foreign_citizenship
FROM filtered_data;
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_output_CF</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>source_update_date</column_name>
        <stream_name>source_update_date</stream_name>
      </field>
      <field>
        <column_name>basis</column_name>
        <stream_name>basis</stream_name>
      </field>
      <field>
        <column_name>acquisition_date</column_name>
        <stream_name>acquisition_date</stream_name>
      </field>
      <field>
        <column_name>oksm_code</column_name>
        <stream_name>oksm_code</stream_name>
      </field>
      <field>
        <column_name>type</column_name>
        <stream_name>type</stream_name>
      </field>
      <field>
        <column_name>source_id</column_name>
        <stream_name>source_id</stream_name>
      </field>
    </fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>Y</specify_fields>
    <table>citizenship_foreign</table>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>896</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_citizen_has_foreign_citizenship</name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <ignore_flag_field/>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_foreign_citizenship</name>
        <rename>has_foreign_citizenship</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>896</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_citizen_has_foreign_residence</name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <ignore_flag_field/>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_foreign_residence</name>
        <rename>has_foreign_residence</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>896</xloc>
      <yloc>640</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>table_output_CF</source_transform>
      <target_transform>change_job_status_on_error_CF</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>update_citizen_has_foreign_citizenship</source_transform>
      <target_transform>change_job_status_on_error_CF</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>update_citizen_has_foreign_residence</source_transform>
      <target_transform>change_job_status_on_error_CF</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
