Skip to content

Instantly share code, notes, and snippets.

@iaditya
Last active May 11, 2017 04:05
Show Gist options
  • Save iaditya/895818e19aac7a6e6d36a277003aab4f to your computer and use it in GitHub Desktop.
Save iaditya/895818e19aac7a6e6d36a277003aab4f to your computer and use it in GitHub Desktop.
public function appointments()
{
/**
* run on terminal
*
* bin/cake Alerts appointments arg[minutes]
*
*/
$notificationManager = new NotificationManager();
$duration = 60;
$start = Time::now()->toDateTimeString();
$end = Time::now()->addMinutes($duration)->toDateTimeString();
$after_one_day = 1440;
Log::debug("appointments start: ".$start);
$this->out(print_r("Start time to {$start} .", true));
$this->out(print_r("end time to {$end} .", true));
Log::debug("appointments end: ".$end);
$events = $this->Events->find("all")
->where(['Events.status' => 'Confirmed'])
->andWhere(['Events.start BETWEEN :now AND :then'])
->bind(':now', $start, 'time')
->bind(':then', $end, 'time');
$tomorrow_start = Time::now()->addMinutes($after_one_day)->toDateTimeString();
$tomorrow_end = Time::now()->addMinutes($duration + $after_one_day)->toDateTimeString();
$tomorrow_events = $this->Events->find("all")
->where(['Events.status' => 'Confirmed'])
->andWhere(['Events.start BETWEEN :now AND :then'])
->bind(':now', $tomorrow_start, 'time')
->bind(':then', $tomorrow_end, 'time');
$i= 0;
foreach ($events as $event) {
$user_id = $this->Patients->get($event->patient_id)->user_id;
$start = $event->start;
$notificationManager->addRecipientList('sendAppointmentAlert', [$user_id]);
$patient = $this->Patients->findById($event->patient_id)->first();
$user = $this->Users->findById($patient->user_id)->first();
$provider = $this->Providers->findById($event->provider_id)->first();
$provider_name = $this->Users->findById($provider->user_id)->first()->first_name;
$facility_name = $this->Facilities->findById($event->facility_id)->first()->name;
$this->out(print_r("notifying {$user->first_name} provider {$provider_name} .", true));
$this->Notifier->notify([
'recipientLists' => 'sendAppointmentAlert',
'template' => 'sendAppointmentAlert',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'provider_name' => $provider_name,
'facility_name' => $facility_name,
'date' => $start->format('d-m-Y'),
'time' => $start->format('h:i A'),
],
], $user_id);
$i++;
Log::debug("Alert Sent For One Hour Before:" . $user->first_name . " facility" . $facility_name . " provider_id:" . $provider_name);
}
$this->out(print_r("Send alert to {$i} users.", true));
$i= 0;
foreach ($tomorrow_events as $tomorrow_event) {
$user_id = $this->Patients->get($tomorrow_event->patient_id)->user_id;
$start = $tomorrow_event->start;
$notificationManager->addRecipientList('sendAppointmentAlert', [$user_id]);
$patient = $this->Patients->findById($tomorrow_event->patient_id)->first();
$user = $this->Users->findById($patient->user_id)->first();
$provider = $this->Providers->findById($tomorrow_event->provider_id)->first();
$provider_name = $this->Users->findById($provider->user_id)->first()->first_name;
$facility_name = $this->Facilities->findById($tomorrow_event->facility_id)->first()->name;
$this->out(print_r("notifying {$user->first_name} provider {$provider_name} .", true));
$this->Notifier->notify([
'recipientLists' => 'sendAppointmentAlert',
'template' => 'sendAppointmentAlert',
'vars' => [
'name' => $user->first_name,
'username' => $user->username,
'provider_name' => $provider_name,
'facility_name' => $facility_name,
'date' => $start->format('d-m-Y'),
'time' => $start->format('h:i A'),
],
], $user_id);
$i++;
Log::debug("Alert Sent For One Day Before:" . $user->first_name . " facility" . $facility_name . " provider_id:" . $provider_name);
}
$this->out(print_r("Send alert to {$i} users.", true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment