Files
twenty/packages/twenty-server/src/engine/guards/feature-flag.guard.ts
T
Félix Malfait 514d0017ea Refactor application module architecture for clarity and explicitness (#18432)
## Summary

- **Module reorganization**: Moved `ApplicationUpgradeService` and cron
jobs to `application-upgrade/`, `ApplicationSyncService` to
`application-manifest/`, and
`runWorkspaceMigration`/`uninstallApplication` mutations to the manifest
resolver — each module now has a single clear responsibility.
- **Explicit install flow**: Removed implicit `ApplicationEntity`
creation from `ApplicationSyncService`. The install service and dev
resolver now explicitly create the `ApplicationEntity` before syncing.
npm packages are resolved at registration time to extract manifest
metadata (universalIdentifier, name, description, etc.), eliminating the
`reconcileUniversalIdentifier` hack.
- **Better error handling**: Frontend hooks now surface actual server
error messages in snackbars instead of swallowing them. Replaced the
ugly `ConfirmationModal` for transfer ownership with a proper form
modal. Fixed `SettingsAdminTableCard` row height overflow and corrected
the `yarn-engine` asset path.

## Test plan
- [ ] Register an npm package — verify manifest metadata (name,
description, universalIdentifier) is extracted correctly
- [ ] Install a registered npm app on a workspace — verify
ApplicationEntity is created and sync succeeds
- [ ] Test `app:dev` CLI flow — verify local app registration and sync
work
- [ ] Upload a tarball — verify registration and install flow
- [ ] Transfer ownership — verify the new modal UX works
- [ ] Verify error messages appear correctly in snackbars when
operations fail


Made with [Cursor](https://cursor.com)
2026-03-06 08:45:08 +01:00

72 lines
1.7 KiB
TypeScript

import {
type CanActivate,
type ExecutionContext,
ForbiddenException,
Injectable,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { GqlExecutionContext } from '@nestjs/graphql';
import { type FeatureFlagKey } from 'twenty-shared/types';
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { TypedReflect } from 'src/utils/typed-reflect';
export const FEATURE_FLAG_KEY = 'feature-flag-metadata-args';
export function RequireFeatureFlag(featureFlag: FeatureFlagKey) {
return (
target: object,
_propertyKey?: string,
descriptor?: PropertyDescriptor,
) => {
TypedReflect.defineMetadata(
FEATURE_FLAG_KEY,
featureFlag,
descriptor?.value || target,
);
return descriptor;
};
}
@Injectable()
export class FeatureFlagGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
private readonly featureFlagService: FeatureFlagService,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().req;
const workspaceId = request.workspace?.id;
if (!workspaceId) {
return false;
}
const featureFlag = this.reflector.get<FeatureFlagKey>(
FEATURE_FLAG_KEY,
context.getHandler(),
);
if (!featureFlag) {
return true;
}
const isEnabled = await this.featureFlagService.isFeatureEnabled(
featureFlag,
workspaceId,
);
if (!isEnabled) {
throw new ForbiddenException(
`Feature flag "${featureFlag}" is not enabled for this workspace`,
);
}
return true;
}
}