/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2016 Applaud Solutions UK Ltd                |
 |                             All rights reserved                            |
 *===========================================================================*/

SET VERIFY OFF
SET FEEDBACK OFF
SET SCAN OFF
WHENEVER OSERROR  EXIT FAILURE ROLLBACK
WHENEVER SQLERROR EXIT FAILURE ROLLBACK

DECLARE

  --
  -- Local constants and variables
  --
  l_SERVICE CONSTANT xxas_com_service_repository.service_name%TYPE
                       := 'PersonContactNumbers';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Person contact numbers'
    ,p_path               => 'people/{personId}/contact-numbers'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPersonContactNumbers'
    ,p_description        => 'Gets a person''s contact numbers'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
      SELECT XMLElement
               ("data"
               ,XMLAttributes
                 ('true' as "json:force-array"
                 ,'http://json.org/' as "xmlns:json")
               ,XMLAgg
                 (XMLElement
                   ("item"
                   ,XMLForest
                     ('flexible' "type"
                     ,cd.contact_type_name "line2"
                     ,cd.contact_data "line1"
                     ,cd.icon "icon"
                     ,CASE WHEN cd.contact_type_code = 'EMAIL' THEN
                                'mailto:' || cd.contact_data
                           WHEN cd.contact_type_code IN ('CELL', 'MOBILE', 'LANDLINE') THEN
                                'tel:' || cd.contact_data
                      END "href"
                     ,xxas_com_contact_pkg.item_actions
                       (cd.contact_type_code, cd.icon, cd.contact_data).get_xml() "linked"
                     )
                   )
                 )
               ) "data"
      FROM     TABLE(xxas_com_contact_pkg.contact_details_tbl
                      ({pathParam.personId}, trunc(sysdate))) cd
      ORDER BY cd.order_sequence
        ]'
    );
    
END;
/
COMMIT;
EXIT;