The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Stamp the requesting application on created call recordings (#26685)
martmull18ad48ea8a83
diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.module.ts b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.module.tsindex 01c55d87f91..907a2b19d1c 100644--- a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.module.ts+++ b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.module.ts@@ -7,6 +7,7 @@ import { WorkspaceQueryHookExplorer } from 'src/engine/api/graphql/workspace-queimport { WorkspaceQueryHookService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/workspace-query-hook.service';import { AgentChatThreadQueryHookModule } from 'src/engine/metadata-modules/ai/ai-chat/query-hooks/agent-chat-thread-query-hook.module';import { BlocklistQueryHookModule } from 'src/modules/blocklist/query-hooks/blocklist-query-hook.module';+import { CallRecordingQueryHookModule } from 'src/modules/call-recording/query-hooks/call-recording-query-hook.module';import { CalendarQueryHookModule } from 'src/modules/calendar/common/query-hooks/calendar-query-hook.module';import { DashboardQueryHookModule } from 'src/modules/dashboard/query-hooks/dashboard-query-hook.module';import { MessagingQueryHookModule } from 'src/modules/messaging/common/query-hooks/messaging-query-hook.module';@@ -20,6 +21,7 @@ import { WorkspaceMemberQueryHookModule } from 'src/modules/workspace-member/queAgentChatThreadQueryHookModule,MessagingQueryHookModule,CalendarQueryHookModule,+ CallRecordingQueryHookModule,DashboardQueryHookModule,BlocklistQueryHookModule,WorkspaceMemberQueryHookModule,diff --git a/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-many.pre-query-hook.ts b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-many.pre-query-hook.tsnew file mode 100644index 00000000000..12807efa653--- /dev/null+++ b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-many.pre-query-hook.ts@@ -0,0 +1,24 @@+import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';+import { type CreateManyResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';++import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';+import { type RecordInput } from 'src/engine/core-modules/actor/services/actor-from-auth-context.service';+import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { stampCallRecordingApplicationId } from 'src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util';++@WorkspaceQueryHook('callRecording.createMany')+export class CallRecordingCreateManyPreQueryHook implements WorkspacePreQueryHookInstance {+ async execute(+ authContext: WorkspaceAuthContext,+ _objectName: string,+ payload: CreateManyResolverArgs<RecordInput>,+ ): Promise<CreateManyResolverArgs<RecordInput>> {+ return {+ ...payload,+ data: stampCallRecordingApplicationId({+ authContext,+ records: payload.data,+ }),+ };+ }+}diff --git a/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-one.pre-query-hook.ts b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-one.pre-query-hook.tsnew file mode 100644index 00000000000..f90af6b2a29--- /dev/null+++ b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-create-one.pre-query-hook.ts@@ -0,0 +1,23 @@+import { type WorkspacePreQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';+import { type CreateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';++import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';+import { type RecordInput } from 'src/engine/core-modules/actor/services/actor-from-auth-context.service';+import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { stampCallRecordingApplicationId } from 'src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util';++@WorkspaceQueryHook('callRecording.createOne')+export class CallRecordingCreateOnePreQueryHook implements WorkspacePreQueryHookInstance {+ async execute(+ authContext: WorkspaceAuthContext,+ _objectName: string,+ payload: CreateOneResolverArgs<RecordInput>,+ ): Promise<CreateOneResolverArgs<RecordInput>> {+ const [data] = stampCallRecordingApplicationId({+ authContext,+ records: [payload.data],+ });++ return { ...payload, data };+ }+}diff --git a/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-query-hook.module.ts b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-query-hook.module.tsnew file mode 100644index 00000000000..ea625a0d7ad--- /dev/null+++ b/packages/twenty-server/src/modules/call-recording/query-hooks/call-recording-query-hook.module.ts@@ -0,0 +1,12 @@+import { Module } from '@nestjs/common';++import { CallRecordingCreateManyPreQueryHook } from 'src/modules/call-recording/query-hooks/call-recording-create-many.pre-query-hook';+import { CallRecordingCreateOnePreQueryHook } from 'src/modules/call-recording/query-hooks/call-recording-create-one.pre-query-hook';++@Module({+ providers: [+ CallRecordingCreateOnePreQueryHook,+ CallRecordingCreateManyPreQueryHook,+ ],+})+export class CallRecordingQueryHookModule {}diff --git a/packages/twenty-server/src/modules/call-recording/query-hooks/utils/__tests__/stamp-call-recording-application-id.util.spec.ts b/packages/twenty-server/src/modules/call-recording/query-hooks/utils/__tests__/stamp-call-recording-application-id.util.spec.tsnew file mode 100644index 00000000000..3ca887eda41--- /dev/null+++ b/packages/twenty-server/src/modules/call-recording/query-hooks/utils/__tests__/stamp-call-recording-application-id.util.spec.ts@@ -0,0 +1,54 @@+import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { stampCallRecordingApplicationId } from 'src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util';++const APPLICATION_ID = '00000000-0000-4000-8000-000000000001';+const FORGED_APPLICATION_ID = '00000000-0000-4000-8000-000000000002';++const workspace = { id: '00000000-0000-4000-8000-000000000010' };+const application = { id: APPLICATION_ID };++const buildAuthContext = (authContext: object) =>+ ({ workspace, ...authContext }) as unknown as WorkspaceAuthContext;++describe('stampCallRecordingApplicationId', () => {+ it('stamps the application id when an application token creates the record', () => {+ expect(+ stampCallRecordingApplicationId({+ authContext: buildAuthContext({ type: 'application', application }),+ records: [{ title: 'Weekly sync' }],+ }),+ ).toEqual([{ title: 'Weekly sync', applicationId: APPLICATION_ID }]);+ });++ it('stamps the application id when a user token issued to an application creates the record', () => {+ expect(+ stampCallRecordingApplicationId({+ authContext: buildAuthContext({ type: 'user', application }),+ records: [{ title: 'Weekly sync' }, { title: 'Demo' }],+ }),+ ).toEqual([+ { title: 'Weekly sync', applicationId: APPLICATION_ID },+ { title: 'Demo', applicationId: APPLICATION_ID },+ ]);+ });++ it('overrides a client-provided application id', () => {+ expect(+ stampCallRecordingApplicationId({+ authContext: buildAuthContext({ type: 'application', application }),+ records: [{ applicationId: FORGED_APPLICATION_ID }],+ }),+ ).toEqual([{ applicationId: APPLICATION_ID }]);+ });++ it('leaves records untouched when no application is involved', () => {+ const records = [{ title: 'Weekly sync' }];++ expect(+ stampCallRecordingApplicationId({+ authContext: buildAuthContext({ type: 'user' }),+ records,+ }),+ ).toBe(records);+ });+});diff --git a/packages/twenty-server/src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util.ts b/packages/twenty-server/src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util.tsnew file mode 100644index 00000000000..cb582a3ca32--- /dev/null+++ b/packages/twenty-server/src/modules/call-recording/query-hooks/utils/stamp-call-recording-application-id.util.ts@@ -0,0 +1,32 @@+import { isDefined } from 'twenty-shared/utils';++import { type RecordInput } from 'src/engine/core-modules/actor/services/actor-from-auth-context.service';+import { isApplicationAuthContext } from 'src/engine/core-modules/auth/guards/is-application-auth-context.guard';+import { isUserAuthContext } from 'src/engine/core-modules/auth/guards/is-user-auth-context.guard';+import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';++const getRequestingApplicationId = (+ authContext: WorkspaceAuthContext,+): string | undefined => {+ if (isApplicationAuthContext(authContext) || isUserAuthContext(authContext)) {+ return authContext.application?.id;+ }++ return undefined;+};++export const stampCallRecordingApplicationId = ({+ authContext,+ records,+}: {+ authContext: WorkspaceAuthContext;+ records: RecordInput[];+}): RecordInput[] => {+ const applicationId = getRequestingApplicationId(authContext);++ if (!isDefined(applicationId)) {+ return records;+ }++ return records.map((record) => ({ ...record, applicationId }));+};