Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(api): add microsoft sign-in and outlook mailbox sync (#73)
github-actions[bot]2a0062fb76ff
diff --git a/.env.example b/.env.exampleindex 738995cc..8770db78 100644--- a/.env.example+++ b/.env.example@@ -25,12 +25,34 @@ ALLOWED_SIGN_IN=""# reads Gmail and Calendar. Set both or neither — half a pair is a sign-in# button that fails at Google.#-# Leave them empty only if you sign in with your own identity provider, added-# on Settings → SSO. Then there is no Google button and no mail sync, and the-# sign-in page says as much rather than showing you nothing.+# Leave them empty only if you sign in with Microsoft below, or with your own+# identity provider added on Settings → SSO. With none of the three there is no+# way in at all, and the sign-in page says as much rather than showing you+# nothing.GOOGLE_CLIENT_ID=""GOOGLE_CLIENT_SECRET=""+# Microsoft 365 / Entra ID — the other sign-in method, and the same app+# registration is what reads Outlook mail. Set both or neither, exactly like+# the Google pair.+#+# Create the app at https://portal.azure.com → Microsoft Entra ID → App+# registrations, add the redirect URI <API_URL>/api/auth/callback/microsoft,+# and give it the delegated Graph permissions User.Read and Mail.Read. The+# README has the full walkthrough.+#+# You can set Google and Microsoft together: the sign-in page offers both, and+# a rep's mail is read from whichever they signed in with.+# MICROSOFT_CLIENT_ID=""+# MICROSOFT_CLIENT_SECRET=""++# Which Entra tenant may sign in. "common" (the default) accepts any work,+# school or personal Microsoft account and leans on ALLOWED_SIGN_IN to decide+# who actually gets in; your own tenant's GUID refuses everyone else at+# Microsoft, before they ever reach us. "organizations" allows any work or+# school account but no personal ones.+# MICROSOFT_TENANT_ID="common"+# Serve the landing page at "/". It markets *this* product, so it is off unless# you say otherwise: on an install of your own, a stranger arriving at the root# is sent to /sign-in instead. The only value that turns it on is "true".diff --git a/.githooks/pre-push b/.githooks/pre-pushnew file mode 100755index 00000000..d803088c--- /dev/null+++ b/.githooks/pre-push@@ -0,0 +1,27 @@+#!/usr/bin/env sh++set -eu++[ -n "${CRM_SKIP_HOOKS:-}" ] && exit 0++deleting_only=1+while read -r _local_ref local_sha _remote_ref _remote_sha; do+ case "$local_sha" in+ 0000000000000000000000000000000000000000) ;;+ *) deleting_only=0 ;;+ esac+done+[ "$deleting_only" = "1" ] && exit 0++fail() {+ echo ""+ echo " \`bun run $1\` failed, so this push would have failed CI."+ echo " Fix it, or push anyway with --no-verify."+ echo ""+ exit 1+}++for task in check-types lint test; do+ echo "pre-push: bun run $task"+ bun run "$task" || fail "$task"+donediff --git a/.github/release-please-config.json b/.github/release-please-config.jsonindex 69e301b4..09f67dec 100644--- a/.github/release-please-config.json+++ b/.github/release-please-config.json@@ -27,5 +27,5 @@"bootstrap-sha": "64f154b086cbad6cac232357dc21c954543ef217","separate-pull-requests": false,"pull-request-title-pattern": "chore(release): ${version}",- "pull-request-header": "The changelog below is what will be published as the release notes. Edit the commit subjects, not this PR body — the body is regenerated on every push to main."+ "pull-request-header": "The changelog below is what will be published as the release notes. Edit the commit subjects, not this PR body — the body is regenerated on every push to release."}diff --git a/.github/scripts/pr-title.sh b/.github/scripts/pr-title.shnew file mode 100755index 00000000..347cc3a2--- /dev/null+++ b/.github/scripts/pr-title.sh@@ -0,0 +1,144 @@+#!/usr/bin/env bash++set -euo pipefail++TYPES='feat|fix|perf|refactor|docs|revert|deps|chore|test|ci|build|style'+MODEL='claude-opus-5'+DIFF_BYTES=40000++check() {+ local title=${1-} subject+ printf '%s' "$title" | grep -Eq "^($TYPES)(\([^)]+\))?!?: .+" || return 1+ subject=${title#*: }+ printf '%s' "$subject" | grep -Eq '^[A-Z][a-z]' && return 1+ case "$subject" in *.) return 1 ;; esac+ return 0+}++scope_for() {+ local files=$1 scopes+ scopes=$(printf '%s\n' "$files" | sed -n \+ -e 's#^apps/\([^/]*\)/.*#\1#p' \+ -e 's#^packages/\([^/]*\)/.*#\1#p' \+ -e 's#^docs/.*#docs#p' \+ -e 's#^\.github/.*#ci#p' |+ sort -u)+ if [ "$(printf '%s\n' "$scopes" | grep -c .)" = "1" ]; then+ printf '%s' "$scopes"+ fi+}++type_for() {+ local files=$1+ if ! grep -qvE '(^|/)(docs/|README|CONTRIBUTING|AGENTS|.*\.md$)' <<<"$files"; then+ printf 'docs'+ elif ! grep -qv '^\.github/' <<<"$files"; then+ printf 'ci'+ elif ! grep -qvE '(^|/)(test|tests|__tests__)/|\.spec\.|\.test\.' <<<"$files"; then+ printf 'test'+ elif ! grep -qvE '(^|/)(package\.json|bun\.lock|package-lock\.json)$' <<<"$files"; then+ printf 'chore'+ else+ printf 'feat'+ fi+}++subject_from_branch() {+ local subject+ subject=$(printf '%s' "${1-}" | sed -e 's#^[^/]*/##' -e 's/[-_]/ /g' -e 's/\.$//' -e 's/[[:space:]]*$//')+ printf '%s' "$subject" | grep -Eq '^[A-Z][a-z]' &&+ subject=$(printf '%s' "$subject" | sed -e 's/^\(.\)/\l\1/')+ printf '%s' "$subject"+}++model_title() {+ local base=$1 head=$2 scopes=$3 evidence body response title+ command -v jq >/dev/null 2>&1 || return 1++ evidence=$(+ printf 'Commit subjects:\n%s\n\n' "$(git log "$base..$head" --no-merges --reverse --format='- %s')"+ printf 'Files changed:\n%s\n\n' "$(git diff --stat "$base..$head")"+ printf 'Diff:\n%s\n' "$(git diff "$base..$head" | head -c "$DIFF_BYTES")"+ )++ body=$(jq -n \+ --arg model "$MODEL" \+ --arg system "You write the Conventional Commit title for a pull request that will be squashed onto main, so the title is the commit subject and the line a person reads in the changelog months from now. Write it for that reader: what the change does for them, not which files moved.++Format: type(scope): subject+Types: feat (new capability, minor bump), fix, perf, refactor, docs, revert (patch), chore, test, ci, build, style (no release). Add ! before the colon only for a breaking change.+Scopes, one only, omitted when the change spans several: $scopes.+Subject: lowercase imperative, no trailing full stop, under 60 characters, no ticket numbers and no file paths.++Reply with the title on one line and nothing else." \+ --arg user "$evidence" \+ '{model: $model,+ max_tokens: 4000,+ system: $system,+ output_config: {effort: "low"},+ messages: [{role: "user", content: $user}]}')++ response=$(curl -fsS --max-time 120 https://api.anthropic.com/v1/messages \+ -H "x-api-key: $ANTHROPIC_API_KEY" \+ -H 'anthropic-version: 2023-06-01' \+ -H 'content-type: application/json' \+ -d "$body") || return 1++ [ "$(printf '%s' "$response" | jq -r '.stop_reason // ""')" = "refusal" ] && return 1++ title=$(printf '%s' "$response" |+ jq -r '[.content[] | select(.type == "text") | .text] | join("")' |+ grep -v '^[[:space:]]*$' | head -1 |+ sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^`//' -e 's/`$//')++ check "$title" || return 1+ printf '%s' "$title"+}++generate() {+ local base=$1 head=$2 branch=${3-} files scope subject conventional title++ files=$(git diff --name-only "$base..$head")+ scope=$(scope_for "$files")++ if [ -n "${ANTHROPIC_API_KEY:-}" ]; then+ local allowed+ allowed=$( (printf '%s\n' "$files" | sed -n -e 's#^apps/\([^/]*\)/.*#\1#p' -e 's#^packages/\([^/]*\)/.*#\1#p'+ printf 'docs\nci\ndeps\n') | sort -u | paste -sd ',' - | sed 's/,/, /g')+ if title=$(model_title "$base" "$head" "$allowed"); then+ printf '%s\n' "$title"+ return 0+ fi+ echo "The model did not return a usable title — falling back to the branch name." >&2+ fi++ conventional=$(+ while IFS= read -r subject; do+ if check "$subject"; then+ printf '%s' "$subject"+ break+ fi+ done < <(git log "$base..$head" --no-merges --reverse --format=%s)+ )+ if [ -n "$conventional" ]; then+ printf '%s\n' "$conventional"+ return 0+ fi++ subject=$(subject_from_branch "$branch")+ [ -n "$subject" ] || subject="update ${files%%$'\n'*}"+ if [ -n "$scope" ]; then+ printf '%s(%s): %s\n' "$(type_for "$files")" "$scope" "$subject"+ else+ printf '%s: %s\n' "$(type_for "$files")" "$subject"+ fi+}++case "${1-}" in+check) check "${2-}" ;;+generate) generate "${2:?base ref}" "${3:?head ref}" "${4-}" ;;+*)+ echo "usage: pr-title.sh check <title> | pr-title.sh generate <base> <head> [branch]" >&2+ exit 2+ ;;+esacdiff --git a/.github/workflows/auto-pr.yml b/.github/workflows/auto-pr.ymlindex 4e335200..bbfcd8ec 100644--- a/.github/workflows/auto-pr.yml+++ b/.github/workflows/auto-pr.yml@@ -19,7 +19,7 @@ permissions:jobs:open:- name: Open a draft pull request into main+ name: Open a pull request into mainruns-on: ubuntu-24.04timeout-minutes: 5steps:@@ -30,6 +30,7 @@ jobs:- name: Open the pull requestenv:GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN || secrets.GITHUB_TOKEN }}+ ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}BRANCH: ${{ github.ref_name }}run: |set -euo pipefail@@ -45,23 +46,17 @@ jobs:exit 0fi- subject=$(git log "origin/main..origin/$BRANCH" --no-merges --reverse --format=%s | head -1)-- if printf '%s' "$subject" | grep -Eq '^(feat|fix|perf|refactor|docs|revert|deps|chore|test|ci|build|style)(\([^)]+\))?!?: .+'; then- title="$subject"- else- title=$(printf '%s' "${BRANCH#*/}" | tr '_' ' ' | tr '-' ' ')- fi+ title=$(.github/scripts/pr-title.sh generate "origin/main" "origin/$BRANCH" "$BRANCH")body=$(cat <<EOFOpened automatically when \`$BRANCH\` was pushed.- This pull request is squashed onto \`main\`, so **its title becomes the commit subject and the changelog line**. Make it a [Conventional Commit](https://www.conventionalcommits.org/) — \`feat(app): …\`, \`fix(api): …\` — before marking it ready for review, which is when the title is checked.+ The title is written from the diff and rewritten as you push, because this is squashed onto \`main\` and the title becomes the commit subject and the changelog line. Retitle it yourself and it is yours — the automation stops touching it.- The type decides the version bump: \`feat\` is a minor, \`fix\` / \`perf\` / \`refactor\` / \`docs\` are a patch, a trailing \`!\` is a major, and \`chore\` / \`ci\` / \`test\` / \`build\` / \`style\` release nothing at all.+ <!-- pr-title: $title -->EOF)- gh pr create --draft --base main --head "$BRANCH" --title "$title" --body "$body"+ gh pr create --base main --head "$BRANCH" --title "$title" --body "$body"- echo "Opened a draft pull request for \`$BRANCH\`." >> "$GITHUB_STEP_SUMMARY"+ echo "Opened a pull request for \`$BRANCH\` titled \`$title\`." >> "$GITHUB_STEP_SUMMARY"diff --git a/.github/workflows/pr-base.yml b/.github/workflows/pr-base.ymlnew file mode 100644index 00000000..6f62f04b--- /dev/null+++ b/.github/workflows/pr-base.yml@@ -0,0 +1,52 @@+name: PR base++on:+ pull_request_target:+ types: [opened, reopened]+ branches: [release]++concurrency:+ group: pr-base-${{ github.event.pull_request.number }}+ cancel-in-progress: true++permissions:+ contents: read+ pull-requests: write++jobs:+ retarget:+ name: Retarget onto main+ if: >-+ github.event.pull_request.head.ref != 'main' &&+ !startsWith(github.event.pull_request.head.ref, 'release-please--')+ runs-on: ubuntu-24.04+ timeout-minutes: 5+ steps:+ - name: Move the pull request to main+ env:+ GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN || secrets.GITHUB_TOKEN }}+ GH_REPO: ${{ github.repository }}+ NUMBER: ${{ github.event.pull_request.number }}+ run: |+ set -euo pipefail++ gh pr edit "$NUMBER" --base main++ echo "Retargeted #$NUMBER from \`release\` onto \`main\`." >> "$GITHUB_STEP_SUMMARY"++ - uses: marocchino/sticky-pull-request-comment@v2+ continue-on-error: true+ with:+ header: pr-base+ message: |+ **Retargeted this onto `main`.**++ `release` is the default branch so that a plain clone runs the last tagged release, but nothing merges into it — it is fast-forwarded onto the tag by the Release workflow and that is all. Changes go to `main`, and reach `release` when a release is cut.++ Nothing is wrong with your branch. If the diff now shows commits that are already on `main`, rebase and force-push:++ ```sh+ git fetch origin main+ git rebase origin/main+ git push --force-with-lease+ ```diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.ymlindex 75d7b9f9..259b354b 100644--- a/.github/workflows/pr-title.yml+++ b/.github/workflows/pr-title.yml@@ -3,6 +3,7 @@ name: PR titleon:pull_request:types: [opened, edited, reopened, ready_for_review, synchronize]+ branches: [main]concurrency:group: pr-title-${{ github.event.pull_request.number }}@@ -19,8 +20,55 @@ jobs:runs-on: ubuntu-24.04timeout-minutes: 5steps:+ - uses: actions/checkout@v5+ with:+ fetch-depth: 0++ - name: Write the title from the diff+ id: autotitle+ env:+ GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN || secrets.GITHUB_TOKEN }}+ ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}+ ACTION: ${{ github.event.action }}+ TITLE: ${{ github.event.pull_request.title }}+ BODY: ${{ github.event.pull_request.body }}+ NUMBER: ${{ github.event.pull_request.number }}+ BRANCH: ${{ github.event.pull_request.head.ref }}+ BASE_SHA: ${{ github.event.pull_request.base.sha }}+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}+ run: |+ set -euo pipefail++ body=$(printf '%s' "$BODY" | tr -d '\r')+ written=$(printf '%s\n' "$body" | sed -n 's/^<!-- pr-title: \(.*\) -->$/\1/p' | head -1)++ if ! .github/scripts/pr-title.sh check "$TITLE"; then+ reason="it is not a release note"+ elif [ "$ACTION" != "edited" ] && [ -n "$written" ] && [ "$written" = "$TITLE" ]; then+ reason="the diff has moved and nobody has retitled it"+ else+ echo "\`$TITLE\` is yours — leaving it alone." >> "$GITHUB_STEP_SUMMARY"+ exit 0+ fi++ title=$(.github/scripts/pr-title.sh generate "$BASE_SHA" "$HEAD_SHA" "$BRANCH")++ if [ "$title" = "$TITLE" ]; then+ echo "\`$TITLE\` still describes the diff." >> "$GITHUB_STEP_SUMMARY"+ echo "written=true" >> "$GITHUB_OUTPUT"+ exit 0+ fi++ body=$(printf '%s\n\n<!-- pr-title: %s -->\n' \+ "$(printf '%s\n' "$body" | grep -v '^<!-- pr-title: ' || true)" "$title")+ gh pr edit "$NUMBER" --title "$title" --body "$body"++ echo "written=true" >> "$GITHUB_OUTPUT"+ echo "Retitled #$NUMBER to \`$title\`, because $reason." >> "$GITHUB_STEP_SUMMARY"+- uses: amannn/action-semantic-pull-request@v6id: lint+ if: steps.autotitle.outputs.written != 'true'env:GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}with:diff --git a/.github/workflows/promote.yml b/.github/workflows/promote.ymlnew file mode 100644index 00000000..5a27b244--- /dev/null+++ b/.github/workflows/promote.yml@@ -0,0 +1,57 @@+name: Promote++on:+ push:+ branches: [main]+ workflow_dispatch:++concurrency:+ group: promote+ cancel-in-progress: false++permissions:+ contents: read+ pull-requests: write++jobs:+ promote:+ name: Pull request from main into release+ runs-on: ubuntu-24.04+ timeout-minutes: 5+ steps:+ - uses: actions/checkout@v5+ with:+ fetch-depth: 0++ - name: Open or update the promotion pull request+ env:+ GH_TOKEN: ${{ secrets.AUTOMATION_TOKEN || secrets.GITHUB_TOKEN }}+ run: |+ set -euo pipefail++ if [ "$(git rev-list --count origin/release..origin/main)" = "0" ]; then+ echo "\`release\` already has everything on \`main\`." >> "$GITHUB_STEP_SUMMARY"+ exit 0+ fi++ body=$(cat <<EOF+ Everything on \`main\` that \`release\` does not have yet. Merging this ships it: the+ commits below land on \`release\`, and release-please then opens a \`chore(release)\` pull+ request that writes the changelog, bumps the version and cuts the tag.++ Merge it with a **merge commit**, not a squash — release-please reads these subjects to+ work out the version, and a squash would flatten them into one.++ $(git log origin/release..origin/main --no-merges --reverse --format='- %s')+ EOF+ )++ existing=$(gh pr list --base release --head main --state open --limit 1 --json number --jq '.[0].number // ""')++ if [ -n "$existing" ]; then+ gh pr edit "$existing" --body "$body"+ echo "Updated promotion pull request #$existing." >> "$GITHUB_STEP_SUMMARY"+ else+ gh pr create --base release --head main --title "release: promote main" --body "$body"+ echo "Opened a promotion pull request into \`release\`." >> "$GITHUB_STEP_SUMMARY"+ fidiff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 50c08906..9409f79b 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -2,7 +2,7 @@ name: Releaseon:push:- branches: [main]+ branches: [release]workflow_dispatch:concurrency:@@ -18,38 +18,23 @@ jobs:name: Release pull request, tag and notesruns-on: ubuntu-24.04timeout-minutes: 10- outputs:- released: ${{ steps.release.outputs.release_created }}- tag: ${{ steps.release.outputs.tag_name }}- sha: ${{ steps.release.outputs.sha }}- url: ${{ steps.release.outputs.html_url }}steps:- uses: googleapis/release-please-action@v5id: releasewith:config-file: .github/release-please-config.jsonmanifest-file: .github/.release-please-manifest.json+ target-branch: releasetoken: ${{ secrets.AUTOMATION_TOKEN || secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}- promote:- name: Fast-forward release- needs: release-please- if: needs.release-please.outputs.released == 'true'- runs-on: ubuntu-24.04- timeout-minutes: 5- permissions:- contents: write- steps:- - uses: actions/checkout@v5- with:- fetch-depth: 0-- - name: Move release to ${{ needs.release-please.outputs.tag }}+ - name: Say what happenedenv:- SHA: ${{ needs.release-please.outputs.sha }}- TAG: ${{ needs.release-please.outputs.tag }}- URL: ${{ needs.release-please.outputs.url }}+ TAG: ${{ steps.release.outputs.tag_name }}+ URL: ${{ steps.release.outputs.html_url }}run: |set -euo pipefail- git push origin "$SHA:refs/heads/release"- echo "\`release\` now points at [$TAG]($URL)." >> "$GITHUB_STEP_SUMMARY"+ if [ -n "${TAG:-}" ]; then+ echo "Released [$TAG]($URL)." >> "$GITHUB_STEP_SUMMARY"+ else+ echo "No tag this run — either the release pull request is still open, or nothing releasable landed." >> "$GITHUB_STEP_SUMMARY"+ fidiff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 7089be64..cffa1c77 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -41,6 +41,17 @@ bun run testAll three run on CI, and `bun run format` fixes most of what `lint` complains about.+**A `pre-push` hook runs them for you**, so a push that would fail CI fails on your machine+instead, where the feedback is in seconds rather than minutes. `bun install` wires it up — the+hooks live in `.githooks/` and `prepare` points `core.hooksPath` at them, so there is nothing to+install and no hook manager in the dependency tree. Turbo caches, so a second push that changed+nothing relevant is nearly free.++It needs the Postgres from `docker compose up -d`, because the API and telemetry tests are real+integration tests. When you need to push past it — a WIP branch, a docker-less machine, a red test+you are deliberately pushing to ask about — `git push --no-verify` skips it, and `CRM_SKIP_HOOKS=1`+skips it for a whole shell.+A few things that trip people up:- **The tRPC router type is generated, and committed.** If the app can't see a procedure you just@@ -53,33 +64,54 @@ A few things that trip people up:## Shipping a change-Push a branch and the rest is mechanical. There are exactly two things you click.+**Start from `main`, which is not the default branch.** `release` is the default so that a plain+clone runs the last tagged release; the only things that ever merge into it are the two release+pull requests below. Run `git switch main && git pull` before you branch. If you open a pull request+by hand, pass `--base main`; one opened against `release` is retargeted to `main` automatically, so+this costs you a comment rather than a rebase.++Push a branch and the rest is mechanical. There are exactly three things you click, and every one+of them is a pull request — neither `main` nor `release` accepts a direct push.```-push a branch ──▶ draft PR opens by itself+push a branch ──▶ PR opens by itself, titled from the diff│- you title it, mark it ready, CI runs+ CI runs, and the title follows the diff as you push│◀── click 1: squash into main│- release PR opens or updates itself+ `release: promote main` opens or updates itself+ │+ ◀── click 2: merge it (a merge commit, not a squash)+ │+ release PR opens against `release`│- ◀── click 2: merge it+ ◀── click 3: merge it│- tag + GitHub Release + CHANGELOG.md, and `release` moves up+ tag + GitHub Release + CHANGELOG.md```-**Pushing any branch that isn't `main` or `release` opens a draft pull request into `main`.** You do-not create it, and re-pushing does not create a second one. If you close it on purpose it stays-closed. The title is guessed from your first commit subject, which is usually wrong — fixing it is-the one thing the automation cannot do for you.+**Pushing any branch that isn't `main` or `release` opens a pull request into `main`.** You do not+create it, and re-pushing does not create a second one. If you close it on purpose it stays closed.+It opens ready for review rather than as a draft, because there is no longer anything you have to do+to it before it is reviewable — convert it to a draft yourself if you want the checks to leave you+alone while you work.**The pull request title is the release note.** The repo squashes, so the title becomes the commitsubject on `main`, and that subject is the line somebody reads in the changelog six months from now.-Write it for them, not for the diff. It has to be a-[Conventional Commit](https://www.conventionalcommits.org/) — `feat(api): …`, `fix(db): …` — and the-`PR title` check enforces that the moment the PR leaves draft. Drafts are deliberately exempt, so an-auto-opened PR does not sit there red while you are still working.+It has to be a [Conventional Commit](https://www.conventionalcommits.org/) — `feat(api): …`,+`fix(db): …`.++**You do not write it.** `.github/scripts/pr-title.sh` reads the diff and writes one when the PR+opens, then rewrites it on every push, so a title written for your first commit does not survive to+describe twenty. It knows which titles are its own — it records the last one it wrote in an HTML+comment at the bottom of the PR body. **Retitle the PR yourself and it stops**: the marker no longer+matches, the automation leaves the title alone from then on, and the `PR title` check is all that is+left, guarding your wording rather than nagging you for it.++The script reaches a model over the `ANTHROPIC_API_KEY` secret, and without it falls back to the+changed paths and the branch name — a valid title, and a duller one. Set the secret if you want the+changelog to read well; nothing breaks if you don't.The type decides both the version bump and the heading it appears under:@@ -96,30 +128,42 @@ because the squashed commit body is left empty on purpose — the title is the w## Releases-Nothing is released by merging to `main`. Merging opens or updates a single-`chore(release): 0.2.0` pull request from [release-please](https://github.com/googleapis/release-please)-that accumulates the changelog and bumps the version; **merging that PR** is what tags `v0.2.0`,-publishes the GitHub Release, and writes `CHANGELOG.md`. So the notes are reviewable before they are-public, and a stack of merges is one release rather than five.+Nothing is released by merging to `main`, and nothing is pushed to `release` by hand. Both branches+only ever move through a pull request, and shipping is two of them.++**The promotion pull request — `release: promote main`.** Opened, and thereafter kept up to date,+every time something lands on `main`. Its body is the list of commits `release` does not have yet,+so it is a standing answer to "what is waiting to ship". **Merge it with a merge commit, not a+squash** — `release` is only allowed to merge that way for a reason: release-please reads those+individual commit subjects to work out the version, and a squash flattens them into one subject it+cannot parse.++**The release pull request — `chore(release): 0.2.0`.** Once the promotion lands,+[release-please](https://github.com/googleapis/release-please) reads the newly promoted commits and+opens this against `release`. Merging it writes `CHANGELOG.md`, bumps the version, tags `v0.2.0` and+publishes the GitHub Release. So the notes are reviewable before they are public, and a stack of+merges is one release rather than five.-Once the tag exists, the `release` branch is fast-forwarded onto it automatically. `release` is-therefore never anything but the last released commit, which is what makes it safe to point a-production deploy at. There is no second pull request to merge — the release PR was the gate.+`CHANGELOG.md`, the version in `package.json` and the release manifest therefore live on `release`+and are not carried back to `main`. That is deliberate: `release` is the default branch, so it is+the tree anyone landing on the repository reads, and `main` never touches those files — which is+also why the promotion merge never conflicts on them.Three consequences worth knowing:- **A release PR with nothing in it is not a bug.** A run of `chore:` and `test:` commits bumpsnothing, so no PR appears. That is the type doing its job.-- **The release PR does not run CI.** A PR opened by `GITHUB_TOKEN` cannot trigger workflows — that- is GitHub's own loop guard, not something to work around. It is safe because the PR only ever- touches `CHANGELOG.md`, the root `version`, and the release manifest, and because CI runs again on- `main` after it lands. The same guard is why an auto-opened draft PR shows no checks until you- push to it or mark it ready.-- **An `AUTOMATION_TOKEN` secret removes that caveat.** A PAT or GitHub App token makes both the- release PR and the auto-opened PR trigger CI like any other. Every workflow already prefers it and- falls back to `GITHUB_TOKEN`, so it is an upgrade rather than a requirement.--`main` is expected to be green when a tag is cut, and the thing that guarantees that is **branch+- **Neither automated PR runs CI.** A PR opened by `GITHUB_TOKEN` cannot trigger workflows — that is+ GitHub's own loop guard, not something to work around. It does not matter on `release`, which has+ no required checks: the code being promoted was already green on `main`, and the release PR only+ ever touches `CHANGELOG.md`, the root `version`, and the manifest.+- **On `main` it does matter**, because `main` requires `check-types, lint, test` and+ `conventional commit`. A branch you push yourself is fine — your push triggers CI and the auto-PR+ inherits the result. Setting an `AUTOMATION_TOKEN` secret (a PAT or GitHub App token) makes the+ automated PRs trigger CI like any other; every workflow already prefers it and falls back to+ `GITHUB_TOKEN`. Until then, a repository admin can merge past a missing check.++`main` is expected to be green when it is promoted, and the thing that guarantees that is **branchprotection requiring the `check-types, lint, test` and `conventional commit` checks** — not therelease workflow, which cannot wait on a run in another workflow.diff --git a/README.md b/README.mdindex 43744443..73fc6934 100644--- a/README.md+++ b/README.md@@ -149,9 +149,9 @@ A [Turborepo](https://turborepo.dev) monorepo on [Bun](https://bun.com), deploye| **Model** | [Vercel AI Gateway](https://vercel.com/docs/ai-gateway) — no provider SDK, and OIDC on Vercel means no key to manage || **Sandbox** | [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox) in production, Docker or microsandbox locally || **Front end** | [Next.js](https://nextjs.org) App Router · [shadcn/ui](https://ui.shadcn.com) · [nuqs](https://nuqs.dev) for URL state |-| **API** | [NestJS](https://nestjs.com) with [nestjs-trpc](https://nestjs-trpc.io) — HTTP, auth, tRPC, Google sync |+| **API** | [NestJS](https://nestjs.com) with [nestjs-trpc](https://nestjs-trpc.io) — HTTP, auth, tRPC, mailbox sync || **Data** | [Prisma](https://prisma.io) · Postgres ([Neon](https://neon.tech)) · optional Redis ([Upstash](https://upstash.com)) |-| **Auth** | [Better Auth](https://better-auth.com), Google-only, one allow-list |+| **Auth** | [Better Auth](https://better-auth.com) — Google, Microsoft, or your own IdP; one allow-list || **Files** | [Vercel Blob](https://vercel.com/docs/vercel-blob) — mirrors profile pictures so they survive the source going away || **Tooling** | [Biome](https://biomejs.dev) · TypeScript everywhere |@@ -166,7 +166,7 @@ reproduces the view.| --- | --- || `apps/agent` | The research agent — tools, skills, schedules, sandbox || `apps/app` | Next.js front end · :3000 |-| `apps/api` | NestJS API — HTTP, auth, tRPC, Google sync · :3001 |+| `apps/api` | NestJS API — HTTP, auth, tRPC, mailbox sync · :3001 || `packages/db` | Prisma schema, migrations, shared Postgres client || `packages/auth` | Better Auth config and the sign-in allow-list || `packages/ui` | shadcn/ui components, the Tailwind theme |@@ -191,7 +191,7 @@ You need [Bun](https://bun.com) and Docker.```shgit clone https://github.com/trycompai/crm.git && cd crm-cp .env.example .env # then fill in the four values below+cp .env.example .env # then fill in the values belowbun installdocker compose up -d # Postgres on :5432@@ -204,7 +204,12 @@ bun run devThe app is on [localhost:3000](http://localhost:3000), the API on[localhost:3001](http://localhost:3001).-### The four values+That clone gives you `release`, the default branch and the last tagged release — what you+want if you are running this. `main` is where unreleased work lands: green, but not cut+yet. If you are here to send a pull request, `git switch main` first and read+[CONTRIBUTING](./CONTRIBUTING.md).++### The values to setOpen `.env` and set these. Everything else in the file is optional and commented out.@@ -213,6 +218,11 @@ Open `.env` and set these. Everything else in the file is optional and commented| `BETTER_AUTH_SECRET` | `openssl rand -base64 32` || `ALLOWED_SIGN_IN` | Your email domain, e.g. `acme.com`. Or one address, e.g. `[email protected]`. || `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET`| A Google OAuth client — 2 minutes, below. Both or neither. |+| `MICROSOFT_CLIENT_ID` / `MICROSOFT_CLIENT_SECRET` | A Microsoft Entra app registration — below. Both or neither. |++**Pick at least one of Google and Microsoft**, or add your own identity provider on+**Settings → SSO** once you are in. Setting both is fine and common: the sign-in page+offers both buttons, and each rep's mail is read from whichever they signed in with.`DATABASE_URL` already matches the `docker compose` Postgres, so leave it alone unlessyou brought your own.@@ -226,12 +236,52 @@ you brought your own.4. Copy the client ID and secret into `.env`.Google is the sign-in method a clone starts with, and the same client reads Gmail and-Calendar — so almost every install wants it. It is nonetheless the one of the four that-the API will still boot without: an install that signs in through its own identity-provider, added on **Settings → SSO**, leaves both empty and gets no Google button and-no mail sync. Set them together or not at all; half a pair is a button that fails at-Google. If your account is on a Google Workspace domain, set the consent screen to-**Internal** and nobody outside your org can even reach the prompt.+Calendar — so most installs want it. The API will nonetheless boot without it: an+install that signs in with Microsoft, or through its own identity provider added on+**Settings → SSO**, leaves both empty and gets no Google button and no Gmail sync. Set+them together or not at all; half a pair is a button that fails at Google. If your+account is on a Google Workspace domain, set the consent screen to **Internal** and+nobody outside your org can even reach the prompt.++</details>++<details>+<summary><strong>Getting the Microsoft app registration</strong></summary>++1. [Azure portal](https://portal.azure.com) → **Microsoft Entra ID** → **App+ registrations** → **New registration**. Microsoft's own walkthrough is+ [Register an application](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app#register-an-application).+2. Under **Supported account types**, pick **Accounts in this organizational directory+ only** if everyone signing in is on your tenant. Anything wider is fine too —+ `ALLOWED_SIGN_IN` still decides who gets an account — but the narrow choice turns+ outsiders away at Microsoft rather than at our door.+3. Set the **Redirect URI** to **Web** and+ `http://localhost:3001/api/auth/callback/microsoft`. In production this is+ `https://<your-api-host>/api/auth/callback/microsoft` — the API's origin, not the+ app's, because the API is what serves `/api/auth/*`.+4. **API permissions** → **Add a permission** → **Microsoft Graph** → **Delegated+ permissions** → tick **User.Read** and **Mail.Read**. `offline_access` is requested+ at sign-in and needs no entry here. If your tenant requires admin consent, click+ **Grant admin consent** so reps are not each asked.+5. **Certificates & secrets** → **New client secret**. Copy the **Value**, not the+ Secret ID — the value is shown once and never again.+6. **Overview** → copy the **Application (client) ID**.+7. Put the two into `.env` as `MICROSOFT_CLIENT_ID` and `MICROSOFT_CLIENT_SECRET`.++`Mail.Read` is read-only: the CRM can list and read messages and can never send,+reply, move or delete. Reading is forward-only — the first check records the current+time and imports nothing, so connecting an old mailbox does not dump years of mail into+the CRM.++To refuse other tenants at Microsoft rather than relying on `ALLOWED_SIGN_IN`, set+`MICROSOFT_TENANT_ID` to your tenant's GUID (**Overview** → **Directory (tenant) ID**).+It defaults to `common`, which accepts any work, school or personal Microsoft account+and leaves the allow-list to sort them out. `organizations` is the middle setting: any+work or school account, no personal ones.++Microsoft client secrets expire — 24 months at most, and 6 months by default. Note the+expiry somewhere, because the symptom of a lapsed one is every rep's mail quietly+failing to sync.</details>@@ -251,7 +301,7 @@ ALLOWED_SIGN_IN="[email protected]" # a one-person installenvironment variables always win, so on a hosting platform you configure it there andthe file is purely a local convenience.-Beyond the four values above, everything is optional and the app runs without any+Beyond the values above, everything is optional and the app runs without anyof it. [`.env.example`](./.env.example) is the full list with a note on each; theshort version:@@ -262,7 +312,7 @@ short version:| `RAPIDAPI_KEY` | Lets the agent read LinkedIn profiles for identity. || `AGENT_BRIDGE_SECRET` | Lets a rep talk to the agent from a contact's **Agent** tab. || `REDIS_URL` | A shared cache. Without it, per-instance and in-memory. |-| `CRON_SECRET` | Guards the Gmail/Calendar sync route. Required to use it. |+| `CRON_SECRET` | Guards the mailbox sync route. Required to use it. || `CRM_TELEMETRY_DISABLED` | Set to `1` and this install reports nothing. `DO_NOT_TRACK` too. |## Tasks@@ -282,7 +332,7 @@ short version:Scope any of them with a Turborepo filter: `bun run dev --filter=api`.-Because Google is the only door, there is no way to get a session from a terminal —+Because sign-in goes through an identity provider, there is no way to get a session from a terminal —`dev:session` writes the rows Better Auth would have written and prints the cookie itwould have set. It refuses to run with `NODE_ENV=production`.@@ -295,9 +345,10 @@ mismatch is a redirect loop rather than an error.Set `API_URL` and `APP_URL` to the real origins, and if the two are on differentsubdomains of one parent, set `AUTH_COOKIE_DOMAIN` to the parent so one cookie covers-both. Add `http://your-api-host/api/auth/callback/google` to the OAuth client's+both. Add `http://your-api-host/api/auth/callback/google` — and, if you use Microsoft,+`http://your-api-host/api/auth/callback/microsoft` — to the provider'sredirect URIs. Set `CRON_SECRET` and point a scheduler at-`POST /internal/sync/google` to keep the mailbox sync running.+`POST /internal/sync/mailboxes` to keep the mailbox sync running.`apps/api/src/generated/server.ts` is committed and `build` must never regenerate it —the generator needs a newer GLIBC than most build images have. Regenerate locally anddiff --git a/apps/api/src/app.module.ts b/apps/api/src/app.module.tsindex 7487b45f..0426549b 100644--- a/apps/api/src/app.module.ts+++ b/apps/api/src/app.module.ts@@ -20,9 +20,12 @@ import { GoogleModule } from "./google/google.module";import { HealthModule } from "./health/health.module";import { LoggingModule } from "./logging/logging.module";import { logAuthRoute } from "./logging/request-logger.middleware";+import { MailboxModule } from "./mailbox/mailbox.module";+import { MicrosoftModule } from "./microsoft/microsoft.module";import { SearchModule } from "./search/search.module";import { SettingsModule } from "./settings/settings.module";import { SsoModule } from "./sso/sso.module";+import { SyncModule } from "./sync/sync.module";import { TelemetryModule } from "./telemetry/telemetry.module";import { TrpcModule } from "./trpc/trpc.module";import { UsersModule } from "./users/users.module";@@ -53,7 +56,10 @@ import { WorkspaceModule } from "./workspace/workspace.module";ActivitiesModule,DashboardModule,SearchModule,+ MailboxModule,GoogleModule,+ MicrosoftModule,+ SyncModule,SettingsModule,WorkspaceModule,SsoModule,diff --git a/apps/api/src/config/env.validation.ts b/apps/api/src/config/env.validation.tsindex 55abd0a5..06c76c4b 100644--- a/apps/api/src/config/env.validation.ts+++ b/apps/api/src/config/env.validation.ts@@ -56,6 +56,18 @@ export class EnvironmentVariables {@IsString()GOOGLE_CLIENT_SECRET?: string;+ @IsOptional()+ @IsString()+ MICROSOFT_CLIENT_ID?: string;++ @IsOptional()+ @IsString()+ MICROSOFT_CLIENT_SECRET?: string;++ @IsOptional()+ @IsString()+ MICROSOFT_TENANT_ID?: string;+@IsOptional()@IsUrl({ require_tld: false })API_URL?: string;diff --git a/apps/api/src/generated/server.ts b/apps/api/src/generated/server.tsindex 11d0e60b..6b3b7271 100644--- a/apps/api/src/generated/server.ts+++ b/apps/api/src/generated/server.ts@@ -22,6 +22,7 @@ import { dashboardSummaryInput } from "../dashboard/dashboard.contracts";import { dealListInput, dealIdInput, dealCreateInput, dealUpdateArgs, setStageInput, dealContactsInput, dealAttachContactInput, dealDetachContactInput, dealContactRoleInput, dealBulkOwnerInput, dealBulkStageInput, dealBulkInput } from "../deals/deals.contracts";import { fieldListInput, fieldByKeyInput, fieldIdInput, fieldCreateInput, fieldUpdateArgs, fieldReorderInput } from "../fields/fields.contracts";import { setAutoCreateInput, suppressDomainInput, threadInput, calendarEventInput } from "../google/google.contracts";+import { setOutlookAutoCreateInput } from "../microsoft/microsoft.contracts";import { setAgentModelInput, setResearchKeyInput } from "../settings/settings.contracts";import { ssoProviderListInput, registerSsoProviderInput, deleteSsoProviderInput } from "../sso/sso.contracts";import { memberListInput, updateWorkspaceInput, setMemberRoleInput } from "../workspace/workspace.contracts";@@ -34,6 +35,7 @@ import type { DashboardRouter } from "../dashboard/dashboard.router";import type { DealsRouter } from "../deals/deals.router";import type { FieldsRouter } from "../fields/fields.router";import type { GoogleRouter } from "../google/google.router";+import type { MicrosoftRouter } from "../microsoft/microsoft.router";import type { SearchRouter } from "../search/search.router";import type { SettingsRouter } from "../settings/settings.router";import type { SsoRouter } from "../sso/sso.router";@@ -260,6 +262,19 @@ const appRouter = t.router({.input(calendarEventInput).query(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<GoogleRouter["event"]>>)}),+ microsoft: t.router({+ status: publicProcedure+ .query(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<MicrosoftRouter["status"]>>),+ purgeSyncedData: publicProcedure+ .mutation(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<MicrosoftRouter["purgeSyncedData"]>>),+ revokeAccess: publicProcedure+ .mutation(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<MicrosoftRouter["revokeAccess"]>>),+ syncNow: publicProcedure+ .mutation(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<MicrosoftRouter["syncNow"]>>),+ setAutoCreate: publicProcedure+ .input(setOutlookAutoCreateInput)+ .mutation(async () => "PLACEHOLDER_DO_NOT_REMOVE" as unknown as Awaited<ReturnType<MicrosoftRouter["setAutoCreate"]>>)+ }),search: t.router({quick: publicProcedure.input(z.object({ q: z.string().default("") }))diff --git a/apps/api/src/google/calendar-sync.service.ts b/apps/api/src/google/calendar-sync.service.tsindex 43b69ab9..d219b3e9 100644--- a/apps/api/src/google/calendar-sync.service.ts+++ b/apps/api/src/google/calendar-sync.service.ts@@ -9,16 +9,19 @@ import { Injectable, Logger } from "@nestjs/common";import { AgentTriggerService } from "../agent/agent-trigger.service";import { ActivityStampService } from "../crm/activity-stamp.service";import { InjectDatabase } from "../database/database.constants";+import {+ MailboxMatchService,+ type MatchContext,+} from "../mailbox/mailbox-match.service";+import { MailboxTokenService } from "../mailbox/mailbox-token.service";+import { isMachineAddress, type Participant } from "../mailbox/participants";+import { SyncStateService } from "../mailbox/sync-state.service";import {CalendarClient,conferenceUrl,eventTime,type GoogleEvent,} from "./calendar.client";-import { GoogleMatchService, type MatchContext } from "./google-match.service";-import { GoogleTokenService } from "./google-token.service";-import { isMachineAddress, type Participant } from "./participants";-import { SyncStateService } from "./sync-state.service";const MAX_PAGES_PER_TICK = 5;@@ -40,8 +43,8 @@ export class CalendarSyncService {constructor(@InjectDatabase() private readonly db: Db,private readonly calendar: CalendarClient,- private readonly tokens: GoogleTokenService,- private readonly match: GoogleMatchService,+ private readonly tokens: MailboxTokenService,+ private readonly match: MailboxMatchService,private readonly state: SyncStateService,private readonly stamp: ActivityStampService,private readonly agent: AgentTriggerService,diff --git a/apps/api/src/google/calendar.client.ts b/apps/api/src/google/calendar.client.tsindex 3ccb293a..8ec52501 100644--- a/apps/api/src/google/calendar.client.ts+++ b/apps/api/src/google/calendar.client.ts@@ -1,5 +1,8 @@import { Injectable } from "@nestjs/common";-import { GoogleApiClient, type GoogleResult } from "./google-api.client";+import {+ MailboxApiClient,+ type MailboxResult,+} from "../mailbox/mailbox-api.client";const EVENTS_URL ="https://www.googleapis.com/calendar/v3/calendars/primary/events";@@ -54,12 +57,12 @@ export type EventsQuery = {@Injectable()export class CalendarClient {- constructor(private readonly api: GoogleApiClient) {}+ constructor(private readonly api: MailboxApiClient) {}async listEvents(accessToken: string,query: EventsQuery,- ): Promise<GoogleResult<EventsPage>> {+ ): Promise<MailboxResult<EventsPage>> {const window = query.syncToken? {}: { timeMin: query.timeMin, timeMax: query.timeMax };diff --git a/apps/api/src/google/conversation.service.ts b/apps/api/src/google/conversation.service.tsindex e08b1874..b995c74d 100644--- a/apps/api/src/google/conversation.service.ts+++ b/apps/api/src/google/conversation.service.ts@@ -30,6 +30,7 @@ export class ConversationService {snippet: true,sentAt: true,gmailMessageId: true,+ outlookWebLink: true,},},},@@ -52,9 +53,14 @@ export class ConversationService {sentAt: message.sentAt.toISOString(),recipients: recipientsOf(message.recipients),fromImageUrl: faces.get(message.fromEmail.toLowerCase()) ?? null,- gmailUrl: message.gmailMessageId+ mailboxUrl: message.gmailMessageId? `https://mail.google.com/mail/u/0/#all/${message.gmailMessageId}`- : null,+ : message.outlookWebLink,+ mailboxName: message.gmailMessageId+ ? "Gmail"+ : message.outlookWebLink+ ? "Outlook"+ : null,})),};}diff --git a/apps/api/src/google/gmail-mime.ts b/apps/api/src/google/gmail-mime.tsnew file mode 100644index 00000000..18fa04da--- /dev/null+++ b/apps/api/src/google/gmail-mime.ts@@ -0,0 +1,72 @@+import {+ decodeBase64Url,+ rootMessageIdFrom,+ stripHtml,+} from "../mailbox/message-text";++export type GmailHeader = { name?: string; value?: string };++export type GmailPart = {+ mimeType?: string;+ filename?: string;+ headers?: GmailHeader[];+ body?: { data?: string; size?: number; attachmentId?: string };+ parts?: GmailPart[];+};++export function header(+ headers: readonly GmailHeader[] | undefined,+ name: string,+): string | null {+ const wanted = name.toLowerCase();+ const found = headers?.find((entry) => entry.name?.toLowerCase() === wanted);+ return found?.value?.trim() ?? null;+}++export function plainTextBody(payload: GmailPart | undefined): string {+ if (!payload) return "";++ const plain = findPart(payload, "text/plain");+ if (plain?.body?.data) return decodeBase64Url(plain.body.data);++ const html = findPart(payload, "text/html");+ if (html?.body?.data) return stripHtml(decodeBase64Url(html.body.data));++ if (payload.body?.data && !payload.filename) {+ return decodeBase64Url(payload.body.data);+ }++ return "";+}++function findPart(part: GmailPart, mimeType: string): GmailPart | null {+ if (part.mimeType === mimeType && !part.filename && part.body?.data) {+ return part;+ }++ for (const child of part.parts ?? []) {+ if (isAttachment(child)) continue;++ const found = findPart(child, mimeType);+ if (found) return found;+ }++ return null;+}++function isAttachment(part: GmailPart): boolean {+ if (part.filename) return true;++ const disposition = header(part.headers, "content-disposition");+ return disposition?.toLowerCase().startsWith("attachment") ?? false;+}++export function rootMessageId(+ headers: readonly GmailHeader[] | undefined,+): string | null {+ return rootMessageIdFrom({+ references: header(headers, "references"),+ inReplyTo: header(headers, "in-reply-to"),+ messageId: header(headers, "message-id"),+ });+}diff --git a/apps/api/src/google/gmail-sync.service.ts b/apps/api/src/google/gmail-sync.service.tsindex eccfcc92..a693261b 100644--- a/apps/api/src/google/gmail-sync.service.ts+++ b/apps/api/src/google/gmail-sync.service.ts@@ -1,32 +1,29 @@import {- ActivityType,type Db,- EmailDirection,GoogleSyncStatus,type MailboxSyncModel as MailboxSync,- RecordSource,} from "@crm/db";import { Injectable, Logger } from "@nestjs/common";-import { ActivityStampService } from "../crm/activity-stamp.service";import { InjectDatabase } from "../database/database.constants";+import type { MatchContext } from "../mailbox/mailbox-match.service";+import { MailboxTokenService } from "../mailbox/mailbox-token.service";+import {+ normaliseMessageId,+ stripQuotedHistory,+} from "../mailbox/message-text";+import { parseAddress, parseAddressList } from "../mailbox/participants";+import { SyncStateService } from "../mailbox/sync-state.service";+import {+ type IncomingMessage,+ ThreadWriterService,+} from "../mailbox/thread-writer.service";import { GmailClient, type GmailMessage } from "./gmail.client";-import { GoogleMatchService, type MatchContext } from "./google-match.service";-import { GoogleTokenService } from "./google-token.service";import {type GmailHeader,header,- normaliseMessageId,plainTextBody,rootMessageId,- snippetOf,- stripQuotedHistory,-} from "./mime";-import {- type Participant,- parseAddress,- parseAddressList,-} from "./participants";-import { SyncStateService } from "./sync-state.service";+} from "./gmail-mime";const MAX_MESSAGES_PER_TICK = 120;@@ -39,17 +36,6 @@ export type GmailSyncOutcome = {reason?: string;};-type ParsedMessage = {- rfcMessageId: string;- rootId: string;- subject: string | null;- from: Participant;- recipients: { email: string; name: string | null; kind: "to" | "cc" }[];- body: string;- sentAt: Date;- gmailMessageId: string | null;-};-@Injectable()export class GmailSyncService {private readonly logger = new Logger(GmailSyncService.name);@@ -57,10 +43,9 @@ export class GmailSyncService {constructor(@InjectDatabase() private readonly db: Db,private readonly gmail: GmailClient,- private readonly tokens: GoogleTokenService,- private readonly match: GoogleMatchService,+ private readonly tokens: MailboxTokenService,private readonly state: SyncStateService,- private readonly stamp: ActivityStampService,+ private readonly threads: ThreadWriterService,) {}async sync(row: MailboxSync): Promise<GmailSyncOutcome> {@@ -89,7 +74,7 @@ export class GmailSyncService {const profile = await this.gmail.profile(token.accessToken);if (profile.outcome !== "ok") {- return this.handleFailure(row, profile, "gmail");+ return this.handleFailure(row, profile);}const mailbox = profile.data.emailAddress?.toLowerCase() ?? null;@@ -159,7 +144,7 @@ export class GmailSyncService {}if (history.outcome !== "ok") {- return this.handleFailure(row, history, "gmail");+ return this.handleFailure(row, history);}const ids = new Set<string>();@@ -223,18 +208,7 @@ export class GmailSyncService {if (batch.length === 0) return { written: 0, remaining };- const [internal, suppressedDomains, suppressedEmails] = await Promise.all([- this.match.internalIdentity(),- this.match.suppressedDomains(),- this.match.suppressedEmails(),- ]);-- const context = {- ourAddresses: internal.addresses,- ourDomains: internal.domains,- suppressedDomains,- suppressedEmails,- };+ const context: MatchContext = await this.threads.context();let written = 0;@@ -242,177 +216,22 @@ export class GmailSyncService {const message = await this.gmail.getMessage(accessToken, id);if (message.outcome !== "ok") continue;- const stored = await this.store(row, mailbox, message.data, context);- if (stored) written += 1;- }+ const parsed = this.parse(message.data);+ if (!parsed) continue;- return { written, remaining };- }-- private async store(- row: MailboxSync,- mailbox: string,- message: GmailMessage,- context: MatchContext,- ): Promise<boolean> {- const parsed = this.parse(message);- if (!parsed) return false;-- const existing = await this.db.emailMessage.findUnique({- where: { rfcMessageId: parsed.rfcMessageId },- select: { id: true },- });- if (existing) return false;-- const participants = [parsed.from, ...parsed.recipients];- const outbound = parsed.from.email === mailbox;-- const thread = await this.db.emailThread.findUnique({- where: { rootMessageId: parsed.rootId },- select: { id: true, companyId: true, contactId: true },- });-- let companyId = thread?.companyId ?? null;- let contactId = thread?.contactId ?? null;-- if (!thread) {- const repliedTo =- outbound || (await this.hasOutboundInThread(parsed.rootId, mailbox));-- const match = await this.match.resolve(- {- participants,- allowCreate: row.autoCreate && repliedTo,- source: RecordSource.EMAIL,- ownerId: row.userId,- },+ const stored = await this.threads.store(+ row,+ { mailbox, origin: "gmail" },+ parsed,context,);-- companyId = match.companyId;- contactId = match.contactId;-- if (!companyId && !contactId) {- return false;- }+ if (stored) written += 1;}- const record = await this.db.emailThread.upsert({- where: { rootMessageId: parsed.rootId },- create: {- rootMessageId: parsed.rootId,- subject: parsed.subject,- companyId,- contactId,- firstMessageAt: parsed.sentAt,- lastMessageAt: parsed.sentAt,- messageCount: 0,- },- update: {},- select: { id: true, firstMessageAt: true, lastMessageAt: true },- });-- await this.db.emailMessage.create({- data: {- threadId: record.id,- rfcMessageId: parsed.rfcMessageId,- syncedByUserId: row.userId,- gmailMessageId: parsed.gmailMessageId,- direction: outbound ? EmailDirection.OUTBOUND : EmailDirection.INBOUND,- fromEmail: parsed.from.email,- fromName: parsed.from.name,- recipients: parsed.recipients,- subject: parsed.subject,- snippet: snippetOf(parsed.body),- body: parsed.body || null,- sentAt: parsed.sentAt,- },- });-- const stats = await this.db.emailMessage.aggregate({- where: { threadId: record.id },- _count: { _all: true },- _min: { sentAt: true },- _max: { sentAt: true },- });-- const firstMessageAt = stats._min.sentAt ?? parsed.sentAt;- const lastMessageAt = stats._max.sentAt ?? parsed.sentAt;-- await this.db.emailThread.update({- where: { id: record.id },- data: {- messageCount: stats._count._all,- firstMessageAt,- lastMessageAt,- ...(parsed.sentAt <= firstMessageAt ? { subject: parsed.subject } : {}),- },- });-- await this.project(record.id, row.userId, {- subject: parsed.subject ?? "(no subject)",- snippet: snippetOf(parsed.body),- lastMessageAt,- companyId,- contactId,- });-- return true;- }-- private async hasOutboundInThread(- rootMessageId: string,- mailbox: string,- ): Promise<boolean> {- const found = await this.db.emailMessage.findFirst({- where: {- thread: { rootMessageId },- fromEmail: mailbox,- },- select: { id: true },- });-- return found !== null;- }-- private async project(- emailThreadId: string,- userId: string,- summary: {- subject: string;- snippet: string | null;- lastMessageAt: Date;- companyId: string | null;- contactId: string | null;- },- ): Promise<void> {- const activity = await this.db.activity.upsert({- where: { emailThreadId },- create: {- type: ActivityType.EMAIL,- subject: summary.subject,- body: summary.snippet,- occurredAt: summary.lastMessageAt,- companyId: summary.companyId,- contactId: summary.contactId,- createdById: userId,- emailThreadId,- meta: { synced: true, source: "gmail" },- },- update: {- body: summary.snippet,- occurredAt: summary.lastMessageAt,- },- select: { createdAt: true },- });-- await this.stamp.touch(- { companyId: summary.companyId, contactId: summary.contactId },- activity.createdAt,- );+ return { written, remaining };}- private parse(message: GmailMessage): ParsedMessage | null {+ private parse(message: GmailMessage): IncomingMessage | null {const headers = message.payload?.headers;const rawMessageId = header(headers, "message-id");@@ -471,12 +290,11 @@ export class GmailSyncService {private async handleFailure(row: MailboxSync,result: { outcome: string; reason: string; retryAfterMs?: number },- source: "gmail",): Promise<GmailSyncOutcome> {if (result.outcome === "unauthorized") {await this.state.markNeedsReconnect(row.id, result.reason);return {- source,+ source: "gmail",userId: row.userId,status: "reconnect",reason: result.reason,@@ -486,7 +304,7 @@ export class GmailSyncService {if (result.outcome === "rate-limited") {await this.state.markRateLimited(row.id, result.retryAfterMs ?? 60_000);return {- source,+ source: "gmail",userId: row.userId,status: "rate-limited",reason: result.reason,@@ -495,7 +313,7 @@ export class GmailSyncService {await this.state.markFailed(row.id, result.reason);return {- source,+ source: "gmail",userId: row.userId,status: "failed",reason: result.reason,diff --git a/apps/api/src/google/gmail.client.ts b/apps/api/src/google/gmail.client.tsindex 260b14c2..4ac2e35b 100644--- a/apps/api/src/google/gmail.client.ts+++ b/apps/api/src/google/gmail.client.ts@@ -1,6 +1,9 @@import { Injectable } from "@nestjs/common";-import { GoogleApiClient, type GoogleResult } from "./google-api.client";-import type { GmailPart } from "./mime";+import {+ MailboxApiClient,+ type MailboxResult,+} from "../mailbox/mailbox-api.client";+import type { GmailPart } from "./gmail-mime";const BASE = "https://gmail.googleapis.com/gmail/v1/users/me";@@ -39,9 +42,9 @@ export const WORK_MAIL_QUERY =@Injectable()export class GmailClient {- constructor(private readonly api: GoogleApiClient) {}+ constructor(private readonly api: MailboxApiClient) {}- async profile(accessToken: string): Promise<GoogleResult<Profile>> {+ async profile(accessToken: string): Promise<MailboxResult<Profile>> {return this.api.get<Profile>(`${BASE}/profile`, accessToken);}@@ -53,7 +56,7 @@ export class GmailClient {pageToken?: string;maxResults?: number;},- ): Promise<GoogleResult<MessageList>> {+ ): Promise<MailboxResult<MessageList>> {const after = Math.floor(options.after.getTime() / 1000);const before = Math.ceil(options.before.getTime() / 1000);@@ -67,7 +70,7 @@ export class GmailClient {async listHistory(accessToken: string,options: { startHistoryId: string; pageToken?: string },- ): Promise<GoogleResult<HistoryList>> {+ ): Promise<MailboxResult<HistoryList>> {return this.api.get<HistoryList>(`${BASE}/history`, accessToken, {startHistoryId: options.startHistoryId,historyTypes: "messageAdded",@@ -79,7 +82,7 @@ export class GmailClient {async getMessage(accessToken: string,id: string,- ): Promise<GoogleResult<GmailMessage>> {+ ): Promise<MailboxResult<GmailMessage>> {return this.api.get<GmailMessage>(`${BASE}/messages/${id}`, accessToken, {format: "full",});diff --git a/apps/api/src/google/google-connection.service.ts b/apps/api/src/google/google-connection.service.tsindex 839bb938..86c0d362 100644--- a/apps/api/src/google/google-connection.service.ts+++ b/apps/api/src/google/google-connection.service.ts@@ -1,21 +1,23 @@import { isGoogleConfigured, signsInWithGoogle } from "@crm/auth";-import { type Db, GoogleSyncStatus } from "@crm/db";+import { type Db, GoogleSyncStatus, type Prisma } from "@crm/db";import { Injectable, Logger, NotFoundException } from "@nestjs/common";import { normalizeDomain } from "../companies/domain";import { ActivityStampService } from "../crm/activity-stamp.service";import { InjectDatabase } from "../database/database.constants";+import { MailboxMatchService } from "../mailbox/mailbox-match.service";+import { MailboxTokenService } from "../mailbox/mailbox-token.service";+import { SyncStateService } from "../mailbox/sync-state.service";import {GOOGLE_PROVIDER_ID,+ GOOGLE_SYNC_SOURCES,+ type GoogleSyncSource,SCOPE_FOR_SOURCE,- SYNC_SOURCES,- type SyncSource,} from "./google.constants";-import { GoogleMatchService } from "./google-match.service";-import { GoogleTokenService } from "./google-token.service";-import { SyncStateService } from "./sync-state.service";++const PURGE_TIMEOUT_MS = 60_000;export type SourceStatus = {- source: SyncSource;+ source: GoogleSyncSource;connected: boolean;status: GoogleSyncStatus | null;lastSyncedAt: string | null;@@ -37,9 +39,9 @@ export class GoogleConnectionService {constructor(@InjectDatabase() private readonly db: Db,- private readonly tokens: GoogleTokenService,+ private readonly tokens: MailboxTokenService,private readonly state: SyncStateService,- private readonly match: GoogleMatchService,+ private readonly match: MailboxMatchService,private readonly stamp: ActivityStampService,) {}@@ -47,17 +49,17 @@ export class GoogleConnectionService {await this.onConnected(userId);const [granted, rows, hasRefreshToken, accounts] = await Promise.all([- this.tokens.grantedScopes(userId),- this.state.listForUser(userId),- this.tokens.hasRefreshToken(userId),+ this.tokens.grantedScopes(userId, GOOGLE_PROVIDER_ID),+ this.state.listForUser(userId, GOOGLE_SYNC_SOURCES),+ this.tokens.hasRefreshToken(userId, GOOGLE_PROVIDER_ID),this.tokens.signInAccounts(userId),]);const bySource = new Map(rows.map((row) => [row.source, row]));- const sources = SYNC_SOURCES.map((source): SourceStatus => {+ const sources = GOOGLE_SYNC_SOURCES.map((source): SourceStatus => {const row = bySource.get(source);- const connected = granted.includes(SCOPE_FOR_SOURCE[source]);+ const connected = granted.has(SCOPE_FOR_SOURCE[source]);return {source,@@ -71,9 +73,9 @@ export class GoogleConnectionService {return {configured: isGoogleConfigured(),- linked: accounts.some(- (account) => account.providerId === GOOGLE_PROVIDER_ID,- ),+ linked:+ accounts.some((account) => account.providerId === GOOGLE_PROVIDER_ID) &&+ sources.some((source) => source.connected),required: signsInWithGoogle(accounts),hasRefreshToken,sources,@@ -82,16 +84,16 @@ export class GoogleConnectionService {async onConnected(userId: string): Promise<void> {const [granted, existing] = await Promise.all([- this.tokens.grantedScopes(userId),- this.state.listForUser(userId),+ this.tokens.grantedScopes(userId, GOOGLE_PROVIDER_ID),+ this.state.listForUser(userId, GOOGLE_SYNC_SOURCES),]);const known = new Set(existing.map((row) => row.source));const added: string[] = [];- for (const source of SYNC_SOURCES) {- if (!granted.includes(SCOPE_FOR_SOURCE[source])) continue;+ for (const source of GOOGLE_SYNC_SOURCES) {+ if (!granted.has(SCOPE_FOR_SOURCE[source])) continue;if (known.has(source)) continue;await this.state.ensure(userId, source, {@@ -109,8 +111,8 @@ export class GoogleConnectionService {async reconcileAll(): Promise<void> {const accounts = await this.db.account.findMany({where: {- providerId: "google",- OR: SYNC_SOURCES.map((source) => ({+ providerId: GOOGLE_PROVIDER_ID,+ OR: GOOGLE_SYNC_SOURCES.map((source) => ({scope: { contains: SCOPE_FOR_SOURCE[source] },})),},@@ -123,31 +125,56 @@ export class GoogleConnectionService {}async purgeSyncedData(userId: string): Promise<{ purged: number }> {- const [threads, events] = await this.db.$transaction([- this.db.emailThread.deleteMany({- where: { messages: { some: { syncedByUserId: userId } } },- }),- this.db.calendarEvent.deleteMany({ where: { syncedByUserId: userId } }),- ]);+ const mine: Prisma.EmailMessageWhereInput = {+ syncedByUserId: userId,+ gmailMessageId: { not: null },+ };- await this.stamp.recomputeAll();+ const purged = await this.db.$transaction(+ async (tx) => {+ const touched = await tx.emailMessage.findMany({+ where: mine,+ select: { threadId: true },+ distinct: ["threadId"],+ });++ const threadIds = touched.map((row) => row.threadId);+ const messages = await tx.emailMessage.deleteMany({ where: mine });++ await tx.emailThread.deleteMany({+ where: { id: { in: threadIds }, messages: { none: {} } },+ });- const purged = threads.count + events.count;+ await rebuildThreads(tx, threadIds);- this.logger.log({ message: "Synced data purged", userId, purged });+ const events = await tx.calendarEvent.deleteMany({+ where: { syncedByUserId: userId },+ });++ return messages.count + events.count;+ },+ { timeout: PURGE_TIMEOUT_MS },+ );++ await this.stamp.recomputeAll();++ this.logger.log({ message: "Google data purged", userId, purged });return { purged };}async revoke(userId: string): Promise<{ revoked: boolean }> {- await this.state.remove(userId);- const revoked = await this.tokens.revoke(userId);+ for (const source of GOOGLE_SYNC_SOURCES) {+ await this.state.remove(userId, source);+ }++ const revoked = await this.tokens.revoke(userId, GOOGLE_PROVIDER_ID);return { revoked };}async setAutoCreate(userId: string,- source: SyncSource,+ source: GoogleSyncSource,enabled: boolean,): Promise<void> {const row = await this.state.get(userId, source);@@ -205,3 +232,45 @@ export class GoogleConnectionService {return { domain: normalised, purged: threads.count + events.count };}}++async function rebuildThreads(+ tx: Prisma.TransactionClient,+ threadIds: string[],+): Promise<void> {+ if (threadIds.length === 0) return;++ const remaining = await tx.emailMessage.findMany({+ where: { threadId: { in: threadIds } },+ select: { threadId: true, sentAt: true, subject: true, snippet: true },+ orderBy: { sentAt: "asc" },+ });++ const byThread = new Map<string, typeof remaining>();++ for (const message of remaining) {+ const group = byThread.get(message.threadId);+ if (group) group.push(message);+ else byThread.set(message.threadId, [message]);+ }++ for (const [threadId, messages] of byThread) {+ const first = messages.at(0);+ const last = messages.at(-1);+ if (!first || !last) continue;++ await tx.emailThread.update({+ where: { id: threadId },+ data: {+ messageCount: messages.length,+ firstMessageAt: first.sentAt,+ lastMessageAt: last.sentAt,+ subject: first.subject,+ },+ });++ await tx.activity.updateMany({+ where: { emailThreadId: threadId },+ data: { body: last.snippet, occurredAt: last.sentAt },+ });+ }+}diff --git a/apps/api/src/google/google-sync.service.ts b/apps/api/src/google/google-sync.service.tsindex 6b43f180..31532336 100644--- a/apps/api/src/google/google-sync.service.ts+++ b/apps/api/src/google/google-sync.service.ts@@ -1,104 +1,18 @@-import { syncError } from "@crm/telemetry";-import { Injectable, Logger } from "@nestjs/common";+import { Injectable } from "@nestjs/common";+import { SyncStateService } from "../mailbox/sync-state.service";import { CalendarSyncService } from "./calendar-sync.service";import { GmailSyncService } from "./gmail-sync.service";-import type { SyncSource } from "./google.constants";-import { GoogleConnectionService } from "./google-connection.service";-import { SyncStateService } from "./sync-state.service";--const TICK_BUDGET_MS = 60_000;--export type TickSummary = {- attempted: number;- synced: number;- skipped: number;- failed: number;- durationMs: number;-};+import { GOOGLE_SYNC_SOURCES, type GoogleSyncSource } from "./google.constants";@Injectable()export class GoogleSyncService {- private readonly logger = new Logger(GoogleSyncService.name);-constructor(private readonly state: SyncStateService,private readonly calendar: CalendarSyncService,private readonly gmail: GmailSyncService,- private readonly connections: GoogleConnectionService,) {}- async runDue(): Promise<TickSummary> {- const startedAt = Date.now();- const summary: TickSummary = {- attempted: 0,- synced: 0,- skipped: 0,- failed: 0,- durationMs: 0,- };-- await this.connections.reconcileAll();-- const due = await this.state.due(new Date());-- for (const row of due) {- if (Date.now() - startedAt > TICK_BUDGET_MS) {- this.logger.log({- message: "Sync tick budget reached",- remaining: due.length - summary.attempted,- });- break;- }-- summary.attempted += 1;-- try {- const outcome = await this.runOne(row.userId, row.source as SyncSource);-- if (outcome === null || outcome.status === "skipped") {- summary.skipped += 1;- } else if (- outcome.status === "failed" ||- outcome.status === "reconnect"- ) {- summary.failed += 1;- } else {- summary.synced += 1;- }- } catch (error) {- summary.failed += 1;- await this.state.markFailed(- row.id,- error instanceof Error ? error.message : String(error),- );- this.logger.error(- {- message: "Sync threw",- userId: row.userId,- source: row.source,- },- error instanceof Error ? error.stack : String(error),- );-- syncError({ error, source: row.source });- }- }-- summary.durationMs = Date.now() - startedAt;-- this.logger.log({- message: "Google sync tick",- attempted: summary.attempted,- synced: summary.synced,- skipped: summary.skipped,- failed: summary.failed,- durationMs: summary.durationMs,- });-- return summary;- }-- async runOne(userId: string, source: SyncSource) {+ async runOne(userId: string, source: GoogleSyncSource) {const row = await this.state.get(userId, source);if (!row) return null;@@ -108,7 +22,7 @@ export class GoogleSyncService {}async runForUser(userId: string): Promise<void> {- for (const source of ["calendar", "gmail"] as const) {+ for (const source of GOOGLE_SYNC_SOURCES) {await this.runOne(userId, source);}}diff --git a/apps/api/src/google/google.constants.ts b/apps/api/src/google/google.constants.tsindex 3d48d833..72588205 100644--- a/apps/api/src/google/google.constants.ts+++ b/apps/api/src/google/google.constants.ts@@ -1,16 +1,9 @@-import { CALENDAR_SCOPE, GMAIL_SCOPE } from "@crm/auth";-export {CALENDAR_SCOPE,GMAIL_SCOPE,GOOGLE_PROVIDER_ID,+ GOOGLE_SYNC_SOURCES,+ type GoogleSyncSource,+ SCOPE_FOR_SOURCE,SYNC_SCOPES,-} from "@crm/auth";--export const SYNC_SOURCES = ["calendar", "gmail"] as const;-export type SyncSource = (typeof SYNC_SOURCES)[number];--export const SCOPE_FOR_SOURCE: Record<SyncSource, string> = {- calendar: CALENDAR_SCOPE,- gmail: GMAIL_SCOPE,-};+} from "../mailbox/mailbox.constants";diff --git a/apps/api/src/google/google.contracts.ts b/apps/api/src/google/google.contracts.tsindex f6b27626..672ac71b 100644--- a/apps/api/src/google/google.contracts.ts+++ b/apps/api/src/google/google.contracts.ts@@ -1,12 +1,8 @@import { z } from "zod";-import { SYNC_SOURCES } from "./google.constants";--export const syncSourceInput = z.object({- source: z.enum(SYNC_SOURCES),-});+import { GOOGLE_SYNC_SOURCES } from "./google.constants";export const setAutoCreateInput = z.object({- source: z.enum(SYNC_SOURCES),+ source: z.enum(GOOGLE_SYNC_SOURCES),enabled: z.boolean(),});diff --git a/apps/api/src/google/google.module.ts b/apps/api/src/google/google.module.tsindex c9ba6c54..7b841cd8 100644--- a/apps/api/src/google/google.module.ts+++ b/apps/api/src/google/google.module.ts@@ -1,6 +1,6 @@import { Module } from "@nestjs/common";import { AgentModule } from "../agent/agent.module";-import { CompaniesModule } from "../companies/companies.module";+import { MailboxModule } from "../mailbox/mailbox.module";import { TrpcModule } from "../trpc/trpc.module";import { CalendarClient } from "./calendar.client";import { CalendarSyncService } from "./calendar-sync.service";@@ -8,22 +8,12 @@ import { ConversationService } from "./conversation.service";import { GmailClient } from "./gmail.client";import { GmailSyncService } from "./gmail-sync.service";import { GoogleRouter } from "./google.router";-import { GoogleApiClient } from "./google-api.client";import { GoogleConnectionService } from "./google-connection.service";-import { GoogleMatchService } from "./google-match.service";import { GoogleSyncService } from "./google-sync.service";-import { GoogleTokenService } from "./google-token.service";-import { SyncController } from "./sync.controller";-import { SyncStateService } from "./sync-state.service";@Module({- imports: [TrpcModule, AgentModule, CompaniesModule],- controllers: [SyncController],+ imports: [TrpcModule, MailboxModule, AgentModule],providers: [- GoogleApiClient,- GoogleTokenService,- SyncStateService,- GoogleMatchService,CalendarClient,CalendarSyncService,GmailClient,diff --git a/apps/api/src/google/google-api.client.ts b/apps/api/src/mailbox/mailbox-api.client.tssimilarity index 87%rename from apps/api/src/google/google-api.client.tsrename to apps/api/src/mailbox/mailbox-api.client.tsindex f84eb112..c286d3ba 100644--- a/apps/api/src/google/google-api.client.ts+++ b/apps/api/src/mailbox/mailbox-api.client.ts@@ -1,6 +1,6 @@import { Injectable, Logger } from "@nestjs/common";-export type GoogleResult<T> =+export type MailboxResult<T> =| { outcome: "ok"; data: T }| { outcome: "cursor-invalid"; reason: string }| { outcome: "unauthorized"; reason: string }@@ -13,14 +13,14 @@ const MIN_BACKOFF_MS = 30_000;const MAX_BACKOFF_MS = 15 * 60_000;@Injectable()-export class GoogleApiClient {- private readonly logger = new Logger(GoogleApiClient.name);+export class MailboxApiClient {+ private readonly logger = new Logger(MailboxApiClient.name);async get<T>(url: string,accessToken: string,params: Record<string, string | number | boolean | undefined> = {},- ): Promise<GoogleResult<T>> {+ ): Promise<MailboxResult<T>> {const target = new URL(url);for (const [key, value] of Object.entries(params)) {if (value !== undefined) target.searchParams.set(key, String(value));@@ -55,7 +55,7 @@ export class GoogleApiClient {private async interpret<T>(response: Response,path: string,- ): Promise<GoogleResult<T>> {+ ): Promise<MailboxResult<T>> {if (response.ok) {return { outcome: "ok", data: (await response.json()) as T };}@@ -90,7 +90,7 @@ export class GoogleApiClient {default: {const retryable = response.status >= 500;this.logger.warn({- message: "Google API call failed",+ message: "Mailbox API call failed",path,status: response.status,retryable,@@ -113,10 +113,13 @@ export class GoogleApiClient {private async reason(response: Response): Promise<string> {try {const body = (await response.json()) as {- error?: { message?: string; status?: string };+ error?: { message?: string; status?: string; code?: string };};return (- body.error?.message ?? body.error?.status ?? `HTTP ${response.status}`+ body.error?.message ??+ body.error?.status ??+ body.error?.code ??+ `HTTP ${response.status}`);} catch {return `HTTP ${response.status}`;diff --git a/apps/api/src/google/google-match.service.ts b/apps/api/src/mailbox/mailbox-match.service.tssimilarity index 97%rename from apps/api/src/google/google-match.service.tsrename to apps/api/src/mailbox/mailbox-match.service.tsindex e65c7cfd..eaf77b2a 100644--- a/apps/api/src/google/google-match.service.ts+++ b/apps/api/src/mailbox/mailbox-match.service.ts@@ -39,8 +39,8 @@ export type MatchRequest = {};@Injectable()-export class GoogleMatchService {- private readonly logger = new Logger(GoogleMatchService.name);+export class MailboxMatchService {+ private readonly logger = new Logger(MailboxMatchService.name);constructor(@InjectDatabase() private readonly db: Db,@@ -191,7 +191,7 @@ export class GoogleMatchService {});this.logger.log({- message: "Company auto-created from Google sync",+ message: "Company auto-created from mailbox sync",companyId,domain,source: request.source,diff --git a/apps/api/src/google/google-token.service.ts b/apps/api/src/mailbox/mailbox-token.service.tssimilarity index 54%rename from apps/api/src/google/google-token.service.tsrename to apps/api/src/mailbox/mailbox-token.service.tsindex 41586fd2..bc5df6ec 100644--- a/apps/api/src/google/google-token.service.ts+++ b/apps/api/src/mailbox/mailbox-token.service.ts@@ -1,12 +1,18 @@-import { auth, type SignInAccount } from "@crm/auth";+import {+ auth,+ type MailboxProviderId,+ parseScopes,+ type SignInAccount,+} from "@crm/auth";import { type Db } from "@crm/db";import { Injectable, Logger } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";import {GOOGLE_PROVIDER_ID,+ PROVIDER_FOR_SOURCE,SCOPE_FOR_SOURCE,type SyncSource,-} from "./google.constants";+} from "./mailbox.constants";export type TokenFailure =| { outcome: "needs-reconnect"; reason: string }@@ -14,29 +20,32 @@ export type TokenFailure =export type TokenResult = { outcome: "ok"; accessToken: string } | TokenFailure;+const GOOGLE_REVOKE_URL = "https://oauth2.googleapis.com/revoke";+@Injectable()-export class GoogleTokenService {- private readonly logger = new Logger(GoogleTokenService.name);+export class MailboxTokenService {+ private readonly logger = new Logger(MailboxTokenService.name);constructor(@InjectDatabase() private readonly db: Db) {}- async grantedScopes(userId: string): Promise<string[]> {+ async grantedScopes(+ userId: string,+ providerId: MailboxProviderId,+ ): Promise<Set<string>> {const account = await this.db.account.findFirst({- where: { userId, providerId: GOOGLE_PROVIDER_ID },+ where: { userId, providerId },select: { scope: true },});- return (- account?.scope- ?.split(",")- .map((scope) => scope.trim())- .filter(Boolean) ?? []- );+ return parseScopes(account?.scope);}async isConnected(userId: string, source: SyncSource): Promise<boolean> {- const scopes = await this.grantedScopes(userId);- return scopes.includes(SCOPE_FOR_SOURCE[source]);+ const scopes = await this.grantedScopes(+ userId,+ PROVIDER_FOR_SOURCE[source],+ );+ return scopes.has(SCOPE_FOR_SOURCE[source]);}async signInAccounts(userId: string): Promise<SignInAccount[]> {@@ -46,9 +55,12 @@ export class GoogleTokenService {});}- async hasRefreshToken(userId: string): Promise<boolean> {+ async hasRefreshToken(+ userId: string,+ providerId: MailboxProviderId,+ ): Promise<boolean> {const account = await this.db.account.findFirst({- where: { userId, providerId: GOOGLE_PROVIDER_ID },+ where: { userId, providerId },select: { refreshToken: true },});@@ -59,6 +71,8 @@ export class GoogleTokenService {userId: string,source: SyncSource,): Promise<TokenResult> {+ const providerId = PROVIDER_FOR_SOURCE[source];+if (!(await this.isConnected(userId, source))) {return {outcome: "not-connected",@@ -68,68 +82,88 @@ export class GoogleTokenService {try {const { accessToken } = await auth.api.getAccessToken({- body: { providerId: GOOGLE_PROVIDER_ID, userId },+ body: { providerId, userId },});if (!accessToken) {return {outcome: "needs-reconnect",- reason: "Google returned no access token.",+ reason: `${label(providerId)} returned no access token.`,};}return { outcome: "ok", accessToken };} catch (error) {this.logger.warn({- message: "Google token refresh failed",+ message: "Mailbox token refresh failed",userId,+ providerId,source,reason: error instanceof Error ? error.message : String(error),});return {outcome: "needs-reconnect",- reason: "Google would not refresh the access token.",+ reason: `${label(providerId)} would not refresh the access token.`,};}}- async revoke(userId: string): Promise<boolean> {+ async revoke(+ userId: string,+ providerId: MailboxProviderId,+ ): Promise<boolean> {+ if (+ providerId === GOOGLE_PROVIDER_ID &&+ !(await this.revokeWithGoogle(userId))+ ) {+ return false;+ }++ const cleared = await this.db.account.updateMany({+ where: { userId, providerId },+ data: {+ accessToken: null,+ refreshToken: null,+ scope: null,+ accessTokenExpiresAt: null,+ refreshTokenExpiresAt: null,+ },+ });++ if (cleared.count === 0) return false;++ this.logger.log({ message: "Mailbox access revoked", userId, providerId });+ return true;+ }++ private async revokeWithGoogle(userId: string): Promise<boolean> {const account = await this.db.account.findFirst({where: { userId, providerId: GOOGLE_PROVIDER_ID },select: { refreshToken: true, accessToken: true },});const token = account?.refreshToken ?? account?.accessToken;- if (!token) return false;+ if (!token) return true;- const response = await fetch("https://oauth2.googleapis.com/revoke", {+ const response = await fetch(GOOGLE_REVOKE_URL, {method: "POST",headers: { "content-type": "application/x-www-form-urlencoded" },body: new URLSearchParams({ token }),});- if (!response.ok) {- this.logger.warn({- message: "Google token revocation failed",- userId,- status: response.status,- });- return false;- }+ if (response.ok) return true;- await this.db.account.updateMany({- where: { userId, providerId: GOOGLE_PROVIDER_ID },- data: {- accessToken: null,- refreshToken: null,- scope: null,- accessTokenExpiresAt: null,- refreshTokenExpiresAt: null,- },+ this.logger.warn({+ message: "Google token revocation failed",+ userId,+ status: response.status,});- this.logger.log({ message: "Google access revoked", userId });- return true;+ return false;}}++function label(providerId: MailboxProviderId): string {+ return providerId === GOOGLE_PROVIDER_ID ? "Google" : "Microsoft";+}diff --git a/apps/api/src/mailbox/mailbox.constants.ts b/apps/api/src/mailbox/mailbox.constants.tsnew file mode 100644index 00000000..4e062580--- /dev/null+++ b/apps/api/src/mailbox/mailbox.constants.ts@@ -0,0 +1,50 @@+import {+ CALENDAR_SCOPE,+ GMAIL_SCOPE,+ GOOGLE_PROVIDER_ID,+ type MailboxProviderId,+ MICROSOFT_PROVIDER_ID,+ OUTLOOK_MAIL_SCOPE,+} from "@crm/auth";++export {+ CALENDAR_SCOPE,+ GMAIL_SCOPE,+ GOOGLE_PROVIDER_ID,+ type MailboxProviderId,+ MICROSOFT_PROVIDER_ID,+ MICROSOFT_SYNC_SCOPES,+ OUTLOOK_MAIL_SCOPE,+ SYNC_SCOPES,+} from "@crm/auth";++export const SYNC_SOURCES = ["calendar", "gmail", "outlook"] as const;+export type SyncSource = (typeof SYNC_SOURCES)[number];++export const GOOGLE_SYNC_SOURCES = ["calendar", "gmail"] as const;+export const MICROSOFT_SYNC_SOURCES = ["outlook"] as const;++export type GoogleSyncSource = (typeof GOOGLE_SYNC_SOURCES)[number];+export type MicrosoftSyncSource = (typeof MICROSOFT_SYNC_SOURCES)[number];++export function isGoogleSyncSource(source: string): source is GoogleSyncSource {+ return (GOOGLE_SYNC_SOURCES as readonly string[]).includes(source);+}++export function isMicrosoftSyncSource(+ source: string,+): source is MicrosoftSyncSource {+ return (MICROSOFT_SYNC_SOURCES as readonly string[]).includes(source);+}++export const SCOPE_FOR_SOURCE: Record<SyncSource, string> = {+ calendar: CALENDAR_SCOPE,+ gmail: GMAIL_SCOPE,+ outlook: OUTLOOK_MAIL_SCOPE,+};++export const PROVIDER_FOR_SOURCE: Record<SyncSource, MailboxProviderId> = {+ calendar: GOOGLE_PROVIDER_ID,+ gmail: GOOGLE_PROVIDER_ID,+ outlook: MICROSOFT_PROVIDER_ID,+};diff --git a/apps/api/src/mailbox/mailbox.module.ts b/apps/api/src/mailbox/mailbox.module.tsnew file mode 100644index 00000000..7e9ca71c--- /dev/null+++ b/apps/api/src/mailbox/mailbox.module.ts@@ -0,0 +1,27 @@+import { Module } from "@nestjs/common";+import { AgentModule } from "../agent/agent.module";+import { CompaniesModule } from "../companies/companies.module";+import { MailboxApiClient } from "./mailbox-api.client";+import { MailboxMatchService } from "./mailbox-match.service";+import { MailboxTokenService } from "./mailbox-token.service";+import { SyncStateService } from "./sync-state.service";+import { ThreadWriterService } from "./thread-writer.service";++@Module({+ imports: [AgentModule, CompaniesModule],+ providers: [+ MailboxApiClient,+ MailboxTokenService,+ MailboxMatchService,+ SyncStateService,+ ThreadWriterService,+ ],+ exports: [+ MailboxApiClient,+ MailboxTokenService,+ MailboxMatchService,+ SyncStateService,+ ThreadWriterService,+ ],+})+export class MailboxModule {}diff --git a/apps/api/src/google/mime.ts b/apps/api/src/mailbox/message-text.tssimilarity index 51%rename from apps/api/src/google/mime.tsrename to apps/api/src/mailbox/message-text.tsindex ffa33af9..7fba65c2 100644--- a/apps/api/src/google/mime.ts+++ b/apps/api/src/mailbox/message-text.ts@@ -1,22 +1,3 @@-export type GmailHeader = { name?: string; value?: string };--export type GmailPart = {- mimeType?: string;- filename?: string;- headers?: GmailHeader[];- body?: { data?: string; size?: number; attachmentId?: string };- parts?: GmailPart[];-};--export function header(- headers: readonly GmailHeader[] | undefined,- name: string,-): string | null {- const wanted = name.toLowerCase();- const found = headers?.find((entry) => entry.name?.toLowerCase() === wanted);- return found?.value?.trim() ?? null;-}-export function decodeBase64Url(data: string): string {const normalised = data.replace(/-/g, "+").replace(/_/g, "/");const padded = normalised.padEnd(@@ -31,35 +12,6 @@ export function decodeBase64Url(data: string): string {}}-export function plainTextBody(payload: GmailPart | undefined): string {- if (!payload) return "";-- const plain = findPart(payload, "text/plain");- if (plain?.body?.data) return decodeBase64Url(plain.body.data);-- const html = findPart(payload, "text/html");- if (html?.body?.data) return stripHtml(decodeBase64Url(html.body.data));-- if (payload.body?.data && !payload.filename) {- return decodeBase64Url(payload.body.data);- }-- return "";-}--function findPart(part: GmailPart, mimeType: string): GmailPart | null {- if (part.mimeType === mimeType && !part.filename && part.body?.data) {- return part;- }-- for (const child of part.parts ?? []) {- const found = findPart(child, mimeType);- if (found) return found;- }-- return null;-}-export function stripHtml(html: string): string {return html.replace(/<style[\s\S]*?<\/style>/gi, "")@@ -105,22 +57,29 @@ export function stripQuotedHistory(body: string): string {return trimmed.replace(/\n{3,}/g, "\n\n").trim();}-export function rootMessageId(- headers: readonly GmailHeader[] | undefined,-): string | null {- const references = header(headers, "references");- if (references) {- const first = references- .split(/\s+/)- .find((entry) => entry.startsWith("<"));- if (first) return normaliseMessageId(first);+export type ThreadHeaders = {+ references: string | null;+ inReplyTo: string | null;+ messageId: string | null;+};++export function rootMessageIdFrom(headers: ThreadHeaders): string | null {+ if (headers.references) {+ const first = firstMessageId(headers.references);+ if (first) return first;}- const inReplyTo = header(headers, "in-reply-to");- if (inReplyTo) return normaliseMessageId(inReplyTo);+ if (headers.inReplyTo) {+ const first = firstMessageId(headers.inReplyTo);+ if (first) return first;+ }++ return headers.messageId ? normaliseMessageId(headers.messageId) : null;+}- const own = header(headers, "message-id");- return own ? normaliseMessageId(own) : null;+export function firstMessageId(value: string): string | null {+ const found = value.match(/<[^<>]+>|[^\s<>]+/);+ return found ? normaliseMessageId(found[0]) : null;}export function normaliseMessageId(value: string): string {diff --git a/apps/api/src/google/participants.ts b/apps/api/src/mailbox/participants.tssimilarity index 100%rename from apps/api/src/google/participants.tsrename to apps/api/src/mailbox/participants.tsdiff --git a/apps/api/src/google/sync-state.service.ts b/apps/api/src/mailbox/sync-state.service.tssimilarity index 72%rename from apps/api/src/google/sync-state.service.tsrename to apps/api/src/mailbox/sync-state.service.tsindex 37cb924f..d669983b 100644--- a/apps/api/src/google/sync-state.service.ts+++ b/apps/api/src/mailbox/sync-state.service.ts@@ -5,7 +5,9 @@ import {} from "@crm/db";import { Injectable, Logger } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";-import type { SyncSource } from "./google.constants";+import type { SyncSource } from "./mailbox.constants";++export const SYNC_LEASE_MS = 300_000;@Injectable()export class SyncStateService {@@ -19,20 +21,41 @@ export class SyncStateService {});}- async listForUser(userId: string): Promise<MailboxSync[]> {- return this.db.mailboxSync.findMany({ where: { userId } });+ async listForUser(+ userId: string,+ sources?: readonly SyncSource[],+ ): Promise<MailboxSync[]> {+ return this.db.mailboxSync.findMany({+ where: { userId, ...(sources ? { source: { in: [...sources] } } : {}) },+ });}async due(now: Date): Promise<MailboxSync[]> {return this.db.mailboxSync.findMany({- where: {- status: { notIn: [GoogleSyncStatus.NEEDS_RECONNECT] },- OR: [{ retryAfter: null }, { retryAfter: { lte: now } }],- },+ where: dueWhere(now),orderBy: [{ lastSyncedAt: { sort: "asc", nulls: "first" } }],});}+ async claim(row: MailboxSync, now: Date): Promise<boolean> {+ const { count } = await this.db.mailboxSync.updateMany({+ where: { id: row.id, updatedAt: row.updatedAt, ...dueWhere(now) },+ data: {+ status: GoogleSyncStatus.RUNNING,+ retryAfter: new Date(now.getTime() + SYNC_LEASE_MS),+ },+ });++ return count === 1;+ }++ async release(id: string): Promise<void> {+ await this.db.mailboxSync.updateMany({+ where: { id },+ data: { retryAfter: null },+ });+ }+async ensure(userId: string,source: SyncSource,@@ -92,6 +115,7 @@ export class SyncStateService {cursor: null,status: GoogleSyncStatus.IDLE,lastError: null,+ retryAfter: null,},});}@@ -120,7 +144,11 @@ export class SyncStateService {async markFailed(id: string, reason: string): Promise<void> {await this.db.mailboxSync.update({where: { id },- data: { status: GoogleSyncStatus.FAILED, lastError: reason },+ data: {+ status: GoogleSyncStatus.FAILED,+ lastError: reason,+ retryAfter: null,+ },});}@@ -141,3 +169,10 @@ export class SyncStateService {});}}++function dueWhere(now: Date) {+ return {+ status: { notIn: [GoogleSyncStatus.NEEDS_RECONNECT] },+ OR: [{ retryAfter: null }, { retryAfter: { lte: now } }],+ };+}diff --git a/apps/api/src/mailbox/thread-writer.service.ts b/apps/api/src/mailbox/thread-writer.service.tsnew file mode 100644index 00000000..57d131b2--- /dev/null+++ b/apps/api/src/mailbox/thread-writer.service.ts@@ -0,0 +1,292 @@+import {+ ActivityType,+ type Db,+ EmailDirection,+ type MailboxSyncModel as MailboxSync,+ type Prisma,+ Prisma as PrismaNamespace,+ RecordSource,+} from "@crm/db";+import { Injectable, Logger } from "@nestjs/common";+import { ActivityStampService } from "../crm/activity-stamp.service";+import { InjectDatabase } from "../database/database.constants";+import type { SyncSource } from "./mailbox.constants";+import {+ MailboxMatchService,+ type MatchContext,+} from "./mailbox-match.service";+import { snippetOf } from "./message-text";+import type { Participant } from "./participants";++export type IncomingMessage = {+ rfcMessageId: string;+ rootId: string;+ subject: string | null;+ from: Participant;+ recipients: { email: string; name: string | null; kind: "to" | "cc" }[];+ body: string;+ sentAt: Date;+ gmailMessageId?: string | null;+ outlookMessageId?: string | null;+ outlookWebLink?: string | null;+};++@Injectable()+export class ThreadWriterService {+ private readonly logger = new Logger(ThreadWriterService.name);++ constructor(+ @InjectDatabase() private readonly db: Db,+ private readonly match: MailboxMatchService,+ private readonly stamp: ActivityStampService,+ ) {}++ async context(): Promise<MatchContext> {+ const [internal, suppressedDomains, suppressedEmails] = await Promise.all([+ this.match.internalIdentity(),+ this.match.suppressedDomains(),+ this.match.suppressedEmails(),+ ]);++ return {+ ourAddresses: internal.addresses,+ ourDomains: internal.domains,+ suppressedDomains,+ suppressedEmails,+ };+ }++ async store(+ row: MailboxSync,+ options: { mailbox: string; origin: SyncSource },+ parsed: IncomingMessage,+ context: MatchContext,+ ): Promise<boolean> {+ const existing = await this.db.emailMessage.findUnique({+ where: { rfcMessageId: parsed.rfcMessageId },+ select: {+ threadId: true,+ thread: {+ select: {+ companyId: true,+ contactId: true,+ activity: { select: { id: true } },+ },+ },+ },+ });+ if (existing?.thread.activity) return false;++ const repair = existing !== null;+ const participants = [parsed.from, ...parsed.recipients];+ const outbound = parsed.from.email === options.mailbox;++ const thread = existing+ ? {+ id: existing.threadId,+ companyId: existing.thread.companyId,+ contactId: existing.thread.contactId,+ }+ : await this.db.emailThread.findUnique({+ where: { rootMessageId: parsed.rootId },+ select: { id: true, companyId: true, contactId: true },+ });++ let companyId = thread?.companyId ?? null;+ let contactId = thread?.contactId ?? null;++ if (!thread) {+ const repliedTo =+ outbound ||+ (await this.hasOutboundInThread(parsed.rootId, options.mailbox));++ const match = await this.match.resolve(+ {+ participants,+ allowCreate: row.autoCreate && repliedTo,+ source: RecordSource.EMAIL,+ ownerId: row.userId,+ },+ context,+ );++ companyId = match.companyId;+ contactId = match.contactId;++ if (!companyId && !contactId) {+ return false;+ }+ }++ let occurredAt: Date;++ try {+ occurredAt = await this.db.$transaction(async (tx) => {+ const record = existing+ ? { id: existing.threadId }+ : await tx.emailThread.upsert({+ where: { rootMessageId: parsed.rootId },+ create: {+ rootMessageId: parsed.rootId,+ subject: parsed.subject,+ companyId,+ contactId,+ firstMessageAt: parsed.sentAt,+ lastMessageAt: parsed.sentAt,+ messageCount: 0,+ },+ update: {},+ select: { id: true },+ });++ if (!repair) {+ await tx.emailMessage.create({+ data: {+ threadId: record.id,+ rfcMessageId: parsed.rfcMessageId,+ syncedByUserId: row.userId,+ gmailMessageId: parsed.gmailMessageId ?? null,+ outlookMessageId: parsed.outlookMessageId ?? null,+ outlookWebLink: parsed.outlookWebLink ?? null,+ direction: outbound+ ? EmailDirection.OUTBOUND+ : EmailDirection.INBOUND,+ fromEmail: parsed.from.email,+ fromName: parsed.from.name,+ recipients: parsed.recipients,+ subject: parsed.subject,+ snippet: snippetOf(parsed.body),+ body: parsed.body || null,+ sentAt: parsed.sentAt,+ },+ });+ }++ const stats = await tx.emailMessage.aggregate({+ where: { threadId: record.id },+ _count: { _all: true },+ _min: { sentAt: true },+ _max: { sentAt: true },+ });++ const firstMessageAt = stats._min.sentAt ?? parsed.sentAt;+ const lastMessageAt = stats._max.sentAt ?? parsed.sentAt;++ await tx.emailThread.update({+ where: { id: record.id },+ data: {+ messageCount: stats._count._all,+ firstMessageAt,+ lastMessageAt,+ ...(parsed.sentAt <= firstMessageAt+ ? { subject: parsed.subject }+ : {}),+ },+ });++ return this.project(tx, record.id, row.userId, {+ subject: parsed.subject ?? "(no subject)",+ snippet: snippetOf(parsed.body),+ lastMessageAt,+ companyId,+ contactId,+ origin: options.origin,+ });+ });+ } catch (error) {+ if (await this.storedElsewhere(error, parsed.rfcMessageId)) return false;+ throw error;+ }++ await this.touch({ companyId, contactId }, occurredAt, parsed.rfcMessageId);++ return !repair;+ }++ private async storedElsewhere(+ error: unknown,+ rfcMessageId: string,+ ): Promise<boolean> {+ const duplicate =+ error instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ error.code === "P2002";+ if (!duplicate) return false;++ const winner = await this.db.emailMessage.findFirst({+ where: { rfcMessageId, thread: { activity: { isNot: null } } },+ select: { id: true },+ });++ return winner !== null;+ }++ private async touch(+ target: { companyId: string | null; contactId: string | null },+ at: Date,+ rfcMessageId: string,+ ): Promise<void> {+ try {+ await this.stamp.touch(target, at);+ } catch (error) {+ this.logger.error(+ {+ message: "An email was stored but its activity stamps were not moved",+ rfcMessageId,+ ...target,+ },+ error instanceof Error ? error.stack : String(error),+ );+ }+ }++ private async hasOutboundInThread(+ rootMessageId: string,+ mailbox: string,+ ): Promise<boolean> {+ const found = await this.db.emailMessage.findFirst({+ where: {+ thread: { rootMessageId },+ fromEmail: mailbox,+ },+ select: { id: true },+ });++ return found !== null;+ }++ private async project(+ tx: Prisma.TransactionClient,+ emailThreadId: string,+ userId: string,+ summary: {+ subject: string;+ snippet: string | null;+ lastMessageAt: Date;+ companyId: string | null;+ contactId: string | null;+ origin: SyncSource;+ },+ ): Promise<Date> {+ const activity = await tx.activity.upsert({+ where: { emailThreadId },+ create: {+ type: ActivityType.EMAIL,+ subject: summary.subject,+ body: summary.snippet,+ occurredAt: summary.lastMessageAt,+ companyId: summary.companyId,+ contactId: summary.contactId,+ createdById: userId,+ emailThreadId,+ meta: { synced: true, source: summary.origin },+ },+ update: {+ body: summary.snippet,+ occurredAt: summary.lastMessageAt,+ },+ select: { createdAt: true },+ });++ return activity.createdAt;+ }+}diff --git a/apps/api/src/microsoft/graph.client.ts b/apps/api/src/microsoft/graph.client.tsnew file mode 100644index 00000000..38a7a099--- /dev/null+++ b/apps/api/src/microsoft/graph.client.ts@@ -0,0 +1,102 @@+import { Injectable } from "@nestjs/common";+import {+ MailboxApiClient,+ type MailboxResult,+} from "../mailbox/mailbox-api.client";++const BASE = "https://graph.microsoft.com/v1.0/me";++const MESSAGE_FIELDS = [+ "id",+ "internetMessageId",+ "conversationId",+ "subject",+ "from",+ "sender",+ "toRecipients",+ "ccRecipients",+ "receivedDateTime",+ "sentDateTime",+ "body",+ "bodyPreview",+ "internetMessageHeaders",+ "parentFolderId",+ "webLink",+].join(",");++export type GraphAddress = {+ emailAddress?: { name?: string; address?: string };+};++export type GraphMessage = {+ id?: string;+ internetMessageId?: string;+ conversationId?: string;+ subject?: string | null;+ from?: GraphAddress;+ sender?: GraphAddress;+ toRecipients?: GraphAddress[];+ ccRecipients?: GraphAddress[];+ receivedDateTime?: string;+ sentDateTime?: string;+ body?: { contentType?: string; content?: string };+ bodyPreview?: string;+ internetMessageHeaders?: { name?: string; value?: string }[];+ parentFolderId?: string;+ webLink?: string;+};++export type MessagePage = {+ value?: GraphMessage[];+ "@odata.nextLink"?: string;+};++export type GraphUser = {+ mail?: string | null;+ userPrincipalName?: string | null;+};++export type GraphFolder = {+ id?: string;+};++@Injectable()+export class GraphClient {+ constructor(private readonly api: MailboxApiClient) {}++ async me(accessToken: string): Promise<MailboxResult<GraphUser>> {+ return this.api.get<GraphUser>(BASE, accessToken, {+ $select: "mail,userPrincipalName",+ });+ }++ async folder(+ accessToken: string,+ wellKnownName: string,+ ): Promise<MailboxResult<GraphFolder>> {+ return this.api.get<GraphFolder>(+ `${BASE}/mailFolders/${wellKnownName}`,+ accessToken,+ { $select: "id" },+ );+ }++ async listMessages(+ accessToken: string,+ options: { after: Date; top: number },+ ): Promise<MailboxResult<MessagePage>> {+ return this.api.get<MessagePage>(`${BASE}/messages`, accessToken, {+ $select: MESSAGE_FIELDS,+ $filter: `receivedDateTime gt ${options.after.toISOString()} and isDraft eq false`,+ $orderby: "receivedDateTime asc",+ $top: options.top,+ });+ }++ async nextPage(+ accessToken: string,+ nextLink: string,+ ): Promise<MailboxResult<MessagePage>> {+ return this.api.get<MessagePage>(nextLink, accessToken);+ }+}diff --git a/apps/api/src/microsoft/microsoft-connection.service.ts b/apps/api/src/microsoft/microsoft-connection.service.tsnew file mode 100644index 00000000..a531a86b--- /dev/null+++ b/apps/api/src/microsoft/microsoft-connection.service.ts@@ -0,0 +1,224 @@+import { isMicrosoftConfigured, signsInWithMicrosoft } from "@crm/auth";+import { type Db, GoogleSyncStatus, type Prisma } from "@crm/db";+import { Injectable, Logger, NotFoundException } from "@nestjs/common";+import { ActivityStampService } from "../crm/activity-stamp.service";+import { InjectDatabase } from "../database/database.constants";+import { MailboxTokenService } from "../mailbox/mailbox-token.service";+import { SyncStateService } from "../mailbox/sync-state.service";+import {+ MICROSOFT_PROVIDER_ID,+ MICROSOFT_SYNC_SOURCES,+ type MicrosoftSyncSource,+ SCOPE_FOR_SOURCE,+} from "./microsoft.constants";++const PURGE_TIMEOUT_MS = 60_000;++export type SourceStatus = {+ source: MicrosoftSyncSource;+ connected: boolean;+ status: GoogleSyncStatus | null;+ lastSyncedAt: string | null;+ lastError: string | null;+ autoCreate: boolean;+};++export type ConnectionStatus = {+ configured: boolean;+ linked: boolean;+ required: boolean;+ hasRefreshToken: boolean;+ sources: SourceStatus[];+};++@Injectable()+export class MicrosoftConnectionService {+ private readonly logger = new Logger(MicrosoftConnectionService.name);++ constructor(+ @InjectDatabase() private readonly db: Db,+ private readonly tokens: MailboxTokenService,+ private readonly state: SyncStateService,+ private readonly stamp: ActivityStampService,+ ) {}++ async status(userId: string): Promise<ConnectionStatus> {+ await this.onConnected(userId);++ const [granted, rows, hasRefreshToken, accounts] = await Promise.all([+ this.tokens.grantedScopes(userId, MICROSOFT_PROVIDER_ID),+ this.state.listForUser(userId, MICROSOFT_SYNC_SOURCES),+ this.tokens.hasRefreshToken(userId, MICROSOFT_PROVIDER_ID),+ this.tokens.signInAccounts(userId),+ ]);++ const bySource = new Map(rows.map((row) => [row.source, row]));++ const sources = MICROSOFT_SYNC_SOURCES.map((source): SourceStatus => {+ const row = bySource.get(source);++ return {+ source,+ connected: granted.has(SCOPE_FOR_SOURCE[source]),+ status: row?.status ?? null,+ lastSyncedAt: row?.lastSyncedAt?.toISOString() ?? null,+ lastError: row?.lastError ?? null,+ autoCreate: row?.autoCreate ?? false,+ };+ });++ return {+ configured: isMicrosoftConfigured(),+ linked:+ accounts.some(+ (account) => account.providerId === MICROSOFT_PROVIDER_ID,+ ) && sources.some((source) => source.connected),+ required: signsInWithMicrosoft(accounts),+ hasRefreshToken,+ sources,+ };+ }++ async onConnected(userId: string): Promise<void> {+ const [granted, existing] = await Promise.all([+ this.tokens.grantedScopes(userId, MICROSOFT_PROVIDER_ID),+ this.state.listForUser(userId, MICROSOFT_SYNC_SOURCES),+ ]);++ const known = new Set(existing.map((row) => row.source));++ const added: string[] = [];++ for (const source of MICROSOFT_SYNC_SOURCES) {+ if (!granted.has(SCOPE_FOR_SOURCE[source])) continue;+ if (known.has(source)) continue;++ await this.state.ensure(userId, source, { autoCreate: false });++ added.push(source);+ }++ if (added.length > 0) {+ this.logger.log({+ message: "Microsoft connected",+ userId,+ sources: added,+ });+ }+ }++ async reconcileAll(): Promise<void> {+ const accounts = await this.db.account.findMany({+ where: {+ providerId: MICROSOFT_PROVIDER_ID,+ OR: MICROSOFT_SYNC_SOURCES.map((source) => ({+ scope: { contains: SCOPE_FOR_SOURCE[source] },+ })),+ },+ select: { userId: true },+ });++ for (const userId of new Set(accounts.map((row) => row.userId))) {+ await this.onConnected(userId);+ }+ }++ async purgeSyncedData(userId: string): Promise<{ purged: number }> {+ const mine: Prisma.EmailMessageWhereInput = {+ syncedByUserId: userId,+ outlookMessageId: { not: null },+ };++ const purged = await this.db.$transaction(+ async (tx) => {+ const touched = await tx.emailMessage.findMany({+ where: mine,+ select: { threadId: true },+ distinct: ["threadId"],+ });++ const threadIds = touched.map((row) => row.threadId);+ const messages = await tx.emailMessage.deleteMany({ where: mine });++ await tx.emailThread.deleteMany({+ where: { id: { in: threadIds }, messages: { none: {} } },+ });++ await rebuildThreads(tx, threadIds);++ return messages.count;+ },+ { timeout: PURGE_TIMEOUT_MS },+ );++ await this.stamp.recomputeAll();++ this.logger.log({ message: "Outlook data purged", userId, purged });++ return { purged };+ }++ async revoke(userId: string): Promise<{ revoked: boolean }> {+ for (const source of MICROSOFT_SYNC_SOURCES) {+ await this.state.remove(userId, source);+ }++ const revoked = await this.tokens.revoke(userId, MICROSOFT_PROVIDER_ID);+ return { revoked };+ }++ async setAutoCreate(+ userId: string,+ source: MicrosoftSyncSource,+ enabled: boolean,+ ): Promise<void> {+ const row = await this.state.get(userId, source);+ if (!row) {+ throw new NotFoundException(`${source} is not connected.`);+ }++ await this.state.setAutoCreate(userId, source, enabled);+ }+}++async function rebuildThreads(+ tx: Prisma.TransactionClient,+ threadIds: string[],+): Promise<void> {+ if (threadIds.length === 0) return;++ const remaining = await tx.emailMessage.findMany({+ where: { threadId: { in: threadIds } },+ select: { threadId: true, sentAt: true, subject: true, snippet: true },+ orderBy: { sentAt: "asc" },+ });++ const byThread = new Map<string, typeof remaining>();++ for (const message of remaining) {+ const group = byThread.get(message.threadId);+ if (group) group.push(message);+ else byThread.set(message.threadId, [message]);+ }++ for (const [threadId, messages] of byThread) {+ const first = messages.at(0);+ const last = messages.at(-1);+ if (!first || !last) continue;++ await tx.emailThread.update({+ where: { id: threadId },+ data: {+ messageCount: messages.length,+ firstMessageAt: first.sentAt,+ lastMessageAt: last.sentAt,+ subject: first.subject,+ },+ });++ await tx.activity.updateMany({+ where: { emailThreadId: threadId },+ data: { body: last.snippet, occurredAt: last.sentAt },+ });+ }+}diff --git a/apps/api/src/microsoft/microsoft-sync.service.ts b/apps/api/src/microsoft/microsoft-sync.service.tsnew file mode 100644index 00000000..74beb40e--- /dev/null+++ b/apps/api/src/microsoft/microsoft-sync.service.ts@@ -0,0 +1,28 @@+import { Injectable } from "@nestjs/common";+import { SyncStateService } from "../mailbox/sync-state.service";+import {+ MICROSOFT_SYNC_SOURCES,+ type MicrosoftSyncSource,+} from "./microsoft.constants";+import { OutlookSyncService } from "./outlook-sync.service";++@Injectable()+export class MicrosoftSyncService {+ constructor(+ private readonly state: SyncStateService,+ private readonly outlook: OutlookSyncService,+ ) {}++ async runOne(userId: string, source: MicrosoftSyncSource) {+ const row = await this.state.get(userId, source);+ if (!row) return null;++ return this.outlook.sync(row);+ }++ async runForUser(userId: string): Promise<void> {+ for (const source of MICROSOFT_SYNC_SOURCES) {+ await this.runOne(userId, source);+ }+ }+}diff --git a/apps/api/src/microsoft/microsoft.constants.ts b/apps/api/src/microsoft/microsoft.constants.tsnew file mode 100644index 00000000..57ef2def--- /dev/null+++ b/apps/api/src/microsoft/microsoft.constants.ts@@ -0,0 +1,8 @@+export {+ MICROSOFT_PROVIDER_ID,+ MICROSOFT_SYNC_SCOPES,+ MICROSOFT_SYNC_SOURCES,+ type MicrosoftSyncSource,+ OUTLOOK_MAIL_SCOPE,+ SCOPE_FOR_SOURCE,+} from "../mailbox/mailbox.constants";diff --git a/apps/api/src/microsoft/microsoft.contracts.ts b/apps/api/src/microsoft/microsoft.contracts.tsnew file mode 100644index 00000000..75b74b93--- /dev/null+++ b/apps/api/src/microsoft/microsoft.contracts.ts@@ -0,0 +1,11 @@+import { z } from "zod";+import { MICROSOFT_SYNC_SOURCES } from "./microsoft.constants";++export const setOutlookAutoCreateInput = z.object({+ source: z.enum(MICROSOFT_SYNC_SOURCES),+ enabled: z.boolean(),+});++export type SetOutlookAutoCreateInput = z.infer<+ typeof setOutlookAutoCreateInput+>;diff --git a/apps/api/src/microsoft/microsoft.module.ts b/apps/api/src/microsoft/microsoft.module.tsnew file mode 100644index 00000000..1c28062f--- /dev/null+++ b/apps/api/src/microsoft/microsoft.module.ts@@ -0,0 +1,21 @@+import { Module } from "@nestjs/common";+import { MailboxModule } from "../mailbox/mailbox.module";+import { TrpcModule } from "../trpc/trpc.module";+import { GraphClient } from "./graph.client";+import { MicrosoftRouter } from "./microsoft.router";+import { MicrosoftConnectionService } from "./microsoft-connection.service";+import { MicrosoftSyncService } from "./microsoft-sync.service";+import { OutlookSyncService } from "./outlook-sync.service";++@Module({+ imports: [TrpcModule, MailboxModule],+ providers: [+ GraphClient,+ OutlookSyncService,+ MicrosoftSyncService,+ MicrosoftConnectionService,+ MicrosoftRouter,+ ],+ exports: [MicrosoftSyncService, MicrosoftConnectionService],+})+export class MicrosoftModule {}diff --git a/apps/api/src/microsoft/microsoft.router.ts b/apps/api/src/microsoft/microsoft.router.tsnew file mode 100644index 00000000..f8da1ca8--- /dev/null+++ b/apps/api/src/microsoft/microsoft.router.ts@@ -0,0 +1,60 @@+import { Inject } from "@nestjs/common";+import {+ Ctx,+ Input,+ Mutation,+ Query,+ Router,+ UseMiddlewares,+} from "nestjs-trpc";+import type { z } from "zod";+import type { AuthedTrpcContext } from "../trpc/context.types";+import { AuthMiddleware } from "../trpc/middlewares/auth.middleware";+import { setOutlookAutoCreateInput } from "./microsoft.contracts";+import { MicrosoftConnectionService } from "./microsoft-connection.service";+import { MicrosoftSyncService } from "./microsoft-sync.service";++@Router({ alias: "microsoft" })+@UseMiddlewares(AuthMiddleware)+export class MicrosoftRouter {+ constructor(+ @Inject(MicrosoftConnectionService)+ private readonly connection: MicrosoftConnectionService,+ @Inject(MicrosoftSyncService)+ private readonly sync: MicrosoftSyncService,+ ) {}++ @Query()+ async status(@Ctx() ctx: AuthedTrpcContext) {+ return this.connection.status(ctx.user.id);+ }++ @Mutation()+ async purgeSyncedData(@Ctx() ctx: AuthedTrpcContext) {+ return this.connection.purgeSyncedData(ctx.user.id);+ }++ @Mutation()+ async revokeAccess(@Ctx() ctx: AuthedTrpcContext) {+ return this.connection.revoke(ctx.user.id);+ }++ @Mutation()+ async syncNow(@Ctx() ctx: AuthedTrpcContext) {+ await this.sync.runForUser(ctx.user.id);+ return this.connection.status(ctx.user.id);+ }++ @Mutation({ input: setOutlookAutoCreateInput })+ async setAutoCreate(+ @Ctx() ctx: AuthedTrpcContext,+ @Input() input: z.infer<typeof setOutlookAutoCreateInput>,+ ) {+ await this.connection.setAutoCreate(+ ctx.user.id,+ input.source,+ input.enabled,+ );+ return this.connection.status(ctx.user.id);+ }+}diff --git a/apps/api/src/microsoft/outlook-sync.service.ts b/apps/api/src/microsoft/outlook-sync.service.tsnew file mode 100644index 00000000..50d3d006--- /dev/null+++ b/apps/api/src/microsoft/outlook-sync.service.ts@@ -0,0 +1,394 @@+import {+ GoogleSyncStatus,+ type MailboxSyncModel as MailboxSync,+} from "@crm/db";+import { Injectable, Logger } from "@nestjs/common";+import type { MailboxResult } from "../mailbox/mailbox-api.client";+import type { MatchContext } from "../mailbox/mailbox-match.service";+import { MailboxTokenService } from "../mailbox/mailbox-token.service";+import {+ normaliseMessageId,+ rootMessageIdFrom,+ stripHtml,+ stripQuotedHistory,+} from "../mailbox/message-text";+import { type Participant, parseAddress } from "../mailbox/participants";+import { SyncStateService } from "../mailbox/sync-state.service";+import {+ type IncomingMessage,+ ThreadWriterService,+} from "../mailbox/thread-writer.service";+import {+ type GraphAddress,+ GraphClient,+ type GraphFolder,+ type GraphMessage,+} from "./graph.client";++const MAX_MESSAGES_PER_TICK = 120;+const PAGE_SIZE = 50;++const OVERLAP_MS = 1_000;++const EXCLUDED_FOLDERS = ["junkemail", "deleteditems"] as const;++const CONVERSATION_ROOT_PREFIX = "outlook-conversation:";++type MailboxFailure<T> = Exclude<MailboxResult<T>, { outcome: "ok" }>;++type ExcludedFolders =+ | { outcome: "ok"; ids: Set<string> }+ | { outcome: "lookup-failed"; failure: MailboxFailure<GraphFolder> };++export type OutlookSyncOutcome = {+ source: "outlook";+ userId: string;+ status: "synced" | "skipped" | "reconnect" | "rate-limited" | "failed";+ messagesWritten?: number;+ reason?: string;+};++@Injectable()+export class OutlookSyncService {+ private readonly logger = new Logger(OutlookSyncService.name);++ constructor(+ private readonly graph: GraphClient,+ private readonly tokens: MailboxTokenService,+ private readonly state: SyncStateService,+ private readonly threads: ThreadWriterService,+ ) {}++ async sync(row: MailboxSync): Promise<OutlookSyncOutcome> {+ const initializedAt = new Date();++ const token = await this.tokens.accessTokenFor(row.userId, "outlook");++ if (token.outcome === "not-connected") {+ return {+ source: "outlook",+ userId: row.userId,+ status: "skipped",+ reason: token.reason,+ };+ }++ if (token.outcome === "needs-reconnect") {+ await this.state.markNeedsReconnect(row.id, token.reason);+ return {+ source: "outlook",+ userId: row.userId,+ status: "reconnect",+ reason: token.reason,+ };+ }++ await this.state.markRunning(row.id);++ const me = await this.graph.me(token.accessToken);+ if (me.outcome !== "ok") {+ return this.handleFailure(row, me);+ }++ const mailbox = (+ me.data.mail ??+ me.data.userPrincipalName ??+ ""+ ).toLowerCase();++ if (!mailbox) {+ await this.state.markFailed(+ row.id,+ "Microsoft returned no mailbox address.",+ );+ return {+ source: "outlook",+ userId: row.userId,+ status: "failed",+ reason: "No mailbox address.",+ };+ }++ if (!row.cursor) {+ return this.start(row, initializedAt);+ }++ return this.incremental(row, token.accessToken, mailbox, row.cursor);+ }++ private async start(+ row: MailboxSync,+ initializedAt: Date,+ ): Promise<OutlookSyncOutcome> {+ await this.state.settle(row.id, {+ cursor: initializedAt.toISOString(),+ status: GoogleSyncStatus.RUNNING,+ });++ this.logger.log({+ message: "Outlook sync started — watching for new mail",+ userId: row.userId,+ });++ return { source: "outlook", userId: row.userId, status: "synced" };+ }++ private async incremental(+ row: MailboxSync,+ accessToken: string,+ mailbox: string,+ cursor: string,+ ): Promise<OutlookSyncOutcome> {+ const from = new Date(cursor);+ if (Number.isNaN(from.getTime())) {+ await this.state.clearCursor(row.id, "The stored cursor was not a date.");+ return {+ source: "outlook",+ userId: row.userId,+ status: "synced",+ reason: "Cursor reset; resuming from now.",+ };+ }++ const folders = await this.excludedFolderIds(accessToken);+ if (folders.outcome !== "ok") {+ return this.handleFailure(row, folders.failure);+ }++ const excluded = folders.ids;++ let page = await this.graph.listMessages(accessToken, {+ after: new Date(from.getTime() - OVERLAP_MS),+ top: PAGE_SIZE,+ });++ let context: MatchContext | null = null;+ let written = 0;+ let seen = 0;+ let furthest = from;++ while (page.outcome === "ok") {+ const remaining = MAX_MESSAGES_PER_TICK - seen;+ const messages = (page.data.value ?? []).slice(0, Math.max(remaining, 0));++ for (const message of messages) {+ seen += 1;++ const receivedAt = message.receivedDateTime+ ? new Date(message.receivedDateTime)+ : null;+ if (receivedAt && !Number.isNaN(receivedAt.getTime())) {+ if (receivedAt > furthest) furthest = receivedAt;+ }++ if (message.parentFolderId && excluded.has(message.parentFolderId)) {+ continue;+ }++ const parsed = this.parse(message);+ if (!parsed) continue;++ context ??= await this.threads.context();++ const stored = await this.threads.store(+ row,+ { mailbox, origin: "outlook" },+ parsed,+ context,+ );+ if (stored) written += 1;+ }++ const nextLink = page.data["@odata.nextLink"];+ if (!nextLink || seen >= MAX_MESSAGES_PER_TICK) break;++ page = await this.graph.nextPage(accessToken, nextLink);+ }++ if (page.outcome !== "ok") {+ return this.handleFailure(row, page);+ }++ await this.state.settle(row.id, {+ cursor: furthest.toISOString(),+ status: GoogleSyncStatus.RUNNING,+ });++ if (written > 0) {+ this.logger.log({+ message: "Outlook incremental sync",+ userId: row.userId,+ messagesWritten: written,+ messagesSeen: seen,+ });+ }++ return {+ source: "outlook",+ userId: row.userId,+ status: "synced",+ messagesWritten: written,+ };+ }++ private async excludedFolderIds(+ accessToken: string,+ ): Promise<ExcludedFolders> {+ const ids = new Set<string>();++ for (const name of EXCLUDED_FOLDERS) {+ const folder = await this.graph.folder(accessToken, name);++ if (folder.outcome === "ok") {+ if (folder.data.id) ids.add(folder.data.id);+ continue;+ }++ if (isMissingFolder(folder)) continue;++ return { outcome: "lookup-failed", failure: folder };+ }++ return { outcome: "ok", ids };+ }++ private parse(message: GraphMessage): IncomingMessage | null {+ const internetMessageId = message.internetMessageId?.trim();+ if (!internetMessageId) return null;++ const from = addressOf(message.from ?? message.sender);+ if (!from) return null;++ const sentAt = this.sentAt(message);+ if (!sentAt) return null;++ const rootId = this.rootIdOf(message, internetMessageId);++ const to = addressList(message.toRecipients, "to");+ const cc = addressList(message.ccRecipients, "cc");++ const raw = message.body?.content ?? message.bodyPreview ?? "";+ const text =+ message.body?.contentType?.toLowerCase() === "html"+ ? stripHtml(raw)+ : raw;++ return {+ rfcMessageId: normaliseMessageId(internetMessageId),+ rootId,+ subject: message.subject?.trim() || null,+ from,+ recipients: [...to, ...cc],+ body: stripQuotedHistory(text),+ sentAt,+ outlookMessageId: message.id ?? null,+ outlookWebLink: message.webLink ?? null,+ };+ }++ private rootIdOf(message: GraphMessage, internetMessageId: string): string {+ const headers = message.internetMessageHeaders ?? [];++ const value = (name: string): string | null => {+ const wanted = name.toLowerCase();+ const found = headers.find(+ (entry) => entry.name?.toLowerCase() === wanted,+ );+ return found?.value?.trim() || null;+ };++ const references = value("references");+ const inReplyTo = value("in-reply-to");++ if (references || inReplyTo) {+ const root = rootMessageIdFrom({+ references,+ inReplyTo,+ messageId: internetMessageId,+ });++ if (root) return root;+ }++ if (message.conversationId) {+ return `${CONVERSATION_ROOT_PREFIX}${message.conversationId}`;+ }++ return normaliseMessageId(internetMessageId);+ }++ private sentAt(message: GraphMessage): Date | null {+ for (const raw of [message.sentDateTime, message.receivedDateTime]) {+ if (!raw) continue;+ const at = new Date(raw);+ if (!Number.isNaN(at.getTime())) return at;+ }++ return null;+ }++ private async handleFailure(+ row: MailboxSync,+ result: { outcome: string; reason: string; retryAfterMs?: number },+ ): Promise<OutlookSyncOutcome> {+ if (result.outcome === "unauthorized") {+ await this.state.markNeedsReconnect(row.id, result.reason);+ return {+ source: "outlook",+ userId: row.userId,+ status: "reconnect",+ reason: result.reason,+ };+ }++ if (result.outcome === "rate-limited") {+ await this.state.markRateLimited(row.id, result.retryAfterMs ?? 60_000);+ return {+ source: "outlook",+ userId: row.userId,+ status: "rate-limited",+ reason: result.reason,+ };+ }++ await this.state.markFailed(row.id, result.reason);+ return {+ source: "outlook",+ userId: row.userId,+ status: "failed",+ reason: result.reason,+ };+ }+}++function isMissingFolder(failure: MailboxFailure<GraphFolder>): boolean {+ return failure.outcome === "cursor-invalid";+}++function addressOf(entry: GraphAddress | undefined): Participant | null {+ const address = entry?.emailAddress?.address?.trim();+ if (!address) return null;++ const name = entry?.emailAddress?.name?.trim();++ return parseAddress(name ? `${name} <${address}>` : address) ?? null;+}++function addressList(+ entries: GraphAddress[] | undefined,+ kind: "to" | "cc",+): { email: string; name: string | null; kind: "to" | "cc" }[] {+ const seen = new Set<string>();+ const people: { email: string; name: string | null; kind: "to" | "cc" }[] =+ [];++ for (const entry of entries ?? []) {+ const person = addressOf(entry);+ if (!person || seen.has(person.email)) continue;++ seen.add(person.email);+ people.push({ email: person.email, name: person.name, kind });+ }++ return people;+}diff --git a/apps/api/src/sso/sso.service.ts b/apps/api/src/sso/sso.service.tsindex b0783bfc..1cfcd6d7 100644--- a/apps/api/src/sso/sso.service.ts+++ b/apps/api/src/sso/sso.service.ts@@ -2,6 +2,7 @@ import {auth,canConfigureSso,isGoogleConfigured,+ isMicrosoftConfigured,isWorkspaceRole,ssoCallbackBase,ssoCallbackURL,@@ -34,6 +35,7 @@ export interface PublicSsoProvider {export interface SignInOptions {google: boolean;+ microsoft: boolean;providers: PublicSsoProvider[];}@@ -143,6 +145,7 @@ export class SsoService {return {google: isGoogleConfigured(),+ microsoft: isMicrosoftConfigured(),providers: rows.map((row) => ({providerId: row.providerId,name: ssoProviderName(row.providerId),diff --git a/apps/api/src/sync/mailbox-sync.service.ts b/apps/api/src/sync/mailbox-sync.service.tsnew file mode 100644index 00000000..3eac76b2--- /dev/null+++ b/apps/api/src/sync/mailbox-sync.service.ts@@ -0,0 +1,124 @@+import { syncError } from "@crm/telemetry";+import { Injectable, Logger } from "@nestjs/common";+import { GoogleConnectionService } from "../google/google-connection.service";+import { GoogleSyncService } from "../google/google-sync.service";+import {+ isGoogleSyncSource,+ isMicrosoftSyncSource,+} from "../mailbox/mailbox.constants";+import { SyncStateService } from "../mailbox/sync-state.service";+import { MicrosoftConnectionService } from "../microsoft/microsoft-connection.service";+import { MicrosoftSyncService } from "../microsoft/microsoft-sync.service";++const TICK_BUDGET_MS = 60_000;++export type TickSummary = {+ attempted: number;+ synced: number;+ skipped: number;+ rateLimited: number;+ failed: number;+ durationMs: number;+};++@Injectable()+export class MailboxSyncService {+ private readonly logger = new Logger(MailboxSyncService.name);++ constructor(+ private readonly state: SyncStateService,+ private readonly google: GoogleSyncService,+ private readonly microsoft: MicrosoftSyncService,+ private readonly googleConnections: GoogleConnectionService,+ private readonly microsoftConnections: MicrosoftConnectionService,+ ) {}++ async runDue(): Promise<TickSummary> {+ const startedAt = Date.now();+ const summary: TickSummary = {+ attempted: 0,+ synced: 0,+ skipped: 0,+ rateLimited: 0,+ failed: 0,+ durationMs: 0,+ };++ await this.googleConnections.reconcileAll();+ await this.microsoftConnections.reconcileAll();++ const due = await this.state.due(new Date());++ for (const [index, row] of due.entries()) {+ if (Date.now() - startedAt > TICK_BUDGET_MS) {+ this.logger.log({+ message: "Sync tick budget reached",+ remaining: due.length - index,+ });+ break;+ }++ if (!(await this.state.claim(row, new Date()))) continue;++ summary.attempted += 1;++ try {+ const outcome = await this.runOne(row.userId, row.source);++ if (outcome === null || outcome.status === "skipped") {+ summary.skipped += 1;+ await this.state.release(row.id);+ } else if (outcome.status === "rate-limited") {+ summary.rateLimited += 1;+ } else if (+ outcome.status === "failed" ||+ outcome.status === "reconnect"+ ) {+ summary.failed += 1;+ } else {+ summary.synced += 1;+ }+ } catch (error) {+ summary.failed += 1;+ await this.state.markFailed(+ row.id,+ error instanceof Error ? error.message : String(error),+ );+ this.logger.error(+ {+ message: "Sync threw",+ userId: row.userId,+ source: row.source,+ },+ error instanceof Error ? error.stack : String(error),+ );++ syncError({ error, source: row.source });+ }+ }++ summary.durationMs = Date.now() - startedAt;++ this.logger.log({+ message: "Mailbox sync tick",+ attempted: summary.attempted,+ synced: summary.synced,+ skipped: summary.skipped,+ rateLimited: summary.rateLimited,+ failed: summary.failed,+ durationMs: summary.durationMs,+ });++ return summary;+ }++ private async runOne(userId: string, source: string) {+ if (isGoogleSyncSource(source)) return this.google.runOne(userId, source);++ if (isMicrosoftSyncSource(source)) {+ return this.microsoft.runOne(userId, source);+ }++ return null;+ }+}diff --git a/apps/api/src/google/sync.controller.ts b/apps/api/src/sync/sync.controller.tssimilarity index 73%rename from apps/api/src/google/sync.controller.tsrename to apps/api/src/sync/sync.controller.tsindex d092183d..9c819181 100644--- a/apps/api/src/google/sync.controller.ts+++ b/apps/api/src/sync/sync.controller.ts@@ -10,7 +10,7 @@ import {import { ConfigService } from "@nestjs/config";import { AllowAnonymous } from "@thallesp/nestjs-better-auth";import type { EnvironmentVariables } from "../config/env.validation";-import { GoogleSyncService } from "./google-sync.service";+import { MailboxSyncService } from "./mailbox-sync.service";@Controller("internal/sync")export class SyncController {@@ -18,25 +18,37 @@ export class SyncController {private readonly secret: string | undefined;constructor(- private readonly sync: GoogleSyncService,+ private readonly sync: MailboxSyncService,config: ConfigService<EnvironmentVariables, true>,) {this.secret = config.get("CRON_SECRET", { infer: true });}+ @Get("mailboxes")+ @AllowAnonymous()+ async mailboxesViaGet(@Headers("authorization") authorization?: string) {+ return this.run(authorization);+ }++ @Post("mailboxes")+ @AllowAnonymous()+ async mailboxesViaPost(@Headers("authorization") authorization?: string) {+ return this.run(authorization);+ }+@Get("google")@AllowAnonymous()async googleViaGet(@Headers("authorization") authorization?: string) {- return this.google(authorization);+ return this.run(authorization);}@Post("google")@AllowAnonymous()async googleViaPost(@Headers("authorization") authorization?: string) {- return this.google(authorization);+ return this.run(authorization);}- private async google(authorization?: string) {+ private async run(authorization?: string) {if (!this.secret) {this.logger.error({message: "CRON_SECRET is not set — refusing to run the sync route.",diff --git a/apps/api/src/sync/sync.module.ts b/apps/api/src/sync/sync.module.tsnew file mode 100644index 00000000..db76e905--- /dev/null+++ b/apps/api/src/sync/sync.module.ts@@ -0,0 +1,14 @@+import { Module } from "@nestjs/common";+import { GoogleModule } from "../google/google.module";+import { MailboxModule } from "../mailbox/mailbox.module";+import { MicrosoftModule } from "../microsoft/microsoft.module";+import { MailboxSyncService } from "./mailbox-sync.service";+import { SyncController } from "./sync.controller";++@Module({+ imports: [MailboxModule, GoogleModule, MicrosoftModule],+ controllers: [SyncController],+ providers: [MailboxSyncService],+ exports: [MailboxSyncService],+})+export class SyncModule {}diff --git a/apps/api/test/auth.e2e.spec.ts b/apps/api/test/auth.e2e.spec.tsindex 54c11f99..d5d79131 100644--- a/apps/api/test/auth.e2e.spec.ts+++ b/apps/api/test/auth.e2e.spec.ts@@ -60,7 +60,15 @@ describe("Auth (e2e)", () => {.get("/api/trpc/sso.signInOptions").expect(200);- expect(response.body.result.data).toEqual({ google: true, providers: [] });+ const microsoftConfigured = Boolean(+ process.env.MICROSOFT_CLIENT_ID && process.env.MICROSOFT_CLIENT_SECRET,+ );++ expect(response.body.result.data).toEqual({+ google: true,+ microsoft: microsoftConfigured,+ providers: [],+ });});it("keeps the SSO configuration itself behind the session", async () => {diff --git a/apps/api/test/google-scopes.spec.ts b/apps/api/test/google-scopes.spec.tsdeleted file mode 100644index 8ad1375b..00000000--- a/apps/api/test/google-scopes.spec.ts+++ /dev/null@@ -1,55 +0,0 @@-import { describe, expect, it } from "bun:test";-import {- CALENDAR_SCOPE,- GMAIL_SCOPE,- hasSyncScopes,- parseScopes,- SYNC_SCOPES,-} from "@crm/auth/scopes";--const BOTH = `openid,email,profile,${GMAIL_SCOPE},${CALENDAR_SCOPE}`;--describe("parseScopes", () => {- it("handles the comma form Better Auth stores", () => {- expect(parseScopes("a,b,c")).toEqual(new Set(["a", "b", "c"]));- });-- it("handles the space form Google returns", () => {- expect(parseScopes("a b c")).toEqual(new Set(["a", "b", "c"]));- });-- it("is empty for null, undefined and blank", () => {- for (const value of [null, undefined, "", " "]) {- expect(parseScopes(value).size).toBe(0);- }- });-});--describe("hasSyncScopes", () => {- it("is true only when both scopes are present", () => {- expect(hasSyncScopes(BOTH)).toBe(true);- });-- it("is false when granular consent dropped one of them", () => {- expect(hasSyncScopes(`openid,email,profile,${GMAIL_SCOPE}`)).toBe(false);- expect(hasSyncScopes(`openid,email,profile,${CALENDAR_SCOPE}`)).toBe(false);- });-- it("is false for an account that predates the requirement", () => {- expect(hasSyncScopes("openid,email,profile")).toBe(false);- });-- it("is false when the grant was revoked and the column cleared", () => {- expect(hasSyncScopes(null)).toBe(false);- });-- it("does not match on a prefix", () => {- expect(hasSyncScopes(`${GMAIL_SCOPE}.metadata,${CALENDAR_SCOPE}`)).toBe(- false,- );- });-- it("covers exactly the scopes the provider requests", () => {- expect(hasSyncScopes(SYNC_SCOPES.join(","))).toBe(true);- });-});diff --git a/apps/api/test/google-api-client.spec.ts b/apps/api/test/mailbox-api-client.spec.tssimilarity index 95%rename from apps/api/test/google-api-client.spec.tsrename to apps/api/test/mailbox-api-client.spec.tsindex 917b7ad3..89315ae9 100644--- a/apps/api/test/google-api-client.spec.ts+++ b/apps/api/test/mailbox-api-client.spec.ts@@ -1,5 +1,5 @@import { afterEach, describe, expect, it } from "bun:test";-import { GoogleApiClient } from "../src/google/google-api.client";+import { MailboxApiClient } from "../src/mailbox/mailbox-api.client";const realFetch = globalThis.fetch;@@ -19,10 +19,10 @@ function stub(})) as unknown as typeof fetch;}-const client = new GoogleApiClient();+const client = new MailboxApiClient();const call = () => client.get<unknown>("https://example.test/x", "token");-describe("GoogleApiClient", () => {+describe("MailboxApiClient", () => {it("returns the payload on success", async () => {stub(200, { ok: true });expect(await call()).toEqual({ outcome: "ok", data: { ok: true } });diff --git a/apps/api/test/google-mime.spec.ts b/apps/api/test/mailbox-message-text.spec.tssimilarity index 56%rename from apps/api/test/google-mime.spec.tsrename to apps/api/test/mailbox-message-text.spec.tsindex b56cf52c..55037496 100644--- a/apps/api/test/google-mime.spec.ts+++ b/apps/api/test/mailbox-message-text.spec.ts@@ -1,15 +1,19 @@import { describe, expect, it } from "bun:test";import {- decodeBase64Url,type GmailPart,header,- normaliseMessageId,plainTextBody,rootMessageId,+} from "../src/google/gmail-mime";+import {+ decodeBase64Url,+ firstMessageId,+ normaliseMessageId,+ rootMessageIdFrom,snippetOf,stripHtml,stripQuotedHistory,-} from "../src/google/mime";+} from "../src/mailbox/message-text";const encode = (text: string): string =>Buffer.from(text, "utf8")@@ -91,6 +95,70 @@ describe("plainTextBody", () => {expect(plainTextBody(payload)).toBe("body text");});+ it("does not read the body out of a forwarded-email attachment", () => {+ const payload: GmailPart = {+ mimeType: "multipart/mixed",+ parts: [+ {+ mimeType: "message/rfc822",+ filename: "Fwd Pricing.eml",+ parts: [+ {+ mimeType: "text/plain",+ body: { data: encode("the forwarded original") },+ },+ ],+ },+ { mimeType: "text/plain", body: { data: encode("See attached.") } },+ ],+ };++ expect(plainTextBody(payload)).toBe("See attached.");+ });++ it("does not read the body out of an attachment declared by disposition", () => {+ const payload: GmailPart = {+ mimeType: "multipart/mixed",+ parts: [+ {+ mimeType: "multipart/alternative",+ headers: [+ { name: "Content-Disposition", value: 'attachment; filename=""' },+ ],+ parts: [+ {+ mimeType: "text/plain",+ body: { data: encode("inside the attachment") },+ },+ ],+ },+ {+ mimeType: "text/html",+ body: { data: encode("<p>The real body</p>") },+ },+ ],+ };++ expect(plainTextBody(payload)).toBe("The real body");+ });++ it("is empty when every candidate is inside an attachment", () => {+ const payload: GmailPart = {+ mimeType: "multipart/mixed",+ parts: [+ {+ mimeType: "message/rfc822",+ filename: "Fwd Pricing.eml",+ parts: [+ { mimeType: "text/plain", body: { data: encode("forwarded") } },+ ],+ },+ ],+ };++ expect(plainTextBody(payload)).toBe("");+ });+it("is empty rather than undefined for an empty payload", () => {expect(plainTextBody(undefined)).toBe("");});@@ -187,6 +255,105 @@ describe("rootMessageId", () => {});});+describe("rootMessageIdFrom", () => {+ it("gives a Gmail copy and an Outlook copy of one thread the same root", () => {+ const viaGmailHeaders = rootMessageId([+ { name: "References", value: "<[email protected]> <[email protected]>" },+ { name: "Message-ID", value: "<[email protected]>" },+ ]);++ const viaGraphFields = rootMessageIdFrom({+ references: "<[email protected]> <[email protected]>",+ inReplyTo: "<[email protected]>",+ messageId: "<[email protected]>",+ });++ expect(viaGraphFields).toBe("[email protected]");+ expect(viaGraphFields).toBe(viaGmailHeaders);+ });++ it("falls back to its own id when Graph returned no headers at all", () => {+ expect(+ rootMessageIdFrom({+ references: null,+ inReplyTo: null,+ messageId: "<[email protected]>",+ }),+ ).toBe("[email protected]");+ });++ it("is null when there is nothing to thread on", () => {+ expect(+ rootMessageIdFrom({ references: null, inReplyTo: null, messageId: null }),+ ).toBeNull();+ });++ it("takes the first id when In-Reply-To carries a list", () => {+ expect(+ rootMessageIdFrom({+ references: null,+ inReplyTo: "<[email protected]> <[email protected]>",+ messageId: "<[email protected]>",+ }),+ ).toBe("[email protected]");+ });++ it("keeps a multi-id In-Reply-To on the same thread as the single-id copy", () => {+ const listed = rootMessageIdFrom({+ references: null,+ inReplyTo: "<[email protected]>\r\n\t<[email protected]>",+ messageId: "<[email protected]>",+ });+ const single = rootMessageIdFrom({+ references: null,+ inReplyTo: "<[email protected]>",+ messageId: "<[email protected]>",+ });++ expect(listed).toBe("[email protected]");+ expect(listed).toBe(single);+ });++ it("accepts a bracketless id in either header", () => {+ expect(+ rootMessageIdFrom({+ references: "[email protected] [email protected]",+ inReplyTo: null,+ messageId: "<[email protected]>",+ }),+ ).toBe("[email protected]");++ expect(+ rootMessageIdFrom({+ references: null,+ inReplyTo: "[email protected]",+ messageId: "<[email protected]>",+ }),+ ).toBe("[email protected]");+ });++ it("ignores a blank References and threads on In-Reply-To", () => {+ expect(+ rootMessageIdFrom({+ references: " ",+ inReplyTo: "<[email protected]>",+ messageId: "<[email protected]>",+ }),+ ).toBe("[email protected]");+ });+});++describe("firstMessageId", () => {+ it("normalises a single bracketed id", () => {+ expect(firstMessageId("<[email protected]>")).toBe("[email protected]");+ });++ it("returns null when there is no id at all", () => {+ expect(firstMessageId(" ")).toBeNull();+ expect(firstMessageId("<>")).toBeNull();+ });+});+describe("normaliseMessageId", () => {it("makes bracketed and bare ids the same identity", () => {expect(normaliseMessageId("<[email protected]>")).toBe(diff --git a/apps/api/test/google-participants.spec.ts b/apps/api/test/mailbox-participants.spec.tssimilarity index 99%rename from apps/api/test/google-participants.spec.tsrename to apps/api/test/mailbox-participants.spec.tsindex a201e178..2f3436a1 100644--- a/apps/api/test/google-participants.spec.ts+++ b/apps/api/test/mailbox-participants.spec.ts@@ -10,7 +10,7 @@ import {parseAddressList,splitName,workDomain,-} from "../src/google/participants";+} from "../src/mailbox/participants";const person = (email: string, name: string | null = null): Participant => ({email,diff --git a/apps/api/test/mailbox-purge.spec.ts b/apps/api/test/mailbox-purge.spec.tsnew file mode 100644index 00000000..7545fa6d--- /dev/null+++ b/apps/api/test/mailbox-purge.spec.ts@@ -0,0 +1,440 @@+import { afterAll, beforeEach, describe, expect, it } from "bun:test";+import { ActivityType, db, EmailDirection, GoogleSyncStatus } from "@crm/db";+import { ActivityStampService } from "../src/crm/activity-stamp.service";+import { GoogleConnectionService } from "../src/google/google-connection.service";+import {+ GOOGLE_PROVIDER_ID,+ MICROSOFT_PROVIDER_ID,+ OUTLOOK_MAIL_SCOPE,+ SYNC_SCOPES,+} from "../src/mailbox/mailbox.constants";+import type { MailboxMatchService } from "../src/mailbox/mailbox-match.service";+import { MailboxTokenService } from "../src/mailbox/mailbox-token.service";+import { SyncStateService } from "../src/mailbox/sync-state.service";+import { MicrosoftConnectionService } from "../src/microsoft/microsoft-connection.service";++const suffix = process.env.TEST_RUN_ID ?? "mailbox-purge-spec";+const domain = `purge-${suffix}.test`;++const gmailRep = `gmail-rep-${suffix}`;+const outlookRep = `outlook-rep-${suffix}`;+const otherRep = `other-rep-${suffix}`;+const userIds = [gmailRep, outlookRep, otherRep];++const shared = `shared-${suffix}`;+const solo = `solo-${suffix}`;+const theirs = `theirs-${suffix}`;+const roots = [shared, solo, theirs];++const tokens = new MailboxTokenService(db);+const state = new SyncStateService(db);+const stamp = new ActivityStampService(db);++const google = new GoogleConnectionService(+ db,+ tokens,+ state,+ {} as unknown as MailboxMatchService,+ stamp,+);+const microsoft = new MicrosoftConnectionService(db, tokens, state, stamp);++function at(hour: number): Date {+ return new Date(Date.UTC(2026, 0, 1, hour));+}++type Wire = {+ id: string;+ syncedByUserId: string;+ provider: "gmail" | "outlook";+ sentAt: Date;+ subject: string;+ snippet: string;+};++async function thread(+ rootMessageId: string,+ companyId: string,+ messages: Wire[],+): Promise<void> {+ const first = messages[0];+ const last = messages[messages.length - 1];+ if (!first || !last) return;++ await db.emailThread.create({+ data: {+ rootMessageId,+ subject: first.subject,+ companyId,+ firstMessageAt: first.sentAt,+ lastMessageAt: last.sentAt,+ messageCount: messages.length,+ messages: {+ create: messages.map((message) => ({+ rfcMessageId: `${message.id}@${domain}`,+ syncedByUserId: message.syncedByUserId,+ gmailMessageId: message.provider === "gmail" ? message.id : null,+ outlookMessageId: message.provider === "outlook" ? message.id : null,+ direction: EmailDirection.INBOUND,+ fromEmail: `them@${domain}`,+ recipients: [],+ subject: message.subject,+ snippet: message.snippet,+ sentAt: message.sentAt,+ })),+ },+ activity: {+ create: {+ type: ActivityType.EMAIL,+ subject: first.subject,+ body: last.snippet,+ occurredAt: last.sentAt,+ companyId,+ createdById: first.syncedByUserId,+ meta: { synced: true },+ },+ },+ },+ });+}++async function seed(): Promise<void> {+ const company = await db.company.create({+ data: { name: "Purge Co", domain },+ select: { id: true },+ });++ await thread(shared, company.id, [+ {+ id: `m1-${suffix}`,+ syncedByUserId: gmailRep,+ provider: "gmail",+ sentAt: at(9),+ subject: "Kickoff",+ snippet: "first",+ },+ {+ id: `m2-${suffix}`,+ syncedByUserId: outlookRep,+ provider: "outlook",+ sentAt: at(10),+ subject: "Re: Kickoff",+ snippet: "second",+ },+ {+ id: `m3-${suffix}`,+ syncedByUserId: otherRep,+ provider: "gmail",+ sentAt: at(11),+ subject: "Re: Kickoff",+ snippet: "third",+ },+ ]);++ await thread(solo, company.id, [+ {+ id: `m4-${suffix}`,+ syncedByUserId: gmailRep,+ provider: "gmail",+ sentAt: at(8),+ subject: "Only mine",+ snippet: "alone",+ },+ ]);++ await thread(theirs, company.id, [+ {+ id: `m5-${suffix}`,+ syncedByUserId: outlookRep,+ provider: "outlook",+ sentAt: at(7),+ subject: "Not mine",+ snippet: "untouched",+ },+ ]);++ await db.calendarEvent.create({+ data: {+ iCalUid: `ical-${suffix}`,+ originalStartTime: at(12),+ startsAt: at(12),+ endsAt: at(13),+ status: "confirmed",+ companyId: company.id,+ syncedByUserId: gmailRep,+ },+ });+}++async function clean(): Promise<void> {+ await db.calendarEvent.deleteMany({+ where: { syncedByUserId: { in: userIds } },+ });+ await db.emailThread.deleteMany({ where: { rootMessageId: { in: roots } } });+ await db.mailboxSync.deleteMany({ where: { userId: { in: userIds } } });+ await db.account.deleteMany({ where: { userId: { in: userIds } } });+ await db.user.deleteMany({ where: { id: { in: userIds } } });+ await db.company.deleteMany({ where: { domain } });+}++async function messagesOn(rootMessageId: string): Promise<string[]> {+ const rows = await db.emailMessage.findMany({+ where: { thread: { rootMessageId } },+ orderBy: { sentAt: "asc" },+ select: { snippet: true },+ });++ return rows.map((row) => row.snippet ?? "");+}++async function threadState(rootMessageId: string) {+ return db.emailThread.findUnique({+ where: { rootMessageId },+ select: {+ subject: true,+ messageCount: true,+ firstMessageAt: true,+ lastMessageAt: true,+ activity: { select: { body: true, occurredAt: true } },+ },+ });+}++beforeEach(async () => {+ await clean();++ await db.user.createMany({+ data: userIds.map((id) => ({+ id,+ name: id,+ email: `${id}@${domain}`,+ })),+ });++ await seed();+});++afterAll(clean);++describe("purging Gmail data", () => {+ it("removes only the caller's Gmail messages and counts them", async () => {+ expect(await google.purgeSyncedData(gmailRep)).toEqual({ purged: 3 });+ });++ it("leaves a thread standing when somebody else's messages remain", async () => {+ await google.purgeSyncedData(gmailRep);++ expect(await messagesOn(shared)).toEqual(["second", "third"]);+ expect(await threadState(shared)).toEqual({+ subject: "Re: Kickoff",+ messageCount: 2,+ firstMessageAt: at(10),+ lastMessageAt: at(11),+ activity: { body: "third", occurredAt: at(11) },+ });+ });++ it("takes the thread and its activity when nothing is left", async () => {+ await google.purgeSyncedData(gmailRep);++ expect(await threadState(solo)).toBeNull();+ expect(await db.activity.count({ where: { subject: "Only mine" } })).toBe(+ 0,+ );+ });++ it("cannot reach a thread the caller never synced into", async () => {+ await google.purgeSyncedData(gmailRep);++ expect(await messagesOn(theirs)).toEqual(["untouched"]);+ expect(await threadState(theirs)).toEqual({+ subject: "Not mine",+ messageCount: 1,+ firstMessageAt: at(7),+ lastMessageAt: at(7),+ activity: { body: "untouched", occurredAt: at(7) },+ });+ });+});++describe("purging Outlook data", () => {+ it("removes only the caller's Outlook messages and counts them", async () => {+ expect(await microsoft.purgeSyncedData(outlookRep)).toEqual({ purged: 2 });+ });++ it("leaves the Gmail messages on a shared thread alone", async () => {+ await microsoft.purgeSyncedData(outlookRep);++ expect(await messagesOn(shared)).toEqual(["first", "third"]);+ expect(await threadState(shared)).toEqual({+ subject: "Kickoff",+ messageCount: 2,+ firstMessageAt: at(9),+ lastMessageAt: at(11),+ activity: { body: "third", occurredAt: at(11) },+ });+ });++ it("cannot delete another user's Gmail thread", async () => {+ await microsoft.purgeSyncedData(outlookRep);++ expect(await messagesOn(solo)).toEqual(["alone"]);+ expect(+ await db.calendarEvent.count({ where: { iCalUid: `ical-${suffix}` } }),+ ).toBe(1);+ });+});++describe("disconnecting Microsoft", () => {+ async function grant(scope: string): Promise<void> {+ await db.account.create({+ data: {+ id: `ms-${suffix}`,+ accountId: `ms-account-${suffix}`,+ providerId: MICROSOFT_PROVIDER_ID,+ userId: outlookRep,+ scope,+ },+ });+ }++ it("clears the grant even when the token columns are already empty", async () => {+ await grant(`openid profile ${OUTLOOK_MAIL_SCOPE}`);+ await state.ensure(outlookRep, "outlook", { autoCreate: false });++ expect(await microsoft.revoke(outlookRep)).toEqual({ revoked: true });++ const account = await db.account.findFirst({+ where: { userId: outlookRep, providerId: MICROSOFT_PROVIDER_ID },+ select: { scope: true },+ });+ expect(account?.scope).toBeNull();+ });++ it("stays disconnected instead of re-creating the sync row", async () => {+ await grant(`openid profile ${OUTLOOK_MAIL_SCOPE}`);+ await state.ensure(outlookRep, "outlook", { autoCreate: false });++ await microsoft.revoke(outlookRep);+ const status = await microsoft.status(outlookRep);++ expect(status.linked).toBe(false);+ expect(status.required).toBe(true);+ expect(await state.get(outlookRep, "outlook")).toBeNull();+ });++ it("offers Connect to a sign-in that never granted the mail scope", async () => {+ await grant("openid profile email https://graph.microsoft.com/User.Read");++ const status = await microsoft.status(outlookRep);++ expect(status.linked).toBe(false);+ expect(status.sources.every((source) => !source.connected)).toBe(true);+ });++ it("is linked while the mail scope is granted", async () => {+ await grant(`openid profile ${OUTLOOK_MAIL_SCOPE}`);++ const status = await microsoft.status(outlookRep);++ expect(status.linked).toBe(true);+ expect(await state.get(outlookRep, "outlook")).not.toBeNull();+ });+});++describe("disconnecting Google", () => {+ const realFetch = globalThis.fetch;++ async function grant(scope: string, refreshToken: string | null) {+ await db.account.create({+ data: {+ id: `goog-${suffix}`,+ accountId: `goog-account-${suffix}`,+ providerId: GOOGLE_PROVIDER_ID,+ userId: gmailRep,+ refreshToken,+ scope,+ },+ });+ }++ async function scopeOf(): Promise<string | null | undefined> {+ const account = await db.account.findFirst({+ where: { userId: gmailRep, providerId: GOOGLE_PROVIDER_ID },+ select: { scope: true },+ });++ return account?.scope;+ }++ it("clears a grant with no token without asking Google", async () => {+ await grant(SYNC_SCOPES.join(" "), null);++ let calls = 0;+ globalThis.fetch = (async () => {+ calls += 1;+ return new Response(null, { status: 200 });+ }) as unknown as typeof fetch;++ try {+ expect(await google.revoke(gmailRep)).toEqual({ revoked: true });+ } finally {+ globalThis.fetch = realFetch;+ }++ expect(calls).toBe(0);+ expect(await scopeOf()).toBeNull();+ expect((await google.status(gmailRep)).linked).toBe(false);+ });++ it("refuses to clear when Google will not revoke the token", async () => {+ await grant(SYNC_SCOPES.join(" "), "refresh-token");++ globalThis.fetch = (async () =>+ new Response(null, { status: 400 })) as unknown as typeof fetch;++ try {+ expect(await google.revoke(gmailRep)).toEqual({ revoked: false });+ } finally {+ globalThis.fetch = realFetch;+ }++ expect(await scopeOf()).toBe(SYNC_SCOPES.join(" "));+ });++ it("clears once Google accepts the revocation", async () => {+ await grant(SYNC_SCOPES.join(" "), "refresh-token");++ globalThis.fetch = (async () =>+ new Response(null, { status: 200 })) as unknown as typeof fetch;++ try {+ expect(await google.revoke(gmailRep)).toEqual({ revoked: true });+ } finally {+ globalThis.fetch = realFetch;+ }++ expect(await scopeOf()).toBeNull();+ });++ it("offers Connect to a sign-in that never granted the sync scopes", async () => {+ await grant("openid email profile", null);++ const status = await google.status(gmailRep);++ expect(status.linked).toBe(false);+ expect(status.sources.every((source) => !source.connected)).toBe(true);+ });++ it("is linked while a sync scope is granted", async () => {+ await grant(SYNC_SCOPES.join(" "), "refresh-token");++ const status = await google.status(gmailRep);++ expect(status.linked).toBe(true);+ expect(+ status.sources.filter((source) => source.status === GoogleSyncStatus.IDLE)+ .length,+ ).toBe(2);+ });+});diff --git a/apps/api/test/mailbox-scopes.spec.ts b/apps/api/test/mailbox-scopes.spec.tsnew file mode 100644index 00000000..e98586a8--- /dev/null+++ b/apps/api/test/mailbox-scopes.spec.ts@@ -0,0 +1,112 @@+import { describe, expect, it } from "bun:test";+import {+ CALENDAR_SCOPE,+ GMAIL_SCOPE,+ hasSyncScopes,+ MICROSOFT_SYNC_SCOPES,+ OUTLOOK_MAIL_SCOPE,+ parseScopes,+ SYNC_SCOPES,+} from "@crm/auth/scopes";++const BOTH = `openid,email,profile,${GMAIL_SCOPE},${CALENDAR_SCOPE}`;++describe("parseScopes", () => {+ it("handles the comma form Better Auth stores", () => {+ expect(parseScopes("a,b,c")).toEqual(new Set(["a", "b", "c"]));+ });++ it("handles the space form Google returns", () => {+ expect(parseScopes("a b c")).toEqual(new Set(["a", "b", "c"]));+ });++ it("is empty for null, undefined and blank", () => {+ for (const value of [null, undefined, "", " "]) {+ expect(parseScopes(value).size).toBe(0);+ }+ });++ it("reduces the Graph resource URI Microsoft returns to the bare permission", () => {+ expect(+ parseScopes(+ "openid profile https://graph.microsoft.com/Mail.Read https://graph.microsoft.com/User.Read",+ ),+ ).toEqual(new Set(["openid", "profile", "Mail.Read", "User.Read"]));+ });+});++describe("hasSyncScopes for Google", () => {+ it("is true only when both scopes are present", () => {+ expect(hasSyncScopes("google", BOTH)).toBe(true);+ });++ it("is false when granular consent dropped one of them", () => {+ expect(hasSyncScopes("google", `openid,email,profile,${GMAIL_SCOPE}`)).toBe(+ false,+ );+ expect(+ hasSyncScopes("google", `openid,email,profile,${CALENDAR_SCOPE}`),+ ).toBe(false);+ });++ it("is false for an account that predates the requirement", () => {+ expect(hasSyncScopes("google", "openid,email,profile")).toBe(false);+ });++ it("is false when the grant was revoked and the column cleared", () => {+ expect(hasSyncScopes("google", null)).toBe(false);+ });++ it("does not match on a prefix", () => {+ expect(+ hasSyncScopes("google", `${GMAIL_SCOPE}.metadata,${CALENDAR_SCOPE}`),+ ).toBe(false);+ });++ it("covers exactly the scopes the provider requests", () => {+ expect(hasSyncScopes("google", SYNC_SCOPES.join(","))).toBe(true);+ });+});++describe("hasSyncScopes for Microsoft", () => {+ it("accepts the bare permission name", () => {+ expect(+ hasSyncScopes("microsoft", `openid profile ${OUTLOOK_MAIL_SCOPE}`),+ ).toBe(true);+ });++ it("accepts the fully-qualified Graph form Microsoft actually returns", () => {+ expect(+ hasSyncScopes(+ "microsoft",+ "openid profile email https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Read",+ ),+ ).toBe(true);+ });++ it("is false when only the sign-in scopes were granted", () => {+ expect(+ hasSyncScopes(+ "microsoft",+ "openid profile email https://graph.microsoft.com/User.Read",+ ),+ ).toBe(false);+ });++ it("covers exactly the scopes the provider requests", () => {+ expect(hasSyncScopes("microsoft", MICROSOFT_SYNC_SCOPES.join(","))).toBe(+ true,+ );+ });++ it("does not read a Google grant as a Microsoft one", () => {+ expect(hasSyncScopes("microsoft", BOTH)).toBe(false);+ expect(hasSyncScopes("google", OUTLOOK_MAIL_SCOPE)).toBe(false);+ });+});++describe("hasSyncScopes for anything else", () => {+ it("is false for a provider with no mailbox at all", () => {+ expect(hasSyncScopes("okta", BOTH)).toBe(false);+ });+});diff --git a/apps/api/test/mailbox-sync-tick.spec.ts b/apps/api/test/mailbox-sync-tick.spec.tsnew file mode 100644index 00000000..e721ed07--- /dev/null+++ b/apps/api/test/mailbox-sync-tick.spec.ts@@ -0,0 +1,281 @@+import { beforeEach, describe, expect, it } from "bun:test";+import {+ GoogleSyncStatus,+ type MailboxSyncModel as MailboxSync,+} from "@crm/db";+import type { GoogleConnectionService } from "../src/google/google-connection.service";+import type { GoogleSyncService } from "../src/google/google-sync.service";+import {+ SYNC_LEASE_MS,+ type SyncStateService,+} from "../src/mailbox/sync-state.service";+import type { MicrosoftConnectionService } from "../src/microsoft/microsoft-connection.service";+import type { MicrosoftSyncService } from "../src/microsoft/microsoft-sync.service";+import { MailboxSyncService } from "../src/sync/mailbox-sync.service";++type Outcome = {+ source: string;+ userId: string;+ status: "synced" | "skipped" | "reconnect" | "rate-limited" | "failed";+};++class FakeState {+ readonly rows = new Map<string, MailboxSync>();+ private revision = 0;++ add(id: string, source: string, overrides: Partial<MailboxSync> = {}): void {+ this.rows.set(id, {+ id,+ userId: `user-${id}`,+ source,+ status: GoogleSyncStatus.IDLE,+ cursor: null,+ lastSyncedAt: null,+ lastError: null,+ retryAfter: null,+ autoCreate: false,+ createdAt: new Date(0),+ updatedAt: this.stamp(),+ ...overrides,+ } as MailboxSync);+ }++ async due(now: Date): Promise<MailboxSync[]> {+ return [...this.rows.values()].filter((row) => this.isDue(row, now));+ }++ async claim(row: MailboxSync, now: Date): Promise<boolean> {+ const stored = this.rows.get(row.id);+ if (!stored) return false;+ if (stored.updatedAt.getTime() !== row.updatedAt.getTime()) return false;+ if (!this.isDue(stored, now)) return false;++ this.write(stored, {+ status: GoogleSyncStatus.RUNNING,+ retryAfter: new Date(now.getTime() + SYNC_LEASE_MS),+ });++ return true;+ }++ async release(id: string): Promise<void> {+ const stored = this.rows.get(id);+ if (stored) this.write(stored, { retryAfter: null });+ }++ async settle(id: string): Promise<void> {+ const stored = this.rows.get(id);+ if (stored) {+ this.write(stored, { retryAfter: null, lastSyncedAt: new Date() });+ }+ }++ async markRateLimited(id: string, retryAfterMs: number): Promise<void> {+ const stored = this.rows.get(id);+ if (stored) {+ this.write(stored, {+ status: GoogleSyncStatus.IDLE,+ retryAfter: new Date(Date.now() + retryAfterMs),+ });+ }+ }++ async markFailed(id: string, reason: string): Promise<void> {+ const stored = this.rows.get(id);+ if (stored) {+ this.write(stored, {+ status: GoogleSyncStatus.FAILED,+ lastError: reason,+ retryAfter: null,+ });+ }+ }++ private isDue(row: MailboxSync, now: Date): boolean {+ if (row.status === GoogleSyncStatus.NEEDS_RECONNECT) return false;++ return row.retryAfter === null || row.retryAfter.getTime() <= now.getTime();+ }++ private write(row: MailboxSync, patch: Partial<MailboxSync>): void {+ this.rows.set(row.id, { ...row, ...patch, updatedAt: this.stamp() });+ }++ private stamp(): Date {+ this.revision += 1;+ return new Date(1_700_000_000_000 + this.revision);+ }+}++const noConnections = {+ reconcileAll: async () => undefined,+};++function build(+ state: FakeState,+ runOne: (userId: string, source: string) => Promise<Outcome | null>,+): MailboxSyncService {+ const provider = { runOne } as unknown as GoogleSyncService;++ return new MailboxSyncService(+ state as unknown as SyncStateService,+ provider,+ provider as unknown as MicrosoftSyncService,+ noConnections as unknown as GoogleConnectionService,+ noConnections as unknown as MicrosoftConnectionService,+ );+}++let state: FakeState;++beforeEach(() => {+ state = new FakeState();+});++describe("runDue claims a mailbox before it syncs", () => {+ it("does not let two overlapping ticks run the same mailbox", async () => {+ state.add("a", "gmail");++ const calls: string[] = [];+ let unblock: (() => void) | undefined;+ const started = new Promise<void>((resolve) => {+ unblock = resolve;+ });++ let inFlight: (() => void) | undefined;+ const held = new Promise<void>((resolve) => {+ inFlight = resolve;+ });++ const service = build(state, async (userId, source) => {+ calls.push(`${userId}:${source}`);+ unblock?.();+ await held;+ return { source, userId, status: "synced" };+ });++ const first = service.runDue();+ await started;++ const second = await service.runDue();++ inFlight?.();+ const firstSummary = await first;++ expect(calls).toEqual(["user-a:gmail"]);+ expect(firstSummary.attempted).toBe(1);+ expect(firstSummary.synced).toBe(1);+ expect(second.attempted).toBe(0);+ expect(second.synced).toBe(0);+ });++ it("leaves the lease in place while the mailbox is running", async () => {+ state.add("a", "outlook");++ let leaseDuringRun: Date | null = null;++ const service = build(state, async (userId, source) => {+ leaseDuringRun = state.rows.get("a")?.retryAfter ?? null;+ return { source, userId, status: "synced" };+ });++ await service.runDue();++ expect(leaseDuringRun).not.toBeNull();+ expect((leaseDuringRun as unknown as Date).getTime()).toBeGreaterThan(+ Date.now(),+ );+ });++ it("skips a row another tick already claimed and does not count it", async () => {+ state.add("a", "gmail", { retryAfter: new Date(Date.now() + 60_000) });++ const service = build(state, async (userId, source) => ({+ source,+ userId,+ status: "synced",+ }));++ expect((await service.runDue()).attempted).toBe(0);+ });+});++describe("runDue always resolves the lease", () => {+ it("releases a not-connected mailbox so the next tick still polls it", async () => {+ state.add("a", "gmail");++ const service = build(state, async (userId, source) => ({+ source,+ userId,+ status: "skipped",+ }));++ const summary = await service.runDue();++ expect(summary.skipped).toBe(1);+ expect(state.rows.get("a")?.retryAfter).toBeNull();+ expect((await state.due(new Date())).map((row) => row.id)).toEqual(["a"]);+ });++ it("releases a mailbox the provider dispatch does not recognise", async () => {+ state.add("a", "pigeon");++ const service = build(state, async () => null);++ const summary = await service.runDue();++ expect(summary.skipped).toBe(1);+ expect(state.rows.get("a")?.retryAfter).toBeNull();+ });++ it("clears the lease when the sync throws", async () => {+ state.add("a", "gmail");++ const service = build(state, async () => {+ throw new Error("boom");+ });++ const summary = await service.runDue();++ expect(summary.failed).toBe(1);+ expect(state.rows.get("a")?.status).toBe(GoogleSyncStatus.FAILED);+ expect(state.rows.get("a")?.retryAfter).toBeNull();+ });+});++describe("runDue counts a rate-limited run honestly", () => {+ it("does not report it as synced", async () => {+ state.add("a", "gmail");++ const service = build(state, async (userId, source) => {+ await state.markRateLimited("a", 30_000);+ return { source, userId, status: "rate-limited" };+ });++ const summary = await service.runDue();++ expect(summary.synced).toBe(0);+ expect(summary.skipped).toBe(0);+ expect(summary.failed).toBe(0);+ expect(summary.rateLimited).toBe(1);+ expect(summary.attempted).toBe(1);+ });++ it("keeps the provider's own retryAfter rather than releasing it", async () => {+ state.add("a", "gmail");++ const service = build(state, async (userId, source) => {+ await state.markRateLimited("a", 30_000);+ return { source, userId, status: "rate-limited" };+ });++ await service.runDue();++ const retryAfter = state.rows.get("a")?.retryAfter;+ expect(retryAfter).not.toBeNull();+ expect((retryAfter as Date).getTime()).toBeGreaterThan(Date.now());+ expect((retryAfter as Date).getTime()).toBeLessThan(+ Date.now() + SYNC_LEASE_MS,+ );+ });+});diff --git a/apps/api/test/mailbox-thread-writer.spec.ts b/apps/api/test/mailbox-thread-writer.spec.tsnew file mode 100644index 00000000..ab636ebc--- /dev/null+++ b/apps/api/test/mailbox-thread-writer.spec.ts@@ -0,0 +1,206 @@+import { afterAll, beforeAll, describe, expect, it } from "bun:test";+import { db, type MailboxSyncModel as MailboxSync } from "@crm/db";+import type { AgentTriggerService } from "../src/agent/agent-trigger.service";+import { CompanyDirectoryService } from "../src/companies/company-directory.service";+import { ActivityStampService } from "../src/crm/activity-stamp.service";+import { EnrichmentLogService } from "../src/crm/enrichment-log.service";+import { MailboxMatchService } from "../src/mailbox/mailbox-match.service";+import {+ type IncomingMessage,+ ThreadWriterService,+} from "../src/mailbox/thread-writer.service";++const suffix = process.env.TEST_RUN_ID ?? "thread-writer-spec";+const domain = `threads-${suffix}.test`;+const userId = `user-${suffix}`;+const mailbox = `rep-${suffix}@example.test`;+const person = `buyer@${domain}`;+const rootId = `<root-${suffix}@mail.test>`;+const movedRoot = `outlook-conversation:${suffix}`;++const agent = {+ contactCreated: async () => undefined,+ companyCreated: async () => undefined,+ companyRequested: async () => undefined,+} as unknown as AgentTriggerService;++const stamp = new ActivityStampService(db);+const directory = new CompanyDirectoryService(db, agent);+const log = new EnrichmentLogService(db, stamp);+const match = new MailboxMatchService(db, directory, agent, log);+const threads = new ThreadWriterService(db, match, stamp);++let row: MailboxSync;++function message(id: string, sentAt: Date, root = rootId): IncomingMessage {+ return {+ rfcMessageId: id,+ rootId: root,+ subject: "Pricing",+ from: { email: mailbox, name: "Test Rep" },+ recipients: [{ email: person, name: "A Buyer", kind: "to" }],+ body: "The numbers you asked for.",+ sentAt,+ gmailMessageId: null,+ outlookMessageId: null,+ outlookWebLink: null,+ };+}++async function clean() {+ await db.emailThread.deleteMany({+ where: { rootMessageId: { in: [rootId, movedRoot] } },+ });+ await db.contact.deleteMany({ where: { email: person } });+ await db.company.deleteMany({ where: { domain } });+ await db.mailboxSync.deleteMany({ where: { userId } });+ await db.user.deleteMany({ where: { id: userId } });+}++beforeAll(async () => {+ await clean();++ await db.user.create({+ data: { id: userId, name: "Test Rep", email: mailbox },+ });+ row = await db.mailboxSync.create({+ data: { userId, source: "gmail", autoCreate: false },+ });++ const company = await db.company.create({+ data: { name: "Buyer Co", domain },+ select: { id: true },+ });+ await db.contact.create({+ data: {+ firstName: "A",+ lastName: "Buyer",+ email: person,+ companyId: company.id,+ },+ });+});++afterAll(clean);++describe("storing a synced email", () => {+ it("writes the message, the counts and the activity together", async () => {+ const stored = await threads.store(+ row,+ { mailbox, origin: "gmail" },+ message(`<one-${suffix}@mail.test>`, new Date("2026-01-01T10:00:00Z")),+ await threads.context(),+ );++ expect(stored).toBe(true);++ const thread = await db.emailThread.findUnique({+ where: { rootMessageId: rootId },+ select: {+ id: true,+ messageCount: true,+ activity: { select: { id: true } },+ },+ });++ expect(thread?.messageCount).toBe(1);+ expect(thread?.activity).not.toBeNull();+ });++ it("repairs a thread whose projection was lost rather than skipping it forever", async () => {+ const thread = await db.emailThread.findUnique({+ where: { rootMessageId: rootId },+ select: { id: true },+ });+ if (!thread) throw new Error("the first message was not stored");++ await db.activity.deleteMany({ where: { emailThreadId: thread.id } });+ await db.emailThread.update({+ where: { id: thread.id },+ data: { messageCount: 0 },+ });++ const stored = await threads.store(+ row,+ { mailbox, origin: "gmail" },+ message(`<one-${suffix}@mail.test>`, new Date("2026-01-01T10:00:00Z")),+ await threads.context(),+ );++ expect(stored).toBe(false);++ const repaired = await db.emailThread.findUnique({+ where: { id: thread.id },+ select: { messageCount: true, activity: { select: { id: true } } },+ });++ expect(repaired?.messageCount).toBe(1);+ expect(repaired?.activity).not.toBeNull();+ });++ it("lets one of two concurrent syncs win without failing the other", async () => {+ const parsed = message(+ `<race-${suffix}@mail.test>`,+ new Date("2026-01-02T10:00:00Z"),+ );+ const context = await threads.context();++ const results = await Promise.all([+ threads.store(row, { mailbox, origin: "gmail" }, parsed, context),+ threads.store(row, { mailbox, origin: "outlook" }, parsed, context),+ ]);++ expect(results.filter(Boolean)).toHaveLength(1);+ expect(+ await db.emailMessage.count({+ where: { rfcMessageId: parsed.rfcMessageId },+ }),+ ).toBe(1);++ const thread = await db.emailThread.findUnique({+ where: { rootMessageId: rootId },+ select: { messageCount: true, activity: { select: { id: true } } },+ });++ expect(thread?.messageCount).toBe(2);+ expect(thread?.activity).not.toBeNull();+ });++ it("repairs the thread the message is already on when the root id has moved", async () => {+ const thread = await db.emailThread.findUnique({+ where: { rootMessageId: rootId },+ select: { id: true },+ });+ if (!thread) throw new Error("the first message was not stored");++ await db.activity.deleteMany({ where: { emailThreadId: thread.id } });+ await db.emailThread.update({+ where: { id: thread.id },+ data: { messageCount: 0 },+ });++ const stored = await threads.store(+ row,+ { mailbox, origin: "outlook" },+ message(+ `<race-${suffix}@mail.test>`,+ new Date("2026-01-02T10:00:00Z"),+ movedRoot,+ ),+ await threads.context(),+ );++ expect(stored).toBe(false);+ expect(+ await db.emailThread.count({ where: { rootMessageId: movedRoot } }),+ ).toBe(0);++ const repaired = await db.emailThread.findUnique({+ where: { id: thread.id },+ select: { messageCount: true, activity: { select: { id: true } } },+ });++ expect(repaired?.messageCount).toBe(2);+ expect(repaired?.activity).not.toBeNull();+ });+});diff --git a/apps/api/test/outlook-sync.spec.ts b/apps/api/test/outlook-sync.spec.tsnew file mode 100644index 00000000..89b33985--- /dev/null+++ b/apps/api/test/outlook-sync.spec.ts@@ -0,0 +1,348 @@+import { describe, expect, it } from "bun:test";+import type { MailboxSyncModel as MailboxSync } from "@crm/db";+import type { MailboxTokenService } from "../src/mailbox/mailbox-token.service";+import type { SyncStateService } from "../src/mailbox/sync-state.service";+import type {+ IncomingMessage,+ ThreadWriterService,+} from "../src/mailbox/thread-writer.service";+import type { GraphClient, GraphMessage } from "../src/microsoft/graph.client";+import { OutlookSyncService } from "../src/microsoft/outlook-sync.service";++type Ok<T> = { outcome: "ok"; data: T };+type NotOk =+ | { outcome: "cursor-invalid"; reason: string }+ | { outcome: "unauthorized"; reason: string }+ | { outcome: "rate-limited"; reason: string; retryAfterMs: number }+ | { outcome: "failed"; reason: string; retryable: boolean };++const ok = <T>(data: T): Ok<T> => ({ outcome: "ok", data });++const row = {+ id: "sync-1",+ userId: "user-1",+ source: "outlook",+ cursor: "2025-08-01T00:00:00.000Z",+ autoCreate: true,+} as unknown as MailboxSync;++type Harness = {+ service: OutlookSyncService;+ stored: IncomingMessage[];+ settled: { cursor?: string | null }[];+ rateLimited: number[];+ reconnected: string[];+ failed: string[];+ meResolvedAt: { value: number };+};++function harness(options: {+ folder?: (name: string) => Ok<{ id?: string }> | NotOk;+ pages?: GraphMessage[][];+ meDelayMs?: number;+}): Harness {+ const stored: IncomingMessage[] = [];+ const settled: { cursor?: string | null }[] = [];+ const rateLimited: number[] = [];+ const reconnected: string[] = [];+ const failed: string[] = [];+ const meResolvedAt = { value: 0 };++ const pages = options.pages ?? [[]];+ let index = 0;++ const graph = {+ async me() {+ if (options.meDelayMs) {+ await new Promise((resolve) => setTimeout(resolve, options.meDelayMs));+ }+ meResolvedAt.value = Date.now();+ return ok({ mail: "[email protected]" });+ },+ async folder(_token: string, name: string) {+ return options.folder+ ? options.folder(name)+ : ok({ id: `folder-${name}` });+ },+ async listMessages() {+ index = 0;+ return ok({+ value: pages[0] ?? [],+ ...(pages.length > 1 ? { "@odata.nextLink": "next-1" } : {}),+ });+ },+ async nextPage() {+ index += 1;+ return ok({+ value: pages[index] ?? [],+ ...(index + 1 < pages.length+ ? { "@odata.nextLink": `next-${index + 1}` }+ : {}),+ });+ },+ } as unknown as GraphClient;++ const tokens = {+ async accessTokenFor() {+ return { outcome: "ok" as const, accessToken: "token" };+ },+ } as unknown as MailboxTokenService;++ const state = {+ async markRunning() {},+ async settle(_id: string, update: { cursor?: string | null }) {+ settled.push(update);+ },+ async clearCursor() {},+ async markNeedsReconnect(_id: string, reason: string) {+ reconnected.push(reason);+ },+ async markRateLimited(_id: string, retryAfterMs: number) {+ rateLimited.push(retryAfterMs);+ },+ async markFailed(_id: string, reason: string) {+ failed.push(reason);+ },+ } as unknown as SyncStateService;++ const threads = {+ async context() {+ return {};+ },+ async store(_row: MailboxSync, _options: unknown, parsed: IncomingMessage) {+ stored.push(parsed);+ return true;+ },+ } as unknown as ThreadWriterService;++ return {+ service: new OutlookSyncService(graph, tokens, state, threads),+ stored,+ settled,+ rateLimited,+ reconnected,+ failed,+ meResolvedAt,+ };+}++const rowWith = (cursor: string | null): MailboxSync =>+ ({ ...row, cursor }) as MailboxSync;++function message(overrides: Partial<GraphMessage> = {}): GraphMessage {+ return {+ id: "graph-1",+ internetMessageId: "<[email protected]>",+ conversationId: "conv-1",+ subject: "Pricing",+ from: { emailAddress: { name: "Jane", address: "[email protected]" } },+ toRecipients: [{ emailAddress: { address: "[email protected]" } }],+ receivedDateTime: "2025-08-01T09:00:00.000Z",+ sentDateTime: "2025-08-01T09:00:00.000Z",+ body: { contentType: "text", content: "Hello" },+ parentFolderId: "folder-inbox",+ ...overrides,+ };+}++describe("OutlookSyncService threading", () => {+ it("keeps a conversation together when Graph returns unrelated headers", async () => {+ const kit = harness({+ pages: [+ [+ message({+ id: "a",+ internetMessageId: "<[email protected]>",+ internetMessageHeaders: [+ { name: "x-ms-exchange-organization-authas", value: "Internal" },+ { name: "Return-Path", value: "<[email protected]>" },+ ],+ }),+ message({+ id: "b",+ internetMessageId: "<[email protected]>",+ receivedDateTime: "2025-08-01T09:05:00.000Z",+ sentDateTime: "2025-08-01T09:05:00.000Z",+ internetMessageHeaders: [+ { name: "Content-Type", value: "text/plain" },+ ],+ }),+ ],+ ],+ });++ await kit.service.sync(row);++ expect(kit.stored.map((parsed) => parsed.rootId)).toEqual([+ "outlook-conversation:conv-1",+ "outlook-conversation:conv-1",+ ]);+ });++ it("prefers a real threading header over the conversation id", async () => {+ const kit = harness({+ pages: [+ [+ message({+ internetMessageHeaders: [+ { name: "Return-Path", value: "<[email protected]>" },+ {+ name: "References",+ value: "<[email protected]> <[email protected]>",+ },+ ],+ }),+ ],+ ],+ });++ await kit.service.sync(row);++ expect(kit.stored[0]?.rootId).toBe("[email protected]");+ });++ it("takes the first id when In-Reply-To carries a list", async () => {+ const kit = harness({+ pages: [+ [+ message({+ internetMessageHeaders: [+ {+ name: "In-Reply-To",+ value: "<[email protected]> <[email protected]>",+ },+ ],+ }),+ ],+ ],+ });++ await kit.service.sync(row);++ expect(kit.stored[0]?.rootId).toBe("[email protected]");+ });++ it("falls back to its own id only when there is no conversation", async () => {+ const kit = harness({+ pages: [[message({ conversationId: undefined })]],+ });++ await kit.service.sync(row);++ expect(kit.stored[0]?.rootId).toBe("[email protected]");+ });+});++describe("OutlookSyncService excluded folders", () => {+ it("defers the tick when a folder lookup is throttled", async () => {+ const kit = harness({+ folder: () => ({+ outcome: "rate-limited",+ reason: "Too many requests",+ retryAfterMs: 30_000,+ }),+ pages: [[message()]],+ });++ const outcome = await kit.service.sync(row);++ expect(outcome.status).toBe("rate-limited");+ expect(kit.rateLimited).toEqual([30_000]);+ expect(kit.stored).toHaveLength(0);+ expect(kit.settled).toHaveLength(0);+ });++ it("asks for a reconnect when a folder lookup is unauthorized", async () => {+ const kit = harness({+ folder: () => ({ outcome: "unauthorized", reason: "Invalid token" }),+ pages: [[message()]],+ });++ const outcome = await kit.service.sync(row);++ expect(outcome.status).toBe("reconnect");+ expect(kit.reconnected).toEqual(["Invalid token"]);+ expect(kit.stored).toHaveLength(0);+ });++ it("treats a 404 as a mailbox without that folder and carries on", async () => {+ const kit = harness({+ folder: (name) =>+ name === "junkemail"+ ? { outcome: "cursor-invalid", reason: "Not found" }+ : ok({ id: "folder-deleteditems" }),+ pages: [+ [+ message({ parentFolderId: "folder-inbox" }),+ message({+ id: "deleted",+ internetMessageId: "<[email protected]>",+ parentFolderId: "folder-deleteditems",+ }),+ ],+ ],+ });++ const outcome = await kit.service.sync(row);++ expect(outcome.status).toBe("synced");+ expect(kit.stored).toHaveLength(1);+ });+});++describe("OutlookSyncService budget", () => {+ const bulk = (count: number, offset: number): GraphMessage[] =>+ Array.from({ length: count }, (_, i) => {+ const n = offset + i;+ return message({+ id: `m-${n}`,+ internetMessageId: `<m-${n}@acme.com>`,+ conversationId: `conv-${n}`,+ receivedDateTime: new Date(+ Date.UTC(2025, 7, 1, 9, 0, 0) + n * 60_000,+ ).toISOString(),+ sentDateTime: new Date(+ Date.UTC(2025, 7, 1, 9, 0, 0) + n * 60_000,+ ).toISOString(),+ });+ });++ it("never processes more than the declared budget", async () => {+ const kit = harness({+ pages: [bulk(50, 0), bulk(50, 50), bulk(50, 100)],+ });++ await kit.service.sync(row);++ expect(kit.stored).toHaveLength(120);+ });++ it("leaves the cursor on the last message it actually processed", async () => {+ const kit = harness({+ pages: [bulk(50, 0), bulk(50, 50), bulk(50, 100)],+ });++ await kit.service.sync(row);++ const lastProcessed = new Date(+ Date.UTC(2025, 7, 1, 9, 0, 0) + 119 * 60_000,+ ).toISOString();++ expect(kit.stored[119]?.rfcMessageId).toBe("[email protected]");+ expect(kit.settled.at(-1)?.cursor).toBe(lastProcessed);+ });+});++describe("OutlookSyncService first run", () => {+ it("watermarks setup before the first round trip, not after", async () => {+ const kit = harness({ meDelayMs: 30 });++ await kit.service.sync(rowWith(null));++ const cursor = kit.settled.at(-1)?.cursor;+ expect(cursor).toBeTruthy();+ expect(new Date(String(cursor)).getTime()).toBeLessThan(+ kit.meResolvedAt.value - 20,+ );+ });+});diff --git a/apps/api/test/record-delete.spec.ts b/apps/api/test/record-delete.spec.tsindex 76451e56..6c361f86 100644--- a/apps/api/test/record-delete.spec.ts+++ b/apps/api/test/record-delete.spec.ts@@ -10,7 +10,7 @@ import { ActivityStampService } from "../src/crm/activity-stamp.service";import { EnrichmentLogService } from "../src/crm/enrichment-log.service";import { ConversionService } from "../src/currency/conversion.service";import { FieldsService } from "../src/fields/fields.service";-import { GoogleMatchService } from "../src/google/google-match.service";+import { MailboxMatchService } from "../src/mailbox/mailbox-match.service";const suffix = process.env.TEST_RUN_ID ?? "record-delete-spec";const domain = `delete-${suffix}.test`;@@ -52,7 +52,7 @@ const companies = new CompaniesService(conversion,fields,);-const match = new GoogleMatchService(db, directory, agent, log);+const match = new MailboxMatchService(db, directory, agent, log);async function matchContext() {const internal = await match.internalIdentity();diff --git a/apps/api/test/workspace.spec.ts b/apps/api/test/workspace.spec.tsindex 7e419127..5e227e7b 100644--- a/apps/api/test/workspace.spec.ts+++ b/apps/api/test/workspace.spec.ts@@ -1,6 +1,6 @@import { beforeEach, describe, expect, it } from "bun:test";import { isWorkspaceEmail } from "@crm/auth/workspace";-import { externalParticipants } from "../src/google/participants";+import { externalParticipants } from "../src/mailbox/participants";beforeEach(() => {process.env.ALLOWED_SIGN_IN = "acme.com";diff --git a/apps/api/turbo.json b/apps/api/turbo.jsonindex 3714bf7c..8a3d1922 100644--- a/apps/api/turbo.json+++ b/apps/api/turbo.json@@ -27,6 +27,9 @@"DATABASE_URL","GOOGLE_CLIENT_ID","GOOGLE_CLIENT_SECRET",+ "MICROSOFT_CLIENT_ID",+ "MICROSOFT_CLIENT_SECRET",+ "MICROSOFT_TENANT_ID","PORT","REDIS_URL"]@@ -42,6 +45,9 @@"DATABASE_URL","GOOGLE_CLIENT_ID","GOOGLE_CLIENT_SECRET",+ "MICROSOFT_CLIENT_ID",+ "MICROSOFT_CLIENT_SECRET",+ "MICROSOFT_TENANT_ID","PORT","REDIS_URL"]diff --git a/apps/api/vercel.json b/apps/api/vercel.jsonindex 0e29716d..b2cab4c1 100644--- a/apps/api/vercel.json+++ b/apps/api/vercel.json@@ -2,7 +2,7 @@"$schema": "https://openapi.vercel.sh/vercel.json","crons": [{- "path": "/internal/sync/google",+ "path": "/internal/sync/mailboxes","schedule": "*/5 * * * *"},{diff --git a/apps/app/app/(app)/[slug]/layout.tsx b/apps/app/app/(app)/[slug]/layout.tsxindex aa7fcb95..e837aca7 100644--- a/apps/app/app/(app)/[slug]/layout.tsx+++ b/apps/app/app/(app)/[slug]/layout.tsx@@ -5,7 +5,7 @@ import { AppIconRail, AppIconRailFallback } from "@/components/app-icon-rail";import { QuickSwitcher } from "@/components/crm/quick-switcher";import { RecordSheetHost } from "@/components/crm/record-sheet/record-sheet-host";import { MobileNavProvider } from "@/components/mobile-nav";-import { requireGoogleAccess } from "@/lib/session";+import { requireMailboxAccess } from "@/lib/session";import { HydrateClient } from "@/lib/trpc/hydrate";import { getServerQueryClient, getServerTrpc } from "@/lib/trpc/server";@@ -43,7 +43,7 @@ async function WorkspaceHeader({params,}: Pick<LayoutProps<"/[slug]">, "params">) {const [{ user }, { slug }] = await Promise.all([- requireGoogleAccess(),+ requireMailboxAccess(),params,]);diff --git a/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx b/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsxindex c346fd6b..77378805 100644--- a/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx+++ b/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx@@ -106,16 +106,16 @@ function GoogleUnavailable() {return (<Card><CardHeader>- <CardTitle>Google</CardTitle>+ <CardTitle>+ <div className="flex items-center gap-2">+ <StatusIndicator size="sm" tone="neutral" label="Not configured" />+ </div>+ </CardTitle><CardDescription>- Gmail and Calendar sync needs a Google OAuth client, and this install- does not have one. Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in- the root .env file and restart.+ Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in the root .env file+ and restart.</CardDescription>-- <CardAction>- <StatusIndicator size="sm" tone="neutral" label="Not configured" />- </CardAction></CardHeader></Card>);@@ -129,6 +129,11 @@ const CONNECT_ERRORS: Record<string, string> = {function ConnectGoogle({ connectError }: { connectError?: string }) {const [pending, setPending] = useState(false);+ function fail(message?: string) {+ setPending(false);+ toast.error(message ?? "Could not reach Google.");+ }+async function handleConnect() {setPending(true);@@ -138,32 +143,47 @@ function ConnectGoogle({ connectError }: { connectError?: string }) {provider: "google",scopes: [...SYNC_SCOPES],callbackURL: `${origin}/settings/connections`,- errorCallbackURL: `${origin}/settings/connections`,+ errorCallbackURL: `${origin}/settings/connections?provider=google`,});- if (error) {- toast.error(error.message ?? "Could not reach Google.");- setPending(false);- }+ if (error) fail(error.message);}return (<Card><CardHeader>- <CardTitle>Google</CardTitle>+ <CardTitle>+ <div className="flex items-center gap-2">+ <StatusIndicator size="sm" tone="neutral" label="Not connected" />+ </div>+ </CardTitle><CardDescription>- Connect Gmail and Calendar, and new meetings and email threads are- added to the matching company as they happen. It is read-only —- nothing is ever sent on your behalf.+ Read-only Gmail and Calendar. Only conversations with companies in the+ CRM are stored.</CardDescription><CardAction>- <StatusIndicator size="sm" tone="neutral" label="Not connected" />+ <Button+ size="sm"+ disabled={pending}+ onClick={() => {+ handleConnect().catch(() => fail());+ }}+ type="button"+ >+ {pending ? (+ <Spinner data-icon="inline-start" />+ ) : (+ <GoogleLogo data-icon="inline-start" className="size-4" />+ )}+ Connect+ </Button></CardAction></CardHeader>- <CardContent>- {connectError ? (+ {connectError ? (+ <CardContent><Alert variant="destructive"><Icon icon={Warning} /><AlertTitle>Google did not finish connecting</AlertTitle>@@ -172,22 +192,8 @@ function ConnectGoogle({ connectError }: { connectError?: string }) {"Google returned an error before the connection was made. Try again."}</AlertDescription></Alert>- ) : null}-- <Button disabled={pending} onClick={handleConnect} type="button">- {pending ? (- <Spinner data-icon="inline-start" />- ) : (- <GoogleLogo data-icon="inline-start" className="size-4" />- )}- Connect Google- </Button>-- <p className="text-muted-foreground text-xs">- Only conversations with companies in the CRM are stored. Personal mail- is discarded without being saved.- </p>- </CardContent>+ </CardContent>+ ) : null}</Card>);}@@ -273,19 +279,22 @@ export function GoogleConnection({ connectError }: { connectError?: string }) {return (<Card><CardHeader>- <CardTitle>Google</CardTitle>+ <CardTitle>+ <div className="flex items-center gap-2">+ <StatusIndicator+ size="sm"+ tone={healthy ? "success" : "warning"}+ label={healthy ? "Connected" : "Needs attention"}+ />+ </div>+ </CardTitle><CardDescription>- New meetings and email threads are added to the matching company as- they happen.+ Meetings and email threads land on the matching company as they+ happen.</CardDescription><CardAction>- <StatusIndicator- size="sm"- tone={healthy ? "success" : "warning"}- label={healthy ? "Connected" : "Needs attention"}- />-<Buttonvariant="contrast"size="sm"diff --git a/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsx b/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsxnew file mode 100644index 00000000..96736236--- /dev/null+++ b/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsx@@ -0,0 +1,366 @@+"use client";++import Warning from "@carbon/icons-react/es/Warning";+import { authClient } from "@crm/auth/client";+import { MICROSOFT_SYNC_SCOPES } from "@crm/auth/scopes";+import { Alert, AlertDescription, AlertTitle } from "@crm/ui/components/alert";+import {+ AlertDialog,+ AlertDialogAction,+ AlertDialogCancel,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+ AlertDialogTrigger,+} from "@crm/ui/components/alert-dialog";+import MicrosoftLogo from "@crm/ui/components/brand-logos/microsoft";+import { Button } from "@crm/ui/components/button";+import {+ Card,+ CardAction,+ CardContent,+ CardDescription,+ CardFooter,+ CardHeader,+ CardTitle,+} from "@crm/ui/components/card";+import { Icon } from "@crm/ui/components/icon";+import { Label } from "@crm/ui/components/label";+import { Spinner } from "@crm/ui/components/spinner";+import { StatusIndicator } from "@crm/ui/components/status-indicator";+import { Switch } from "@crm/ui/components/switch";+import { relativeTimeFromIso } from "@crm/ui/lib/format";+import { useMutation, useQuery } from "@tanstack/react-query";+import Link from "next/link";+import { useState } from "react";+import { toast } from "sonner";+import { isSyncing, SYNC_POLL_MS } from "@/components/crm/sync-status";+import { useCrmCache } from "@/lib/trpc/cache";+import { useTRPC } from "@/lib/trpc/client";++const AUTO_CREATE = "Add the company and contact when you reply to someone new";++const CONNECT_ERRORS: Record<string, string> = {+ "email_doesn't_match":+ "That Microsoft account has a different email address to the one you sign in with, so it cannot be attached to your account. Connect the Microsoft account that matches your sign-in address.",+};++function MicrosoftUnavailable() {+ return (+ <Card>+ <CardHeader>+ <CardTitle>+ <div className="flex items-center gap-2">+ Microsoft+ <StatusIndicator size="sm" tone="neutral" label="Not configured" />+ </div>+ </CardTitle>+ <CardDescription>+ Set MICROSOFT_CLIENT_ID and MICROSOFT_CLIENT_SECRET in the root .env+ file and restart.+ </CardDescription>+ </CardHeader>+ </Card>+ );+}++function ConnectMicrosoft({ connectError }: { connectError?: string }) {+ const [pending, setPending] = useState(false);++ function fail(message?: string) {+ setPending(false);+ toast.error(message ?? "Could not reach Microsoft.");+ }++ async function handleConnect() {+ setPending(true);++ const origin = window.location.origin;++ const { error } = await authClient.linkSocial({+ provider: "microsoft",+ scopes: [...MICROSOFT_SYNC_SCOPES],+ callbackURL: `${origin}/settings/connections`,+ errorCallbackURL: `${origin}/settings/connections?provider=microsoft`,+ });++ if (error) fail(error.message);+ }++ return (+ <Card>+ <CardHeader>+ <CardTitle>+ <div className="flex items-center gap-2">+ Microsoft+ <StatusIndicator size="sm" tone="neutral" label="Not connected" />+ </div>+ </CardTitle>+ <CardDescription>+ Read-only Outlook mail. Only conversations with companies in the CRM+ are stored.+ </CardDescription>++ <CardAction>+ <Button+ size="sm"+ disabled={pending}+ onClick={() => {+ handleConnect().catch(() => fail());+ }}+ type="button"+ >+ {pending ? (+ <Spinner data-icon="inline-start" />+ ) : (+ <MicrosoftLogo data-icon="inline-start" className="size-4" />+ )}+ Connect+ </Button>+ </CardAction>+ </CardHeader>++ {connectError ? (+ <CardContent>+ <Alert variant="destructive">+ <Icon icon={Warning} />+ <AlertTitle>Microsoft did not finish connecting</AlertTitle>+ <AlertDescription>+ {CONNECT_ERRORS[connectError] ??+ "Microsoft returned an error before the connection was made. Try again."}+ </AlertDescription>+ </Alert>+ </CardContent>+ ) : null}+ </Card>+ );+}++export function MicrosoftConnection({+ connectError,+}: {+ connectError?: string;+}) {+ const trpc = useTRPC();+ const cache = useCrmCache();++ const status = useQuery({+ ...trpc.microsoft.status.queryOptions(),+ refetchInterval: (query) =>+ query.state.data?.sources.some((source) => isSyncing(source.status))+ ? SYNC_POLL_MS+ : false,+ });++ const purge = useMutation(+ trpc.microsoft.purgeSyncedData.mutationOptions({+ onSuccess: async (result) => {+ await cache.microsoft();+ toast.success(`Removed ${result.purged} synced items.`);+ },+ onError: (error) => toast.error(error.message),+ }),+ );++ const revoke = useMutation(+ trpc.microsoft.revokeAccess.mutationOptions({+ onSuccess: () =>+ window.location.assign(+ status.data?.required ? "/" : "/settings/connections",+ ),+ onError: (error) => toast.error(error.message),+ }),+ );++ const setAutoCreate = useMutation(+ trpc.microsoft.setAutoCreate.mutationOptions({+ onSuccess: () => cache.microsoft({ settle: "record" }),+ onError: (error) => toast.error(error.message),+ }),+ );++ const syncNow = useMutation(+ trpc.microsoft.syncNow.mutationOptions({+ onSuccess: () => cache.microsoft(),+ onError: (error) => toast.error(error.message),+ }),+ );++ if (!status.data) return null;++ const { sources, hasRefreshToken, configured, linked, required } =+ status.data;++ if (!configured) return <MicrosoftUnavailable />;+ if (!linked) return <ConnectMicrosoft connectError={connectError} />;++ const failing = sources.filter(+ (source) => source.status === "NEEDS_RECONNECT" || source.lastError,+ );+ const lastSyncedAt = sources+ .map((source) => source.lastSyncedAt)+ .filter((at): at is string => at !== null)+ .sort()+ .at(-1);++ const healthy = failing.length === 0 && hasRefreshToken;++ return (+ <Card>+ <CardHeader>+ <CardTitle>+ <div className="flex items-center gap-2">+ Microsoft+ <StatusIndicator+ size="sm"+ tone={healthy ? "success" : "warning"}+ label={healthy ? "Connected" : "Needs attention"}+ />+ </div>+ </CardTitle>+ <CardDescription>+ Email threads land on the matching company as they happen.+ </CardDescription>++ <CardAction>+ <Button+ variant="contrast"+ size="sm"+ disabled={syncNow.isPending}+ onClick={() => syncNow.mutate()}+ >+ {syncNow.isPending ? "Checking…" : "Check now"}+ </Button>+ </CardAction>+ </CardHeader>++ <CardContent>+ {!hasRefreshToken ? (+ <Alert variant="destructive">+ <Icon icon={Warning} />+ <AlertTitle>Microsoft did not return a refresh token</AlertTitle>+ <AlertDescription>Sign out and back in.</AlertDescription>+ </Alert>+ ) : failing.length > 0 ? (+ failing.map((source) => (+ <Alert key={source.source} variant="destructive">+ <Icon icon={Warning} />+ <AlertTitle>Email sync failed</AlertTitle>+ <AlertDescription>+ {source.lastError ?? "Microsoft needs reconnecting."}+ </AlertDescription>+ </Alert>+ ))+ ) : (+ <p className="text-muted-foreground text-xs">+ {lastSyncedAt+ ? `Last checked ${relativeTimeFromIso(lastSyncedAt)}`+ : "Waiting for the first check"}+ </p>+ )}++ {sources.map((source) => (+ <div+ key={source.source}+ className="flex items-center justify-between gap-6"+ >+ <Label+ htmlFor={`auto-create-${source.source}`}+ className="flex flex-col items-start gap-1"+ >+ <span className="text-sm">Email</span>+ <span className="font-normal text-muted-foreground text-xs">+ {AUTO_CREATE}+ </span>+ </Label>++ <Switch+ id={`auto-create-${source.source}`}+ checked={source.autoCreate}+ disabled={setAutoCreate.isPending}+ onCheckedChange={(enabled) =>+ setAutoCreate.mutate({ source: source.source, enabled })+ }+ />+ </div>+ ))}++ <CardFooter>+ <div className="-ml-2 flex flex-wrap items-center gap-1 text-muted-foreground">+ <AlertDialog>+ <AlertDialogTrigger asChild>+ <Button variant="ghost" size="xs" disabled={purge.isPending}>+ Delete synced data+ </Button>+ </AlertDialogTrigger>++ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>Delete synced data?</AlertDialogTitle>+ <AlertDialogDescription>+ Every email brought in from Outlook is removed from the CRM.+ The next check starts from now, so nothing deleted here+ comes back.+ </AlertDialogDescription>+ </AlertDialogHeader>++ <AlertDialogFooter>+ <AlertDialogCancel>Cancel</AlertDialogCancel>+ <AlertDialogAction+ variant="destructive"+ onClick={() => purge.mutate()}+ >+ Delete+ </AlertDialogAction>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>++ <AlertDialog>+ <AlertDialogTrigger asChild>+ <Button variant="ghost" size="xs" disabled={revoke.isPending}>+ Disconnect Microsoft+ </Button>+ </AlertDialogTrigger>++ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>Disconnect Microsoft?</AlertDialogTitle>+ <AlertDialogDescription>+ {required+ ? "You will be signed out, and you cannot use the CRM again until you grant access."+ : "New email stops arriving. Everything already synced stays, and you can connect Microsoft again from this page."}{" "}+ Microsoft has no way for us to withdraw the consent itself —+ remove this app from your Microsoft account to do that.+ </AlertDialogDescription>+ </AlertDialogHeader>++ <AlertDialogFooter>+ <AlertDialogCancel>Cancel</AlertDialogCancel>+ <AlertDialogAction+ variant="destructive"+ onClick={() => revoke.mutate()}+ >+ Disconnect+ </AlertDialogAction>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>++ <Button variant="ghost" size="xs" asChild>+ <Link+ href="https://myapplications.microsoft.com"+ target="_blank"+ rel="noreferrer"+ >+ Manage in your Microsoft account+ </Link>+ </Button>+ </div>+ </CardFooter>+ </CardContent>+ </Card>+ );+}diff --git a/apps/app/app/(app)/[slug]/settings/connections/page.tsx b/apps/app/app/(app)/[slug]/settings/connections/page.tsxindex 6bbb45da..38075762 100644--- a/apps/app/app/(app)/[slug]/settings/connections/page.tsx+++ b/apps/app/app/(app)/[slug]/settings/connections/page.tsx@@ -13,6 +13,7 @@ import { requireSession } from "@/lib/session";import { HydrateClient } from "@/lib/trpc/hydrate";import { getServerQueryClient, getServerTrpc } from "@/lib/trpc/server";import { GoogleConnection } from "./google-connection";+import { MicrosoftConnection } from "./microsoft-connection";export const metadata: Metadata = {title: "Connections",@@ -49,18 +50,30 @@ async function Connections({const trpc = getServerTrpc();const queryClient = getServerQueryClient();- const [{ error }] = await Promise.all([+ const [{ error, provider }] = await Promise.all([searchParams,queryClient.prefetchQuery(trpc.google.status.queryOptions()),+ queryClient.prefetchQuery(trpc.microsoft.status.queryOptions()),]);+ const connectError = first(error);+ const failed = first(provider);+return (<HydrateClient><div className="flex max-w-3xl flex-col gap-6"><GoogleConnection- connectError={Array.isArray(error) ? error[0] : error}+ connectError={failed === "google" ? connectError : undefined}+ />++ <MicrosoftConnection+ connectError={failed === "microsoft" ? connectError : undefined}/></div></HydrateClient>);}++function first(value: string | string[] | undefined): string | undefined {+ return Array.isArray(value) ? value[0] : value;+}diff --git a/apps/app/app/(landing)/grant-access/grant-access.tsx b/apps/app/app/(landing)/grant-access/grant-access.tsxindex a5d5e62c..6c8c4fa6 100644--- a/apps/app/app/(landing)/grant-access/grant-access.tsx+++ b/apps/app/app/(landing)/grant-access/grant-access.tsx@@ -1,32 +1,56 @@"use client";import { authClient, signOut } from "@crm/auth/client";-import { SYNC_SCOPES } from "@crm/auth/scopes";+import {+ type MailboxProviderId,+ MICROSOFT_SYNC_SCOPES,+ SYNC_SCOPES,+} from "@crm/auth/scopes";import GoogleLogo from "@crm/ui/components/brand-logos/google";+import MicrosoftLogo from "@crm/ui/components/brand-logos/microsoft";import { Button } from "@crm/ui/components/button";import { Spinner } from "@crm/ui/components/spinner";import { useState } from "react";import { toast } from "sonner";-export function GrantAccess() {- const [pending, setPending] = useState(false);+const PROVIDERS = {+ google: {+ label: "Grant Google access",+ scopes: [...SYNC_SCOPES],+ Logo: GoogleLogo,+ },+ microsoft: {+ label: "Grant Microsoft access",+ scopes: [...MICROSOFT_SYNC_SCOPES],+ Logo: MicrosoftLogo,+ },+} as const satisfies Record<MailboxProviderId, unknown>;- async function handleGrant() {- setPending(true);+export function GrantAccess({+ providers,+}: {+ providers: readonly MailboxProviderId[];+}) {+ const [pending, setPending] = useState<MailboxProviderId | null>(null);++ function fail(message?: string) {+ setPending(null);+ toast.error(message ?? "Could not reach the provider.");+ }++ async function handleGrant(provider: MailboxProviderId) {+ setPending(provider);const origin = window.location.origin;const { error } = await authClient.linkSocial({- provider: "google",- scopes: [...SYNC_SCOPES],+ provider,+ scopes: [...PROVIDERS[provider].scopes],callbackURL: `${origin}/`,errorCallbackURL: `${origin}/grant-access`,});- if (error) {- toast.error(error.message ?? "Could not reach Google.");- setPending(false);- }+ if (error) fail(error.message);}async function handleSignOut() {@@ -40,21 +64,32 @@ export function GrantAccess() {window.location.assign("/sign-in");}+ const single = providers.length === 1;+return (<div className="flex flex-col gap-3">- <Button- className="w-full"- disabled={pending}- onClick={handleGrant}- type="button"- >- {pending ? (- <Spinner data-icon="inline-start" />- ) : (- <GoogleLogo data-icon="inline-start" className="size-4" />- )}- Grant access- </Button>+ {providers.map((provider) => {+ const { label, Logo } = PROVIDERS[provider];++ return (+ <Button+ key={provider}+ className="w-full"+ disabled={pending !== null}+ onClick={() => {+ handleGrant(provider).catch(() => fail());+ }}+ type="button"+ >+ {pending === provider ? (+ <Spinner data-icon="inline-start" />+ ) : (+ <Logo data-icon="inline-start" className="size-4" />+ )}+ {single ? "Grant access" : label}+ </Button>+ );+ })}<ButtonclassName="w-full"diff --git a/apps/app/app/(landing)/grant-access/page.tsx b/apps/app/app/(landing)/grant-access/page.tsxindex 51806ab5..f5ee181d 100644--- a/apps/app/app/(landing)/grant-access/page.tsx+++ b/apps/app/app/(landing)/grant-access/page.tsx@@ -1,4 +1,4 @@-import { needsGoogleGrant } from "@crm/auth";+import { mailboxGrantsNeeded } from "@crm/auth";import type { Metadata } from "next";import { redirect } from "next/navigation";import { AuthHeading, AuthShell } from "@/components/auth-shell";@@ -11,21 +11,35 @@ export const metadata: Metadata = {export const instant = false;+const DESCRIPTION: Record<string, string> = {+ google:+ "This CRM reads your Gmail and Calendar so meetings and email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.",+ microsoft:+ "This CRM reads your Outlook mail so email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.",+};++const BOTH =+ "This CRM reads your mail and calendar so meetings and email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.";+export default async function GrantAccessPage() {const { user } = await requireSession();- if (!needsGoogleGrant(await signInAccounts(user.id))) {+ const providers = mailboxGrantsNeeded(await signInAccounts(user.id));++ if (providers.length === 0) {redirect("/");}+ const only = providers.length === 1 ? providers[0] : undefined;+return (<AuthShell><AuthHeadingtitle="One more step"- description="This CRM reads your Gmail and Calendar so meetings and email threads show up on the right company. It is read-only — nothing is ever sent on your behalf."+ description={(only ? DESCRIPTION[only] : undefined) ?? BOTH}/>- <GrantAccess />+ <GrantAccess providers={providers} /><p className="text-center text-muted-foreground text-sm/5">Only conversations with companies in the CRM are stored. Personal maildiff --git a/apps/app/app/(landing)/onboarding/page.tsx b/apps/app/app/(landing)/onboarding/page.tsxindex 2c3d4954..c6133f3b 100644--- a/apps/app/app/(landing)/onboarding/page.tsx+++ b/apps/app/app/(landing)/onboarding/page.tsx@@ -1,7 +1,7 @@import { DEFAULT_WORKSPACE_NAME } from "@crm/auth";import type { Metadata } from "next";import { AuthHeading, AuthShell } from "@/components/auth-shell";-import { requireGoogleAccess } from "@/lib/session";+import { requireMailboxAccess } from "@/lib/session";import { OnboardingForm } from "./onboarding-form";export const metadata: Metadata = {@@ -11,7 +11,7 @@ export const metadata: Metadata = {export const instant = false;export default async function OnboardingPage() {- await requireGoogleAccess();+ await requireMailboxAccess();return (<AuthShell>diff --git a/apps/app/app/(landing)/onboarding/research/page.tsx b/apps/app/app/(landing)/onboarding/research/page.tsxindex 8d067e39..4fc83d2d 100644--- a/apps/app/app/(landing)/onboarding/research/page.tsx+++ b/apps/app/app/(landing)/onboarding/research/page.tsx@@ -1,6 +1,6 @@import type { Metadata } from "next";import { AuthHeading, AuthShell } from "@/components/auth-shell";-import { requireGoogleAccess } from "@/lib/session";+import { requireMailboxAccess } from "@/lib/session";import { ResearchForm } from "./research-form";export const metadata: Metadata = {@@ -10,7 +10,7 @@ export const metadata: Metadata = {export const instant = false;export default async function ResearchKeyPage() {- await requireGoogleAccess();+ await requireMailboxAccess();return (<AuthShell>diff --git a/apps/app/app/(landing)/sign-in/google-sign-in.tsx b/apps/app/app/(landing)/sign-in/google-sign-in.tsxdeleted file mode 100644index 8bee5dd7..00000000--- a/apps/app/app/(landing)/sign-in/google-sign-in.tsx+++ /dev/null@@ -1,46 +0,0 @@-"use client";--import { signIn } from "@crm/auth/client";-import GoogleLogo from "@crm/ui/components/brand-logos/google";-import { Button } from "@crm/ui/components/button";-import { Spinner } from "@crm/ui/components/spinner";-import { useState } from "react";-import { toast } from "sonner";--export function GoogleSignIn() {- const [pending, setPending] = useState(false);-- async function handleClick() {- setPending(true);-- const origin = window.location.origin;-- const { error } = await signIn.social({- provider: "google",- callbackURL: `${origin}/`,- errorCallbackURL: `${origin}/sign-in`,- });-- if (error) {- toast.error(error.message ?? "Could not reach the sign-in service.");- setPending(false);- }- }-- return (- <Button- className="w-full"- disabled={pending}- onClick={handleClick}- type="button"- variant="outline"- >- {pending ? (- <Spinner data-icon="inline-start" />- ) : (- <GoogleLogo data-icon="inline-start" className="size-4" />- )}- Continue with Google- </Button>- );-}diff --git a/apps/app/app/(landing)/sign-in/page.tsx b/apps/app/app/(landing)/sign-in/page.tsxindex 38298589..0a7929fc 100644--- a/apps/app/app/(landing)/sign-in/page.tsx+++ b/apps/app/app/(landing)/sign-in/page.tsx@@ -1,17 +1,22 @@+import type { MailboxProviderId } from "@crm/auth/scopes";import type { Metadata } from "next";import { redirect, unstable_rethrow } from "next/navigation";import { Suspense } from "react";import { AuthHeading, AuthShell } from "@/components/auth-shell";import { getSession } from "@/lib/session";import { getServerQueryClient, getServerTrpc } from "@/lib/trpc/server";-import { GoogleSignIn } from "./google-sign-in";+import { SocialSignIn } from "./social-sign-in";import { type SsoProvider, SsoSignIn } from "./sso-sign-in";export const metadata: Metadata = {title: "Sign in",};-type SignInOptions = { google: boolean; providers: SsoProvider[] };+type SignInOptions = {+ google: boolean;+ microsoft: boolean;+ providers: SsoProvider[];+};async function signInOptions(): Promise<SignInOptions | null> {try {@@ -59,14 +64,22 @@ async function SignIn({redirect("/");}- const google = options?.google ?? true;+ const configured: MailboxProviderId[] = [];+ if (options?.google ?? true) configured.push("google");+ if (options?.microsoft ?? false) configured.push("microsoft");+const providers = options?.providers ?? [];- const insistOnGoogle = method === "google" && google;- const showSso = providers.length > 0 && !insistOnGoogle;- const showGoogle = google && (providers.length === 0 || insistOnGoogle);+ const insisted = configured.find((provider) => provider === method);+ const showSso = providers.length > 0 && insisted === undefined;+ const social =+ insisted !== undefined+ ? [insisted]+ : providers.length === 0+ ? configured+ : [];- if (!showSso && !showGoogle) {+ if (!showSso && social.length === 0) {return (<><AuthHeading@@ -75,9 +88,10 @@ async function SignIn({/><p className="text-center text-muted-foreground text-sm/5">- Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in the root .env file- and restart. Your own identity provider can be added from Settings- once somebody is signed in.+ Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET — or MICROSOFT_CLIENT_ID+ and MICROSOFT_CLIENT_SECRET — in the root .env file and restart. Your+ own identity provider can be added from Settings once somebody is+ signed in.</p></>);@@ -91,7 +105,9 @@ async function SignIn({/>{showSso ? <SsoSignIn providers={providers} /> : null}- {showGoogle ? <GoogleSignIn /> : null}+ {social.map((provider) => (+ <SocialSignIn key={provider} provider={provider} />+ ))}</>);}diff --git a/apps/app/app/(landing)/sign-in/social-sign-in.tsx b/apps/app/app/(landing)/sign-in/social-sign-in.tsxnew file mode 100644index 00000000..021d2249--- /dev/null+++ b/apps/app/app/(landing)/sign-in/social-sign-in.tsx@@ -0,0 +1,59 @@+"use client";++import { signIn } from "@crm/auth/client";+import type { MailboxProviderId } from "@crm/auth/scopes";+import GoogleLogo from "@crm/ui/components/brand-logos/google";+import MicrosoftLogo from "@crm/ui/components/brand-logos/microsoft";+import { Button } from "@crm/ui/components/button";+import { Spinner } from "@crm/ui/components/spinner";+import { useState } from "react";+import { toast } from "sonner";++const PROVIDERS = {+ google: { label: "Continue with Google", Logo: GoogleLogo },+ microsoft: { label: "Continue with Microsoft", Logo: MicrosoftLogo },+} as const satisfies Record<MailboxProviderId, unknown>;++export function SocialSignIn({ provider }: { provider: MailboxProviderId }) {+ const [pending, setPending] = useState(false);++ const { label, Logo } = PROVIDERS[provider];++ function fail(message?: string) {+ setPending(false);+ toast.error(message ?? "Could not reach the sign-in service.");+ }++ async function handleClick() {+ setPending(true);++ const origin = window.location.origin;++ const { error } = await signIn.social({+ provider,+ callbackURL: `${origin}/`,+ errorCallbackURL: `${origin}/sign-in`,+ });++ if (error) fail(error.message);+ }++ return (+ <Button+ className="w-full"+ disabled={pending}+ onClick={() => {+ handleClick().catch(() => fail());+ }}+ type="button"+ variant="outline"+ >+ {pending ? (+ <Spinner data-icon="inline-start" />+ ) : (+ <Logo data-icon="inline-start" className="size-4" />+ )}+ {label}+ </Button>+ );+}diff --git a/apps/app/components/crm/timeline/email-thread-entry.tsx b/apps/app/components/crm/timeline/email-thread-entry.tsxindex 3bdd0e5c..415411fc 100644--- a/apps/app/components/crm/timeline/email-thread-entry.tsx+++ b/apps/app/components/crm/timeline/email-thread-entry.tsx@@ -69,14 +69,14 @@ export function EmailThreadEntry({direction={message.direction}body={message.body}action={- message.gmailUrl ? (+ message.mailboxUrl ? (<a- href={message.gmailUrl}+ href={message.mailboxUrl}target="_blank"rel="noreferrer"className="text-muted-foreground underline underline-offset-3 hover:text-foreground">- Open in Gmail+ Open in {message.mailboxName}</a>) : null}diff --git a/apps/app/lib/session.ts b/apps/app/lib/session.tsindex ab6cf5cf..48fbb231 100644--- a/apps/app/lib/session.ts+++ b/apps/app/lib/session.ts@@ -1,4 +1,4 @@-import { auth, needsGoogleGrant, type Session } from "@crm/auth";+import { auth, needsMailboxGrant, type Session } from "@crm/auth";import { db } from "@crm/db";import { headers } from "next/headers";import { redirect } from "next/navigation";@@ -26,10 +26,10 @@ export const signInAccounts = cache(async (userId: string) =>}),);-export async function requireGoogleAccess(): Promise<Session> {+export async function requireMailboxAccess(): Promise<Session> {const session = await requireSession();- if (needsGoogleGrant(await signInAccounts(session.user.id))) {+ if (needsMailboxGrant(await signInAccounts(session.user.id))) {redirect("/grant-access");}diff --git a/apps/app/lib/trpc/cache.ts b/apps/app/lib/trpc/cache.tsindex bdf462e2..a60162ca 100644--- a/apps/app/lib/trpc/cache.ts+++ b/apps/app/lib/trpc/cache.ts@@ -25,6 +25,7 @@ export type CrmCache = {removedMany(records: RemovedRecords): Promise<void>;activity(options?: Options): Promise<void>;google(options?: Options): Promise<void>;+ microsoft(options?: Options): Promise<void>;settings(options?: Options): Promise<void>;currency(options?: Options): Promise<void>;workspace(options?: Options): Promise<void>;@@ -210,6 +211,19 @@ export function useCrmCache(): CrmCache {options,),+ microsoft: (options) =>+ run(+ [trpc.microsoft.status.queryKey()],+ [+ ...activityKeys(),+ ...listKeys(),+ trpc.companies.byId.queryKey(),+ trpc.contacts.byId.queryKey(),+ trpc.dashboard.summary.queryKey(),+ ],+ options,+ ),+settings: (options) =>run([diff --git a/docs/api.md b/docs/api.mdindex 46008ff7..56c9e7d5 100644--- a/docs/api.md+++ b/docs/api.md@@ -19,7 +19,7 @@## Intelligence never lives in the API-The API serves HTTP, auth, tRPC and the Google sync. It does **not** research, enrich,+The API serves HTTP, auth, tRPC and the mailbox sync. It does **not** research, enrich,score, summarise, match identities or decide anything about a person or company — notas a fallback, not behind a flag. That is the eve agent in `apps/agent`, which ownsthe vendor clients, the confidence model and the writes.@@ -71,7 +71,7 @@ Onboarding, then `/onboarding/research` for the Context key. Asked server-side erequest.- **`getSessionCookie()` decides signed-in**; pages still resolve the real session via- `requireGoogleAccess()`.+ `requireMailboxAccess()`.- **Nothing is cached in a cookie** — both facts revert on a database reset while ayear-long marker insists the gate passed. Cache in the API if cost ever matters.- **Both reads run concurrently**, but order decides which is *asked* — the research@@ -112,12 +112,15 @@ self-hoster's admin cannot redeploy.takes `AuthMiddleware` at the *method*, which is what leaves it open. A clientsecret is never read back out.- **It is the API's answer, not the app's** — the API serves `/api/auth/*`.-- **An install with neither Google nor a provider says so**, naming the two variables;- a read that *fails* falls back to offering Google.-- **A provider hides the Google button, it does not disable it** —- `/sign-in?method=google` still works, so a mistyped issuer cannot lock an admin out.-- **Signing in with an IdP does not cost you Gmail.** `needsGoogleGrant` (`@crm/auth`)- walls only an account whose sole sign-in row is Google.+- **An install with no Google, no Microsoft and no provider says so**, naming the+ variables; a read that *fails* falls back to offering Google.+- **A provider hides the social buttons, it does not disable them** —+ `/sign-in?method=google` and `?method=microsoft` still work, so a mistyped issuer+ cannot lock an admin out.+- **Signing in with an IdP does not cost you Gmail.** `needsMailboxGrant` (`@crm/auth`)+ walls only an account whose sign-in rows are *all* mailbox providers — Google,+ Microsoft, or both — and none of them granted. `mailboxGrantsNeeded` returns which,+ so `/grant-access` offers the button they can actually use.- `ALLOWED_SIGN_IN` still decides who gets an account, in`databaseHooks.user.create.before`, for SSO sign-ups too.- `organizationProvisioning: { disabled: true }` — `ensureWorkspaceMembership` already@@ -138,9 +141,41 @@ self-hoster's admin cannot redeploy.Only `check-types` and `dev` run it. If the app cannot see a new procedure, it hasnot run.+## Two mail providers, one pipeline++`apps/api/src/mailbox` is everything neither Google nor Microsoft owns:+`MailboxApiClient` (bearer GET, and the one place a status code becomes an outcome),+`SyncStateService` (the `MailboxSync` row), `MailboxTokenService`,+`MailboxMatchService`, `participants.ts`, `message-text.ts`, and+`ThreadWriterService`.++- **`ThreadWriterService.store` is the only writer of `EmailThread`, `EmailMessage`+ and the `EMAIL` activity.** Gmail and Outlook each parse their own wire format down+ to one `IncomingMessage` and hand it over; matching, threading, counting and+ stamping happen once. A second copy of that is how a rule like *reply before you+ create a company* comes to be true in one inbox and not the other.+- **A thread is keyed by RFC message id, not by the provider's thread id.** Root comes+ from `References` → `In-Reply-To` → own `Message-ID`, so a rep on Gmail and a rep on+ Outlook land on the same `EmailThread` for the same conversation. Graph only returns+ `internetMessageHeaders` when `$select`ed and not for every message, so Outlook falls+ back to `outlook-conversation:<conversationId>` — threading that still holds inside+ Outlook, just not across to Gmail.+- **`MailboxSync.source` is the discriminator** — `calendar`, `gmail`, `outlook`. Each+ provider's module only ever sees its own, and `sync/mailbox-sync.service.ts` is the+ one place that dispatches. One cron, one budget:+ `POST /internal/sync/mailboxes` (`/google` is kept as an alias so an existing+ deployment's cron keeps working).+- **Gmail is forward-only from a `historyId`, Outlook from a timestamp.** Graph has no+ mailbox-wide delta, so the Outlook cursor is the last `receivedDateTime` seen,+ re-read with a one-second overlap; `rfcMessageId` is unique, so the overlap costs a+ duplicate fetch and never a duplicate row.+- **Microsoft has no token-revocation endpoint.** `revoke` clears the columns and the+ UI says the consent itself is removed in the user's Microsoft account. Google's still+ posts to `oauth2.googleapis.com/revoke` and refuses to clear if that fails.+## Not every address on a thread is a person-`externalParticipants` (`google/participants.ts`) is the one gate, discarding **us**+`externalParticipants` (`mailbox/participants.ts`) is the one gate, discarding **us**(allow-list domains, `User` table), **rep decisions** (`SuppressedContact`,`SuppressedDomain`), and **addresses no human reads**.diff --git a/docs/environment.md b/docs/environment.mdindex b9345b55..0d11c3ba 100644--- a/docs/environment.md+++ b/docs/environment.md@@ -16,6 +16,17 @@ and nothing that is not read. `packages/env` walks up to the workspace root and- **The root marker is a `package.json` declaring `workspaces`** — stopping at thefirst `turbo.json` resolves the API's root to `apps/api`.+## A new variable has three homes, not two++`.env.example` and — if the API reads it — `env.validation.ts` are the two people+remember. The third is **`globalPassThroughEnv` in the root `turbo.json`**, and it is+the one that bites: Turborepo hides an undeclared variable from every task it runs, so+a deployment that sets the variable perfectly still hands the code `undefined`, and+nothing anywhere says so. That is how `MICROSOFT_CLIENT_ID` shipped with the sign-in+button quietly missing. **`passThroughEnv`, never `env`** — a secret in `env` is a+cache key, which means a cache miss on every rotation and the secret in the cache+metadata. The root file's comment has the whole account.+## Required`DATABASE_URL`, `BETTER_AUTH_SECRET`, `ALLOWED_SIGN_IN`. Everything else has a@@ -25,6 +36,16 @@ localhost default or is genuinely optional.Gmail/Calendar sync — optional, so an SSO-only install needn't create a Google project,but **set together or not at all** (`packages/auth/src/env.ts` throws on one).+**`MICROSOFT_CLIENT_ID` + `MICROSOFT_CLIENT_SECRET`** are the same bargain for Entra+ID: the other sign-in button *and* the Outlook mail sync, one app registration, the+same pair rule. **`MICROSOFT_TENANT_ID`** defaults to `common` and is the only one of+the three that is genuinely optional on its own — set it to your tenant's GUID to+refuse other tenants at Microsoft instead of at `ALLOWED_SIGN_IN`. There is **no+Microsoft equivalent of `hd`**: `tenantId` is the whole of it.++**Neither pair is required, but an install wants one of them or an SSO provider** —+with none, the sign-in page says so by name rather than rendering nothing.+**`ALLOWED_SIGN_IN`** — comma-separated whole domains or single addresses (bareaddresses exist for a solo self-hoster, where `gmail.com` would be an open door). **Onelist, read by the sign-in guard *and* the sync's "which side is external" decision** —@@ -106,30 +127,41 @@ General — an admin who cannot redeploy cannot set a variable.check that cannot be made is not a failed check** — `unknown` saves anyway and logs itunverified.-## Gmail and Calendar sync+## Mailbox sync++Always on, on whichever social provider is configured, so there is no extra redirect+URI beyond the sign-in one. Scopes are requested at sign-in and gated by+`requireMailboxAccess()`, because granular consent lets a user untick one and still+sign in.++**An SSO rep is not gated** — `needsMailboxGrant` (`@crm/auth`) walls only an account+whose sign-in rows are *all* mailbox providers. It cannot be "has the scopes": an SSO+rep has no Google or Microsoft account to grant on, and `revoke()` keeps the `account`+row, so trying the optional feature and revoking would lock them out. They connect from+Settings → Connections, posting the same `linkSocial` call.-Always on, on the existing Google provider, so there is no extra redirect URI. Scopes-are requested at sign-in and gated by `requireGoogleAccess()`, because granular consent-lets a user untick one and still sign in.+**One granted mailbox is enough.** A rep with both providers linked who granted Google+is not asked for Outlook; `mailboxGrantsNeeded` names the ones still outstanding and+`/grant-access` offers exactly those buttons.-**An SSO rep is not gated** — `needsGoogleGrant` (`@crm/auth`) walls only an account-whose *sole* sign-in row is Google. It cannot be "has the scopes": an SSO rep has no-Google account to grant on, and `revoke()` keeps the `account` row, so trying the-optional feature and revoking would lock them out. They connect from Settings →-Connections, posting the same `linkSocial` call.+**Microsoft's granted scopes come back fully qualified** —+`https://graph.microsoft.com/Mail.Read`, not `Mail.Read`. `parseScopes` is the one+canonicaliser and strips that prefix, so the comparison is against the bare permission+everywhere.**Sync is forward-only** — Gmail records the current `historyId` on its first pass and-imports nothing; Calendar reads from `now`.+imports nothing, Calendar reads from `now`, and Outlook records `now` as its cursor.-**`CRON_SECRET`** (min 16 chars) guards `POST /internal/sync/google` and-`/internal/sync/rates`; both **fail closed when unset**. **Crons live in-`apps/api/vercel.json`** — Google `*/5 * * * *`, rates daily. Minute-level schedules-need a Pro plan; on Hobby it silently becomes daily.+**`CRON_SECRET`** (min 16 chars) guards `POST /internal/sync/mailboxes` and+`/internal/sync/rates`; both **fail closed when unset**. `/internal/sync/google` is+kept as an alias of the first, so an existing deployment's cron does not break on+deploy. **Crons live in `apps/api/vercel.json`** — mailboxes `*/5 * * * *`, rates+daily. Minute-level schedules need a Pro plan; on Hobby it silently becomes daily.Deliberate absences: **no `GOOGLE_SYNC_ENABLED`** (a switch that can disable a mandatoryfeature is only ever wrong), **no `GOOGLE_WORKSPACE_DOMAIN`** (`ALLOWED_SIGN_IN` alreadysays who is internal — two sources is how a colleague becomes a lead), **no-`GMAIL_BACKFILL_DAYS`**, **no rate provider variable**.+`GMAIL_BACKFILL_DAYS`**, **no `OUTLOOK_BACKFILL_DAYS`**, **no rate provider variable**.## Telemetry is on, and turning it off is one variable@@ -157,4 +189,4 @@ is sent. No client is constructed, so there is no queue waiting to flush later.- **Cache TTL** — `DEFAULT_TTL_MS` (60s) in `cache.module.ts`; `CACHE_TTL_MS` overrides.- **Redis** — optional; without `REDIS_URL` the cache is per-instance in-memory, whichis wrong for multi-instance.-- **Sign-in method** — Google is in code; an IdP is a row (SSO, in `api.md`).+- **Sign-in method** — Google and Microsoft are in code; an IdP is a row (SSO, in `api.md`).diff --git a/docs/telemetry.md b/docs/telemetry.mdindex e2602987..aa303241 100644--- a/docs/telemetry.md+++ b/docs/telemetry.md@@ -120,7 +120,7 @@ Each is only whether the key is set. `cap_context_dev` is whether an `AppSetting| `recheck_interval_days` | Their intervals, in day bands || `agent_conversations` | How many `AgentConversation` rows exist |-Tool names are matched against the twenty tools in `apps/agent/agent/tools/` and eve's own+Tool names are matched against the twenty-four tools in `apps/agent/agent/tools/` and eve's ownbuiltins. Anything else is counted as `other`. Task kinds are matched against `TASK_KINDS`.Tool counts are read from the `agentEvent` rows the audit hook already writes — the event `type`@@ -206,10 +206,15 @@ our errors would carry contact fields.| Event | Properties || --- | --- || `agent_error` | `error_class`, `tool`, `task_kind`, `error_source` (`tool` / `turn` / `session`) |-| `sync_error` | `error_class`, `sync_source` (`gmail` / `calendar`) |+| `sync_error` | `error_class`, `sync_source` (`gmail` / `calendar` / `outlook`), `error_source` (`google_sync` / `microsoft_sync` / `mailbox_sync`) || `api_error` | `error_class`, `route`, `status_code` || `model_error` | `error_class`, `model_id` |+`sync_error`'s `error_source` is derived from `sync_source` rather than passed in, so one+mailbox pipeline that dispatches to both providers cannot report an Outlook failure as a Google+one. A source that is not one of the three is sent as `other`, and its `error_source` as+`mailbox_sync`.+`route` is the **route pattern** — `/internal/sync/google`, `/trpc/contacts.list` — never a URLwith parameters in it. Anything that does not match a strict path shape is sent as `other`, andso is any error class that is not a plain identifier.diff --git a/package.json b/package.jsonindex 19de5a78..58f50e8b 100644--- a/package.json+++ b/package.json@@ -4,6 +4,7 @@"license": "MIT","version": "1.1.0","scripts": {+ "prepare": "git rev-parse --git-dir >/dev/null 2>&1 && git config core.hooksPath .githooks || true","build": "turbo run build","dev": "turbo run dev","lint": "turbo run lint",diff --git a/packages/auth/src/auth.ts b/packages/auth/src/auth.tsindex edb54197..1e23d22e 100644--- a/packages/auth/src/auth.ts+++ b/packages/auth/src/auth.ts@@ -7,7 +7,12 @@ import { organization } from "better-auth/plugins/organization";import { AUTH_COOKIE_PREFIX } from "./cookies";import { env } from "./env";import { ensureWorkspaceMembership } from "./organization";-import { SYNC_SCOPES } from "./scopes";+import {+ GOOGLE_PROVIDER_ID,+ MICROSOFT_PROVIDER_ID,+ MICROSOFT_SYNC_SCOPES,+ SYNC_SCOPES,+} from "./scopes";import { notifySignedIn } from "./signed-in";import {hasSignInAllowList,@@ -29,6 +34,24 @@ if (env.google) {};}+if (env.microsoft) {+ socialProviders.microsoft = {+ clientId: env.microsoft.clientId,+ clientSecret: env.microsoft.clientSecret,+ tenantId: env.microsoft.tenantId,++ scope: [...MICROSOFT_SYNC_SCOPES],++ prompt: "select_account",++ disableProfilePhoto: true,++ mapProfileToUser: (profile) => ({+ email: profile.email ?? profile.preferred_username ?? profile.upn,+ }),+ };+}+export const auth = betterAuth({appName: "CRM",@@ -45,7 +68,7 @@ export const auth = betterAuth({account: {accountLinking: {enabled: true,- trustedProviders: ["google"],+ trustedProviders: [GOOGLE_PROVIDER_ID, MICROSOFT_PROVIDER_ID],},},diff --git a/packages/auth/src/env.ts b/packages/auth/src/env.tsindex 792b9ecd..552c6810 100644--- a/packages/auth/src/env.ts+++ b/packages/auth/src/env.ts@@ -2,23 +2,23 @@ import "@crm/env/load";const DEFAULT_API_URL = "http://localhost:3001";const DEFAULT_APP_URL = "http://localhost:3000";+const DEFAULT_MICROSOFT_TENANT = "common";const optional = (key: string): string | undefined => {const value = process.env[key];return value && value.length > 0 ? value : undefined;};-const googleCredentials = ():- | { clientId: string; clientSecret: string }- | undefined => {- const clientId = optional("GOOGLE_CLIENT_ID");- const clientSecret = optional("GOOGLE_CLIENT_SECRET");+const pair = (+ idKey: string,+ secretKey: string,+): { clientId: string; clientSecret: string } | undefined => {+ const clientId = optional(idKey);+ const clientSecret = optional(secretKey);if (!clientId || !clientSecret) {if (clientId || clientSecret) {- throw new Error(- "GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET must be set together.",- );+ throw new Error(`${idKey} and ${secretKey} must be set together.`);}return undefined;}@@ -26,6 +26,22 @@ const googleCredentials = ():return { clientId, clientSecret };};+const googleCredentials = ():+ | { clientId: string; clientSecret: string }+ | undefined => pair("GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET");++const microsoftCredentials = ():+ | { clientId: string; clientSecret: string; tenantId: string }+ | undefined => {+ const credentials = pair("MICROSOFT_CLIENT_ID", "MICROSOFT_CLIENT_SECRET");+ if (!credentials) return undefined;++ return {+ ...credentials,+ tenantId: optional("MICROSOFT_TENANT_ID") ?? DEFAULT_MICROSOFT_TENANT,+ };+};+const apiUrl =optional("API_URL") ?? optional("BETTER_AUTH_URL") ?? DEFAULT_API_URL;@@ -39,6 +55,7 @@ const appUrl = appUrls[0] ?? DEFAULT_APP_URL;export const env = {appUrl: apiUrl,google: googleCredentials(),+ microsoft: microsoftCredentials(),cookieDomain: optional("AUTH_COOKIE_DOMAIN"),trustedOrigins: [...new Set([...appUrls, apiUrl])],isProduction: process.env.NODE_ENV === "production",@@ -48,4 +65,8 @@ export function isGoogleConfigured(): boolean {return env.google !== undefined;}+export function isMicrosoftConfigured(): boolean {+ return env.microsoft !== undefined;+}+export { apiUrl, appUrl };diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.tsindex cbd838e8..f66f443f 100644--- a/packages/auth/src/index.ts+++ b/packages/auth/src/index.ts@@ -1,6 +1,6 @@export { type Auth, auth, type Session, type SessionUser } from "./auth";export { AUTH_COOKIE_PREFIX } from "./cookies";-export { isGoogleConfigured } from "./env";+export { isGoogleConfigured, isMicrosoftConfigured } from "./env";export {canChangeRole,canManageCurrency,@@ -19,12 +19,22 @@ export {GOOGLE_PROVIDER_ID,hasSyncScopes,IDENTITY_SCOPES,- needsGoogleGrant,+ isMailboxProvider,+ MAILBOX_PROVIDER_IDS,+ type MailboxProviderId,+ MICROSOFT_PROVIDER_ID,+ MICROSOFT_SYNC_SCOPES,+ mailboxGrantsNeeded,+ needsMailboxGrant,+ OUTLOOK_MAIL_SCOPE,parseScopes,REQUIRED_SCOPES,type SignInAccount,SYNC_SCOPES,+ SYNC_SCOPES_FOR,+ signsInOnlyWith,signsInWithGoogle,+ signsInWithMicrosoft,} from "./scopes";export { onSignedIn, type SignedInHandler } from "./signed-in";export {diff --git a/packages/auth/src/scopes.ts b/packages/auth/src/scopes.tsindex ce3ca7ff..9a2ad5c3 100644--- a/packages/auth/src/scopes.ts+++ b/packages/auth/src/scopes.ts@@ -1,18 +1,46 @@export const GOOGLE_PROVIDER_ID = "google";+export const MICROSOFT_PROVIDER_ID = "microsoft";++export const MAILBOX_PROVIDER_IDS = [+ GOOGLE_PROVIDER_ID,+ MICROSOFT_PROVIDER_ID,+] as const;++export type MailboxProviderId = (typeof MAILBOX_PROVIDER_IDS)[number];export const IDENTITY_SCOPES = ["openid", "email", "profile"] as const;export const GMAIL_SCOPE = "https://www.googleapis.com/auth/gmail.readonly";export const CALENDAR_SCOPE ="https://www.googleapis.com/auth/calendar.readonly";+export const OUTLOOK_MAIL_SCOPE = "Mail.Read";export const SYNC_SCOPES = [GMAIL_SCOPE, CALENDAR_SCOPE] as const;+export const MICROSOFT_SYNC_SCOPES = [OUTLOOK_MAIL_SCOPE] as const;++export const SYNC_SCOPES_FOR: Record<MailboxProviderId, readonly string[]> = {+ [GOOGLE_PROVIDER_ID]: SYNC_SCOPES,+ [MICROSOFT_PROVIDER_ID]: MICROSOFT_SYNC_SCOPES,+};export const REQUIRED_SCOPES = [...IDENTITY_SCOPES, ...SYNC_SCOPES] as const;-export function hasSyncScopes(scope: string | null | undefined): boolean {+const GRAPH_SCOPE_PREFIX = "https://graph.microsoft.com/";++export function isMailboxProvider(+ providerId: string,+): providerId is MailboxProviderId {+ return (MAILBOX_PROVIDER_IDS as readonly string[]).includes(providerId);+}++export function hasSyncScopes(+ providerId: string,+ scope: string | null | undefined,+): boolean {+ if (!isMailboxProvider(providerId)) return false;+const granted = parseScopes(scope);- return SYNC_SCOPES.every((needed) => granted.has(needed));+ return SYNC_SCOPES_FOR[providerId].every((needed) => granted.has(needed));}export type SignInAccount = {@@ -20,17 +48,49 @@ export type SignInAccount = {scope?: string | null;};-export function signsInWithGoogle(accounts: readonly SignInAccount[]): boolean {+export function signsInOnlyWith(+ accounts: readonly SignInAccount[],+ providerId: string,+): boolean {return (accounts.length > 0 &&- accounts.every((account) => account.providerId === GOOGLE_PROVIDER_ID)+ accounts.every((account) => account.providerId === providerId));}-export function needsGoogleGrant(accounts: readonly SignInAccount[]): boolean {- if (!signsInWithGoogle(accounts)) return false;+export function signsInWithGoogle(accounts: readonly SignInAccount[]): boolean {+ return signsInOnlyWith(accounts, GOOGLE_PROVIDER_ID);+}++export function signsInWithMicrosoft(+ accounts: readonly SignInAccount[],+): boolean {+ return signsInOnlyWith(accounts, MICROSOFT_PROVIDER_ID);+}++export function mailboxGrantsNeeded(+ accounts: readonly SignInAccount[],+): MailboxProviderId[] {+ const everyRowIsAMailbox =+ accounts.length > 0 &&+ accounts.every((account) => isMailboxProvider(account.providerId));++ if (!everyRowIsAMailbox) return [];++ const granted = accounts.some((account) =>+ hasSyncScopes(account.providerId, account.scope),+ );+ if (granted) return [];++ return [+ ...new Set(+ accounts.map((account) => account.providerId).filter(isMailboxProvider),+ ),+ ];+}- return !accounts.some((account) => hasSyncScopes(account.scope));+export function needsMailboxGrant(accounts: readonly SignInAccount[]): boolean {+ return mailboxGrantsNeeded(accounts).length > 0;}export function parseScopes(scope: string | null | undefined): Set<string> {@@ -38,6 +98,11 @@ export function parseScopes(scope: string | null | undefined): Set<string> {(scope ?? "").split(/[,\s]+/).map((entry) => entry.trim())- .filter(Boolean),+ .filter(Boolean)+ .map((entry) =>+ entry.startsWith(GRAPH_SCOPE_PREFIX)+ ? entry.slice(GRAPH_SCOPE_PREFIX.length)+ : entry,+ ),);}diff --git a/packages/auth/test/google-grant.spec.ts b/packages/auth/test/google-grant.spec.tsdeleted file mode 100644index 76cad1a8..00000000--- a/packages/auth/test/google-grant.spec.ts+++ /dev/null@@ -1,77 +0,0 @@-import { describe, expect, it } from "bun:test";-import {- needsGoogleGrant,- SYNC_SCOPES,- signsInWithGoogle,-} from "../src/scopes";--const GRANTED = SYNC_SCOPES.join(",");--describe("who has to grant Gmail and Calendar", () => {- it("walls someone who signed in with Google and granted neither scope", () => {- expect(- needsGoogleGrant([{ providerId: "google", scope: "openid,email" }]),- ).toBe(true);- });-- it("walls someone whose granular consent dropped one of them", () => {- expect(- needsGoogleGrant([- { providerId: "google", scope: `openid,${SYNC_SCOPES[0]}` },- ]),- ).toBe(true);- });-- it("lets a Google account through once both scopes are there", () => {- expect(needsGoogleGrant([{ providerId: "google", scope: GRANTED }])).toBe(- false,- );- });-- it("never walls someone who signed in through their own IdP", () => {- expect(needsGoogleGrant([{ providerId: "okta", scope: null }])).toBe(false);- });-- it("never walls an SSO rep who has linked Google and then revoked it", () => {- expect(- needsGoogleGrant([- { providerId: "okta", scope: null },- { providerId: "google", scope: null },- ]),- ).toBe(false);- });-- it("still lets an SSO rep with Gmail connected through", () => {- expect(- needsGoogleGrant([- { providerId: "okta", scope: null },- { providerId: "google", scope: GRANTED },- ]),- ).toBe(false);- });-- it("does not wall an account with no sign-in rows at all", () => {- expect(needsGoogleGrant([])).toBe(false);- });-});--describe("whether revoking Google costs someone the CRM", () => {- it("is true when Google is the only way in", () => {- expect(signsInWithGoogle([{ providerId: "google", scope: GRANTED }])).toBe(- true,- );- });-- it("is false once an IdP is also on the account", () => {- expect(- signsInWithGoogle([- { providerId: "okta", scope: null },- { providerId: "google", scope: GRANTED },- ]),- ).toBe(false);- });-- it("is false for an account with nothing linked", () => {- expect(signsInWithGoogle([])).toBe(false);- });-});diff --git a/packages/auth/test/mailbox-grant.spec.ts b/packages/auth/test/mailbox-grant.spec.tsnew file mode 100644index 00000000..7e3bc546--- /dev/null+++ b/packages/auth/test/mailbox-grant.spec.ts@@ -0,0 +1,148 @@+import { describe, expect, it } from "bun:test";+import {+ MICROSOFT_SYNC_SCOPES,+ mailboxGrantsNeeded,+ needsMailboxGrant,+ SYNC_SCOPES,+ signsInWithGoogle,+ signsInWithMicrosoft,+} from "../src/scopes";++const GRANTED = SYNC_SCOPES.join(",");+const GRANTED_MICROSOFT = MICROSOFT_SYNC_SCOPES.join(",");++describe("who has to grant a mailbox", () => {+ it("walls someone who signed in with Google and granted neither scope", () => {+ expect(+ needsMailboxGrant([{ providerId: "google", scope: "openid,email" }]),+ ).toBe(true);+ });++ it("walls someone whose granular consent dropped one of them", () => {+ expect(+ needsMailboxGrant([+ { providerId: "google", scope: `openid,${SYNC_SCOPES[0]}` },+ ]),+ ).toBe(true);+ });++ it("lets a Google account through once both scopes are there", () => {+ expect(needsMailboxGrant([{ providerId: "google", scope: GRANTED }])).toBe(+ false,+ );+ });++ it("walls someone who signed in with Microsoft and did not grant Mail.Read", () => {+ expect(+ needsMailboxGrant([+ { providerId: "microsoft", scope: "openid profile email User.Read" },+ ]),+ ).toBe(true);+ });++ it("lets a Microsoft account through once Mail.Read is there", () => {+ expect(+ needsMailboxGrant([+ { providerId: "microsoft", scope: GRANTED_MICROSOFT },+ ]),+ ).toBe(false);+ });++ it("never walls someone who signed in through their own IdP", () => {+ expect(needsMailboxGrant([{ providerId: "okta", scope: null }])).toBe(+ false,+ );+ });++ it("never walls an SSO rep who has linked Google and then revoked it", () => {+ expect(+ needsMailboxGrant([+ { providerId: "okta", scope: null },+ { providerId: "google", scope: null },+ ]),+ ).toBe(false);+ });++ it("still lets an SSO rep with Gmail connected through", () => {+ expect(+ needsMailboxGrant([+ { providerId: "okta", scope: null },+ { providerId: "google", scope: GRANTED },+ ]),+ ).toBe(false);+ });++ it("does not wall an account with no sign-in rows at all", () => {+ expect(needsMailboxGrant([])).toBe(false);+ });++ it("asks for nothing more once one of two mailboxes is granted", () => {+ expect(+ needsMailboxGrant([+ { providerId: "google", scope: GRANTED },+ { providerId: "microsoft", scope: null },+ ]),+ ).toBe(false);+ });+});++describe("which provider the grant page should offer", () => {+ it("offers the one they signed in with", () => {+ expect(+ mailboxGrantsNeeded([{ providerId: "microsoft", scope: null }]),+ ).toEqual(["microsoft"]);+ });++ it("offers both when neither has been granted", () => {+ expect(+ mailboxGrantsNeeded([+ { providerId: "google", scope: null },+ { providerId: "microsoft", scope: null },+ ]),+ ).toEqual(["google", "microsoft"]);+ });++ it("offers nothing to an IdP rep", () => {+ expect(mailboxGrantsNeeded([{ providerId: "okta", scope: null }])).toEqual(+ [],+ );+ });+});++describe("whether revoking a provider costs someone the CRM", () => {+ it("is true when Google is the only way in", () => {+ expect(signsInWithGoogle([{ providerId: "google", scope: GRANTED }])).toBe(+ true,+ );+ });++ it("is true when Microsoft is the only way in", () => {+ expect(+ signsInWithMicrosoft([+ { providerId: "microsoft", scope: GRANTED_MICROSOFT },+ ]),+ ).toBe(true);+ });++ it("is false once an IdP is also on the account", () => {+ expect(+ signsInWithGoogle([+ { providerId: "okta", scope: null },+ { providerId: "google", scope: GRANTED },+ ]),+ ).toBe(false);+ });++ it("is false for Google once Microsoft is also a way in", () => {+ expect(+ signsInWithGoogle([+ { providerId: "google", scope: GRANTED },+ { providerId: "microsoft", scope: GRANTED_MICROSOFT },+ ]),+ ).toBe(false);+ });++ it("is false for an account with nothing linked", () => {+ expect(signsInWithGoogle([])).toBe(false);+ });+});diff --git a/packages/db/prisma/migrations/20260807120000_outlook_mail/migration.sql b/packages/db/prisma/migrations/20260807120000_outlook_mail/migration.sqlnew file mode 100644index 00000000..7f6b674a--- /dev/null+++ b/packages/db/prisma/migrations/20260807120000_outlook_mail/migration.sql@@ -0,0 +1,3 @@+-- AlterTable+ALTER TABLE "emailMessage" ADD COLUMN "outlookMessageId" TEXT;+ALTER TABLE "emailMessage" ADD COLUMN "outlookWebLink" TEXT;diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prismaindex 13b6579d..7034d2c1 100644--- a/packages/db/prisma/schema.prisma+++ b/packages/db/prisma/schema.prisma@@ -653,9 +653,11 @@ model EmailMessage {threadId Stringthread EmailThread @relation(fields: [threadId], references: [id], onDelete: Cascade)- rfcMessageId String @unique- syncedByUserId String?- gmailMessageId String?+ rfcMessageId String @unique+ syncedByUserId String?+ gmailMessageId String?+ outlookMessageId String?+ outlookWebLink String?direction EmailDirectionfromEmail Stringdiff --git a/packages/telemetry/src/allowlist.ts b/packages/telemetry/src/allowlist.tsindex 5f2e64ec..c58791b1 100644--- a/packages/telemetry/src/allowlist.ts+++ b/packages/telemetry/src/allowlist.ts@@ -106,13 +106,16 @@ export function permitted(}export const AGENT_TOOLS = [+ "archive_field","enrich_company","fetch_contact_photo","find_contact_socials","get_contact_work_history","get_linkedin_profile","identify_contact",+ "list_fields","list_outstanding_work",+ "manage_fields","read_company_history","read_crm_history","read_deal_history",@@ -124,6 +127,7 @@ export const AGENT_TOOLS = ["schedule_recheck","search_crm","set_contact_socials",+ "set_field_value","write_brief","write_workspace_profile",] as const;@@ -214,7 +218,9 @@ export function permittedTaskKind(kind: string | null | undefined): string {return kind && TASK_KIND_SET.has(kind) ? kind : OTHER;}-export const SYNC_SOURCES = ["gmail", "calendar"] as const;+export const SYNC_SOURCES = ["gmail", "calendar", "outlook"] as const;++export type TelemetrySyncSource = (typeof SYNC_SOURCES)[number];const SYNC_SOURCE_SET = new Set<string>(SYNC_SOURCES);@@ -222,6 +228,24 @@ export function permittedSyncSource(source: string | null | undefined): string {return source && SYNC_SOURCE_SET.has(source) ? source : OTHER;}+const MAILBOX_SYNC = "mailbox_sync";++const SYNC_ERROR_SOURCES: Record<TelemetrySyncSource, string> = {+ gmail: "google_sync",+ calendar: "google_sync",+ outlook: "microsoft_sync",+};++export function permittedSyncErrorSource(+ source: string | null | undefined,+): string {+ const name = permittedSyncSource(source);++ return name === OTHER+ ? MAILBOX_SYNC+ : SYNC_ERROR_SOURCES[name as TelemetrySyncSource];+}+const ROUTE_SHAPE = /^\/[A-Za-z0-9/_:.*-]*$/;const ROUTE_MAX = 120;diff --git a/packages/telemetry/src/events.ts b/packages/telemetry/src/events.tsindex 390193d1..75f213f3 100644--- a/packages/telemetry/src/events.ts+++ b/packages/telemetry/src/events.ts@@ -3,6 +3,7 @@ import {permittedErrorClass,permittedModelId,permittedRoute,+ permittedSyncErrorSource,permittedSyncSource,permittedTaskKind,permittedTool,@@ -80,7 +81,7 @@ export function agentError(input: {export function syncError(input: { error: unknown; source: string }): void {capture(SYNC_ERROR, {error_class: permittedErrorClass(input.error),- error_source: "google_sync",+ error_source: permittedSyncErrorSource(input.source),sync_source: permittedSyncSource(input.source),});}diff --git a/packages/ui/src/components/brand-logos/microsoft.tsx b/packages/ui/src/components/brand-logos/microsoft.tsxnew file mode 100644index 00000000..3917721b--- /dev/null+++ b/packages/ui/src/components/brand-logos/microsoft.tsx@@ -0,0 +1,18 @@+import type * as React from "react";++const MicrosoftLogo = (props: React.SVGProps<SVGSVGElement>) => (+ <svg+ viewBox="0 0 23 23"+ xmlns="http://www.w3.org/2000/svg"+ preserveAspectRatio="xMidYMid"+ aria-hidden="true"+ {...props}+ >+ <path d="M0 0h11v11H0z" fill="#f25022" />+ <path d="M12 0h11v11H12z" fill="#7fba00" />+ <path d="M0 12h11v11H0z" fill="#00a4ef" />+ <path d="M12 12h11v11H12z" fill="#ffb900" />+ </svg>+);++export default MicrosoftLogo;diff --git a/turbo.json b/turbo.jsonindex 9f3884ee..7b99d88b 100644--- a/turbo.json+++ b/turbo.json@@ -12,9 +12,13 @@* build, so the browser bundle was compiled with the localhost fallback and* every request from a deployed page went to the reader's own machine; and* `DATABASE_URL` was stripped from the agent build, where `eve build`- * evaluates the authored modules and `@crm/db` throws without it.+ * evaluates the authored modules and `@crm/db` throws without it. The+ * `MICROSOFT_*` trio was the same omission again: undeclared, so+ * `microsoftCredentials()` read nothing, `isMicrosoftConfigured()` was+ * false, and the sign-in page dropped the Microsoft button with no error+ * anywhere. A configured `MICROSOFT_TENANT_ID` was ignored the same way.*- * `globalPassThroughEnv` is the right list for both, because none of these+ * `globalPassThroughEnv` is the right list for all of them, because none* change what a build *produces* — they are read at runtime, or inlined from* the separately-declared public copy — so they must not become part of the* cache key. A secret in the cache key means a cache miss every rotation and@@ -28,6 +32,9 @@"ALLOWED_SIGN_IN","GOOGLE_CLIENT_ID","GOOGLE_CLIENT_SECRET",+ "MICROSOFT_CLIENT_ID",+ "MICROSOFT_CLIENT_SECRET",+ "MICROSOFT_TENANT_ID","AUTH_COOKIE_DOMAIN","CRON_SECRET","REDIS_URL",