Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc0259975b | |||
| f27110eb07 | |||
| d069eff7d6 | |||
| 3ed1e10ecb | |||
| 904cd9c1b9 | |||
| 573869e517 | |||
| b31cbce82e | |||
| 2398dabe3a | |||
| c2dd1dbf84 | |||
| 7339d51acf | |||
| 8eec29ad90 | |||
| 050d478621 | |||
| 795081cf10 | |||
| 8d5b71dc0f | |||
| c2d38bd3ee | |||
| 6a7229f330 | |||
| 9d9d7da13d | |||
| 2c29c5e4a9 | |||
| ba5f8a916d | |||
| e873f11e4f | |||
| aae11c0c4d | |||
| 537b5cb0b3 | |||
| d60200f8a7 | |||
| f150663047 | |||
| e605e1be74 | |||
| c4978be280 | |||
| f43e566dbd | |||
| 9c9568b80c | |||
| d0ba537b31 | |||
| a9b9a0a733 | |||
| e818bdef4e | |||
| dce9c96442 | |||
| f50d240e56 | |||
| 22135859c2 | |||
| a5115f5291 | |||
| e64538822d | |||
| 53ab415713 | |||
| a330e342e1 | |||
| 0f841e27fc | |||
| cd25d98384 | |||
| e9fceb78b3 | |||
| 0cae8adef8 | |||
| 674626ba1e | |||
| 903fbf55d5 | |||
| 7bf9cf9734 | |||
| bf159f8b1f | |||
| 2f3d4d8d01 | |||
| db9bb31702 | |||
| b38db65dde | |||
| 3178f81b99 | |||
| 544d65959d | |||
| f38bb244a4 | |||
| abee344ca4 |
@@ -119,6 +119,8 @@ jobs:
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Build all packages
|
||||
env:
|
||||
VITE_API_URL: ""
|
||||
run: pnpm build
|
||||
|
||||
docker:
|
||||
@@ -340,7 +342,7 @@ jobs:
|
||||
name: Update Infra Image Tags
|
||||
runs-on: ubuntu-latest
|
||||
needs: [docker]
|
||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') && github.event_name == 'push'
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
@@ -373,7 +375,7 @@ jobs:
|
||||
echo "Updating dev overlay image tags to: $TAG"
|
||||
echo "Updating migration/seed Job names with SHA: $SHORT_SHA"
|
||||
cd /tmp/infra
|
||||
DEV_KUST="apps/groombook/overlays/dev/kustomization.yaml"
|
||||
DEV_KUST="apps/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/web")).newTag = env(TAG)' "$DEV_KUST"
|
||||
yq -i '(.images[] | select(.name == "ghcr.io/groombook/migrate")).newTag = env(TAG)' "$DEV_KUST"
|
||||
@@ -381,7 +383,7 @@ jobs:
|
||||
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)
|
||||
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
||||
if [ -f "$MIGRATE_JOB" ]; then
|
||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
||||
@@ -390,7 +392,7 @@ jobs:
|
||||
fi
|
||||
|
||||
# Update seed Job name to include short SHA (immutable template fix)
|
||||
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||
SEED_JOB="apps/base/seed-job.yaml"
|
||||
if [ -f "$SEED_JOB" ]; then
|
||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$SEED_JOB"
|
||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$SEED_JOB"
|
||||
@@ -413,7 +415,7 @@ jobs:
|
||||
git config user.name "groombook-engineer[bot]"
|
||||
git config user.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||
git checkout -b "chore/update-image-tags-${TAG}"
|
||||
git add apps/groombook/overlays/dev/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||
git add apps/overlays/dev/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
||||
git commit -m "chore: update image tags and migration/seed Job names to ${TAG}"
|
||||
|
||||
git push -u origin "chore/update-image-tags-${TAG}"
|
||||
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
TAG: ${{ inputs.tag }}
|
||||
run: |
|
||||
cd /tmp/infra
|
||||
PROD_KUST="apps/groombook/overlays/prod/kustomization.yaml"
|
||||
PROD_KUST="apps/overlays/prod/kustomization.yaml"
|
||||
|
||||
SHORT_SHA="${TAG##*-}"
|
||||
export SHORT_SHA
|
||||
@@ -70,14 +70,14 @@ jobs:
|
||||
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)
|
||||
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
||||
if [ -f "$MIGRATE_JOB" ]; then
|
||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$MIGRATE_JOB"
|
||||
yq -i '.metadata.annotations."groombook.app/deploy-version" = env(TAG)' "$MIGRATE_JOB"
|
||||
fi
|
||||
|
||||
# Update seed Job name to include short SHA (immutable template fix)
|
||||
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||
SEED_JOB="apps/base/seed-job.yaml"
|
||||
if [ -f "$SEED_JOB" ]; then
|
||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$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.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||
git checkout -b "release/promote-prod-${TAG}"
|
||||
git add apps/groombook/overlays/prod/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||
git add apps/overlays/prod/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
||||
git commit -m "release: promote ${TAG} to production"
|
||||
git push -u origin "release/promote-prod-${TAG}"
|
||||
gh pr create \
|
||||
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
run: |
|
||||
echo "Updating UAT overlay image tags to: $TAG"
|
||||
cd /tmp/infra
|
||||
UAT_KUST="apps/groombook/overlays/uat/kustomization.yaml"
|
||||
UAT_KUST="apps/overlays/uat/kustomization.yaml"
|
||||
|
||||
if [ ! -f "$UAT_KUST" ]; then
|
||||
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"
|
||||
|
||||
# Update migrate Job name to include short SHA (immutable template fix)
|
||||
MIGRATE_JOB="apps/groombook/base/migrate-job.yaml"
|
||||
MIGRATE_JOB="apps/base/migrate-job.yaml"
|
||||
if [ -f "$MIGRATE_JOB" ]; then
|
||||
yq -i '.metadata.name = "migrate-schema-" + env(SHORT_SHA)' "$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)
|
||||
# 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.
|
||||
SEED_JOB="apps/groombook/base/seed-job.yaml"
|
||||
SEED_JOB="apps/base/seed-job.yaml"
|
||||
if [ -f "$SEED_JOB" ]; then
|
||||
yq -i '.metadata.name = "seed-test-data-" + env(SHORT_SHA)' "$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.email "3141748+groombook-engineer[bot]@users.noreply.github.com"
|
||||
git checkout -b "chore/update-uat-image-tags-${TAG}"
|
||||
git add apps/groombook/overlays/uat/ apps/groombook/base/migrate-job.yaml apps/groombook/base/seed-job.yaml
|
||||
git add apps/overlays/uat/ apps/base/migrate-job.yaml apps/base/seed-job.yaml
|
||||
git commit -m "chore: promote ${TAG} to UAT"
|
||||
|
||||
git push -u origin "chore/update-uat-image-tags-${TAG}"
|
||||
|
||||
@@ -217,6 +217,17 @@ 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.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
|
||||
|
||||
**Pass:** All test cases execute without errors. Expected results match actual results. No regressions are observed. All functionality works as documented.
|
||||
|
||||
@@ -0,0 +1,318 @@
|
||||
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 }) }
|
||||
);
|
||||
|
||||
const businessSettings = new Proxy(
|
||||
const businessSettings = new Proxy(
|
||||
{ _name: "businessSettings" },
|
||||
{ get: (t, p) => (p === "_name" ? "businessSettings" : { table: "businessSettings", column: p }) }
|
||||
);
|
||||
@@ -134,6 +134,11 @@ vi.mock("@groombook/db", () => {
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
insert: () => ({
|
||||
values: () => ({
|
||||
returning: () => [],
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
impersonationSessions,
|
||||
appointments,
|
||||
@@ -143,7 +148,6 @@ vi.mock("@groombook/db", () => {
|
||||
messages,
|
||||
eq: vi.fn(),
|
||||
and: vi.fn(),
|
||||
lt: vi.fn(),
|
||||
desc: vi.fn((col: unknown) => ({ _name: "desc", col })),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -275,6 +275,7 @@ api.route("/admin/settings", settingsRouter);
|
||||
api.route("/admin/auth-provider", authProviderRouter);
|
||||
api.route("/admin/seed", adminSeedRouter);
|
||||
api.route("/search", searchRouter);
|
||||
api.route("/conversations", conversationsRouter);
|
||||
|
||||
const port = Number(process.env.PORT ?? 3000);
|
||||
await initAuth();
|
||||
|
||||
@@ -23,7 +23,8 @@ if (process.env.AUTH_DISABLED === "true") {
|
||||
}
|
||||
|
||||
export const authMiddleware: MiddlewareHandler = async (c, next) => {
|
||||
if (c.req.path.startsWith("/api/auth/")) {
|
||||
const path = c.req.path;
|
||||
if (path.startsWith("/api/auth/") || path.startsWith("/api/webhooks/")) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,214 +1,273 @@
|
||||
import { Hono } from "hono";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { z } from "zod/v3";
|
||||
import { and, eq, desc, lt, isNull, sql, count } from "@groombook/db";
|
||||
import { getDb, conversations, messages, clients } from "@groombook/db";
|
||||
import { resolveStaffMiddleware } from "../middleware/rbac.js";
|
||||
import {
|
||||
and,
|
||||
eq,
|
||||
desc,
|
||||
lt,
|
||||
sql,
|
||||
getDb,
|
||||
conversations,
|
||||
messages,
|
||||
clients,
|
||||
businessSettings,
|
||||
} from "@groombook/db";
|
||||
import type { AppEnv } from "../middleware/rbac.js";
|
||||
import { sendMessage } from "../services/messaging/outbound.js";
|
||||
|
||||
export const conversationsRouter = new Hono<AppEnv>();
|
||||
|
||||
conversationsRouter.use("/*", resolveStaffMiddleware);
|
||||
const sendMessageSchema = z.object({
|
||||
body: z.string().min(1).max(1600),
|
||||
});
|
||||
|
||||
// GET /api/conversations — list all conversations for staff's business
|
||||
// GET /api/conversations — List conversations
|
||||
conversationsRouter.get("/", async (c) => {
|
||||
const db = getDb();
|
||||
const businessId = c.get("staff").businessId;
|
||||
const staffRow = c.get("staff");
|
||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
||||
|
||||
const rows = await db
|
||||
const [settings] = 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({
|
||||
id: conversations.id,
|
||||
businessId: conversations.businessId,
|
||||
clientId: conversations.clientId,
|
||||
channel: conversations.channel,
|
||||
externalNumber: conversations.externalNumber,
|
||||
businessNumber: conversations.businessNumber,
|
||||
lastMessageAt: conversations.lastMessageAt,
|
||||
status: conversations.status,
|
||||
createdAt: conversations.createdAt,
|
||||
staffReadAt: conversations.staffReadAt,
|
||||
clientName: clients.name,
|
||||
clientPhone: clients.phone,
|
||||
channel: conversations.channel,
|
||||
})
|
||||
.from(conversations)
|
||||
.where(eq(conversations.businessId, businessId))
|
||||
.innerJoin(clients, eq(conversations.clientId, clients.id))
|
||||
.where(eq(conversations.businessId, settings.id))
|
||||
.orderBy(desc(conversations.lastMessageAt))
|
||||
.limit(20);
|
||||
.limit(limit + 1);
|
||||
|
||||
// For each conversation, fetch client name and count unread messages
|
||||
const enriched = await Promise.all(
|
||||
if (cursor) {
|
||||
const [cursorRow] = await db
|
||||
.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) => {
|
||||
const [client] = await db
|
||||
.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() })
|
||||
const [unreadRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(messages)
|
||||
.where(
|
||||
and(
|
||||
eq(messages.conversationId, row.id),
|
||||
eq(messages.direction, "inbound"),
|
||||
isNull(messages.readByClientAt)
|
||||
sql`${messages.createdAt} > COALESCE(${row.staffReadAt}, '1970-01-01'::timestamp)`
|
||||
)
|
||||
);
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
// Fetch last message body for preview
|
||||
const [lastMsg] = await db
|
||||
.select({ body: messages.body, createdAt: messages.createdAt })
|
||||
.select({
|
||||
body: messages.body,
|
||||
direction: messages.direction,
|
||||
createdAt: messages.createdAt,
|
||||
})
|
||||
.from(messages)
|
||||
.where(eq(messages.conversationId, row.id))
|
||||
.orderBy(desc(messages.createdAt))
|
||||
.limit(1);
|
||||
|
||||
return {
|
||||
...row,
|
||||
clientName: client?.name ?? "Unknown",
|
||||
lastMessageBody: lastMsg?.body ?? null,
|
||||
unreadCount: Number(unreadCount),
|
||||
id: row.id,
|
||||
clientId: row.clientId,
|
||||
clientName: row.clientName,
|
||||
clientPhone: row.clientPhone,
|
||||
channel: row.channel,
|
||||
lastMessageAt: row.lastMessageAt,
|
||||
status: row.status,
|
||||
unreadCount: Number(unreadRow?.count ?? 0),
|
||||
lastMessage: lastMsg ?? null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return c.json(enriched);
|
||||
const lastRow = rows[rows.length - 1];
|
||||
const nextCursor = hasMore && lastRow ? lastRow.id : null;
|
||||
return c.json({ items, nextCursor });
|
||||
});
|
||||
|
||||
// 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
|
||||
// GET /api/conversations/:id/messages — List messages for a conversation
|
||||
conversationsRouter.get("/:id/messages", async (c) => {
|
||||
const db = getDb();
|
||||
const businessId = c.get("staff").businessId;
|
||||
const conversationId = c.req.param("id");
|
||||
const limit = parseInt(c.req.query("limit") ?? "50", 10);
|
||||
const cursor = c.req.query("cursor");
|
||||
const staffRow = c.get("staff");
|
||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
||||
|
||||
// Verify staff owns this conversation
|
||||
const [conversation] = await db
|
||||
const conversationId = c.req.param("id");
|
||||
const cursor = c.req.query("cursor") || undefined;
|
||||
const limit = Math.min(Number(c.req.query("limit") || "50"), 100);
|
||||
|
||||
const [settings] = await db
|
||||
.select({ id: businessSettings.id })
|
||||
.from(businessSettings)
|
||||
.limit(1);
|
||||
if (!settings) return c.json({ error: "Business not found" }, 404);
|
||||
|
||||
const [conv] = await db
|
||||
.select({ id: conversations.id })
|
||||
.from(conversations)
|
||||
.where(and(eq(conversations.id, conversationId), eq(conversations.businessId, businessId)))
|
||||
.where(
|
||||
and(eq(conversations.id, conversationId), eq(conversations.businessId, settings.id))
|
||||
)
|
||||
.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
|
||||
.update(conversations)
|
||||
.set({ staffReadAt: new Date() })
|
||||
.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) {
|
||||
const [cursorMsg] = await db
|
||||
const [cursorRow] = await db
|
||||
.select({ createdAt: messages.createdAt })
|
||||
.from(messages)
|
||||
.where(eq(messages.id, cursor))
|
||||
.limit(1);
|
||||
|
||||
if (cursorMsg) {
|
||||
const rows = await db
|
||||
.select()
|
||||
if (cursorRow?.createdAt) {
|
||||
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(and(eq(messages.conversationId, conversationId), lt(messages.createdAt, cursorMsg.createdAt)))
|
||||
.where(
|
||||
and(
|
||||
eq(messages.conversationId, conversationId),
|
||||
lt(messages.createdAt, cursorRow.createdAt)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(messages.createdAt))
|
||||
.limit(limit);
|
||||
|
||||
return c.json({ messages: rows.reverse(), nextCursor: rows.length === limit ? rows[0]?.id : null });
|
||||
.limit(limit + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(messages)
|
||||
.where(eq(messages.conversationId, conversationId))
|
||||
.orderBy(desc(messages.createdAt))
|
||||
.limit(limit);
|
||||
const rows = await query;
|
||||
const hasMore = rows.length > limit;
|
||||
if (hasMore) rows.pop();
|
||||
|
||||
return c.json({ messages: rows.reverse(), nextCursor: null });
|
||||
});
|
||||
|
||||
// POST /api/conversations/:id/messages — send a message
|
||||
const sendMessageSchema = z.object({
|
||||
body: z.string().min(1).max(1600),
|
||||
const lastRow = rows[rows.length - 1];
|
||||
const nextCursor = hasMore && lastRow ? lastRow.id : null;
|
||||
return c.json({ items: rows, nextCursor });
|
||||
});
|
||||
|
||||
// POST /api/conversations/:id/messages — Send a message
|
||||
conversationsRouter.post(
|
||||
"/:id/messages",
|
||||
zValidator("json", sendMessageSchema),
|
||||
async (c) => {
|
||||
const db = getDb();
|
||||
const businessId = c.get("staff").businessId;
|
||||
const staffRow = c.get("staff");
|
||||
if (!staffRow) return c.json({ error: "Unauthorized" }, 401);
|
||||
|
||||
const conversationId = c.req.param("id");
|
||||
const { body } = c.req.valid("json");
|
||||
|
||||
// Verify staff owns this conversation
|
||||
const [conversation] = await db
|
||||
.select()
|
||||
const [settings] = await db
|
||||
.select({ id: businessSettings.id })
|
||||
.from(businessSettings)
|
||||
.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)
|
||||
.where(and(eq(conversations.id, conversationId), eq(conversations.businessId, businessId)))
|
||||
.where(
|
||||
and(eq(conversations.id, conversationId), eq(conversations.businessId, settings.id))
|
||||
)
|
||||
.limit(1);
|
||||
if (!conv) return c.json({ error: "Not found" }, 404);
|
||||
|
||||
if (!conversation) {
|
||||
return c.json({ error: "Not found" }, 404);
|
||||
}
|
||||
const result = await sendMessage({
|
||||
businessId: settings.id,
|
||||
clientId: conv.clientId,
|
||||
body,
|
||||
sentByStaffId: staffRow.id,
|
||||
});
|
||||
|
||||
// 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) {
|
||||
if (result.suppressed) {
|
||||
return c.json({ error: "Client has opted out of SMS" }, 409);
|
||||
}
|
||||
|
||||
// Create outbound message
|
||||
const [msg] = await db
|
||||
.insert(messages)
|
||||
.values({
|
||||
conversationId,
|
||||
direction: "outbound",
|
||||
body,
|
||||
status: "queued",
|
||||
sentByStaffId: staffRow.id,
|
||||
.select({
|
||||
id: messages.id,
|
||||
direction: messages.direction,
|
||||
body: messages.body,
|
||||
status: messages.status,
|
||||
sentByStaffId: messages.sentByStaffId,
|
||||
createdAt: messages.createdAt,
|
||||
deliveredAt: messages.deliveredAt,
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Update conversation lastMessageAt
|
||||
await db
|
||||
.update(conversations)
|
||||
.set({ lastMessageAt: new Date() })
|
||||
.where(eq(conversations.id, conversationId));
|
||||
|
||||
// TODO: Enqueue Telnyx outbound job
|
||||
.from(messages)
|
||||
.where(eq(messages.id, result.messageId))
|
||||
.limit(1);
|
||||
|
||||
return c.json(msg, 201);
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
VITE_API_URL=
|
||||
VITE_API_URL=https://uat.groombook.dev
|
||||
|
||||
@@ -11,6 +11,8 @@ RUN pnpm install --frozen-lockfile
|
||||
|
||||
# Build
|
||||
FROM deps AS builder
|
||||
ARG VITE_API_URL=
|
||||
ENV VITE_API_URL=
|
||||
COPY packages/types/ packages/types/
|
||||
COPY apps/web/ apps/web/
|
||||
RUN pnpm --filter @groombook/web build
|
||||
|
||||
@@ -40,7 +40,10 @@ function LoginPage() {
|
||||
const handleSocialLogin = async (provider: string) => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
const result = await signIn.social({ provider, callbackURL: window.location.origin });
|
||||
// Use /admin as callback URL so Better-Auth redirects to the app's dashboard
|
||||
// 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) {
|
||||
setError(result.error.message ?? "Sign-in failed");
|
||||
setIsLoading(false);
|
||||
|
||||
@@ -8,10 +8,9 @@ const mockConversations = [
|
||||
clientId: "client-1",
|
||||
clientName: "Alice Smith",
|
||||
channel: "sms",
|
||||
externalNumber: "+1234567890",
|
||||
clientPhone: "+1234567890",
|
||||
lastMessageAt: "2026-05-14T10:00:00Z",
|
||||
staffReadAt: null,
|
||||
lastMessageBody: "Hello, is my dog ready?",
|
||||
lastMessage: { body: "Hello, is my dog ready?", direction: "inbound", createdAt: "2026-05-14T10:00:00Z" },
|
||||
unreadCount: 2,
|
||||
status: "active",
|
||||
},
|
||||
@@ -20,10 +19,9 @@ const mockConversations = [
|
||||
clientId: "client-2",
|
||||
clientName: "Bob Jones",
|
||||
channel: "sms",
|
||||
externalNumber: "+1987654321",
|
||||
clientPhone: "+1987654321",
|
||||
lastMessageAt: "2026-05-13T08:00:00Z",
|
||||
staffReadAt: "2026-05-13T09:00:00Z",
|
||||
lastMessageBody: "Thanks for the update",
|
||||
lastMessage: { body: "Thanks for the update", direction: "outbound", createdAt: "2026-05-13T08:05:00Z" },
|
||||
unreadCount: 0,
|
||||
status: "active",
|
||||
},
|
||||
@@ -73,7 +71,7 @@ afterEach(() => {
|
||||
|
||||
describe("MessagesPage", () => {
|
||||
it("renders empty state when no conversations", async () => {
|
||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse([]));
|
||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse({ items: [], nextCursor: null }));
|
||||
|
||||
render(<MessagesPage />);
|
||||
await waitFor(() => {
|
||||
@@ -82,7 +80,7 @@ describe("MessagesPage", () => {
|
||||
});
|
||||
|
||||
it("renders conversation list", async () => {
|
||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse(mockConversations));
|
||||
vi.mocked(global.fetch).mockResolvedValue(makeResponse({ items: mockConversations, nextCursor: null }));
|
||||
|
||||
render(<MessagesPage />);
|
||||
await waitFor(() => {
|
||||
@@ -98,10 +96,10 @@ describe("MessagesPage", () => {
|
||||
vi.mocked(global.fetch).mockImplementation((input) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/conversations?limit=20") {
|
||||
return Promise.resolve(makeResponse(mockConversations));
|
||||
return Promise.resolve(makeResponse({ items: mockConversations, nextCursor: null }));
|
||||
}
|
||||
if (url === "/api/conversations/conv-1/messages?limit=50") {
|
||||
return Promise.resolve(makeResponse({ messages: mockMessages }));
|
||||
return Promise.resolve(makeResponse({ items: mockMessages, nextCursor: null }));
|
||||
}
|
||||
return Promise.resolve(makeResponseWithStatus(null, 404));
|
||||
});
|
||||
@@ -112,7 +110,7 @@ describe("MessagesPage", () => {
|
||||
fireEvent.click(screen.getByText("Alice Smith"));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Hello, is my dog ready?")).toBeInTheDocument();
|
||||
expect(screen.getAllByText("Hello, is my dog ready?").length).toBeGreaterThanOrEqual(1);
|
||||
expect(screen.getByText("Yes, she is all done!")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -132,7 +130,7 @@ describe("MessagesPage", () => {
|
||||
sentByStaffId: "staff-1",
|
||||
}, 201));
|
||||
}
|
||||
return Promise.resolve(makeResponse(mockConversations));
|
||||
return Promise.resolve(makeResponse({ items: mockConversations, nextCursor: null }));
|
||||
});
|
||||
|
||||
render(<MessagesPage />);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: import.meta.env.VITE_API_URL ?? "",
|
||||
baseURL: import.meta.env.VITE_API_URL || window.location.origin,
|
||||
});
|
||||
|
||||
export const { signIn, signOut, useSession, changePassword } = authClient;
|
||||
@@ -5,12 +5,11 @@ interface Conversation {
|
||||
clientId: string;
|
||||
clientName: string;
|
||||
channel: string;
|
||||
externalNumber: string;
|
||||
clientPhone: string;
|
||||
lastMessageAt: string | null;
|
||||
staffReadAt: string | null;
|
||||
lastMessageBody: string | null;
|
||||
unreadCount: number;
|
||||
status: string;
|
||||
lastMessage: { body: string | null; direction: string; createdAt: string } | null;
|
||||
}
|
||||
|
||||
interface Message {
|
||||
@@ -55,7 +54,8 @@ export function MessagesPage() {
|
||||
try {
|
||||
const res = await fetch("/api/conversations?limit=20");
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = (await res.json()) as Conversation[];
|
||||
const json = await res.json();
|
||||
const data = json.items as Conversation[];
|
||||
setConversations(data);
|
||||
} catch (e: unknown) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load conversations");
|
||||
@@ -68,8 +68,8 @@ export function MessagesPage() {
|
||||
try {
|
||||
const res = await fetch(`/api/conversations/${conversationId}/messages?limit=50`);
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = (await res.json()) as { messages: Message[] };
|
||||
setMessages(data.messages);
|
||||
const json = await res.json();
|
||||
setMessages((json.items as Message[]).reverse());
|
||||
} catch (e: unknown) {
|
||||
setMessageError(e instanceof Error ? e.message : "Failed to load messages");
|
||||
} finally {
|
||||
@@ -93,7 +93,7 @@ export function MessagesPage() {
|
||||
|
||||
useEffect(() => {
|
||||
if (messages.length > 0) {
|
||||
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||
messagesEndRef.current?.scrollIntoView?.({ behavior: "smooth" });
|
||||
}
|
||||
}, [messages]);
|
||||
|
||||
@@ -180,7 +180,7 @@ export function MessagesPage() {
|
||||
)}
|
||||
</div>
|
||||
<div style={{ marginTop: 2, color: "#6b7280", fontSize: 12 }}>
|
||||
{truncate(conv.lastMessageBody, 60)}
|
||||
{truncate(conv.lastMessage?.body ?? null, 60)}
|
||||
</div>
|
||||
<div style={{ marginTop: 2, color: "#9ca3af", fontSize: 11 }}>
|
||||
{relativeTime(conv.lastMessageAt)}
|
||||
|
||||
@@ -58,7 +58,7 @@ interface MessageThreadProps {
|
||||
readOnly: boolean;
|
||||
}
|
||||
|
||||
function MessageThread({ sessionId, readOnly }: MessageThreadProps) {
|
||||
function MessageThread({ sessionId, readOnly: _readOnly }: MessageThreadProps) {
|
||||
const [businessName, setBusinessName] = useState<string>("Business");
|
||||
|
||||
const { conversation, loading: convLoading, error: convError } = useConversation(sessionId);
|
||||
@@ -144,7 +144,6 @@ function MessageThread({ sessionId, readOnly }: MessageThreadProps) {
|
||||
) : (
|
||||
messages.map((msg: ApiMessage) => {
|
||||
const sender = msg.direction === "inbound" ? "customer" : "business";
|
||||
const senderName = sender === "customer" ? "You" : businessName;
|
||||
return (
|
||||
<div key={msg.id} className={`flex ${sender === "customer" ? "justify-end" : "justify-start"}`}>
|
||||
<div className={`max-w-[80%] rounded-2xl px-4 py-2.5 ${
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "conversations" ADD COLUMN "staff_read_at" timestamp;
|
||||
@@ -222,8 +222,8 @@
|
||||
{
|
||||
"idx": 32,
|
||||
"version": "7",
|
||||
"when": 1778818472097,
|
||||
"tag": "0032_add_staff_read_at",
|
||||
"when": 1778818472097,
|
||||
"tag": "0032_staff_read_at",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as schema from "./schema.js";
|
||||
|
||||
export * from "./schema.js";
|
||||
export { encryptSecret, decryptSecret } from "./crypto.js";
|
||||
export { and, asc, desc, eq, exists, gte, gt, ilike, inArray, isNull, lt, lte, ne, or, sql } from "drizzle-orm";
|
||||
export { and, asc, count, desc, eq, exists, gte, gt, ilike, inArray, isNull, lt, lte, ne, or, sql } from "drizzle-orm";
|
||||
|
||||
let _db: ReturnType<typeof drizzle> | null = null;
|
||||
|
||||
|
||||
@@ -883,6 +883,7 @@ async function seed() {
|
||||
let appointmentCount = 0;
|
||||
let invoiceCount = 0;
|
||||
let visitLogCount = 0;
|
||||
let paidInvoiceCounter = 0;
|
||||
|
||||
// Process in batches per client to keep memory manageable
|
||||
const apptBatchSize = 100;
|
||||
@@ -977,8 +978,11 @@ async function seed() {
|
||||
|
||||
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;
|
||||
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({
|
||||
id: invoiceId,
|
||||
appointmentId: apptId,
|
||||
@@ -1094,14 +1098,16 @@ async function seed() {
|
||||
const taxCents = Math.round(effectivePrice * 0.08);
|
||||
const totalCents = effectivePrice + taxCents + tipCents;
|
||||
const paidAt = new Date(endTime.getTime() + randInt(5, 30) * 60 * 1000);
|
||||
const stripePaymentIntentId = rand() < 0.2 ? `pi_test_${uuid().replace(/-/g, "").slice(0, 24)}` : null;
|
||||
paidInvoiceCounter++;
|
||||
|
||||
invoiceBatch.push({
|
||||
id: invoiceId, appointmentId: apptId, clientId,
|
||||
subtotalCents: effectivePrice, taxCents, tipCents, totalCents,
|
||||
status: "paid" as const,
|
||||
paymentMethod: pick(["cash", "card", "card", "card", "check"]) as "cash" | "card" | "check",
|
||||
paidAt, stripePaymentIntentId, notes: null,
|
||||
paidAt,
|
||||
stripePaymentIntentId: `pi_test_seed_${String(paidInvoiceCounter).padStart(6, "0")}`,
|
||||
notes: null,
|
||||
});
|
||||
lineItemBatch.push({
|
||||
id: uuid(), invoiceId, description: svc.name, quantity: 1,
|
||||
|
||||
@@ -32,6 +32,8 @@ export interface Pet {
|
||||
name: string;
|
||||
species: string;
|
||||
breed: string | null;
|
||||
sizeCategory: string | null;
|
||||
coatType: string | null;
|
||||
weightKg: number | null;
|
||||
dateOfBirth: string | null;
|
||||
healthAlerts: string | null;
|
||||
@@ -64,6 +66,7 @@ export interface Service {
|
||||
description: string | null;
|
||||
basePriceCents: number;
|
||||
durationMinutes: number;
|
||||
defaultBufferMinutes: number;
|
||||
active: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
@@ -114,6 +117,7 @@ export interface Appointment {
|
||||
cancelledAt: string | null;
|
||||
confirmationToken: string | null;
|
||||
customerNotes: string | null;
|
||||
bufferMinutes: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user