f4ead89956
## Summary Follow-up to #20953. Migrates 23 of the 30 entities that were left in `WORKSPACE_SCOPED_EXEMPTIONS` last time, so the lint rule's workspaceId-enforcement default now covers most of the core/metadata schema. ### Migrated (23 entities, 88 files, 22 commits) | Family | Entities | |---|---| | Trivial caches | `NavigationMenuItem`, `Skill`, `DataSource`, `Webhook`, `CommandMenuItem`, `IndexMetadata` | | Views | `View`, `ViewField`, `ViewFieldGroup`, `ViewFilter`, `ViewFilterGroup`, `ViewGroup`, `ViewSort` | | Layouts | `PageLayout`, `PageLayoutTab`, `PageLayoutWidget` | | Roles & permissions | `Role`, `RoleTarget`, `PermissionFlag`, `ObjectPermission`, `FieldPermission`, `RowLevelPermissionPredicate`, `RowLevelPermissionPredicateGroup` | For each entity: swap `@InjectRepository(X)` → `@InjectWorkspaceScopedRepository(X)` (and the field type → `WorkspaceScopedRepository<X>`); rewrite every call site to pass `workspaceId` as the first arg (stripped from `where`/criteria — the wrapper throws if you include it now); register `provideWorkspaceScopedRepository(X)` in every owning NestJS module; update affected spec providers to `getWorkspaceScopedRepositoryToken(X)`. ### Rule update - `ApplicationRegistrationVariableEntity` was misclassified — moved to `STRUCTURAL_EXEMPTIONS` (no `workspaceId` column; it's keyed on `applicationRegistrationId` at the instance level). - 22 of the 23 migrated entities removed from `WORKSPACE_SCOPED_EXEMPTIONS` entirely (zero remaining raw `@InjectRepository` sites). - `RoleTargetEntity` also removed; one call site in `user-workspace.service.ts` keeps a raw injection with an `eslint-disable` + reason because `softRemove(...)` is not on the wrapper API yet (the migration would require threading `workspaceId` through `deleteUserWorkspace`'s three callers). ### Still exempted (7 entities, follow-up PRs) | Entity | Why deferred | |---|---| | `ApplicationEntity` | ~50 sites with several cross-workspace lookups by id (auth, OAuth, file-storage, cleanup) | | `CalendarChannelEntity` / `MessageChannelEntity` | Use `.increment(...)` (not on wrapper) and `repository.manager.transaction(...)` — wrapper needs to grow `.increment` + the transaction sites need `withManager` or dual-inject | | `FieldMetadataEntity` / `ObjectMetadataEntity` | The metadata services `extends TypeOrmQueryService<X>` and `super(rawRepo)` — requires dual-inject or reworking the inheritance | | `KeyValuePairEntity` | Allows `workspaceId: IsNull()` for instance-level config; wrapper rejects null | | `UpgradeMigrationEntity` | Same — instance-level + cross-workspace ledger | ## Test plan - [x] `npx nx typecheck twenty-server` — clean - [x] `npx nx lint twenty-server` — clean (0/0) - [x] All 10 affected unit specs pass (115 tests) — api-key, agent-role, permissions, workspace-roles-permissions-cache, view-filter-group, workflow-version-step-operations, two-factor-authentication (service + resolver), user-workspace, file - [ ] Server integration tests in CI
275 lines
10 KiB
TypeScript
275 lines
10 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
import { IsNull, Repository } from 'typeorm';
|
|
|
|
import { ApplicationEntity } from 'src/engine/core-modules/application/application.entity';
|
|
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
|
|
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
|
|
import { findFlatEntityByIdInFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps-or-throw.util';
|
|
import { fromCreateWebhookInputToFlatWebhookToCreate } from 'src/engine/metadata-modules/flat-webhook/utils/from-create-webhook-input-to-flat-webhook-to-create.util';
|
|
import { fromDeleteWebhookInputToFlatWebhookOrThrow } from 'src/engine/metadata-modules/flat-webhook/utils/from-delete-webhook-input-to-flat-webhook-or-throw.util';
|
|
import { fromFlatWebhookToWebhookDto } from 'src/engine/metadata-modules/flat-webhook/utils/from-flat-webhook-to-webhook-dto.util';
|
|
import { fromUpdateWebhookInputToFlatWebhookToUpdateOrThrow } from 'src/engine/metadata-modules/flat-webhook/utils/from-update-webhook-input-to-flat-webhook-to-update-or-throw.util';
|
|
import { fromWebhookEntityToFlatWebhook } from 'src/engine/metadata-modules/flat-webhook/utils/from-webhook-entity-to-flat-webhook.util';
|
|
import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';
|
|
import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';
|
|
import { type CreateWebhookInput } from 'src/engine/metadata-modules/webhook/dtos/create-webhook.input';
|
|
import { type UpdateWebhookInput } from 'src/engine/metadata-modules/webhook/dtos/update-webhook.input';
|
|
import { type WebhookDTO } from 'src/engine/metadata-modules/webhook/dtos/webhook.dto';
|
|
import { WebhookEntity } from 'src/engine/metadata-modules/webhook/entities/webhook.entity';
|
|
import { createIdToUniversalIdentifierMap } from 'src/engine/workspace-cache/utils/create-id-to-universal-identifier-map.util';
|
|
import { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
|
|
import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';
|
|
|
|
@Injectable()
|
|
export class WebhookService {
|
|
constructor(
|
|
@InjectWorkspaceScopedRepository(WebhookEntity)
|
|
private readonly webhookRepository: WorkspaceScopedRepository<WebhookEntity>,
|
|
@InjectRepository(ApplicationEntity)
|
|
private readonly applicationRepository: Repository<ApplicationEntity>,
|
|
private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,
|
|
private readonly workspaceManyOrAllFlatEntityMapsCacheService: WorkspaceManyOrAllFlatEntityMapsCacheService,
|
|
private readonly applicationService: ApplicationService,
|
|
) {}
|
|
|
|
private normalizeTargetUrl(targetUrl: string): string {
|
|
try {
|
|
const url = new URL(targetUrl);
|
|
|
|
return url.toString();
|
|
} catch {
|
|
return targetUrl;
|
|
}
|
|
}
|
|
|
|
async findAll(workspaceId: string): Promise<WebhookDTO[]> {
|
|
const [webhooks, applications] = await Promise.all([
|
|
this.webhookRepository.find(workspaceId, {
|
|
where: { deletedAt: IsNull() },
|
|
order: { createdAt: 'ASC' },
|
|
}),
|
|
this.applicationRepository.find({
|
|
where: { workspaceId },
|
|
select: ['id', 'universalIdentifier'],
|
|
}),
|
|
]);
|
|
|
|
const applicationIdToUniversalIdentifierMap =
|
|
createIdToUniversalIdentifierMap(applications);
|
|
|
|
return webhooks
|
|
.map((webhookEntity) =>
|
|
fromWebhookEntityToFlatWebhook({
|
|
entity: webhookEntity,
|
|
applicationIdToUniversalIdentifierMap,
|
|
}),
|
|
)
|
|
.map(fromFlatWebhookToWebhookDto);
|
|
}
|
|
|
|
async findById(id: string, workspaceId: string): Promise<WebhookDTO | null> {
|
|
const [webhook, applications] = await Promise.all([
|
|
this.webhookRepository.findOne(workspaceId, {
|
|
where: { id, deletedAt: IsNull() },
|
|
}),
|
|
this.applicationRepository.find({
|
|
where: { workspaceId },
|
|
select: ['id', 'universalIdentifier'],
|
|
}),
|
|
]);
|
|
|
|
if (!isDefined(webhook)) {
|
|
return null;
|
|
}
|
|
|
|
const applicationIdToUniversalIdentifierMap =
|
|
createIdToUniversalIdentifierMap(applications);
|
|
|
|
return fromFlatWebhookToWebhookDto(
|
|
fromWebhookEntityToFlatWebhook({
|
|
entity: webhook,
|
|
applicationIdToUniversalIdentifierMap,
|
|
}),
|
|
);
|
|
}
|
|
|
|
async create(
|
|
input: CreateWebhookInput,
|
|
workspaceId: string,
|
|
): Promise<WebhookDTO> {
|
|
const normalizedTargetUrl = this.normalizeTargetUrl(input.targetUrl);
|
|
|
|
const { workspaceCustomFlatApplication } =
|
|
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
|
{ workspaceId },
|
|
);
|
|
|
|
const flatWebhookToCreate = fromCreateWebhookInputToFlatWebhookToCreate({
|
|
createWebhookInput: {
|
|
...input,
|
|
targetUrl: normalizedTargetUrl,
|
|
},
|
|
workspaceId,
|
|
flatApplication: workspaceCustomFlatApplication,
|
|
});
|
|
|
|
const validateAndBuildResult =
|
|
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
|
{
|
|
allFlatEntityOperationByMetadataName: {
|
|
webhook: {
|
|
flatEntityToCreate: [flatWebhookToCreate],
|
|
flatEntityToDelete: [],
|
|
flatEntityToUpdate: [],
|
|
},
|
|
},
|
|
workspaceId,
|
|
applicationUniversalIdentifier:
|
|
workspaceCustomFlatApplication.universalIdentifier,
|
|
},
|
|
);
|
|
|
|
if (validateAndBuildResult.status === 'fail') {
|
|
throw new WorkspaceMigrationBuilderException(
|
|
validateAndBuildResult,
|
|
'Multiple validation errors occurred while creating webhook',
|
|
);
|
|
}
|
|
|
|
const { flatWebhookMaps: recomputedFlatWebhookMaps } =
|
|
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
|
{
|
|
workspaceId,
|
|
flatMapsKeys: ['flatWebhookMaps'],
|
|
},
|
|
);
|
|
|
|
return fromFlatWebhookToWebhookDto(
|
|
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
|
flatEntityId: flatWebhookToCreate.id,
|
|
flatEntityMaps: recomputedFlatWebhookMaps,
|
|
}),
|
|
);
|
|
}
|
|
|
|
async update(
|
|
input: UpdateWebhookInput,
|
|
workspaceId: string,
|
|
): Promise<WebhookDTO> {
|
|
const { workspaceCustomFlatApplication } =
|
|
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
|
{ workspaceId },
|
|
);
|
|
|
|
const normalizedInput = {
|
|
...input,
|
|
update: {
|
|
...input.update,
|
|
...(isDefined(input.update.targetUrl) && {
|
|
targetUrl: this.normalizeTargetUrl(input.update.targetUrl),
|
|
}),
|
|
},
|
|
};
|
|
|
|
const { flatWebhookMaps: existingFlatWebhookMaps } =
|
|
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
|
{
|
|
workspaceId,
|
|
flatMapsKeys: ['flatWebhookMaps'],
|
|
},
|
|
);
|
|
|
|
const flatWebhookToUpdate =
|
|
fromUpdateWebhookInputToFlatWebhookToUpdateOrThrow({
|
|
flatWebhookMaps: existingFlatWebhookMaps,
|
|
updateWebhookInput: normalizedInput,
|
|
});
|
|
|
|
const validateAndBuildResult =
|
|
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
|
{
|
|
allFlatEntityOperationByMetadataName: {
|
|
webhook: {
|
|
flatEntityToCreate: [],
|
|
flatEntityToDelete: [],
|
|
flatEntityToUpdate: [flatWebhookToUpdate],
|
|
},
|
|
},
|
|
workspaceId,
|
|
applicationUniversalIdentifier:
|
|
workspaceCustomFlatApplication.universalIdentifier,
|
|
},
|
|
);
|
|
|
|
if (validateAndBuildResult.status === 'fail') {
|
|
throw new WorkspaceMigrationBuilderException(
|
|
validateAndBuildResult,
|
|
'Multiple validation errors occurred while updating webhook',
|
|
);
|
|
}
|
|
|
|
const { flatWebhookMaps: recomputedFlatWebhookMaps } =
|
|
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
|
{
|
|
workspaceId,
|
|
flatMapsKeys: ['flatWebhookMaps'],
|
|
},
|
|
);
|
|
|
|
return fromFlatWebhookToWebhookDto(
|
|
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
|
flatEntityId: input.id,
|
|
flatEntityMaps: recomputedFlatWebhookMaps,
|
|
}),
|
|
);
|
|
}
|
|
|
|
async delete(id: string, workspaceId: string): Promise<WebhookDTO> {
|
|
const { workspaceCustomFlatApplication } =
|
|
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
|
{ workspaceId },
|
|
);
|
|
|
|
const { flatWebhookMaps: existingFlatWebhookMaps } =
|
|
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
|
{
|
|
workspaceId,
|
|
flatMapsKeys: ['flatWebhookMaps'],
|
|
},
|
|
);
|
|
|
|
const flatWebhookToDelete = fromDeleteWebhookInputToFlatWebhookOrThrow({
|
|
flatWebhookMaps: existingFlatWebhookMaps,
|
|
webhookId: id,
|
|
});
|
|
|
|
const validateAndBuildResult =
|
|
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
|
{
|
|
allFlatEntityOperationByMetadataName: {
|
|
webhook: {
|
|
flatEntityToCreate: [],
|
|
flatEntityToDelete: [flatWebhookToDelete],
|
|
flatEntityToUpdate: [],
|
|
},
|
|
},
|
|
workspaceId,
|
|
applicationUniversalIdentifier:
|
|
workspaceCustomFlatApplication.universalIdentifier,
|
|
},
|
|
);
|
|
|
|
if (validateAndBuildResult.status === 'fail') {
|
|
throw new WorkspaceMigrationBuilderException(
|
|
validateAndBuildResult,
|
|
'Multiple validation errors occurred while deleting webhook',
|
|
);
|
|
}
|
|
|
|
return fromFlatWebhookToWebhookDto(flatWebhookToDelete);
|
|
}
|
|
}
|