--- data-clients

-- crms
INSERT INTO crms (
  project_id,
  name,
  type,
  deleted,
  isimple_url,
  created_at,
  updated_at)
  SELECT
    projects.id,
    'iSimple Bank',
    'isimple',
    0,
    '',
    sysdatetimeoffset(),
    sysdatetimeoffset()
  FROM projects;

-- crm_customers
INSERT INTO crm_customers (
  crm_id,
  external_id,

  name,
  first_name,
  middle_name,
  last_name,

  sex,
  birthday,
  cellphone,
  email,

  created_at,
  updated_at
) SELECT
    (SELECT max(id)
     FROM crms),
    integration_id,

    name,
    name,
    '',
    '',

    '',
    '',
    '',
    '',

    created_at,
    updated_at
  FROM clients
  WHERE integration_id IS NOT NULL;

-- clients
UPDATE clients
SET customer_id = (
  SELECT id
  FROM crm_customers
  WHERE external_id = integration_id)
WHERE integration_id IS NOT NULL;
