ユーザーアイコン

mizuko

2か月前

0
0

Calendar Watch 通知からの データ更新フロー

バックエンド
Google Calendar API
Webhook

Google Calendar の変更通知を受信して Activity レコードを更新する処理フロー。

  1. Webhook エンドポイントで通知受信

    @Post('google') @HttpCode(HttpStatus.OK) async handleGoogleCalendarWebhook( @Headers() headers: GoogleCalendarNotificationHeaders, @Body() _body: any, ): Promise<void> { const channelId = headers['x-goog-channel-id']; const resourceState = headers['x-goog-resource-state']; if (resourceState !== 'sync') { // 非同期で同期処理を実行 setImmediate(() => { this.calendarSyncService.syncFromGoogleCalendar(channelId, resourceId) .catch(error => this.logger.error('同期エラー', error)); }); } }
  2. Activity の更新処理

    private async updateDataFromEvent( event: calendar_v3.Schema$Event, data: DataEntity ): Promise<void> { const updates: Partial<DataEntity> = { name: event.summary || data.name, startDateTime: event.start?.dateTime ? new Date(event.start.dateTime) : undefined, endDateTime: event.end?.dateTime ? new Date(event.end.dateTime) : undefined, location: event.location, description: event.description }; await this.dataRepository.update(data.id, updates); }
  3. 同期ループの回避

    • Google Calendar 側で更新されたイベントのみ処理
    • 自システムから更新したイベントはスキップ