@app-vitals/agent-admin 1.101.2 → 1.102.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/accounts.ts +11 -0
- package/src/agent-members.integration.test.ts +99 -0
- package/src/agent-members.ts +56 -14
- package/src/agents-api.integration.test.ts +22 -6
- package/src/agents.unit.test.ts +6 -1
- package/src/caller-scope.integration.test.ts +94 -0
- package/src/caller-scope.ts +51 -4
- package/src/caller-scope.unit.test.ts +113 -3
- package/src/member-id-test-support.ts +29 -0
- package/src/push-service.integration.test.ts +154 -0
- package/src/push-service.ts +83 -34
- package/src/push-service.unit.test.ts +84 -82
- package/src/user-notification-prefs.integration.test.ts +84 -0
- package/src/user-notification-prefs.ts +77 -0
- package/src/agent-members.unit.test.ts +0 -255
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.102.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@anthropic-ai/sdk": "0.137.0",
|
|
47
|
-
"@app-vitals/agent-lib": "1.
|
|
47
|
+
"@app-vitals/agent-lib": "1.102.0",
|
|
48
48
|
"@hono/zod-openapi": "^1.0.0",
|
|
49
49
|
"@prisma/adapter-pg": "^7.10.0",
|
|
50
50
|
"@prisma/client": "^7.10.0",
|
package/src/accounts.ts
CHANGED
|
@@ -71,6 +71,17 @@ export class AccountService {
|
|
|
71
71
|
return member?.account ?? null;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/** Account id + role of the member with this email, or null. */
|
|
75
|
+
async getMembershipByEmail(
|
|
76
|
+
email: string,
|
|
77
|
+
): Promise<{ accountId: string; role: string } | null> {
|
|
78
|
+
const member = await this.prisma.accountMember.findUnique({
|
|
79
|
+
where: { email: normalizeEmail(email) },
|
|
80
|
+
select: { accountId: true, role: true },
|
|
81
|
+
});
|
|
82
|
+
return member ?? null;
|
|
83
|
+
}
|
|
84
|
+
|
|
74
85
|
async list(): Promise<Account[]> {
|
|
75
86
|
return this.prisma.account.findMany({ orderBy: { createdAt: "asc" } });
|
|
76
87
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-members.integration.test.ts (ACM-4.1)
|
|
3
|
+
* AgentMemberService reads and writes by memberId; email is resolved through
|
|
4
|
+
* the AccountMember lookup. Rows with a null memberId are skipped.
|
|
5
|
+
*
|
|
6
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
+
import { AgentMemberService } from "./agent-members.ts";
|
|
12
|
+
import { resetMemberIdFixture } from "./member-id-test-support.ts";
|
|
13
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
14
|
+
|
|
15
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
16
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
17
|
+
|
|
18
|
+
describeOrSkip("AgentMemberService by memberId (integration)", () => {
|
|
19
|
+
let prisma: PrismaClient;
|
|
20
|
+
let service: AgentMemberService;
|
|
21
|
+
let aliceId: string;
|
|
22
|
+
let bobId: string;
|
|
23
|
+
let agentId: string;
|
|
24
|
+
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
27
|
+
const f = await resetMemberIdFixture(prisma);
|
|
28
|
+
aliceId = f.alice.id;
|
|
29
|
+
bobId = f.bob.id;
|
|
30
|
+
agentId = f.agent.id;
|
|
31
|
+
service = new AgentMemberService(prisma);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterEach(async () => {
|
|
35
|
+
await prisma.$disconnect();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("add stamps memberId resolved from the email (case-insensitive)", async () => {
|
|
39
|
+
const row = await service.add(agentId, "alice@acme.com");
|
|
40
|
+
expect(row.memberId).toBe(aliceId);
|
|
41
|
+
const stored = await prisma.agentMember.findUniqueOrThrow({
|
|
42
|
+
where: { id: row.id },
|
|
43
|
+
});
|
|
44
|
+
expect(stored.memberId).toBe(aliceId);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("add inside a transaction resolves memberId through the tx client", async () => {
|
|
48
|
+
await prisma.$transaction(async (tx) => {
|
|
49
|
+
await service.add(agentId, "bob@acme.com", tx);
|
|
50
|
+
});
|
|
51
|
+
const rows = await prisma.agentMember.findMany();
|
|
52
|
+
expect(rows.map((r) => r.memberId)).toEqual([bobId]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("add for an email with no AccountMember stores a null memberId", async () => {
|
|
56
|
+
const row = await service.add(agentId, "ghost@nowhere.com");
|
|
57
|
+
expect(row.memberId).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("listByEmail and listByMemberId return the member's agents", async () => {
|
|
61
|
+
await service.add(agentId, "alice@acme.com");
|
|
62
|
+
expect((await service.listByEmail("Alice@Acme.com")).length).toBe(1);
|
|
63
|
+
expect((await service.listByMemberId(aliceId)).length).toBe(1);
|
|
64
|
+
expect(await service.listByMemberId(bobId)).toEqual([]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("exists keys on memberId", async () => {
|
|
68
|
+
await service.add(agentId, "alice@acme.com");
|
|
69
|
+
expect(await service.exists(agentId, "alice@acme.com")).toBe(true);
|
|
70
|
+
expect(await service.exists(agentId, "bob@acme.com")).toBe(false);
|
|
71
|
+
expect(await service.exists(agentId, "ghost@nowhere.com")).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("skips null-memberId rows on every read without crashing", async () => {
|
|
75
|
+
// Legacy row: email only, never backfilled.
|
|
76
|
+
await prisma.agentMember.create({
|
|
77
|
+
data: { agentId, email: "alice@acme.com" },
|
|
78
|
+
});
|
|
79
|
+
expect(await service.listByEmail("alice@acme.com")).toEqual([]);
|
|
80
|
+
expect(await service.exists(agentId, "alice@acme.com")).toBe(false);
|
|
81
|
+
expect(await service.listByAgentId(agentId)).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("listByAgentId returns rows with the email resolved through the lookup", async () => {
|
|
85
|
+
await service.add(agentId, "alice@acme.com");
|
|
86
|
+
// Stored email drifted from the member's current email.
|
|
87
|
+
await prisma.agentMember.updateMany({ data: { email: "stale@old.com" } });
|
|
88
|
+
const rows = await service.listByAgentId(agentId);
|
|
89
|
+
expect(rows.map((r) => r.email)).toEqual(["alice@acme.com"]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("remove deletes by row id scoped to the agent", async () => {
|
|
93
|
+
const row = await service.add(agentId, "alice@acme.com");
|
|
94
|
+
await service.remove("other-agent", row.id);
|
|
95
|
+
expect((await service.listByMemberId(aliceId)).length).toBe(1);
|
|
96
|
+
await service.remove(agentId, row.id);
|
|
97
|
+
expect(await service.listByMemberId(aliceId)).toEqual([]);
|
|
98
|
+
});
|
|
99
|
+
});
|
package/src/agent-members.ts
CHANGED
|
@@ -2,46 +2,81 @@
|
|
|
2
2
|
* agent/src/agent-members.ts
|
|
3
3
|
* AgentMemberService — authorized human members per agent (access control).
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* ACM-4.1: reads and writes key on AgentMember.memberId (AccountMember.id).
|
|
6
|
+
* Callers still pass emails; they are resolved to a member id through the
|
|
7
|
+
* AccountMember lookup, and emails handed back are resolved the same way. Rows
|
|
8
|
+
* whose memberId is null (not yet backfilled) are skipped. The [agentId, email]
|
|
9
|
+
* unique constraint stays in place and still guards duplicate adds.
|
|
8
10
|
*/
|
|
9
11
|
|
|
10
12
|
import type { AgentMember, PrismaClient } from "../prisma/client/client.ts";
|
|
13
|
+
import { normalizeEmail } from "./account-email.ts";
|
|
14
|
+
import { PrincipalLookupService } from "./principal-lookup.ts";
|
|
11
15
|
import type { PrismaTransactionClient } from "./prisma-tx.ts";
|
|
12
16
|
|
|
13
17
|
export type { AgentMember };
|
|
14
18
|
|
|
15
19
|
export class AgentMemberService {
|
|
16
|
-
|
|
20
|
+
private readonly lookup: PrincipalLookupService;
|
|
21
|
+
|
|
22
|
+
constructor(private prisma: PrismaClient) {
|
|
23
|
+
this.lookup = new PrincipalLookupService(prisma);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private async memberIdForEmail(email: string): Promise<string | null> {
|
|
27
|
+
return (await this.lookup.findIdByEmail(email))?.id ?? null;
|
|
28
|
+
}
|
|
17
29
|
|
|
18
30
|
/**
|
|
19
|
-
* List all memberships for a given agent.
|
|
31
|
+
* List all memberships for a given agent. Rows with a null memberId, or a
|
|
32
|
+
* memberId that no longer resolves, are skipped; `email` on each returned
|
|
33
|
+
* row is the member's current email from the lookup.
|
|
20
34
|
*/
|
|
21
35
|
async listByAgentId(agentId: string): Promise<AgentMember[]> {
|
|
22
|
-
|
|
36
|
+
const rows = await this.prisma.agentMember.findMany({
|
|
37
|
+
where: { agentId, memberId: { not: null } },
|
|
38
|
+
});
|
|
39
|
+
const { principals } = await this.lookup.resolveMany(
|
|
40
|
+
rows.flatMap((r) => (r.memberId ? [r.memberId] : [])),
|
|
41
|
+
);
|
|
42
|
+
const emails = new Map(principals.map((p) => [p.id, p.email]));
|
|
43
|
+
return rows.flatMap((r) => {
|
|
44
|
+
const email = r.memberId ? emails.get(r.memberId) : null;
|
|
45
|
+
return email ? [{ ...r, email }] : [];
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** List all memberships for a given AccountMember id. */
|
|
50
|
+
async listByMemberId(memberId: string): Promise<AgentMember[]> {
|
|
51
|
+
return this.prisma.agentMember.findMany({ where: { memberId } });
|
|
23
52
|
}
|
|
24
53
|
|
|
25
54
|
/**
|
|
26
|
-
* List all memberships for
|
|
27
|
-
*
|
|
55
|
+
* List all memberships for the member owning `email` (case-insensitive).
|
|
56
|
+
* Empty when the email has no AccountMember.
|
|
28
57
|
*/
|
|
29
58
|
async listByEmail(email: string): Promise<AgentMember[]> {
|
|
30
|
-
|
|
59
|
+
const memberId = await this.memberIdForEmail(email);
|
|
60
|
+
return memberId ? this.listByMemberId(memberId) : [];
|
|
31
61
|
}
|
|
32
62
|
|
|
33
63
|
/**
|
|
34
|
-
* Returns whether a membership row
|
|
64
|
+
* Returns whether the member owning `email` has a membership row for the agent.
|
|
35
65
|
*/
|
|
36
66
|
async exists(agentId: string, email: string): Promise<boolean> {
|
|
37
|
-
const
|
|
38
|
-
|
|
67
|
+
const memberId = await this.memberIdForEmail(email);
|
|
68
|
+
if (!memberId) return false;
|
|
69
|
+
const membership = await this.prisma.agentMember.findFirst({
|
|
70
|
+
where: { agentId, memberId },
|
|
71
|
+
select: { id: true },
|
|
39
72
|
});
|
|
40
73
|
return membership !== null;
|
|
41
74
|
}
|
|
42
75
|
|
|
43
76
|
/**
|
|
44
|
-
* Create a membership row for the given agentId and email
|
|
77
|
+
* Create a membership row for the given agentId and email, stamped with the
|
|
78
|
+
* email's AccountMember id (null when the email has no member yet — such a
|
|
79
|
+
* row is skipped by reads until backfilled).
|
|
45
80
|
* Throws (unique-constraint violation) if the membership already exists —
|
|
46
81
|
* callers are expected to catch and handle "already a member" as a no-op.
|
|
47
82
|
*
|
|
@@ -54,7 +89,14 @@ export class AgentMemberService {
|
|
|
54
89
|
email: string,
|
|
55
90
|
client: PrismaTransactionClient = this.prisma,
|
|
56
91
|
): Promise<AgentMember> {
|
|
57
|
-
|
|
92
|
+
const normalized = normalizeEmail(email);
|
|
93
|
+
const member = await client.accountMember.findUnique({
|
|
94
|
+
where: { email: normalized },
|
|
95
|
+
select: { id: true },
|
|
96
|
+
});
|
|
97
|
+
return client.agentMember.create({
|
|
98
|
+
data: { agentId, email: normalized, memberId: member?.id ?? null },
|
|
99
|
+
});
|
|
58
100
|
}
|
|
59
101
|
|
|
60
102
|
/**
|
|
@@ -96,6 +96,22 @@ const CODING_TYPE: SpecialtyType = {
|
|
|
96
96
|
* specialty. tryGetType() returns undefined (no fallback) for anything
|
|
97
97
|
* not in the map, mirroring SpecialtyTypes's real contract.
|
|
98
98
|
*/
|
|
99
|
+
// ACM-4.1: AgentMember reads key on memberId, so seeded members need an
|
|
100
|
+
// AccountMember to resolve through.
|
|
101
|
+
async function seedAgentMember(
|
|
102
|
+
prisma: PrismaClient,
|
|
103
|
+
agentId: string,
|
|
104
|
+
email: string,
|
|
105
|
+
) {
|
|
106
|
+
const account = await prisma.account.create({ data: { name: "Acme" } });
|
|
107
|
+
const member = await prisma.accountMember.create({
|
|
108
|
+
data: { accountId: account.id, email, role: "member" },
|
|
109
|
+
});
|
|
110
|
+
await prisma.agentMember.create({
|
|
111
|
+
data: { agentId, email, memberId: member.id },
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
99
115
|
function fakeSpecialtyTypes(
|
|
100
116
|
byType: Record<string, SpecialtyType> = { coding: CODING_TYPE },
|
|
101
117
|
): SpecialtyTypeResolver {
|
|
@@ -181,6 +197,8 @@ describeOrSkip("admin CRUD API (integration)", () => {
|
|
|
181
197
|
await prisma.agentEnv.deleteMany();
|
|
182
198
|
await prisma.agentMember.deleteMany();
|
|
183
199
|
await prisma.agent.deleteMany();
|
|
200
|
+
await prisma.accountMember.deleteMany();
|
|
201
|
+
await prisma.account.deleteMany();
|
|
184
202
|
|
|
185
203
|
agentId = await createAgent(prisma);
|
|
186
204
|
cookie = await makeSessionCookie();
|
|
@@ -615,9 +633,7 @@ describeOrSkip("admin CRUD API (integration)", () => {
|
|
|
615
633
|
Cookie: `admin_session=${cookie}`,
|
|
616
634
|
},
|
|
617
635
|
});
|
|
618
|
-
await prisma.
|
|
619
|
-
data: { agentId, email: "member@example.com" },
|
|
620
|
-
});
|
|
636
|
+
await seedAgentMember(prisma, agentId, "member@example.com");
|
|
621
637
|
|
|
622
638
|
const secondPatchRes = await app.request(`/agents/${agentId}`, {
|
|
623
639
|
method: "PATCH",
|
|
@@ -745,6 +761,8 @@ describeOrSkip("PATCH /agents/:id podServiceAccountName (integration)", () => {
|
|
|
745
761
|
await prisma.agentEnv.deleteMany();
|
|
746
762
|
await prisma.agentMember.deleteMany();
|
|
747
763
|
await prisma.agent.deleteMany();
|
|
764
|
+
await prisma.accountMember.deleteMany();
|
|
765
|
+
await prisma.account.deleteMany();
|
|
748
766
|
agentId = await createAgent(prisma);
|
|
749
767
|
cookie = await makeSessionCookie();
|
|
750
768
|
reconciled.length = 0;
|
|
@@ -784,9 +802,7 @@ describeOrSkip("PATCH /agents/:id podServiceAccountName (integration)", () => {
|
|
|
784
802
|
SESSION_SECRET,
|
|
785
803
|
"HS256",
|
|
786
804
|
);
|
|
787
|
-
await prisma.
|
|
788
|
-
data: { agentId, email: "member@example.com" },
|
|
789
|
-
});
|
|
805
|
+
await seedAgentMember(prisma, agentId, "member@example.com");
|
|
790
806
|
const res = await patch(
|
|
791
807
|
appWith(true),
|
|
792
808
|
{ podServiceAccountName: "sa" },
|
package/src/agents.unit.test.ts
CHANGED
|
@@ -814,7 +814,12 @@ describe("AgentService.getById", () => {
|
|
|
814
814
|
},
|
|
815
815
|
],
|
|
816
816
|
}) as unknown as FakePrisma;
|
|
817
|
-
|
|
817
|
+
// ACM-4.1: AgentMemberService resolves emails through AccountMember and is
|
|
818
|
+
// covered by agent-members.integration.test.ts; here it is a seeded double.
|
|
819
|
+
const service = new AgentService(prisma as never, undefined, {
|
|
820
|
+
listByAgentId: async (agentId: string) =>
|
|
821
|
+
(await prisma.agentMember.findMany({ where: { agentId } })) as never,
|
|
822
|
+
});
|
|
818
823
|
|
|
819
824
|
const result = await service.getById("a1");
|
|
820
825
|
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/caller-scope.integration.test.ts
|
|
3
|
+
* Role resolver + role-aware scope against real Postgres (ACM-7.1).
|
|
4
|
+
*
|
|
5
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
9
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
|
+
import { AccountMemberService } from "./account-members.ts";
|
|
11
|
+
import { AccountService } from "./accounts.ts";
|
|
12
|
+
import { AgentMemberService } from "./agent-members.ts";
|
|
13
|
+
import {
|
|
14
|
+
callerScopeDepsFromServices,
|
|
15
|
+
resolveCallerScope,
|
|
16
|
+
resolveViewerRole,
|
|
17
|
+
} from "./caller-scope.ts";
|
|
18
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
19
|
+
|
|
20
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
21
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
22
|
+
|
|
23
|
+
describeOrSkip("caller scope role resolver (integration)", () => {
|
|
24
|
+
let prisma: PrismaClient;
|
|
25
|
+
let deps: ReturnType<typeof callerScopeDepsFromServices>;
|
|
26
|
+
let accountId: string;
|
|
27
|
+
let accountAgentIds: string[];
|
|
28
|
+
let sideAgentId: string;
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
32
|
+
await prisma.agentMember.deleteMany();
|
|
33
|
+
await prisma.accountInvite.deleteMany();
|
|
34
|
+
await prisma.accountMember.deleteMany();
|
|
35
|
+
await prisma.agent.deleteMany();
|
|
36
|
+
await prisma.account.deleteMany();
|
|
37
|
+
const accountService = new AccountService(prisma);
|
|
38
|
+
const agentMemberService = new AgentMemberService(prisma);
|
|
39
|
+
deps = callerScopeDepsFromServices({ accountService, agentMemberService });
|
|
40
|
+
|
|
41
|
+
const account = await accountService.create("Acme", "Owner@Acme.com");
|
|
42
|
+
accountId = account.id;
|
|
43
|
+
await new AccountMemberService(prisma).add(
|
|
44
|
+
account.id,
|
|
45
|
+
"member@acme.com",
|
|
46
|
+
"member",
|
|
47
|
+
);
|
|
48
|
+
const a1 = await prisma.agent.create({
|
|
49
|
+
data: { name: "a1", accountId: account.id },
|
|
50
|
+
});
|
|
51
|
+
const a2 = await prisma.agent.create({
|
|
52
|
+
data: { name: "a2", accountId: account.id },
|
|
53
|
+
});
|
|
54
|
+
accountAgentIds = [a1.id, a2.id];
|
|
55
|
+
const side = await prisma.agent.create({ data: { name: "side" } });
|
|
56
|
+
sideAgentId = side.id;
|
|
57
|
+
await agentMemberService.add(side.id, "member@acme.com");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
afterEach(async () => {
|
|
61
|
+
await prisma.$disconnect();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("resolves admin / owner / member / null", async () => {
|
|
65
|
+
const role = (email: string, isAdmin = false) =>
|
|
66
|
+
resolveViewerRole({ email, isAdmin }, deps);
|
|
67
|
+
expect(await role("anyone@x.com", true)).toBe("admin");
|
|
68
|
+
expect(await role("OWNER@acme.com")).toBe("owner");
|
|
69
|
+
expect(await role("member@acme.com")).toBe("member");
|
|
70
|
+
expect(await role("stranger@x.com")).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("owner gets account agents; member only AgentMember agents (flag on)", async () => {
|
|
74
|
+
const scope = (email: string, flagEnabled: boolean) =>
|
|
75
|
+
resolveCallerScope({ email, isAdmin: false, flagEnabled }, deps);
|
|
76
|
+
|
|
77
|
+
const owner = await scope("owner@acme.com", true);
|
|
78
|
+
expect(owner.kind === "scoped" && owner.accountId).toBe(accountId);
|
|
79
|
+
expect(owner.kind === "scoped" && owner.agentIds.sort()).toEqual(
|
|
80
|
+
[...accountAgentIds].sort(),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(await scope("member@acme.com", true)).toEqual({
|
|
84
|
+
kind: "scoped",
|
|
85
|
+
accountId,
|
|
86
|
+
agentIds: [sideAgentId],
|
|
87
|
+
});
|
|
88
|
+
expect(await scope("owner@acme.com", false)).toEqual({
|
|
89
|
+
kind: "scoped",
|
|
90
|
+
accountId: null,
|
|
91
|
+
agentIds: [],
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
package/src/caller-scope.ts
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Resolved per request from the caller's email — the account is deliberately
|
|
9
9
|
* NOT stored in the session JWT, so membership changes take effect
|
|
10
|
-
* immediately. Account
|
|
11
|
-
* flag is on;
|
|
10
|
+
* immediately. Account agents contribute to the scope only for account owners
|
|
11
|
+
* and only when the self-serve flag is on; members (and flag off) get exactly
|
|
12
|
+
* the AgentMember-only scope.
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
import type { AccountService } from "./accounts.ts";
|
|
@@ -32,6 +33,36 @@ export interface CallerScopeDeps {
|
|
|
32
33
|
listAgentIdsByAccount(accountId: string): Promise<string[]>;
|
|
33
34
|
/** Ids of agents the email holds an AgentMember row for. */
|
|
34
35
|
listMemberAgentIds(email: string): Promise<string[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Account id + AccountMember.role for the email, or null. Optional so
|
|
38
|
+
* existing consumers keep compiling; when absent, the role is unknown and
|
|
39
|
+
* account agents contribute as before.
|
|
40
|
+
*/
|
|
41
|
+
getAccountMembershipByEmail?(
|
|
42
|
+
email: string,
|
|
43
|
+
): Promise<{ accountId: string; role: string } | null>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** How the caller relates to the platform/account: see resolveViewerRole. */
|
|
47
|
+
export type ViewerRole = "admin" | "owner" | "member";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The caller's role: `admin` for allowlisted callers, otherwise the
|
|
51
|
+
* AccountMember.role (`owner` | `member`), or null when the caller has no
|
|
52
|
+
* account. Independent of the self-serve flag.
|
|
53
|
+
*/
|
|
54
|
+
export async function resolveViewerRole(
|
|
55
|
+
input: { email: string; isAdmin: boolean },
|
|
56
|
+
deps: Pick<CallerScopeDeps, "getAccountMembershipByEmail">,
|
|
57
|
+
): Promise<ViewerRole | null> {
|
|
58
|
+
if (input.isAdmin) return "admin";
|
|
59
|
+
const membership = await deps.getAccountMembershipByEmail?.(
|
|
60
|
+
input.email.toLowerCase(),
|
|
61
|
+
);
|
|
62
|
+
if (membership?.role === "owner" || membership?.role === "member") {
|
|
63
|
+
return membership.role;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
35
66
|
}
|
|
36
67
|
|
|
37
68
|
export async function resolveCallerScope(
|
|
@@ -44,8 +75,18 @@ export async function resolveCallerScope(
|
|
|
44
75
|
const accountId = input.flagEnabled
|
|
45
76
|
? await deps.getAccountIdByEmail(email)
|
|
46
77
|
: null;
|
|
78
|
+
// Only owners get account-wide agents; with no role dep the role is unknown
|
|
79
|
+
// and the legacy behavior (account agents contribute) is kept.
|
|
80
|
+
const role = accountId
|
|
81
|
+
? await resolveViewerRole({ email, isAdmin: false }, deps)
|
|
82
|
+
: null;
|
|
83
|
+
const includeAccountAgents =
|
|
84
|
+
accountId !== null &&
|
|
85
|
+
(role === "owner" || !deps.getAccountMembershipByEmail);
|
|
47
86
|
const [accountAgentIds, memberAgentIds] = await Promise.all([
|
|
48
|
-
|
|
87
|
+
includeAccountAgents && accountId
|
|
88
|
+
? deps.listAgentIdsByAccount(accountId)
|
|
89
|
+
: [],
|
|
49
90
|
deps.listMemberAgentIds(email),
|
|
50
91
|
]);
|
|
51
92
|
return {
|
|
@@ -100,14 +141,20 @@ export function createCallerScopeResolver(
|
|
|
100
141
|
|
|
101
142
|
/** Wire the resolver's I/O to the real services. */
|
|
102
143
|
export function callerScopeDepsFromServices(services: {
|
|
103
|
-
accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds"
|
|
144
|
+
accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds"> &
|
|
145
|
+
Partial<Pick<AccountService, "getMembershipByEmail">>;
|
|
104
146
|
agentMemberService: Pick<AgentMemberService, "listByEmail">;
|
|
105
147
|
}): CallerScopeDeps {
|
|
148
|
+
const getMembership = services.accountService.getMembershipByEmail?.bind(
|
|
149
|
+
services.accountService,
|
|
150
|
+
);
|
|
106
151
|
return {
|
|
107
152
|
getAccountIdByEmail: async (email) =>
|
|
108
153
|
(await services.accountService.getByMemberEmail(email))?.id ?? null,
|
|
109
154
|
listAgentIdsByAccount: (accountId) =>
|
|
110
155
|
services.accountService.listAgentIds(accountId),
|
|
156
|
+
getAccountMembershipByEmail:
|
|
157
|
+
getMembership && ((email) => getMembership(email)),
|
|
111
158
|
listMemberAgentIds: async (email) =>
|
|
112
159
|
(await services.agentMemberService.listByEmail(email)).map(
|
|
113
160
|
(m) => m.agentId,
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
type CallerScope,
|
|
4
4
|
type CallerScopeDeps,
|
|
5
5
|
resolveCallerScope,
|
|
6
|
+
resolveViewerRole,
|
|
6
7
|
scopeIncludesAgent,
|
|
7
8
|
taskStoreViewScope,
|
|
8
9
|
} from "./caller-scope.ts";
|
|
@@ -11,18 +12,39 @@ function makeDeps(opts: {
|
|
|
11
12
|
account?: Record<string, string>;
|
|
12
13
|
accountAgents?: Record<string, string[]>;
|
|
13
14
|
members?: Record<string, string[]>;
|
|
15
|
+
roles?: Record<string, string>;
|
|
14
16
|
}): CallerScopeDeps {
|
|
15
17
|
return {
|
|
16
18
|
getAccountIdByEmail: async (e) => opts.account?.[e] ?? null,
|
|
17
19
|
listAgentIdsByAccount: async (id) => opts.accountAgents?.[id] ?? [],
|
|
18
20
|
listMemberAgentIds: async (e) => opts.members?.[e] ?? [],
|
|
21
|
+
getAccountMembershipByEmail: async (e) => {
|
|
22
|
+
const accountId = opts.account?.[e];
|
|
23
|
+
const role = opts.roles?.[e];
|
|
24
|
+
return accountId && role ? { accountId, role } : null;
|
|
25
|
+
},
|
|
19
26
|
};
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
const deps = makeDeps({
|
|
23
|
-
account: {
|
|
30
|
+
account: {
|
|
31
|
+
"a@x.com": "acct-a",
|
|
32
|
+
"both@x.com": "acct-a",
|
|
33
|
+
"mem@x.com": "acct-a",
|
|
34
|
+
"memboth@x.com": "acct-a",
|
|
35
|
+
},
|
|
36
|
+
roles: {
|
|
37
|
+
"a@x.com": "owner",
|
|
38
|
+
"both@x.com": "owner",
|
|
39
|
+
"mem@x.com": "member",
|
|
40
|
+
"memboth@x.com": "member",
|
|
41
|
+
},
|
|
24
42
|
accountAgents: { "acct-a": ["ag-1", "ag-2"] },
|
|
25
|
-
members: {
|
|
43
|
+
members: {
|
|
44
|
+
"m@x.com": ["ag-9"],
|
|
45
|
+
"both@x.com": ["ag-2", "ag-9"],
|
|
46
|
+
"memboth@x.com": ["ag-9"],
|
|
47
|
+
},
|
|
26
48
|
});
|
|
27
49
|
|
|
28
50
|
const run = (email: string, isAdmin = false, flagEnabled = true) =>
|
|
@@ -33,7 +55,7 @@ describe("resolveCallerScope", () => {
|
|
|
33
55
|
expect(await run("a@x.com", true)).toEqual({ kind: "all" });
|
|
34
56
|
});
|
|
35
57
|
|
|
36
|
-
it("account
|
|
58
|
+
it("account owner gets every account agent", async () => {
|
|
37
59
|
expect(await run("a@x.com")).toEqual({
|
|
38
60
|
kind: "scoped",
|
|
39
61
|
accountId: "acct-a",
|
|
@@ -41,6 +63,33 @@ describe("resolveCallerScope", () => {
|
|
|
41
63
|
});
|
|
42
64
|
});
|
|
43
65
|
|
|
66
|
+
it("account member (flag on) gets only AgentMember agents, keeps accountId", async () => {
|
|
67
|
+
expect(await run("mem@x.com")).toEqual({
|
|
68
|
+
kind: "scoped",
|
|
69
|
+
accountId: "acct-a",
|
|
70
|
+
agentIds: [],
|
|
71
|
+
});
|
|
72
|
+
expect(await run("memboth@x.com")).toEqual({
|
|
73
|
+
kind: "scoped",
|
|
74
|
+
accountId: "acct-a",
|
|
75
|
+
agentIds: ["ag-9"],
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("deps without the role lookup keep the legacy account-agent behavior", async () => {
|
|
80
|
+
const { getAccountMembershipByEmail: _omit, ...legacy } = deps;
|
|
81
|
+
expect(
|
|
82
|
+
await resolveCallerScope(
|
|
83
|
+
{ email: "mem@x.com", isAdmin: false, flagEnabled: true },
|
|
84
|
+
legacy,
|
|
85
|
+
),
|
|
86
|
+
).toEqual({
|
|
87
|
+
kind: "scoped",
|
|
88
|
+
accountId: "acct-a",
|
|
89
|
+
agentIds: ["ag-1", "ag-2"],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
44
93
|
it("explicit member gets only AgentMember agents", async () => {
|
|
45
94
|
expect(await run("m@x.com")).toEqual({
|
|
46
95
|
kind: "scoped",
|
|
@@ -114,3 +163,64 @@ describe("taskStoreViewScope (SSP-6.8)", () => {
|
|
|
114
163
|
).toEqual({ kind: "none" });
|
|
115
164
|
});
|
|
116
165
|
});
|
|
166
|
+
|
|
167
|
+
describe("resolveViewerRole", () => {
|
|
168
|
+
const role = (email: string, isAdmin = false) =>
|
|
169
|
+
resolveViewerRole({ email, isAdmin }, deps);
|
|
170
|
+
|
|
171
|
+
it("admin for allowlisted callers, even with a member row", async () => {
|
|
172
|
+
expect(await role("mem@x.com", true)).toBe("admin");
|
|
173
|
+
expect(await role("nobody@x.com", true)).toBe("admin");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("owner or member from the account role, case-insensitive", async () => {
|
|
177
|
+
expect(await role("A@x.com")).toBe("owner");
|
|
178
|
+
expect(await role("mem@x.com")).toBe("member");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("null for a caller with no account", async () => {
|
|
182
|
+
expect(await role("none@x.com")).toBeNull();
|
|
183
|
+
expect(await role("m@x.com")).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("null for an unknown role value or deps without the lookup", async () => {
|
|
187
|
+
expect(
|
|
188
|
+
await resolveViewerRole(
|
|
189
|
+
{ email: "x@x.com", isAdmin: false },
|
|
190
|
+
{
|
|
191
|
+
getAccountMembershipByEmail: async () => ({
|
|
192
|
+
accountId: "a",
|
|
193
|
+
role: "weird",
|
|
194
|
+
}),
|
|
195
|
+
},
|
|
196
|
+
),
|
|
197
|
+
).toBeNull();
|
|
198
|
+
expect(
|
|
199
|
+
await resolveViewerRole({ email: "x@x.com", isAdmin: false }, {}),
|
|
200
|
+
).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe("resolveCallerScope matrix: role x flag", () => {
|
|
205
|
+
const cases: Array<[string, boolean, string[], string | null]> = [
|
|
206
|
+
["a@x.com", true, ["ag-1", "ag-2"], "acct-a"], // owner, flag on
|
|
207
|
+
["a@x.com", false, [], null], // owner, flag off
|
|
208
|
+
["memboth@x.com", true, ["ag-9"], "acct-a"], // member, flag on
|
|
209
|
+
["memboth@x.com", false, ["ag-9"], null], // member, flag off
|
|
210
|
+
["none@x.com", true, [], null], // no account
|
|
211
|
+
["none@x.com", false, [], null],
|
|
212
|
+
];
|
|
213
|
+
for (const [email, flag, agentIds, accountId] of cases) {
|
|
214
|
+
it(`${email} flag=${flag}`, async () => {
|
|
215
|
+
expect(await run(email, false, flag)).toEqual({
|
|
216
|
+
kind: "scoped",
|
|
217
|
+
accountId,
|
|
218
|
+
agentIds,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
it("admin is all regardless of flag", async () => {
|
|
223
|
+
expect(await run("a@x.com", true, false)).toEqual({ kind: "all" });
|
|
224
|
+
expect(await run("a@x.com", true, true)).toEqual({ kind: "all" });
|
|
225
|
+
});
|
|
226
|
+
});
|