<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Core_JobManager
{
    /**
     * @param TB_Shared_Ent_TeamData_Appointment $appointment
     */
    public static function createForAppointment( TB_Shared_Ent_TeamData_Appointment $appointment )
    {
        if ( $appointment->state === TB_Shared_Ent_TeamData_Appointment::STATE_DRAFT ) return;

        try
        {
            $refId = TB_Server_Job_AppointmentReminder::getRefId( $appointment->id );
            TB_Shared_Ent_TeamData_Job::deleteByRefId( $refId );
            $triggerDt = clone $appointment->start_dt;
            $triggerDt->modify( '-1 hour' );

            $jobEntity = new TB_Shared_Ent_TeamData_Job();
            $jobEntity->ref_id = $refId;
            $jobEntity->job_name = 'appointmentReminder';
            $jobEntity->job_status = TB_Shared_Ent_TeamData_Job::JOB_STATUS_READY;
            $jobEntity->job_data_js = array( 'appointmentId' => $appointment->id );
            $jobEntity->trigger_dt = $triggerDt;
            $jobEntity->save();
        }
        catch( \Exception $e )
        {
            TB_Server_Utils_Log::get()->log( $e );
        }
    }
}