<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>deputy_flow_repeat</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <pipeline_status>0</pipeline_status>
    <parameters>
      <parameter>
        <name>IDM_ID</name>
        <default_value>b5265855-1972-4ff5-8b5e-0f746127fa70</default_value>
        <description/>
      </parameter>
      <parameter>
        <name>JOB_NAME</name>
        <default_value>deputy_job</default_value>
        <description/>
      </parameter>
    </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/09/14 15:17:48.470</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/09/14 15:17:48.470</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Create job execution record</from>
      <to>deputy_input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_flag_update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_upsert</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_input</from>
      <to>deputy_flag_update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_input</from>
      <to>deputy_upsert</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>deputy_upsert</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT</abort_option>
    <always_log_rows>Y</always_log_rows>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1072</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on error</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>816</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on success</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>1072</xloc>
      <yloc>336</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Create job execution record</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 (job_name, recruitment_id, status, execution_datetime, error_description)
VALUES (
           '${JOB_NAME}',
           '${IDM_ID}',
           'PROCESSING',
           current_timestamp,
           NULL
       )
ON CONFLICT (job_name, recruitment_id)
    DO UPDATE SET
                  status = EXCLUDED.status,
                  execution_datetime = EXCLUDED.execution_datetime,
                  error_description = EXCLUDED.error_description;</sql>
    <attributes/>
    <GUI>
      <xloc>368</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>576</xloc>
      <yloc>336</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter rows</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</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1072</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform>
    <name>deputy_flag_update</name>
    <type>Update</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>N</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_deputy_info</name>
        <rename>has_deputy_info</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>576</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>deputy_input</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</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 filteredData AS (SELECT ri.recruit_id,
                             ri.info -> 'svedFL' -> 'svedDeputat' -> 'deputat' as deputy_elem,
				 ctid
                      FROM ervu_dashboard.recruits_info ri
                      WHERE ri.info -> 'svedFL' -> 'svedDeputat' ->> 'deputat' &lt;&gt; 'null'
                        AND (ri.current_recruitment_id = '${IDM_ID}' or (ri.current_recruitment_id is null and ri.target_recruitment_id = '${IDM_ID}'))
)
SELECT fd.recruit_id,
       deputy_elem ->> 'id'                                 AS source_id,
       to_date(deputy_elem ->> 'dataSved', 'YYYY-MM-DD')    AS source_update_date,
       deputy_elem -> 'extend' ->> 'grazhdUID'              AS citizen_unique_id,
       deputy_elem ->> 'priznakAktualNaim'                  AS actual_status_name,
       to_date(deputy_elem ->> 'dataIzbran', 'YYYY-MM-DD')  AS election_date,
       to_date(deputy_elem ->> 'dataOkonch', 'YYYY-MM-DD')  AS end_date,
       deputy_elem ->> 'prichinaDosrochKod'                 AS termination_reason_code,
       to_date(deputy_elem ->> 'dataDosroch', 'YYYY-MM-DD') AS termination_date,
	true as has_deputy_info,
	fd.ctid = (select max(ctid) from filteredData) as last_row
FROM filteredData fd;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>576</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform>
    <name>deputy_upsert</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>source_id</field>
        <name>source_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>deputy</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>source_id</name>
        <rename>source_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>source_update_date</name>
        <rename>source_update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>citizen_unique_id</name>
        <rename>citizen_unique_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>actual_status_name</name>
        <rename>actual_status_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>election_date</name>
        <rename>election_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>end_date</name>
        <rename>end_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>termination_reason_code</name>
        <rename>termination_reason_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>termination_date</name>
        <rename>termination_date</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>816</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>deputy_flag_update</source_transform>
      <target_transform>Change job status on error</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>deputy_upsert</source_transform>
      <target_transform>Change job status on error</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>
