# Microsoft Teams Integration Setup

Use this note for the Microsoft Teams / Microsoft Graph integration.

## Current Scope

This package supports the Microsoft Graph channel integration and the CRM-side callback needed for real-time Teams calling screen-pop:

- Admin-initiated Microsoft sign-in from `/admin/integrations`.
- OAuth 2.0 authorization-code callback at `/api/admin/microsoft/callback`.
- Encrypted delegated access/refresh token storage.
- Microsoft Graph `/me` connection test.
- Admin disconnect and audit logging.
- Teams channel notifications through Microsoft Graph or an incoming webhook.
- CRM call center at `/admin/calls`.
- Manual Microsoft Graph call-record sync from `/admin/calls`.
- Calling-bot callback at `/api/integrations/teams/calling-bot`.
- Generic telephony bridge callback at `/api/integrations/teams/calls`.

Teams user synchronization, calendar sync, call records, recordings, transcription, and AI summaries are separate follow-up phases.

## Current Azure App Registration

- Display name: `Microcode CRM - Microsoft 365 Integration`
- Tenant ID: `43a29103-c098-49bf-985f-c57f61462ce8`
- Application client ID: `70aa82a5-da70-47f0-95ef-1cd815aee177`
- Supported account type: `My organization only`

Do not store the client secret in source control, release archives, screenshots, or tickets. If a secret is pasted into chat or a shared document, rotate it in Azure before production use.

## Environment Variables

Set these only in the production server environment or private `.env`:

```env
TEAMS_ENABLED=true
TEAMS_TENANT_ID=43a29103-c098-49bf-985f-c57f61462ce8
TEAMS_CLIENT_ID=70aa82a5-da70-47f0-95ef-1cd815aee177
TEAMS_CLIENT_SECRET=<rotated-client-secret>
TEAMS_REDIRECT_URI=https://crm.microcode.email/api/admin/microsoft/callback
TEAMS_CALLING_BOT_APP_ID=<azure-bot-application-id>
TEAMS_CALLING_BOT_VALIDATE_TOKEN=true
```

If the Teams calling bot uses the same Azure app registration as the Graph integration, `TEAMS_CALLING_BOT_APP_ID` can match `TEAMS_CLIENT_ID`. For production, keep `TEAMS_CALLING_BOT_VALIDATE_TOKEN=true` so Microsoft callback tokens are validated before CRM creates a call event.

## Production Checks

When `NODE_ENV=production` and `TEAMS_ENABLED=true`, `npm run preflight:production` validates:

- Tenant ID format
- Client ID format
- Client secret is non-placeholder and at least 32 characters
- Redirect URI is a valid HTTP(S) URL

## Azure Checklist

- Add the production redirect URI in Azure before enabling the integration.
- Grant only the Microsoft Graph permissions required by the final feature.
- For call-record sync, add Microsoft Graph **Application permission** `CallRecords.Read.All` and grant admin consent.
- The normal user sign-in uses delegated permissions; call-record sync uses the server-side application token.
- Record the secret expiry date in the admin handoff notes.
- Rotate client secrets before expiry and after accidental disclosure.

## Real-Time Incoming Call Screen-Pop

The CRM endpoint is ready:

```text
POST https://crm.microcode.email/api/integrations/teams/calling-bot
```

Use this URL as the Teams calling bot webhook/callback URL when registering the bot for calls and meetings.

Microsoft sends incoming call notifications to that URL. The CRM validates the callback token when `TEAMS_CALLING_BOT_VALIDATE_TOKEN=true`, extracts the caller number when present, creates a `call_events` record, and the admin panel polls for unacknowledged ringing/answered calls. Matching is done against Leads and Sales phone numbers.

Expected CRM behavior:

- Incoming call notification arrives.
- CRM creates a call event with provider `teams-calling-bot`.
- `/admin/calls` shows the event.
- The admin layout shows a screen-pop notification.
- The popup opens the matched lead or sale if the number exists in CRM.

The generic fallback endpoint remains available for any PBX, SBC, Power Automate, or third-party telephony bridge:

```text
POST https://crm.microcode.email/api/integrations/teams/calls
Authorization: Bearer <LEAD_SYNC_SECRET>
Content-Type: application/json

{
  "externalId": "call-123",
  "provider": "teams",
  "direction": "incoming",
  "status": "ringing",
  "phone": "+15550100",
  "customerName": "Optional Name"
}
```

## Azure Calling Bot Checklist

- Register a Teams bot with calling and meeting support.
- Set the calling webhook URL to `https://crm.microcode.email/api/integrations/teams/calling-bot`.
- Configure the bot application ID in `TEAMS_CALLING_BOT_APP_ID`.
- Enable token validation with `TEAMS_CALLING_BOT_VALIDATE_TOKEN=true`.
- Grant the required Graph cloud communications permissions for the selected bot architecture.
- Route or forward the Teams Phone number/call queue/auto attendant flow to the bot if real-time ringing events must reach CRM.
- Test with a real incoming call and confirm `/admin/calls` receives a `teams-calling-bot` event.
