# Real check: Google Calendar connect, sync, and meetings created from the CRM

The tests use a fake Google Calendar, so connecting a real account has to be checked once
(HUB-173), and so does creating meetings with Google Meet (HUB-174). Run it on staging, or locally with the Google app's settings.

## What you need

- The Google Cloud setup from `deploy/README.md` ("Google Calendar"): the Calendar API enabled,
  the `calendar.readonly` and `calendar.events` scopes on the consent screen, and the callback redirect URI registered.
- The API and the worker running, with `API_URL`, `APP_URL`, `GOOGLE_CLIENT_ID` and
  `GOOGLE_CLIENT_SECRET` set.
- A Google Workspace account you can sign in to, with a few meetings in its calendar in the next
  days, one of them with a guest whose address is a contact in the CRM.
- Database access (to look at a few rows).

## Steps

1. **Connect.** Swagger (`/docs`), sign in (or use the dev login headers), then
   `POST /api/v1/calendars/google/connect` and open the returned `authUrl`. Approve. The browser
   lands on `${APP_URL}/settings/calendar?connected=1`. `GET /api/v1/calendars/me` shows
   `syncing`, then `connected` within a minute.

   If it shows `error` with "Google returned no sync token", Google does not give a sync token for
   a sync limited to a time window (the tests assume it does). Stop and tell the team: the initial
   sync must then get its token another way before calendar sync is used.

2. **My meetings.** `GET /api/v1/calendars/events?from=<today>T00:00:00Z&to=<in 7 days>T00:00:00Z`
   lists the meetings of the coming week, with the same titles, times and guests as Google
   Calendar. An all-day event shows `allDay: true`; a repeating meeting shows each occurrence.

3. **Meeting on the contact.** Take the contact who is a guest of one meeting:
   `GET /api/v1/calendars/meetings?recordType=contact&recordId=<contact id>` lists that meeting.
   If the contact has a primary company or an open deal, the same call with `recordType=company`
   or `recordType=deal` and that record's id lists it too. A meeting with no CRM contact appears
   on no record.

4. **Changes follow.** In Google Calendar, rename one meeting and delete another. Within 5 minutes
   the events list shows the new title and no longer shows the deleted meeting.

5. **Contact added later.** Create a contact with the address of a guest of a meeting who is not
   in the CRM yet. Within 5 minutes that meeting is listed for the new contact (step 3).

6. **Restart.** Restart the worker (`pm2 restart crm-worker`), change a meeting in Google
   Calendar, and check that it is updated within 5 minutes. The sync continues from where it was:

   ```sql
   SELECT status, last_synced_at FROM calendar_connections WHERE email_address = '<account>';
   SELECT sync_token IS NOT NULL AS has_token FROM calendars
   WHERE connection_id = (SELECT id FROM calendar_connections WHERE email_address = '<account>');
   ```

   `status` is `connected`, `has_token` is `1`, and `last_synced_at` is recent.

7. **Create a meeting (HUB-174).** `POST /api/v1/meetings` with a time tomorrow, your second
   address in `attendees` and a contact in `links`:

   ```json
   {
     "title": "CRM meeting check",
     "start": "<tomorrow>T15:00:00Z",
     "end": "<tomorrow>T15:30:00Z",
     "timezone": "America/Chicago",
     "attendees": ["<your second address>"],
     "links": [{ "type": "contact", "id": "<contact id>" }]
   }
   ```

   The answer has `googleStatus: "synced"` and a `meetingUrl` on `meet.google.com`. The meeting
   is in Google Calendar with that Meet link, the second address received the invitation, and the
   Meet link opens a call. `GET /api/v1/meetings/<id>` shows it linked to the contact.

8. **Change and cancel it.** `PATCH /api/v1/meetings/<id>` with a new `start` / `end`: Google
   Calendar shows the new time and the attendee gets an updated invitation. Accept it from the
   second address; after the next sync (a few minutes) the meeting's `attendees` show
   `accepted`. Then `DELETE /api/v1/meetings/<id>`: the event is gone from Google Calendar, the
   attendee gets a cancellation, and the meeting shows `status: "cancelled"`.

9. **Disconnect.** `DELETE /api/v1/calendars/me`: the CRM no longer appears under the account's
   Google "Third-party apps with account access", and the events list is empty.

## Result

Fill this in and attach screenshots of steps 2, 3 and 7 to the tickets (HUB-173, HUB-174).

|                                                   |                     |
| ------------------------------------------------- | ------------------- |
| Date, environment                                 |                     |
| Who ran it                                        |                     |
| Connected and synced (step 1)?                    |                     |
| Meetings match Google Calendar (step 2)?          |                     |
| Meeting on the contact, company, deal (step 3)?   |                     |
| Rename and delete followed (step 4)?              |                     |
| Contact added later linked (step 5)?              |                     |
| Sync continued after the worker restart (step 6)? |                     |
| Meeting in Google with a Meet link, invited (7)?  |                     |
| Change, response and cancel followed (step 8)?    |                     |
| Access removed on disconnect (step 9)?            |                     |
| Screenshot                                        | attached to HUB-173 |
