axtg's avatar
Level 5

Anyone experience working with ICS files?

Hi,

I have a node script setup to parse incoming ICS calendar files. It's payload, delivered to a Laravel webhook endpoint is this:

'invite' => 
    array (
      'id' => '[email protected]',
      'status' => 'CONFIRMED',
      'sequence' => '0',
      'title' => 'Series test',
      'organizer' => 'organizer@mail',
      'mailbox' => 'admin'
      'labels' => 
      array (
      ),
      'description' => '',
      'location' => '',
      'attendees' => 
      array (
        0 => 'mail1',
        1 => 'mail2',
        2 => 'mail3',
      ),
    ),
    'runs_at' => 
    array (
      'timezone' => 'Etc/UTC',
      'dates' => 
      array (
        0 => '2024-09-02T10:00:00+00:00',
        1 => '2024-09-09T10:00:00+00:00',
        2 => '2024-09-16T10:00:00+00:00',
        3 => '2024-09-23T10:00:00+00:00',
        4 => '2024-09-30T10:00:00+00:00',
      ),
      'duration' => 30,
      'string' => 'every week on Monday for 5 times (~ approximate)',
    ),
  ),

I'm successfully storing these as they come in (foreach on dates basically). And when updating, check the ID and update the payload.

Yet, and here it comes, Calendar providers update the ID to a new ID when you change something mid-series (+ forward). So when you hit - as a user - update this and future meetings, you get a new ID. And so I'm struggling to keep the original ones (e.g. 1 and 2), while creating a new set for 3, 4 and 5 and removing those (3, 4 and 5) from the original set.

Anyone seen this? Tips, tricks? Nudges?

0 likes
1 reply
LaryAI's avatar
Level 58

To handle the scenario where calendar providers update the ID for future events in a series, you can implement a strategy to track and manage the original and new event IDs. Here's a step-by-step approach to achieve this:

  1. Store Original and New IDs: Maintain a mapping of original event IDs to their new IDs. This will help you track which events have been updated and which ones remain unchanged.

  2. Update Logic: When an update is received, check if the event ID is new. If it is, update the mapping and adjust the events accordingly.

  3. Database Schema: Ensure your database schema can handle the mapping of original to new IDs. You might need an additional table or fields to store this information.

  4. Code Implementation: Below is a sample implementation in Laravel to illustrate the concept.

Step 1: Database Migration

Create a migration to add a new table for tracking event ID mappings.

php artisan make:migration create_event_id_mappings_table

In the migration file:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEventIdMappingsTable extends Migration
{
    public function up()
    {
        Schema::create('event_id_mappings', function (Blueprint $table) {
            $table->id();
            $table->string('original_id');
            $table->string('new_id');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('event_id_mappings');
    }
}

Run the migration:

php artisan migrate

Step 2: Model for Event ID Mapping

Create a model for the event_id_mappings table.

php artisan make:model EventIdMapping

In the EventIdMapping model:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class EventIdMapping extends Model
{
    use HasFactory;

    protected $fillable = ['original_id', 'new_id'];
}

Step 3: Update Logic in Controller

In your controller, handle the incoming ICS data and update the mappings.

Step 4: Handling Event Updates

When an event is updated, ensure you check the mapping and update the events accordingly.

public function updateEvent(Request $request)
{
    $payload = $request->input('invite');
    $originalId = $payload['id'];
    $newId = $this->generateNewId($originalId);

    // Check if the ID has been updated
    $mapping = EventIdMapping::where('original_id', $originalId)->first();
    if ($mapping) {
        // Update the mapping with the new ID
        $mapping->update(['new_id' => $newId]);
    } else {
        // Create a new mapping
        EventIdMapping::create([
            'original_id' => $originalId,
            'new_id' => $newId,
        ]);
    }

    // Update the event in your database
    // ...
}

This approach ensures that you can track and manage the original and new event IDs, allowing you to handle updates to recurring events effectively.

Please or to participate in this conversation.