-
Notifications
You must be signed in to change notification settings - Fork 0
[1c] MCP entrypoint + identity resolution #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import { IdentityService } from "./identity.service"; | ||
| import type { PrismaService } from "../prisma/prisma.service"; | ||
|
|
||
| /** Minimal mock shape for PrismaService covering only what IdentityService uses. */ | ||
| function makeMockPrisma() { | ||
| return { | ||
| userDepartment: { | ||
| findFirst: jest.fn(), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("IdentityService", () => { | ||
| let service: IdentityService; | ||
| let prisma: ReturnType<typeof makeMockPrisma>; | ||
|
|
||
| beforeEach(() => { | ||
| prisma = makeMockPrisma(); | ||
| service = new IdentityService(prisma as unknown as PrismaService); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("resolveScope", () => { | ||
| it("returns organizationId and departmentId when a primary department exists", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce({ | ||
| id: "ud_1", | ||
| userId: "user_123", | ||
| departmentId: "dept_789", | ||
| isPrimary: true, | ||
| }); | ||
|
|
||
| const result = await service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(result).toEqual({ | ||
| organizationId: "org_456", | ||
| departmentId: "dept_789", | ||
| }); | ||
| }); | ||
|
|
||
| it("returns null departmentId when no primary department row exists", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| const result = await service.resolveScope("user_no_dept", "org_456"); | ||
|
|
||
| expect(result).toEqual({ | ||
| organizationId: "org_456", | ||
| departmentId: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("queries userDepartment with userId, isPrimary: true, and organization constraint", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| await service.resolveScope("user_abc", "org_xyz"); | ||
|
|
||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledWith({ | ||
| where: { | ||
| userId: "user_abc", | ||
| isPrimary: true, | ||
| department: { organizationId: "org_xyz" }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("passes organizationId through from the parameter, not from the database", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce({ | ||
| id: "ud_2", | ||
| userId: "user_123", | ||
| departmentId: "dept_from_db", | ||
| isPrimary: true, | ||
| }); | ||
|
|
||
| const result = await service.resolveScope("user_123", "org_from_jwt"); | ||
|
|
||
| expect(result.organizationId).toBe("org_from_jwt"); | ||
| }); | ||
|
|
||
| it("uses departmentId from the matched UserDepartment row, not the row's own id", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce({ | ||
| id: "join_table_row_id", | ||
| userId: "user_123", | ||
| departmentId: "the_real_dept_id", | ||
| isPrimary: true, | ||
| }); | ||
|
|
||
| const result = await service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(result.departmentId).toBe("the_real_dept_id"); | ||
| expect(result.departmentId).not.toBe("join_table_row_id"); | ||
| }); | ||
|
|
||
| it("forwards the exact userId to the prisma query", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| await service.resolveScope("user_exact_id_check", "org_456"); | ||
|
|
||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| where: expect.objectContaining({ userId: "user_exact_id_check" }), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("always queries with isPrimary: true regardless of what the db returns", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce({ | ||
| id: "ud_3", | ||
| userId: "user_123", | ||
| departmentId: "dept_456", | ||
| isPrimary: true, | ||
| }); | ||
|
|
||
| await service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| where: expect.objectContaining({ isPrimary: true }), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("calls userDepartment.findFirst exactly once per resolveScope call", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| await service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("issues separate db queries for separate resolveScope calls", async () => { | ||
| prisma.userDepartment.findFirst | ||
| .mockResolvedValueOnce({ id: "ud_a", userId: "user_a", departmentId: "dept_a", isPrimary: true }) | ||
| .mockResolvedValueOnce(null); | ||
|
|
||
| const [resultA, resultB] = await Promise.all([ | ||
| service.resolveScope("user_a", "org_a"), | ||
| service.resolveScope("user_b", "org_b"), | ||
| ]); | ||
|
|
||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledTimes(2); | ||
| expect(resultA.departmentId).toBe("dept_a"); | ||
| expect(resultB.departmentId).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns a Promise", () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| const returnValue = service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(returnValue).toBeInstanceOf(Promise); | ||
| }); | ||
|
|
||
| it("different users with the same organizationId get independent dept lookups", async () => { | ||
| prisma.userDepartment.findFirst | ||
| .mockResolvedValueOnce({ id: "ud_1", userId: "user_1", departmentId: "dept_for_user1", isPrimary: true }) | ||
| .mockResolvedValueOnce({ id: "ud_2", userId: "user_2", departmentId: "dept_for_user2", isPrimary: true }); | ||
|
|
||
| const result1 = await service.resolveScope("user_1", "shared_org"); | ||
| const result2 = await service.resolveScope("user_2", "shared_org"); | ||
|
|
||
| expect(result1.departmentId).toBe("dept_for_user1"); | ||
| expect(result2.departmentId).toBe("dept_for_user2"); | ||
| expect(result1.organizationId).toBe("shared_org"); | ||
| expect(result2.organizationId).toBe("shared_org"); | ||
| }); | ||
|
|
||
| it("handles an empty string userId without throwing", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce(null); | ||
|
|
||
| const result = await service.resolveScope("", "org_456"); | ||
|
|
||
| expect(result.departmentId).toBeNull(); | ||
| expect(result.organizationId).toBe("org_456"); | ||
| expect(prisma.userDepartment.findFirst).toHaveBeenCalledWith({ | ||
| where: { | ||
| userId: "", | ||
| isPrimary: true, | ||
| department: { organizationId: "org_456" }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("propagates errors thrown by the prisma query", async () => { | ||
| const dbError = new Error("Database connection lost"); | ||
| prisma.userDepartment.findFirst.mockRejectedValueOnce(dbError); | ||
|
|
||
| await expect(service.resolveScope("user_123", "org_456")).rejects.toThrow( | ||
| "Database connection lost", | ||
| ); | ||
| }); | ||
|
|
||
| it("scope object has exactly the organizationId and departmentId keys", async () => { | ||
| prisma.userDepartment.findFirst.mockResolvedValueOnce({ | ||
| id: "ud_1", | ||
| userId: "user_123", | ||
| departmentId: "dept_789", | ||
| isPrimary: true, | ||
| }); | ||
|
|
||
| const result = await service.resolveScope("user_123", "org_456"); | ||
|
|
||
| expect(Object.keys(result).sort()).toEqual( | ||
| ["departmentId", "organizationId"].sort(), | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Injectable } from "@nestjs/common"; | ||
| import { PrismaService } from "../prisma/prisma.service"; | ||
|
|
||
| export interface ResolvedScope { | ||
| organizationId: string; | ||
| departmentId: string | null; | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class IdentityService { | ||
| constructor(private readonly prisma: PrismaService) {} | ||
|
|
||
| /** | ||
| * Resolves the identity scope for a request. | ||
| * | ||
| * The organizationId comes directly from the validated JWT claim. | ||
| * The departmentId is looked up from the UserDepartment join table, | ||
| * selecting the row flagged as the user's Primary Department. | ||
| * The department relation filter on organizationId ensures cross-org | ||
| * isolation: a user cannot accidentally resolve a department that belongs | ||
| * to a different organization. | ||
| * Returns null for departmentId when no primary department is configured. | ||
| */ | ||
| async resolveScope( | ||
| userId: string, | ||
| organizationId: string, | ||
| ): Promise<ResolvedScope> { | ||
| const primaryDept = await this.prisma.userDepartment.findFirst({ | ||
| where: { userId, isPrimary: true, department: { organizationId } }, | ||
| }); | ||
|
|
||
| return { | ||
| organizationId, | ||
| departmentId: primaryDept?.departmentId ?? null, | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.