text: -- Deploy org:function/build_organization_location to pg
  -- requires: structure/organization
  
  BEGIN;
  
    create function org_fn.build_organization_location(
      _organization_id uuid
      ,_name text
      ,_address1 text
      ,_address2 text
      ,_city text
      ,_state text
      ,_zip text
      ,_lat text
      ,_lon text
    )
    returns org.organization as $$
    declare
      _app_user auth.app_user;
      _organization org.organization;
      _location org.location;
    begin
      _app_user := auth_fn.current_app_user();
      
      SELECT *
      INTO _organization
      FROM org.organization o
      WHERE id = _organization_id
      AND auth_fn.app_user_has_access(o.app_tenant_id)
      ;
  
      IF _organization.id IS NULL THEN
        RAISE EXCEPTION 'No organization for id: %', _organization_id;
      END IF;
      
      IF _organization.location_id IS NULL THEN
        _location := org_fn.build_location(
          _name
          ,_address1
          ,_address2
          ,_city
          ,_state
          ,_zip
          ,_lat
          ,_lon
        );
  
        UPDATE org.organization
        SET location_id = _location.id
        WHERE id = _organization_id
        RETURNING *
        INTO _organization
        ;
      ELSE
        _location := org_fn.modify_location(
          _organization.location_id
          ,_name
          ,_address1
          ,_address2
          ,_city
          ,_state
          ,_zip
          ,_lat
          ,_lon
        )
        ;
      END IF;
  
      RETURN _organization;
  
    end;
    $$ language plpgsql strict security definer;
  
    GRANT EXECUTE ON FUNCTION org_fn.build_organization_location(
      uuid
      ,text
      ,text
      ,text
      ,text
      ,text
      ,text
      ,text
      ,text
    ) TO app_user;
  
  COMMIT;
  