Changes between Version 8 and Version 9 of AdvancedConcepts


Ignore:
Timestamp:
06/15/26 22:40:50 (13 hours ago)
Author:
231093
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedConcepts

    v8 v9  
    157157    v_request_id int4,
    158158    v_dispatcher_user_id int4,
     159    v_driver_user_id int4,
    159160    v_price numeric(19, 2),
    160161    v_currency_catalog_id int4,
     
    165166$$
    166167declare
    167     v_driver_user_id        int4;
    168168    v_dispatcher_company_id int4;
    169169    v_start_position        geometry;
    170170    v_customer_user_id      int4;
     171    v_computed_driver_id    int4;
    171172begin
    172173    if not exists(select * from request where request.id = v_request_id and request.status = 'pending') then
     
    182183    v_customer_user_id := (select customer_user_id from request where request.id = v_request_id);
    183184
    184     v_dispatcher_company_id := (select eh.company_id
    185                                 from dispatcher d
    186                                          join employmenthistory eh on eh.employee_user_id = d.user_id and
    187                                                                       (eh.end_date is null or eh.end_date > now())
    188                                 where d.user_id = v_dispatcher_user_id
    189                                 limit 1);
    190 
    191     if v_dispatcher_company_id is null then
    192         raise exception 'Dispatcher % is not assigned to a company',
    193             v_dispatcher_user_id;
    194     end if;
    195 
    196     v_start_position := (select start_location from request where id = v_request_id);
    197 
    198     v_driver_user_id := (select d.user_id
    199                          from driver d
    200                                   join driver_vehicle dc on d.user_id = dc.id_driver
    201                                   join employmenthistory eh
    202                                        on eh.employee_user_id = d.user_id and (eh.end_date is null or
    203                                                                                eh.end_date > now())
    204                          where dc.time_to is null
    205                            and eh.company_id = v_dispatcher_company_id
    206                            and d.location is not null
    207                          order by d.location <-> v_start_position
    208                          limit 1);
    209 
    210     if v_driver_user_id is null then
    211         raise exception 'No available drivers found for request %', v_request_id;
     185    if v_dispatcher_user_id is not null then
     186        v_dispatcher_company_id := (select eh.company_id
     187                                    from dispatcher d
     188                                             join employmenthistory eh on eh.employee_user_id = d.user_id and
     189                                                                          (eh.end_date is null or eh.end_date > now())
     190                                    where d.user_id = v_dispatcher_user_id
     191                                    limit 1);
     192
     193        if v_dispatcher_company_id is null then
     194            raise exception 'Dispatcher % is not assigned to a company', v_dispatcher_user_id;
     195        end if;
     196
     197        v_start_position := (select start_location from request where id = v_request_id);
     198
     199       
     200        v_computed_driver_id := (select d.user_id
     201                                 from driver d
     202                                          join driver_vehicle dc on d.user_id = dc.id_driver
     203                                          join employmenthistory eh
     204                                               on eh.employee_user_id = d.user_id and (eh.end_date is null or
     205                                                                                       eh.end_date > now())
     206                                 where dc.time_to is null
     207                                   and eh.company_id = v_dispatcher_company_id
     208                                   and d.location is not null
     209                                 order by d.location <-> v_start_position
     210                                 limit 1);
     211
     212        if v_computed_driver_id is null then
     213            raise exception 'No available drivers found for request %', v_request_id;
     214        end if;
     215
     216    else
     217        if v_driver_user_id is null then
     218            raise exception 'A driver_user_id must be specified if dispatcher is null';
     219        end if;
     220
     221        if not exists(select 1 from freelancedriver where driver_user_id = v_driver_user_id) then
     222            raise exception 'Driver % is not a registered freelance driver', v_driver_user_id;
     223        end if;
     224
     225        v_computed_driver_id := v_driver_user_id;
    212226    end if;
    213227
    214228    insert into offer(status, created_at, request_id, dispatcher_user_id, driver_user_id, price, currency_catalog_id,
    215229                      eta, customer_user_id)
    216     values ('pending', now(), v_request_id, v_dispatcher_user_id, v_driver_user_id,
     230    values ('pending', now(), v_request_id, v_dispatcher_user_id, v_computed_driver_id,
    217231            v_price, v_currency_catalog_id, v_eta, v_customer_user_id);
     232           
    218233    commit;
    219234end;