ST80
ST80

Reputation: 3903

Laravel How to update a calendar event by using Cronofy API

In my laravel-application I want to include my google-calendar by using the Cronofy API and the package from cronofy-php.

Now when I try to update an event, the event gets duplicated and I don't really know why:

Here is my code:

public function updateEvent(){

   $cronofy = new Cronofy\Cronofy([
      "client_id" => config('cronofy.client_id'),
      "client_secret" => config('cronofy.client_secret'),
      "access_token" => $access_token->value,
      "refresh_token" => $refresh_token->value
   ]);


   $new_start_date = date("Y-m-d\TH:m:s\Z", strtotime(request()->event['changes']['start']['_date']));
   $new_end_date = date("Y-m-d\TH:m:s\Z", strtotime(request()->event['changes']['end']['_date']));

    $params = [
       'calendar_id' => (string)request()->event['schedule']['calendarId'],
       'event_id' => (string)request()->event['schedule']['id'],
       'summary' => request()->event['schedule']['title'],
       'description' => request()->event['schedule']['body'],
       'start' =>  (object)['time' => $new_start_date, 'tzid' => 'Etc/UTC'],
       'end' => (object)['time' => $new_end_date, 'tzid' => 'Etc/UTC'],
   ];

   $updated_event = $cronofy->upsertEvent($params);

   return response(['success' => true, 'event' => $params]);

}

As mentioned above, this creates a duplicate of the event, which I intentionally want to update/edit.

Can someone help me out?

Upvotes: 0

Views: 263

Answers (1)

So the way I am seeing it you are creating a new Cronofy Object and afterwards upserting it which will lead to it being inserted. To only edit the existing calendar event you should use edit the already existing Object
So instead of:

public function updateEvent(){

$cronofy = new Cronofy\Cronofy([
      "client_id" => config('cronofy.client_id'),
      "client_secret" => config('cronofy.client_secret'),
      "access_token" => $access_token->value,
      "refresh_token" => $refresh_token->value
   ]);
...

It should be something like

public function updateEvent($id){

$cronofy = Cronofy\Cronofy::find($id);

$new_start_date = date("Y-m-d\TH:m:s\Z", strtotime(request()->event['changes']['start']['_date']));
$new_end_date = date("Y-m-d\TH:m:s\Z", strtotime(request()->event['changes']['end']['_date']));

$params = [
   'calendar_id' => (string)request()->event['schedule']['calendarId'],
   'event_id' => (string)request()->event['schedule']['id'],
   'summary' => request()->event['schedule']['title'],
   'description' => request()->event['schedule']['body'],
   'start' =>  (object)['time' => $new_start_date, 'tzid' => 'Etc/UTC'],
   'end' => (object)['time' => $new_end_date, 'tzid' => 'Etc/UTC'],
];

$updated_event = $cronofy->upsertEvent($params);
...

I hope I could help

Upvotes: 0

Related Questions