feat: implement FeatureOptInService (#25805)
* feat: implement FeatureOptInService WIP * clean up * feat: consolidate feature repositories and add updateFeatureForUser - Implement updateFeatureForUser in FeaturesRepository (similar to updateFeatureForTeam) - Move getUserFeatureState and getTeamFeatureState from PrismaFeatureOptInRepository to FeaturesRepository - Update FeatureOptInService to use only FeaturesRepository - Add setUserFeatureState and setTeamFeatureState methods to FeatureOptInService - Update _router.ts to remove PrismaFeatureOptInRepository usage - Remove PrismaFeatureOptInRepository.ts and FeatureOptInRepositoryInterface.ts - Update features.repository.interface.ts and features.repository.mock.ts - Add integration tests for updateFeatureForUser, getUserFeatureState, getTeamFeatureState - Update service.integration-test.ts to use FeaturesRepository Co-Authored-By: [email protected] <[email protected]> * refactor: rename updateFeatureForUser to setUserFeatureState Rename to match the convention used for setTeamFeatureState Co-Authored-By: [email protected] <[email protected]> * refactor: return FeatureState type from getUserFeatureState and getTeamFeatureState * fix integration tests * clean up logics * update services and router * refactor: change getUserFeatureState and getTeamFeatureState to accept featureIds array - Renamed getUserFeatureState to getUserFeatureStates - Renamed getTeamFeatureState to getTeamFeatureStates - Changed parameter from featureId: string to featureIds: string[] - Changed return type from FeatureState to Record<string, FeatureState> - Updated FeatureOptInService to use the new batch methods - Added tests for querying multiple features in a single call - Optimized listFeaturesForTeam to fetch all feature states in one query Co-Authored-By: [email protected] <[email protected]> * feat: add getFeatureStateForTeams for batch querying multiple teams - Added getFeatureStateForTeams method to query a single feature across multiple teams in one call - Updated FeatureOptInService.resolveFeatureStateAcrossTeams to use the new batch method - Replaces N+1 queries with a single database query for team states - Added comprehensive integration tests for the new method Co-Authored-By: [email protected] <[email protected]> * refactor: combine org and team state queries into single call - Include orgId in the teamIds array passed to getFeatureStateForTeams - Extract org state and team states from the combined result - Reduces database queries from 3 to 2 in resolveFeatureStateAcrossTeams Co-Authored-By: [email protected] <[email protected]> * refactor: use team.isOrganization and clarify computeEffectiveState comment Co-Authored-By: [email protected] <[email protected]> * refactor: use MembershipRepository.findAllByUserId with isOrganization Co-Authored-By: [email protected] <[email protected]> * feat: add featureId validation using isOptInFeature type guard Co-Authored-By: [email protected] <[email protected]> * less queries * add fallback value * fix type error * move files * add autoOptInFeatures column * use autoOptInFeatures flag within FeatureOptInService * add setUserAutoOptIn and setTeamAutoOptIn * fix computeEffectiveState logic * rewrite computeEffectiveState * clean up integration tests * clean up in afterEach * fix type error * refactor: use FeaturesRepository methods instead of direct Prisma calls Replace all manual userFeatures and teamFeatures Prisma operations with the new setUserFeatureState and setTeamFeatureState repository methods. Changes include: - Admin handlers (assignFeatureToTeam, unassignFeatureFromTeam) - Test fixtures and integration tests - Playwright fixtures - Development scripts This ensures consistent feature flag management through the repository pattern and supports the new tri-state semantics (enabled/disabled/inherit). Co-Authored-By: [email protected] <[email protected]> * clean up * fix the logic * extract some logic into applyAutoOptIn() * remove wrong code * refactor: convert setUserFeatureState and setTeamFeatureState to object params with discriminated union - Convert multiple positional parameters to single object parameter - Use discriminated union types: assignedBy required for enabled/disabled, omitted for inherit - Update all callers across repository, service, handlers, fixtures, and tests * fix type error * use Promise.all * fix --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
750408ec21
commit
f4248bf20d
35 files changed
+3564
-450
No files matched your search
+79
-60
@@ -1,5 +1,7 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, beforeAll } from "vitest";
|
||||
|
||||
import type { FeatureId } from "@calcom/features/flags/config";
|
||||
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
@@ -8,12 +10,14 @@ import { PermissionRepository } from "../PermissionRepository";
|
||||
|
||||
describe("PermissionRepository - Integration Tests", () => {
|
||||
let repository: PermissionRepository;
|
||||
let featuresRepository: FeaturesRepository;
|
||||
let testRoleId: string;
|
||||
let testUserId: number;
|
||||
let testTeamId: number;
|
||||
|
||||
beforeAll(async () => {
|
||||
repository = new PermissionRepository(prisma);
|
||||
featuresRepository = new FeaturesRepository(prisma);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -58,8 +62,10 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
await prisma.role.deleteMany({
|
||||
where: { id: testRoleId },
|
||||
});
|
||||
await prisma.teamFeatures.deleteMany({
|
||||
where: { teamId: testTeamId },
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "inherit",
|
||||
});
|
||||
await prisma.team.deleteMany({
|
||||
where: { id: testTeamId },
|
||||
@@ -340,13 +346,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
|
||||
it("should return team IDs for PBAC-enabled team with matching permissions", async () => {
|
||||
// Enable PBAC for the team
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create membership with custom role
|
||||
@@ -386,13 +390,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
|
||||
it("should not return team IDs when permissions do not match", async () => {
|
||||
// Enable PBAC for the team
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create membership with custom role
|
||||
@@ -484,13 +486,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
});
|
||||
|
||||
// Enable PBAC for org
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: org.id,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: org.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create org role
|
||||
@@ -547,7 +547,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
await prisma.rolePermission.deleteMany({ where: { roleId: orgRole.id } });
|
||||
await prisma.membership.deleteMany({ where: { userId: testUserId } });
|
||||
await prisma.role.deleteMany({ where: { id: orgRole.id } });
|
||||
await prisma.teamFeatures.deleteMany({ where: { teamId: org.id } });
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: org.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "inherit",
|
||||
});
|
||||
await prisma.team.deleteMany({ where: { id: { in: [org.id, childTeam.id] } } });
|
||||
});
|
||||
|
||||
@@ -599,13 +603,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
|
||||
it("should handle wildcard permissions for PBAC teams", async () => {
|
||||
// Enable PBAC for the team
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create membership with custom role
|
||||
@@ -645,13 +647,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
|
||||
it("should require all permissions to match (not just some)", async () => {
|
||||
// Enable PBAC for the team
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create membership with custom role
|
||||
@@ -685,13 +685,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
|
||||
it("should not return teams for non-accepted memberships", async () => {
|
||||
// Enable PBAC for the team
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: testTeamId,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create membership with accepted: false
|
||||
@@ -755,12 +753,20 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
});
|
||||
|
||||
// Enable PBAC for both teams
|
||||
await prisma.teamFeatures.createMany({
|
||||
data: [
|
||||
{ teamId: team1.id, featureId: "pbac", assignedBy: "test", enabled: true },
|
||||
{ teamId: team2.id, featureId: "pbac", assignedBy: "test", enabled: true },
|
||||
],
|
||||
});
|
||||
await Promise.all([
|
||||
featuresRepository.setTeamFeatureState({
|
||||
teamId: team1.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
}),
|
||||
featuresRepository.setTeamFeatureState({
|
||||
teamId: team2.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
}),
|
||||
]);
|
||||
|
||||
// Create memberships for both teams
|
||||
const membership1 = await prisma.membership.create({
|
||||
@@ -825,7 +831,18 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
await prisma.rolePermission.deleteMany({ where: { roleId: { in: [role1.id, role2.id] } } });
|
||||
await prisma.membership.deleteMany({ where: { userId: testUserId } });
|
||||
await prisma.role.deleteMany({ where: { id: { in: [role1.id, role2.id] } } });
|
||||
await prisma.teamFeatures.deleteMany({ where: { teamId: { in: [team1.id, team2.id] } } });
|
||||
await Promise.all([
|
||||
featuresRepository.setTeamFeatureState({
|
||||
teamId: team1.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "inherit",
|
||||
}),
|
||||
featuresRepository.setTeamFeatureState({
|
||||
teamId: team2.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "inherit",
|
||||
}),
|
||||
]);
|
||||
await prisma.team.deleteMany({ where: { id: { in: [team1.id, team2.id] } } });
|
||||
});
|
||||
|
||||
@@ -855,13 +872,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
});
|
||||
|
||||
// Enable PBAC for first team only
|
||||
await prisma.teamFeatures.create({
|
||||
data: {
|
||||
teamId: team1.id,
|
||||
featureId: "pbac",
|
||||
assignedBy: "test",
|
||||
enabled: true,
|
||||
},
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: team1.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "enabled",
|
||||
assignedBy: "test",
|
||||
});
|
||||
|
||||
// Create PBAC membership
|
||||
@@ -914,7 +929,11 @@ describe("PermissionRepository - Integration Tests", () => {
|
||||
await prisma.rolePermission.deleteMany({ where: { roleId: role1.id } });
|
||||
await prisma.membership.deleteMany({ where: { userId: testUserId } });
|
||||
await prisma.role.deleteMany({ where: { id: role1.id } });
|
||||
await prisma.teamFeatures.deleteMany({ where: { teamId: team1.id } });
|
||||
await featuresRepository.setTeamFeatureState({
|
||||
teamId: team1.id,
|
||||
featureId: "pbac" as FeatureId,
|
||||
state: "inherit",
|
||||
});
|
||||
await prisma.team.deleteMany({ where: { id: { in: [team1.id, team2.id] } } });
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user