Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 389c10fe92 | |||
| d2291e3a4a | |||
| fbf3527085 | |||
| 5f717c4467 |
@@ -119,8 +119,6 @@ jobs:
|
|||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
|
|
||||||
- name: Build all packages
|
- name: Build all packages
|
||||||
env:
|
|
||||||
VITE_API_URL: ""
|
|
||||||
run: pnpm build
|
run: pnpm build
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
@@ -342,7 +340,7 @@ jobs:
|
|||||||
name: Update Infra Image Tags
|
name: Update Infra Image Tags
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [docker]
|
needs: [docker]
|
||||||
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') && github.event_name == 'push'
|
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
@@ -375,7 +373,7 @@ jobs:
|
|||||||
echo "Updating dev overlay image tags to: $TAG"
|
echo "Updating dev overlay image tags to: $TAG"
|
||||||
echo "Updating migration/seed Job names with SHA: $SHORT_SHA"
|
echo "Updating migration/seed Job names with SHA: $SHORT_SHA"
|
||||||
cd /tmp/infra
|
cd /tmp/infra
|
||||||
DEV_KUST="apps/overlays/dev/kustomization.yaml"
|
DEV_KUST="apps/groombook/overlays/dev/kustomization.yaml"
|
||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/api")).newTag = env(TAG)' "$DEV_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/api")).newTag = env(TAG)' "$DEV_KUST"
|
||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/web")).newTag = env(TAG)' "$DEV_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/web")).newTag = env(TAG)' "$DEV_KUST"
|
||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/migrate")).newTag = env(TAG)' "$DEV_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/migrate")).newTag = env(TAG)' "$DEV_KUST"
|
||||||
@@ -383,7 +381,7 @@ jobs:
|
|||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/reset")).newTag = env(TAG)' "$DEV_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/reset")).newTag = env(TAG)' "$DEV_KUST"
|
||||||
|
|
||||||
# Update migrate Job name to include short SHA (immutable template fix)
|
# Update migrate Job name to include short SHA (immutable template fix)
|
||||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||||
if [ -f "$MIGRATE_JOB" ]; then
|
if [ -f "$MIGRATE_JOB" ]; then
|
||||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
||||||
@@ -392,7 +390,7 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Update seed Job name to include short SHA (immutable template fix)
|
# Update seed Job name to include short SHA (immutable template fix)
|
||||||
SEED_JOB="apps/base/seed-job.yaml"
|
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||||
if [ -f "$SEED_JOB" ]; then
|
if [ -f "$SEED_JOB" ]; then
|
||||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
||||||
@@ -415,7 +413,7 @@ jobs:
|
|||||||
git config user.name "groombook-engineer[bot]"
|
git config user.name "groombook-engineer[bot]"
|
||||||
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||||
git checkout -b "chore/update-image-tags-${TAG}"
|
git checkout -b "chore/update-image-tags-${TAG}"
|
||||||
git add apps/overlays/dev/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
git add apps/groombook/overlays/dev/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||||
git commit -m "chore: update image tags and migration/seed Job names to ${TAG}"
|
git commit -m "chore: update image tags and migration/seed Job names to ${TAG}"
|
||||||
|
|
||||||
git push -u origin "chore/update-image-tags-${TAG}"
|
git push -u origin "chore/update-image-tags-${TAG}"
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ jobs:
|
|||||||
TAG: ${{ inputs.tag }}
|
TAG: ${{ inputs.tag }}
|
||||||
run: |
|
run: |
|
||||||
cd /tmp/infra
|
cd /tmp/infra
|
||||||
PROD_KUST="apps/overlays/prod/kustomization.yaml"
|
PROD_KUST="apps/groombook/overlays/prod/kustomization.yaml"
|
||||||
|
|
||||||
SHORT_SHA="${TAG##*-}"
|
SHORT_SHA="${TAG##*-}"
|
||||||
export SHORT_SHA
|
export SHORT_SHA
|
||||||
@@ -70,14 +70,14 @@ jobs:
|
|||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/seed")).newTag = env(TAG)' "$PROD_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/seed")).newTag = env(TAG)' "$PROD_KUST"
|
||||||
|
|
||||||
# Update migrate Job name to include short SHA (immutable template fix)
|
# Update migrate Job name to include short SHA (immutable template fix)
|
||||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||||
if [ -f "$MIGRATE_JOB" ]; then
|
if [ -f "$MIGRATE_JOB" ]; then
|
||||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Update seed Job name to include short SHA (immutable template fix)
|
# Update seed Job name to include short SHA (immutable template fix)
|
||||||
SEED_JOB="apps/base/seed-job.yaml"
|
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||||
if [ -f "$SEED_JOB" ]; then
|
if [ -f "$SEED_JOB" ]; then
|
||||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
||||||
@@ -94,7 +94,7 @@ jobs:
|
|||||||
git config user.name "groombook-engineer[bot]"
|
git config user.name "groombook-engineer[bot]"
|
||||||
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||||
git checkout -b "release/promote-prod-${TAG}"
|
git checkout -b "release/promote-prod-${TAG}"
|
||||||
git add apps/overlays/prod/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
git add apps/groombook/overlays/prod/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||||
git commit -m "release: promote ${TAG} to production"
|
git commit -m "release: promote ${TAG} to production"
|
||||||
git push -u origin "release/promote-prod-${TAG}"
|
git push -u origin "release/promote-prod-${TAG}"
|
||||||
gh pr create \
|
gh pr create \
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo "Updating UAT overlay image tags to: $TAG"
|
echo "Updating UAT overlay image tags to: $TAG"
|
||||||
cd /tmp/infra
|
cd /tmp/infra
|
||||||
UAT_KUST="apps/overlays/uat/kustomization.yaml"
|
UAT_KUST="apps/groombook/overlays/uat/kustomization.yaml"
|
||||||
|
|
||||||
if [ ! -f "$UAT_KUST" ]; then
|
if [ ! -f "$UAT_KUST" ]; then
|
||||||
echo "ERROR: UAT overlay not found at $UAT_KUST. Ensure GRO-427 has been completed."
|
echo "ERROR: UAT overlay not found at $UAT_KUST. Ensure GRO-427 has been completed."
|
||||||
@@ -55,7 +55,7 @@ jobs:
|
|||||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/seed")).newTag = env(TAG)' "$UAT_KUST"
|
yq -i '(.images[] | select(.name == "ghcr.io/groombook/seed")).newTag = env(TAG)' "$UAT_KUST"
|
||||||
|
|
||||||
# Update migrate Job name to include short SHA (immutable template fix)
|
# Update migrate Job name to include short SHA (immutable template fix)
|
||||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||||
if [ -f "$MIGRATE_JOB" ]; then
|
if [ -f "$MIGRATE_JOB" ]; then
|
||||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
||||||
@@ -64,7 +64,7 @@ jobs:
|
|||||||
# Update seed Job name to include short SHA (immutable template fix)
|
# Update seed Job name to include short SHA (immutable template fix)
|
||||||
# NOTE: Do NOT update the image tag here — let the Kustomize images transformer
|
# NOTE: Do NOT update the image tag here — let the Kustomize images transformer
|
||||||
# in the UAT overlay handle it via newTag. This avoids the immutable template issue.
|
# in the UAT overlay handle it via newTag. This avoids the immutable template issue.
|
||||||
SEED_JOB="apps/base/seed-job.yaml"
|
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||||
if [ -f "$SEED_JOB" ]; then
|
if [ -f "$SEED_JOB" ]; then
|
||||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
||||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
||||||
@@ -81,7 +81,7 @@ jobs:
|
|||||||
git config user.name "groombook-engineer[bot]"
|
git config user.name "groombook-engineer[bot]"
|
||||||
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||||
git checkout -b "chore/update-uat-image-tags-${TAG}"
|
git checkout -b "chore/update-uat-image-tags-${TAG}"
|
||||||
git add apps/overlays/uat/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
git add apps/groombook/overlays/uat/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||||
git commit -m "chore: promote ${TAG} to UAT"
|
git commit -m "chore: promote ${TAG} to UAT"
|
||||||
|
|
||||||
git push -u origin "chore/update-uat-image-tags-${TAG}"
|
git push -u origin "chore/update-uat-image-tags-${TAG}"
|
||||||
|
|||||||
@@ -217,17 +217,6 @@ GroomBook is an open-source, self-hostable pet grooming business management & CR
|
|||||||
| TC-APP-4.19.3 | Empty states | 1. Navigate to pages with no data (empty calendar, no clients)<br>2. Verify UI | Helpful empty state message with call-to-action displayed |
|
| TC-APP-4.19.3 | Empty states | 1. Navigate to pages with no data (empty calendar, no clients)<br>2. Verify UI | Helpful empty state message with call-to-action displayed |
|
||||||
| TC-APP-4.19.4 | Network error handling | 1. Disable network in DevTools<br>2. Attempt actions that require API calls<br>3. Re-enable network | Appropriate error message shown, app recovers when network restored |
|
| TC-APP-4.19.4 | Network error handling | 1. Disable network in DevTools<br>2. Attempt actions that require API calls<br>3. Re-enable network | Appropriate error message shown, app recovers when network restored |
|
||||||
|
|
||||||
### 4.20 Staff Messages
|
|
||||||
|
|
||||||
| # | Scenario | Steps | Expected |
|
|
||||||
|---|----------|-------|----------|
|
|
||||||
| TC-APP-4.20.1 | Staff messages inbox loads | 1. Log in as Staff<br>2. Navigate to Messages | Conversation list renders with client phone and last message preview |
|
|
||||||
| TC-APP-4.20.2 | Open conversation | 1. Select a conversation from the list | Full message thread loads chronologically |
|
|
||||||
| TC-APP-4.20.3 | Send message | 1. Type a reply and submit | Message appears in thread; POST /api/conversations/:id/messages succeeds |
|
|
||||||
| TC-APP-4.20.4 | Empty state | 1. Log in as Staff with no conversations | Empty state shown; no crash |
|
|
||||||
| TC-APP-4.20.5 | Unread indicator | 1. Client sends a new message | Thread marked unread until staff views it |
|
|
||||||
| TC-APP-4.20.6 | Cross-tenant isolation | 1. Staff from Business A attempts to read Business B conversations | 403 or empty response returned |
|
|
||||||
|
|
||||||
## 5. Pass/Fail Criteria
|
## 5. Pass/Fail Criteria
|
||||||
|
|
||||||
**Pass:** All test cases execute without errors. Expected results match actual results. No regressions are observed. All functionality works as documented.
|
**Pass:** All test cases execute without errors. Expected results match actual results. No regressions are observed. All functionality works as documented.
|
||||||
|
|||||||
@@ -1,318 +0,0 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
||||||
import { Hono } from "hono";
|
|
||||||
|
|
||||||
// ─── Mock data ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
const STAFF_ROW = {
|
|
||||||
id: "staff-uuid-1",
|
|
||||||
email: "groomer@groombook.com",
|
|
||||||
name: "Groomer",
|
|
||||||
role: "groomer" as const,
|
|
||||||
businessId: "business-uuid-1",
|
|
||||||
active: true,
|
|
||||||
userId: null,
|
|
||||||
oidcSub: null,
|
|
||||||
isSuperUser: false,
|
|
||||||
icalToken: null,
|
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
const BUSINESS_SETTINGS = {
|
|
||||||
id: "business-uuid-1",
|
|
||||||
businessName: "Test Salon",
|
|
||||||
};
|
|
||||||
|
|
||||||
const CONV_1 = {
|
|
||||||
id: "conv-uuid-1",
|
|
||||||
businessId: "business-uuid-1",
|
|
||||||
clientId: "client-uuid-1",
|
|
||||||
channel: "sms",
|
|
||||||
externalNumber: "+15551111111",
|
|
||||||
businessNumber: "+15552222222",
|
|
||||||
lastMessageAt: new Date("2025-01-10T10:00:00Z"),
|
|
||||||
status: "active",
|
|
||||||
createdAt: new Date("2025-01-01T00:00:00Z"),
|
|
||||||
updatedAt: new Date("2025-01-10T10:00:00Z"),
|
|
||||||
staffReadAt: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const MSG_INBOUND_1 = {
|
|
||||||
id: "msg-uuid-1",
|
|
||||||
conversationId: "conv-uuid-1",
|
|
||||||
direction: "inbound",
|
|
||||||
body: "Hello",
|
|
||||||
status: "delivered",
|
|
||||||
sentByStaffId: null,
|
|
||||||
createdAt: new Date("2025-01-10T09:00:00Z"),
|
|
||||||
deliveredAt: new Date("2025-01-10T09:01:00Z"),
|
|
||||||
};
|
|
||||||
|
|
||||||
const MSG_OUTBOUND_1 = {
|
|
||||||
id: "msg-uuid-2",
|
|
||||||
conversationId: "conv-uuid-1",
|
|
||||||
direction: "outbound",
|
|
||||||
body: "Hi Alice!",
|
|
||||||
status: "delivered",
|
|
||||||
sentByStaffId: "staff-uuid-1",
|
|
||||||
createdAt: new Date("2025-01-10T10:00:00Z"),
|
|
||||||
deliveredAt: new Date("2025-01-10T10:01:00Z"),
|
|
||||||
};
|
|
||||||
|
|
||||||
// ─── Queue-based mock DB ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
let selectRows: Record<string, unknown>[] = [];
|
|
||||||
let selectRows2: Record<string, unknown>[] = [];
|
|
||||||
let selectRows3: Record<string, unknown>[] = [];
|
|
||||||
let updatedValues: Record<string, unknown>[] = [];
|
|
||||||
let selectCallCount = 0;
|
|
||||||
|
|
||||||
function resetMock() {
|
|
||||||
selectRows = [];
|
|
||||||
selectRows2 = [];
|
|
||||||
selectRows3 = [];
|
|
||||||
updatedValues = [];
|
|
||||||
selectCallCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetAll() {
|
|
||||||
resetMock();
|
|
||||||
vi.clearAllMocks();
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockSendMessage = vi.hoisted(() => vi.fn());
|
|
||||||
|
|
||||||
vi.mock("@groombook/db", () => {
|
|
||||||
function makeChainable(data: unknown[]): unknown {
|
|
||||||
const arr = [...data];
|
|
||||||
const chain = new Proxy(arr, {
|
|
||||||
get(target, prop) {
|
|
||||||
if (prop === "where" || prop === "orderBy" || prop === "limit" || prop === "innerJoin") {
|
|
||||||
return () => chain;
|
|
||||||
}
|
|
||||||
if (prop === "from") {
|
|
||||||
return (table: unknown) => {
|
|
||||||
const tableName = (table as { _name?: string })._name;
|
|
||||||
const rows = tableName === "businessSettings" ? [BUSINESS_SETTINGS] : selectRows;
|
|
||||||
return makeChainable(rows);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// @ts-expect-error proxy
|
|
||||||
return target[prop];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return chain;
|
|
||||||
}
|
|
||||||
|
|
||||||
const conversations = new Proxy(
|
|
||||||
{ _name: "conversations" },
|
|
||||||
{ get: (t, p) => (p === "_name" ? "conversations" : { table: "conversations", column: p }) }
|
|
||||||
);
|
|
||||||
|
|
||||||
const messages = new Proxy(
|
|
||||||
{ _name: "messages" },
|
|
||||||
{ get: (t, p) => (p === "_name" ? "messages" : { table: "messages", column: p }) }
|
|
||||||
);
|
|
||||||
|
|
||||||
const clients = new Proxy(
|
|
||||||
{ _name: "clients" },
|
|
||||||
{ get: (t, p) => (p === "_name" ? "clients" : { table: "clients", column: p }) }
|
|
||||||
);
|
|
||||||
|
|
||||||
const businessSettings = new Proxy(
|
|
||||||
{ _name: "businessSettings" },
|
|
||||||
{ get: (t, p) => (p === "_name" ? "businessSettings" : { table: "businessSettings", column: p }) }
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
getDb: () => ({
|
|
||||||
select: () => ({
|
|
||||||
from: (table: unknown) => {
|
|
||||||
const tableName = (table as { _name?: string })._name;
|
|
||||||
if (tableName === "businessSettings") return makeChainable([BUSINESS_SETTINGS]);
|
|
||||||
if (tableName === "messages") {
|
|
||||||
// Return selectRows3 if it has data (POST re-query), else cycle through selectRows/selectRows2
|
|
||||||
if (selectRows3.length > 0) {
|
|
||||||
return makeChainable(selectRows3);
|
|
||||||
}
|
|
||||||
if (selectCallCount === 0 || selectCallCount === 1) {
|
|
||||||
const rows = selectCallCount === 0 ? selectRows : selectRows2;
|
|
||||||
selectCallCount++;
|
|
||||||
return makeChainable(rows);
|
|
||||||
}
|
|
||||||
return makeChainable(selectRows);
|
|
||||||
}
|
|
||||||
return makeChainable(selectRows);
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
update: () => ({
|
|
||||||
set: (vals: Record<string, unknown>) => ({
|
|
||||||
where: () => {
|
|
||||||
updatedValues.push(vals);
|
|
||||||
return { returning: () => [vals] };
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
insert: () => ({
|
|
||||||
values: (vals: Record<string, unknown>) => {
|
|
||||||
return { returning: () => [{ ...vals, id: "msg-uuid-new" }] };
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
conversations,
|
|
||||||
messages,
|
|
||||||
clients,
|
|
||||||
businessSettings,
|
|
||||||
eq: vi.fn((a, b) => ({ type: "eq", a, b })),
|
|
||||||
and: vi.fn((...args) => ({ type: "and", args })),
|
|
||||||
desc: vi.fn((col) => ({ type: "desc", col })),
|
|
||||||
lt: vi.fn((a, b) => ({ type: "lt", a, b })),
|
|
||||||
sql: vi.fn(() => ({ __type: "sql" })),
|
|
||||||
isNull: vi.fn((col) => ({ type: "isNull", col })),
|
|
||||||
count: vi.fn((col) => ({ type: "count", col })),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../services/messaging/outbound.js", () => ({
|
|
||||||
sendMessage: mockSendMessage,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// ─── App setup ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
const { conversationsRouter } = await import("../routes/conversations.js");
|
|
||||||
|
|
||||||
const app = new Hono();
|
|
||||||
app.use("*", async (c, next) => {
|
|
||||||
// @ts-expect-error — test-only context injection
|
|
||||||
c.set("staff", STAFF_ROW);
|
|
||||||
await next();
|
|
||||||
});
|
|
||||||
app.route("/conversations", conversationsRouter);
|
|
||||||
|
|
||||||
function jsonRequest(method: string, path: string, body?: unknown) {
|
|
||||||
return app.request(path, {
|
|
||||||
method,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(() => resetAll());
|
|
||||||
|
|
||||||
// ─── GET /conversations ───────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe("GET /api/conversations", () => {
|
|
||||||
it("returns conversations sorted by recency with unread count", async () => {
|
|
||||||
selectRows = [
|
|
||||||
{ ...CONV_1, clientName: "Alice", clientPhone: "+15551111111", channel: "sms" },
|
|
||||||
];
|
|
||||||
selectRows2 = [{ count: "1" }];
|
|
||||||
const res = await app.request("/conversations");
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
const body = await res.json();
|
|
||||||
expect(body.items).toHaveLength(1);
|
|
||||||
expect(body.items[0]!.id).toBe("conv-uuid-1");
|
|
||||||
expect(body.items[0]!.clientName).toBe("Alice");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("supports cursor-based pagination", async () => {
|
|
||||||
selectRows = [];
|
|
||||||
const res = await app.request("/conversations?cursor=conv-uuid-1&limit=1");
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("enforces max limit of 50", async () => {
|
|
||||||
selectRows = [];
|
|
||||||
const res = await app.request("/conversations?limit=200");
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ─── GET /conversations/:id/messages ─────────────────────────────────────────
|
|
||||||
|
|
||||||
describe("GET /api/conversations/:id/messages", () => {
|
|
||||||
it("returns paginated messages and marks conversation as read", async () => {
|
|
||||||
selectRows = [{ ...MSG_INBOUND_1 }, { ...MSG_OUTBOUND_1 }];
|
|
||||||
const res = await app.request("/conversations/conv-uuid-1/messages");
|
|
||||||
expect(res.status).toBe(200);
|
|
||||||
const body = await res.json();
|
|
||||||
expect(body.items).toHaveLength(2);
|
|
||||||
expect(body.items[0]!.id).toBe("msg-uuid-1");
|
|
||||||
expect(updatedValues.some((u) => u.staffReadAt !== undefined)).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns 404 when conversation belongs to different business", async () => {
|
|
||||||
selectRows = [];
|
|
||||||
const res = await app.request("/conversations/conv-uuid-other/messages");
|
|
||||||
expect(res.status).toBe(404);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns 401 when not authenticated", async () => {
|
|
||||||
const appNoAuth = new Hono();
|
|
||||||
appNoAuth.route("/conversations", conversationsRouter);
|
|
||||||
const res = await appNoAuth.request("/conversations/conv-uuid-1/messages");
|
|
||||||
expect(res.status).toBe(401);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ─── POST /conversations/:id/messages ─────────────────────────────────────────
|
|
||||||
|
|
||||||
describe("POST /api/conversations/:id/messages", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
resetMock();
|
|
||||||
vi.clearAllMocks();
|
|
||||||
selectRows = [{ ...CONV_1, clientName: "Alice", clientPhone: "+15551111111", channel: "sms" }];
|
|
||||||
selectRows2 = [];
|
|
||||||
selectRows3 = [{ id: "msg-uuid-new", conversationId: "conv-uuid-1", direction: "outbound" as const, body: "Hello Alice!", status: "queued" as const, sentByStaffId: "staff-uuid-1", createdAt: new Date(), deliveredAt: null }];
|
|
||||||
updatedValues = [];
|
|
||||||
});
|
|
||||||
|
|
||||||
it("sends via outbound service and returns 201", async () => {
|
|
||||||
mockSendMessage.mockResolvedValueOnce({
|
|
||||||
messageId: "msg-uuid-new",
|
|
||||||
providerMessageId: "provider-msg-1",
|
|
||||||
status: "queued",
|
|
||||||
suppressed: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const res = await jsonRequest("POST", "/conversations/conv-uuid-1/messages", {
|
|
||||||
body: "Hello Alice!",
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(201);
|
|
||||||
const body = await res.json();
|
|
||||||
expect(body.id).toBe("msg-uuid-new");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns 409 when client opted out", async () => {
|
|
||||||
mockSendMessage.mockResolvedValueOnce({ suppressed: true });
|
|
||||||
|
|
||||||
const res = await jsonRequest("POST", "/conversations/conv-uuid-1/messages", {
|
|
||||||
body: "Hello",
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(409);
|
|
||||||
const body = await res.json();
|
|
||||||
expect(body.error).toMatch(/opted out/i);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns 404 for cross-tenant conversation", async () => {
|
|
||||||
selectRows = [];
|
|
||||||
const res = await jsonRequest("POST", "/conversations/conv-uuid-other/messages", {
|
|
||||||
body: "Hello",
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(404);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects empty body", async () => {
|
|
||||||
const res = await jsonRequest("POST", "/conversations/conv-uuid-1/messages", {
|
|
||||||
body: "",
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects body over 1600 chars", async () => {
|
|
||||||
const res = await jsonRequest("POST", "/conversations/conv-uuid-1/messages", {
|
|
||||||
body: "a".repeat(1601),
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -78,7 +78,7 @@ vi.mock("@groombook/db", () => {
|
|||||||
{ get: (t, p) => (p === "_name" ? "appointments" : { table: "appointments", column: p }) }
|
{ get: (t, p) => (p === "_name" ? "appointments" : { table: "appointments", column: p }) }
|
||||||
);
|
);
|
||||||
|
|
||||||
const businessSettings = new Proxy(
|
const businessSettings = new Proxy(
|
||||||
{ _name: "businessSettings" },
|
{ _name: "businessSettings" },
|
||||||
{ get: (t, p) => (p === "_name" ? "businessSettings" : { table: "businessSettings", column: p }) }
|
{ get: (t, p) => (p === "_name" ? "businessSettings" : { table: "businessSettings", column: p }) }
|
||||||
);
|
);
|
||||||
@@ -134,11 +134,6 @@ const businessSettings = new Proxy(
|
|||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
insert: () => ({
|
|
||||||
values: () => ({
|
|
||||||
returning: () => [],
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
impersonationSessions,
|
impersonationSessions,
|
||||||
appointments,
|
appointments,
|
||||||
@@ -148,6 +143,7 @@ const businessSettings = new Proxy(
|
|||||||
messages,
|
messages,
|
||||||
eq: vi.fn(),
|
eq: vi.fn(),
|
||||||
and: vi.fn(),
|
and: vi.fn(),
|
||||||
|
lt: vi.fn(),
|
||||||
desc: vi.fn((col: unknown) => ({ _name: "desc", col })),
|
desc: vi.fn((col: unknown) => ({ _name: "desc", col })),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -275,7 +275,6 @@ api.route("/admin/settings", settingsRouter);
|
|||||||
api.route("/admin/auth-provider", authProviderRouter);
|
api.route("/admin/auth-provider", authProviderRouter);
|
||||||
api.route("/admin/seed", adminSeedRouter);
|
api.route("/admin/seed", adminSeedRouter);
|
||||||
api.route("/search", searchRouter);
|
api.route("/search", searchRouter);
|
||||||
api.route("/conversations", conversationsRouter);
|
|
||||||
|
|
||||||
const port = Number(process.env.PORT ?? 3000);
|
const port = Number(process.env.PORT ?? 3000);
|
||||||
await initAuth();
|
await initAuth();
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ if (process.env.AUTH_DISABLED === "true") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const authMiddleware: MiddlewareHandler = async (c, next) => {
|
export const authMiddleware: MiddlewareHandler = async (c, next) => {
|
||||||
const path = c.req.path;
|
if (c.req.path.startsWith("/api/auth/")) {
|
||||||
if (path.startsWith("/api/auth/") || path.startsWith("/api/webhooks/")) {
|
|
||||||
await next();
|
await next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,273 +1,214 @@
|
|||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { z } from "zod/v3";
|
import { z } from "zod/v3";
|
||||||
import {
|
import { and, eq, desc, lt, isNull, sql, count } from "@groombook/db";
|
||||||
and,
|
import { getDb, conversations, messages, clients } from "@groombook/db";
|
||||||
eq,
|
import { resolveStaffMiddleware } from "../middleware/rbac.js";
|
||||||
desc,
|
|
||||||
lt,
|
|
||||||
sql,
|
|
||||||
getDb,
|
|
||||||
conversations,
|
|
||||||
messages,
|
|
||||||
clients,
|
|
||||||
businessSettings,
|
|
||||||
} from "@groombook/db";
|
|
||||||
import type { AppEnv } from "../middleware/rbac.js";
|
import type { AppEnv } from "../middleware/rbac.js";
|
||||||
import { sendMessage } from "../services/messaging/outbound.js";
|
|
||||||
|
|
||||||
export const conversationsRouter = new Hono<AppEnv>();
|
export const conversationsRouter = new Hono<AppEnv>();
|
||||||
|
|
||||||
const sendMessageSchema = z.object({
|
conversationsRouter.use("/*", resolveStaffMiddleware);
|
||||||
body: z.string().min(1).max(1600),
|
|
||||||
});
|
|
||||||
|
|
||||||
// GET /api/conversations — List conversations
|
// GET /api/conversations — list all conversations for staff's business
|
||||||
conversationsRouter.get("/", async (c) => {
|
conversationsRouter.get("/", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const staffRow = c.get("staff");
|
const businessId = c.get("staff").businessId;
|
||||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const [settings] = await db
|
const rows = await db
|
||||||
.select({ id: businessSettings.id })
|
|
||||||
.from(businessSettings)
|
|
||||||
.limit(1);
|
|
||||||
if (!settings) return c.json({ error: "Business not found" }, 404);
|
|
||||||
|
|
||||||
const cursor = c.req.query("cursor") || undefined;
|
|
||||||
const limit = Math.min(Number(c.req.query("limit") || "20"), 50);
|
|
||||||
|
|
||||||
let baseQuery = db
|
|
||||||
.select({
|
.select({
|
||||||
id: conversations.id,
|
id: conversations.id,
|
||||||
|
businessId: conversations.businessId,
|
||||||
clientId: conversations.clientId,
|
clientId: conversations.clientId,
|
||||||
|
channel: conversations.channel,
|
||||||
|
externalNumber: conversations.externalNumber,
|
||||||
|
businessNumber: conversations.businessNumber,
|
||||||
lastMessageAt: conversations.lastMessageAt,
|
lastMessageAt: conversations.lastMessageAt,
|
||||||
status: conversations.status,
|
status: conversations.status,
|
||||||
|
createdAt: conversations.createdAt,
|
||||||
staffReadAt: conversations.staffReadAt,
|
staffReadAt: conversations.staffReadAt,
|
||||||
clientName: clients.name,
|
|
||||||
clientPhone: clients.phone,
|
|
||||||
channel: conversations.channel,
|
|
||||||
})
|
})
|
||||||
.from(conversations)
|
.from(conversations)
|
||||||
.innerJoin(clients, eq(conversations.clientId, clients.id))
|
.where(eq(conversations.businessId, businessId))
|
||||||
.where(eq(conversations.businessId, settings.id))
|
|
||||||
.orderBy(desc(conversations.lastMessageAt))
|
.orderBy(desc(conversations.lastMessageAt))
|
||||||
.limit(limit + 1);
|
.limit(20);
|
||||||
|
|
||||||
if (cursor) {
|
// For each conversation, fetch client name and count unread messages
|
||||||
const [cursorRow] = await db
|
const enriched = await Promise.all(
|
||||||
.select({ lastMessageAt: conversations.lastMessageAt })
|
|
||||||
.from(conversations)
|
|
||||||
.where(eq(conversations.id, cursor))
|
|
||||||
.limit(1);
|
|
||||||
if (cursorRow?.lastMessageAt) {
|
|
||||||
baseQuery = db
|
|
||||||
.select({
|
|
||||||
id: conversations.id,
|
|
||||||
clientId: conversations.clientId,
|
|
||||||
lastMessageAt: conversations.lastMessageAt,
|
|
||||||
status: conversations.status,
|
|
||||||
staffReadAt: conversations.staffReadAt,
|
|
||||||
clientName: clients.name,
|
|
||||||
clientPhone: clients.phone,
|
|
||||||
channel: conversations.channel,
|
|
||||||
})
|
|
||||||
.from(conversations)
|
|
||||||
.innerJoin(clients, eq(conversations.clientId, clients.id))
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(conversations.businessId, settings.id),
|
|
||||||
lt(conversations.lastMessageAt, cursorRow.lastMessageAt)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orderBy(desc(conversations.lastMessageAt))
|
|
||||||
.limit(limit + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const rows = await baseQuery;
|
|
||||||
|
|
||||||
const hasMore = rows.length > limit;
|
|
||||||
if (hasMore) rows.pop();
|
|
||||||
|
|
||||||
const items = await Promise.all(
|
|
||||||
rows.map(async (row) => {
|
rows.map(async (row) => {
|
||||||
const [unreadRow] = await db
|
const [client] = await db
|
||||||
.select({ count: sql<number>`count(*)` })
|
.select({ name: clients.name })
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.id, row.clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
// Count messages where direction = 'inbound' AND readByClientAt IS NULL
|
||||||
|
const [{ count: unreadCount }] = await db
|
||||||
|
.select({ count: count() })
|
||||||
.from(messages)
|
.from(messages)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(messages.conversationId, row.id),
|
eq(messages.conversationId, row.id),
|
||||||
eq(messages.direction, "inbound"),
|
eq(messages.direction, "inbound"),
|
||||||
sql`${messages.createdAt} > COALESCE(${row.staffReadAt}, '1970-01-01'::timestamp)`
|
isNull(messages.readByClientAt)
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
|
// Fetch last message body for preview
|
||||||
const [lastMsg] = await db
|
const [lastMsg] = await db
|
||||||
.select({
|
.select({ body: messages.body, createdAt: messages.createdAt })
|
||||||
body: messages.body,
|
|
||||||
direction: messages.direction,
|
|
||||||
createdAt: messages.createdAt,
|
|
||||||
})
|
|
||||||
.from(messages)
|
.from(messages)
|
||||||
.where(eq(messages.conversationId, row.id))
|
.where(eq(messages.conversationId, row.id))
|
||||||
.orderBy(desc(messages.createdAt))
|
.orderBy(desc(messages.createdAt))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: row.id,
|
...row,
|
||||||
clientId: row.clientId,
|
clientName: client?.name ?? "Unknown",
|
||||||
clientName: row.clientName,
|
lastMessageBody: lastMsg?.body ?? null,
|
||||||
clientPhone: row.clientPhone,
|
unreadCount: Number(unreadCount),
|
||||||
channel: row.channel,
|
|
||||||
lastMessageAt: row.lastMessageAt,
|
|
||||||
status: row.status,
|
|
||||||
unreadCount: Number(unreadRow?.count ?? 0),
|
|
||||||
lastMessage: lastMsg ?? null,
|
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const lastRow = rows[rows.length - 1];
|
return c.json(enriched);
|
||||||
const nextCursor = hasMore && lastRow ? lastRow.id : null;
|
|
||||||
return c.json({ items, nextCursor });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET /api/conversations/:id/messages — List messages for a conversation
|
// GET /api/conversations/:id — get a single conversation
|
||||||
|
conversationsRouter.get("/:id", async (c) => {
|
||||||
|
const db = getDb();
|
||||||
|
const businessId = c.get("staff").businessId;
|
||||||
|
const conversationId = c.req.param("id");
|
||||||
|
|
||||||
|
const [row] = await db
|
||||||
|
.select()
|
||||||
|
.from(conversations)
|
||||||
|
.where(and(eq(conversations.id, conversationId), eq(conversations.businessId, businessId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!row) {
|
||||||
|
return c.json({ error: "Not found" }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [client] = await db
|
||||||
|
.select({ name: clients.name })
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.id, row.clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
return c.json({ ...row, clientName: client?.name ?? "Unknown" });
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /api/conversations/:id/messages — get messages for a conversation
|
||||||
conversationsRouter.get("/:id/messages", async (c) => {
|
conversationsRouter.get("/:id/messages", async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const staffRow = c.get("staff");
|
const businessId = c.get("staff").businessId;
|
||||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const conversationId = c.req.param("id");
|
const conversationId = c.req.param("id");
|
||||||
const cursor = c.req.query("cursor") || undefined;
|
const limit = parseInt(c.req.query("limit") ?? "50", 10);
|
||||||
const limit = Math.min(Number(c.req.query("limit") || "50"), 100);
|
const cursor = c.req.query("cursor");
|
||||||
|
|
||||||
const [settings] = await db
|
// Verify staff owns this conversation
|
||||||
.select({ id: businessSettings.id })
|
const [conversation] = await db
|
||||||
.from(businessSettings)
|
|
||||||
.limit(1);
|
|
||||||
if (!settings) return c.json({ error: "Business not found" }, 404);
|
|
||||||
|
|
||||||
const [conv] = await db
|
|
||||||
.select({ id: conversations.id })
|
.select({ id: conversations.id })
|
||||||
.from(conversations)
|
.from(conversations)
|
||||||
.where(
|
.where(and(eq(conversations.id, conversationId), eq(conversations.businessId, businessId)))
|
||||||
and(eq(conversations.id, conversationId), eq(conversations.businessId, settings.id))
|
|
||||||
)
|
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (!conv) return c.json({ error: "Not found" }, 404);
|
|
||||||
|
|
||||||
|
if (!conversation) {
|
||||||
|
return c.json({ error: "Not found" }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark conversation as read by staff
|
||||||
await db
|
await db
|
||||||
.update(conversations)
|
.update(conversations)
|
||||||
.set({ staffReadAt: new Date() })
|
.set({ staffReadAt: new Date() })
|
||||||
.where(eq(conversations.id, conversationId));
|
.where(eq(conversations.id, conversationId));
|
||||||
|
|
||||||
let query = db
|
|
||||||
.select({
|
|
||||||
id: messages.id,
|
|
||||||
direction: messages.direction,
|
|
||||||
body: messages.body,
|
|
||||||
status: messages.status,
|
|
||||||
sentByStaffId: messages.sentByStaffId,
|
|
||||||
createdAt: messages.createdAt,
|
|
||||||
deliveredAt: messages.deliveredAt,
|
|
||||||
})
|
|
||||||
.from(messages)
|
|
||||||
.where(eq(messages.conversationId, conversationId))
|
|
||||||
.orderBy(desc(messages.createdAt))
|
|
||||||
.limit(limit + 1);
|
|
||||||
|
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
const [cursorRow] = await db
|
const [cursorMsg] = await db
|
||||||
.select({ createdAt: messages.createdAt })
|
.select({ createdAt: messages.createdAt })
|
||||||
.from(messages)
|
.from(messages)
|
||||||
.where(eq(messages.id, cursor))
|
.where(eq(messages.id, cursor))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (cursorRow?.createdAt) {
|
|
||||||
query = db
|
if (cursorMsg) {
|
||||||
.select({
|
const rows = await db
|
||||||
id: messages.id,
|
.select()
|
||||||
direction: messages.direction,
|
|
||||||
body: messages.body,
|
|
||||||
status: messages.status,
|
|
||||||
sentByStaffId: messages.sentByStaffId,
|
|
||||||
createdAt: messages.createdAt,
|
|
||||||
deliveredAt: messages.deliveredAt,
|
|
||||||
})
|
|
||||||
.from(messages)
|
.from(messages)
|
||||||
.where(
|
.where(and(eq(messages.conversationId, conversationId), lt(messages.createdAt, cursorMsg.createdAt)))
|
||||||
and(
|
|
||||||
eq(messages.conversationId, conversationId),
|
|
||||||
lt(messages.createdAt, cursorRow.createdAt)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orderBy(desc(messages.createdAt))
|
.orderBy(desc(messages.createdAt))
|
||||||
.limit(limit + 1);
|
.limit(limit);
|
||||||
|
|
||||||
|
return c.json({ messages: rows.reverse(), nextCursor: rows.length === limit ? rows[0]?.id : null });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const rows = await query;
|
const rows = await db
|
||||||
const hasMore = rows.length > limit;
|
.select()
|
||||||
if (hasMore) rows.pop();
|
.from(messages)
|
||||||
|
.where(eq(messages.conversationId, conversationId))
|
||||||
|
.orderBy(desc(messages.createdAt))
|
||||||
|
.limit(limit);
|
||||||
|
|
||||||
const lastRow = rows[rows.length - 1];
|
return c.json({ messages: rows.reverse(), nextCursor: null });
|
||||||
const nextCursor = hasMore && lastRow ? lastRow.id : null;
|
});
|
||||||
return c.json({ items: rows, nextCursor });
|
|
||||||
|
// POST /api/conversations/:id/messages — send a message
|
||||||
|
const sendMessageSchema = z.object({
|
||||||
|
body: z.string().min(1).max(1600),
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /api/conversations/:id/messages — Send a message
|
|
||||||
conversationsRouter.post(
|
conversationsRouter.post(
|
||||||
"/:id/messages",
|
"/:id/messages",
|
||||||
zValidator("json", sendMessageSchema),
|
zValidator("json", sendMessageSchema),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
|
const businessId = c.get("staff").businessId;
|
||||||
const staffRow = c.get("staff");
|
const staffRow = c.get("staff");
|
||||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
|
||||||
|
|
||||||
const conversationId = c.req.param("id");
|
const conversationId = c.req.param("id");
|
||||||
const { body } = c.req.valid("json");
|
const { body } = c.req.valid("json");
|
||||||
|
|
||||||
const [settings] = await db
|
// Verify staff owns this conversation
|
||||||
.select({ id: businessSettings.id })
|
const [conversation] = await db
|
||||||
.from(businessSettings)
|
.select()
|
||||||
.limit(1);
|
|
||||||
if (!settings) return c.json({ error: "Business not found" }, 404);
|
|
||||||
|
|
||||||
const [conv] = await db
|
|
||||||
.select({ id: conversations.id, clientId: conversations.clientId })
|
|
||||||
.from(conversations)
|
.from(conversations)
|
||||||
.where(
|
.where(and(eq(conversations.id, conversationId), eq(conversations.businessId, businessId)))
|
||||||
and(eq(conversations.id, conversationId), eq(conversations.businessId, settings.id))
|
|
||||||
)
|
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (!conv) return c.json({ error: "Not found" }, 404);
|
|
||||||
|
|
||||||
const result = await sendMessage({
|
if (!conversation) {
|
||||||
businessId: settings.id,
|
return c.json({ error: "Not found" }, 404);
|
||||||
clientId: conv.clientId,
|
}
|
||||||
body,
|
|
||||||
sentByStaffId: staffRow.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.suppressed) {
|
// Check if client has opted out
|
||||||
|
const [client] = await db
|
||||||
|
.select({ optedOutAt: clients.optedOutAt })
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.id, conversation.clientId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (client?.optedOutAt) {
|
||||||
return c.json({ error: "Client has opted out of SMS" }, 409);
|
return c.json({ error: "Client has opted out of SMS" }, 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create outbound message
|
||||||
const [msg] = await db
|
const [msg] = await db
|
||||||
.select({
|
.insert(messages)
|
||||||
id: messages.id,
|
.values({
|
||||||
direction: messages.direction,
|
conversationId,
|
||||||
body: messages.body,
|
direction: "outbound",
|
||||||
status: messages.status,
|
body,
|
||||||
sentByStaffId: messages.sentByStaffId,
|
status: "queued",
|
||||||
createdAt: messages.createdAt,
|
sentByStaffId: staffRow.id,
|
||||||
deliveredAt: messages.deliveredAt,
|
|
||||||
})
|
})
|
||||||
.from(messages)
|
.returning();
|
||||||
.where(eq(messages.id, result.messageId))
|
|
||||||
.limit(1);
|
// Update conversation lastMessageAt
|
||||||
|
await db
|
||||||
|
.update(conversations)
|
||||||
|
.set({ lastMessageAt: new Date() })
|
||||||
|
.where(eq(conversations.id, conversationId));
|
||||||
|
|
||||||
|
// TODO: Enqueue Telnyx outbound job
|
||||||
|
|
||||||
return c.json(msg, 201);
|
return c.json(msg, 201);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
VITE_API_URL=https://uat.groombook.dev
|
VITE_API_URL=
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ RUN pnpm install --frozen-lockfile
|
|||||||
|
|
||||||
# Build
|
# Build
|
||||||
FROM deps AS builder
|
FROM deps AS builder
|
||||||
ARG VITE_API_URL=
|
|
||||||
ENV VITE_API_URL=
|
|
||||||
COPY packages/types/ packages/types/
|
COPY packages/types/ packages/types/
|
||||||
COPY apps/web/ apps/web/
|
COPY apps/web/ apps/web/
|
||||||
RUN pnpm --filter @groombook/web build
|
RUN pnpm --filter @groombook/web build
|
||||||
|
|||||||
@@ -40,10 +40,7 @@ function LoginPage() {
|
|||||||
const handleSocialLogin = async (provider: string) => {
|
const handleSocialLogin = async (provider: string) => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
// Use /admin as callback URL so Better-Auth redirects to the app's dashboard
|
const result = await signIn.social({ provider, callbackURL: window.location.origin });
|
||||||
// after the OAuth callback completes, rather than back to /login
|
|
||||||
const callbackURL = `${window.location.origin}/admin`;
|
|
||||||
const result = await signIn.social({ provider, callbackURL });
|
|
||||||
if (result?.error) {
|
if (result?.error) {
|
||||||
setError(result.error.message ?? "Sign-in failed");
|
setError(result.error.message ?? "Sign-in failed");
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ const mockConversations = [
|
|||||||
clientId: "client-1",
|
clientId: "client-1",
|
||||||
clientName: "Alice Smith",
|
clientName: "Alice Smith",
|
||||||
channel: "sms",
|
channel: "sms",
|
||||||
clientPhone: "+1234567890",
|
externalNumber: "+1234567890",
|
||||||
lastMessageAt: "2026-05-14T10:00:00Z",
|
lastMessageAt: "2026-05-14T10:00:00Z",
|
||||||
lastMessage: { body: "Hello, is my dog ready?", direction: "inbound", createdAt: "2026-05-14T10:00:00Z" },
|
staffReadAt: null,
|
||||||
|
lastMessageBody: "Hello, is my dog ready?",
|
||||||
unreadCount: 2,
|
unreadCount: 2,
|
||||||
status: "active",
|
status: "active",
|
||||||
},
|
},
|
||||||
@@ -19,9 +20,10 @@ const mockConversations = [
|
|||||||
clientId: "client-2",
|
clientId: "client-2",
|
||||||
clientName: "Bob Jones",
|
clientName: "Bob Jones",
|
||||||
channel: "sms",
|
channel: "sms",
|
||||||
clientPhone: "+1987654321",
|
externalNumber: "+1987654321",
|
||||||
lastMessageAt: "2026-05-13T08:00:00Z",
|
lastMessageAt: "2026-05-13T08:00:00Z",
|
||||||
lastMessage: { body: "Thanks for the update", direction: "outbound", createdAt: "2026-05-13T08:05:00Z" },
|
staffReadAt: "2026-05-13T09:00:00Z",
|
||||||
|
lastMessageBody: "Thanks for the update",
|
||||||
unreadCount: 0,
|
unreadCount: 0,
|
||||||
status: "active",
|
status: "active",
|
||||||
},
|
},
|
||||||
@@ -71,7 +73,7 @@ afterEach(() => {
|
|||||||
|
|
||||||
describe("MessagesPage", () => {
|
describe("MessagesPage", () => {
|
||||||
it("renders empty state when no conversations", async () => {
|
it("renders empty state when no conversations", async () => {
|
||||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse({ items: [], nextCursor: null }));
|
vi.mocked(global.fetch).mockResolvedValue(makeResponse([]));
|
||||||
|
|
||||||
render(<MessagesPage />);
|
render(<MessagesPage />);
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
@@ -80,7 +82,7 @@ describe("MessagesPage", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("renders conversation list", async () => {
|
it("renders conversation list", async () => {
|
||||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse({ items: mockConversations, nextCursor: null }));
|
vi.mocked(global.fetch).mockResolvedValue(makeResponse(mockConversations));
|
||||||
|
|
||||||
render(<MessagesPage />);
|
render(<MessagesPage />);
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
@@ -96,10 +98,10 @@ describe("MessagesPage", () => {
|
|||||||
vi.mocked(global.fetch).mockImplementation((input) => {
|
vi.mocked(global.fetch).mockImplementation((input) => {
|
||||||
const url = String(input);
|
const url = String(input);
|
||||||
if (url === "/api/conversations?limit=20") {
|
if (url === "/api/conversations?limit=20") {
|
||||||
return Promise.resolve(makeResponse({ items: mockConversations, nextCursor: null }));
|
return Promise.resolve(makeResponse(mockConversations));
|
||||||
}
|
}
|
||||||
if (url === "/api/conversations/conv-1/messages?limit=50") {
|
if (url === "/api/conversations/conv-1/messages?limit=50") {
|
||||||
return Promise.resolve(makeResponse({ items: mockMessages, nextCursor: null }));
|
return Promise.resolve(makeResponse({ messages: mockMessages }));
|
||||||
}
|
}
|
||||||
return Promise.resolve(makeResponseWithStatus(null, 404));
|
return Promise.resolve(makeResponseWithStatus(null, 404));
|
||||||
});
|
});
|
||||||
@@ -110,7 +112,7 @@ describe("MessagesPage", () => {
|
|||||||
fireEvent.click(screen.getByText("Alice Smith"));
|
fireEvent.click(screen.getByText("Alice Smith"));
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(screen.getAllByText("Hello, is my dog ready?").length).toBeGreaterThanOrEqual(1);
|
expect(screen.getByText("Hello, is my dog ready?")).toBeInTheDocument();
|
||||||
expect(screen.getByText("Yes, she is all done!")).toBeInTheDocument();
|
expect(screen.getByText("Yes, she is all done!")).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -130,7 +132,7 @@ describe("MessagesPage", () => {
|
|||||||
sentByStaffId: "staff-1",
|
sentByStaffId: "staff-1",
|
||||||
}, 201));
|
}, 201));
|
||||||
}
|
}
|
||||||
return Promise.resolve(makeResponse({ items: mockConversations, nextCursor: null }));
|
return Promise.resolve(makeResponse(mockConversations));
|
||||||
});
|
});
|
||||||
|
|
||||||
render(<MessagesPage />);
|
render(<MessagesPage />);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createAuthClient } from "better-auth/react";
|
import { createAuthClient } from "better-auth/react";
|
||||||
|
|
||||||
export const authClient = createAuthClient({
|
export const authClient = createAuthClient({
|
||||||
baseURL: import.meta.env.VITE_API_URL || window.location.origin,
|
baseURL: import.meta.env.VITE_API_URL ?? "",
|
||||||
});
|
});
|
||||||
|
|
||||||
export const { signIn, signOut, useSession, changePassword } = authClient;
|
export const { signIn, signOut, useSession, changePassword } = authClient;
|
||||||
@@ -5,11 +5,12 @@ interface Conversation {
|
|||||||
clientId: string;
|
clientId: string;
|
||||||
clientName: string;
|
clientName: string;
|
||||||
channel: string;
|
channel: string;
|
||||||
clientPhone: string;
|
externalNumber: string;
|
||||||
lastMessageAt: string | null;
|
lastMessageAt: string | null;
|
||||||
|
staffReadAt: string | null;
|
||||||
|
lastMessageBody: string | null;
|
||||||
unreadCount: number;
|
unreadCount: number;
|
||||||
status: string;
|
status: string;
|
||||||
lastMessage: { body: string | null; direction: string; createdAt: string } | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Message {
|
interface Message {
|
||||||
@@ -54,8 +55,7 @@ export function MessagesPage() {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch("/api/conversations?limit=20");
|
const res = await fetch("/api/conversations?limit=20");
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||||
const json = await res.json();
|
const data = (await res.json()) as Conversation[];
|
||||||
const data = json.items as Conversation[];
|
|
||||||
setConversations(data);
|
setConversations(data);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
setError(e instanceof Error ? e.message : "Failed to load conversations");
|
setError(e instanceof Error ? e.message : "Failed to load conversations");
|
||||||
@@ -68,8 +68,8 @@ export function MessagesPage() {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/conversations/${conversationId}/messages?limit=50`);
|
const res = await fetch(`/api/conversations/${conversationId}/messages?limit=50`);
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||||
const json = await res.json();
|
const data = (await res.json()) as { messages: Message[] };
|
||||||
setMessages((json.items as Message[]).reverse());
|
setMessages(data.messages);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
setMessageError(e instanceof Error ? e.message : "Failed to load messages");
|
setMessageError(e instanceof Error ? e.message : "Failed to load messages");
|
||||||
} finally {
|
} finally {
|
||||||
@@ -93,7 +93,7 @@ export function MessagesPage() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (messages.length > 0) {
|
if (messages.length > 0) {
|
||||||
messagesEndRef.current?.scrollIntoView?.({ behavior: "smooth" });
|
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||||
}
|
}
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
@@ -180,7 +180,7 @@ export function MessagesPage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div style={{ marginTop: 2, color: "#6b7280", fontSize: 12 }}>
|
<div style={{ marginTop: 2, color: "#6b7280", fontSize: 12 }}>
|
||||||
{truncate(conv.lastMessage?.body ?? null, 60)}
|
{truncate(conv.lastMessageBody, 60)}
|
||||||
</div>
|
</div>
|
||||||
<div style={{ marginTop: 2, color: "#9ca3af", fontSize: 11 }}>
|
<div style={{ marginTop: 2, color: "#9ca3af", fontSize: 11 }}>
|
||||||
{relativeTime(conv.lastMessageAt)}
|
{relativeTime(conv.lastMessageAt)}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ interface MessageThreadProps {
|
|||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function MessageThread({ sessionId, readOnly: _readOnly }: MessageThreadProps) {
|
function MessageThread({ sessionId, readOnly }: MessageThreadProps) {
|
||||||
const [businessName, setBusinessName] = useState<string>("Business");
|
const [businessName, setBusinessName] = useState<string>("Business");
|
||||||
|
|
||||||
const { conversation, loading: convLoading, error: convError } = useConversation(sessionId);
|
const { conversation, loading: convLoading, error: convError } = useConversation(sessionId);
|
||||||
@@ -144,6 +144,7 @@ function MessageThread({ sessionId, readOnly: _readOnly }: MessageThreadProps) {
|
|||||||
) : (
|
) : (
|
||||||
messages.map((msg: ApiMessage) => {
|
messages.map((msg: ApiMessage) => {
|
||||||
const sender = msg.direction === "inbound" ? "customer" : "business";
|
const sender = msg.direction === "inbound" ? "customer" : "business";
|
||||||
|
const senderName = sender === "customer" ? "You" : businessName;
|
||||||
return (
|
return (
|
||||||
<div key={msg.id} className={`flex ${sender === "customer" ? "justify-end" : "justify-start"}`}>
|
<div key={msg.id} className={`flex ${sender === "customer" ? "justify-end" : "justify-start"}`}>
|
||||||
<div className={`max-w-[80%] rounded-2xl px-4 py-2.5 ${
|
<div className={`max-w-[80%] rounded-2xl px-4 py-2.5 ${
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
ALTER TABLE "conversations" ADD COLUMN "staff_read_at" timestamp;
|
|
||||||
@@ -222,8 +222,8 @@
|
|||||||
{
|
{
|
||||||
"idx": 32,
|
"idx": 32,
|
||||||
"version": "7",
|
"version": "7",
|
||||||
"when": 1778818472097,
|
"when": 1778818472097,
|
||||||
"tag": "0032_staff_read_at",
|
"tag": "0032_add_staff_read_at",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import * as schema from "./schema.js";
|
|||||||
|
|
||||||
export * from "./schema.js";
|
export * from "./schema.js";
|
||||||
export { encryptSecret, decryptSecret } from "./crypto.js";
|
export { encryptSecret, decryptSecret } from "./crypto.js";
|
||||||
export { and, asc, count, desc, eq, exists, gte, gt, ilike, inArray, isNull, lt, lte, ne, or, sql } from "drizzle-orm";
|
export { and, asc, desc, eq, exists, gte, gt, ilike, inArray, isNull, lt, lte, ne, or, sql } from "drizzle-orm";
|
||||||
|
|
||||||
let _db: ReturnType<typeof drizzle> | null = null;
|
let _db: ReturnType<typeof drizzle> | null = null;
|
||||||
|
|
||||||
|
|||||||
@@ -883,7 +883,6 @@ async function seed() {
|
|||||||
let appointmentCount = 0;
|
let appointmentCount = 0;
|
||||||
let invoiceCount = 0;
|
let invoiceCount = 0;
|
||||||
let visitLogCount = 0;
|
let visitLogCount = 0;
|
||||||
let paidInvoiceCounter = 0;
|
|
||||||
|
|
||||||
// Process in batches per client to keep memory manageable
|
// Process in batches per client to keep memory manageable
|
||||||
const apptBatchSize = 100;
|
const apptBatchSize = 100;
|
||||||
@@ -978,11 +977,8 @@ async function seed() {
|
|||||||
|
|
||||||
const invoiceStatus = rand() < 0.95 ? "paid" as const : "pending" as const;
|
const invoiceStatus = rand() < 0.95 ? "paid" as const : "pending" as const;
|
||||||
const paidAt = invoiceStatus === "paid" ? new Date(endTime.getTime() + randInt(5, 30) * 60 * 1000) : null;
|
const paidAt = invoiceStatus === "paid" ? new Date(endTime.getTime() + randInt(5, 30) * 60 * 1000) : null;
|
||||||
paidInvoiceCounter++;
|
|
||||||
const stripePaymentIntentId = invoiceStatus === "paid"
|
|
||||||
? `pi_test_seed_${String(paidInvoiceCounter).padStart(6, "0")}`
|
|
||||||
: null;
|
|
||||||
|
|
||||||
|
const stripePaymentIntentId = invoiceStatus === "paid" && rand() < 0.2 ? `pi_test_${uuid().replace(/-/g, "").slice(0, 24)}` : null;
|
||||||
invoiceBatch.push({
|
invoiceBatch.push({
|
||||||
id: invoiceId,
|
id: invoiceId,
|
||||||
appointmentId: apptId,
|
appointmentId: apptId,
|
||||||
@@ -1098,16 +1094,14 @@ async function seed() {
|
|||||||
const taxCents = Math.round(effectivePrice * 0.08);
|
const taxCents = Math.round(effectivePrice * 0.08);
|
||||||
const totalCents = effectivePrice + taxCents + tipCents;
|
const totalCents = effectivePrice + taxCents + tipCents;
|
||||||
const paidAt = new Date(endTime.getTime() + randInt(5, 30) * 60 * 1000);
|
const paidAt = new Date(endTime.getTime() + randInt(5, 30) * 60 * 1000);
|
||||||
paidInvoiceCounter++;
|
const stripePaymentIntentId = rand() < 0.2 ? `pi_test_${uuid().replace(/-/g, "").slice(0, 24)}` : null;
|
||||||
|
|
||||||
invoiceBatch.push({
|
invoiceBatch.push({
|
||||||
id: invoiceId, appointmentId: apptId, clientId,
|
id: invoiceId, appointmentId: apptId, clientId,
|
||||||
subtotalCents: effectivePrice, taxCents, tipCents, totalCents,
|
subtotalCents: effectivePrice, taxCents, tipCents, totalCents,
|
||||||
status: "paid" as const,
|
status: "paid" as const,
|
||||||
paymentMethod: pick(["cash", "card", "card", "card", "check"]) as "cash" | "card" | "check",
|
paymentMethod: pick(["cash", "card", "card", "card", "check"]) as "cash" | "card" | "check",
|
||||||
paidAt,
|
paidAt, stripePaymentIntentId, notes: null,
|
||||||
stripePaymentIntentId: `pi_test_seed_${String(paidInvoiceCounter).padStart(6, "0")}`,
|
|
||||||
notes: null,
|
|
||||||
});
|
});
|
||||||
lineItemBatch.push({
|
lineItemBatch.push({
|
||||||
id: uuid(), invoiceId, description: svc.name, quantity: 1,
|
id: uuid(), invoiceId, description: svc.name, quantity: 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user