/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2015 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
                       := 'Favorites';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Adds the entity to Favorites'
    ,p_path               => '/favorites'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'SaveVacancyFavorite'
    ,p_description        => 'Saves Vacancy to User Favorites'
    ,p_methods            => 'POST'
    ,p_path               => '/vacancies/{vacancyId}'
    ,p_plsql              =>
      q'[
          DECLARE
          
         
          l_resp           xxas_http_resp_obj;
          l_job_favorite  VARCHAR2(10);
          
        BEGIN
        
          IF  fnd_global.user_id = 6 OR
          NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_EMP_VAC_FAV', {pathParam.vacancyId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
            l_job_favorite := {requestParam.favorite};
            
            IF l_job_favorite = 'true' THEN
              xxas_job_favorites_pkg.add_to_favorite
                (p_person_id   => fnd_global.employee_id
                ,p_vacancy_id  => {pathParam.vacancyId} 
                );
            ELSIF l_job_favorite = 'false' THEN
              xxas_job_favorites_pkg.remove_from_favorites
                (p_person_id   => fnd_global.employee_id
                ,p_vacancy_id  => {pathParam.vacancyId} 
                );              
            END IF;
            
            l_resp := xxas_http_response.ok().build();

          END IF;

          {response} := l_resp;
          
        END
      ]'
    );

END;
/
COMMIT;
EXIT;
