Skip to content

The open alternative to Salesforce, designed for AI.

Clear a Recording Bot On the call recorder cannot honor (#25543)

nitin3f63bd9d098d
diff --git a/packages/twenty-apps/public/call-recorder/src/__tests__/call-recorder-lifecycle.integration-test.ts b/packages/twenty-apps/public/call-recorder/src/__tests__/call-recorder-lifecycle.integration-test.ts
index c6cc784d2ff..a4a5dfab390 100644
--- a/packages/twenty-apps/public/call-recorder/src/__tests__/call-recorder-lifecycle.integration-test.ts
+++ b/packages/twenty-apps/public/call-recorder/src/__tests__/call-recorder-lifecycle.integration-test.ts
@@ -1212,10 +1212,83 @@ describe('call recorder app lifecycle (integration)', () => {
expect(result).toEqual({
skipped: true,
- reason: 'no relevant calendar event change',
+ reason: 'preference change on a meeting whose bot is already requested',
});
});
+ it('schedules a bot when a user sets On on an eligible meeting that has none yet', async () => {
+ const calendarEventId = await createCalendarEvent({
+ callRecorderPreference: 'ON',
+ });
+
+ const result = await deliverCalendarEventUpdate({
+ calendarEventId,
+ updatedFields: ['callRecorderPreference'],
+ before: { callRecorderPreference: null },
+ after: { callRecorderPreference: 'ON' },
+ });
+
+ expect(result).toEqual(expect.objectContaining({ reconciled: true }));
+ expect(await fetchCallRecorderPreference(calendarEventId)).toBe('ON');
+
+ const callRecording = (
+ await findCallRecordings({ calendarEventId: { in: [calendarEventId] } })
+ )[0];
+
+ expect(callRecording).toBeDefined();
+ expect(callRecording.recordingRequestStatus).toBe('REQUESTED');
+ expect(callRecording.externalBotId).toBeTruthy();
+ });
+
+ it('clears an On set by hand on a meeting that already ended', async () => {
+ const calendarEventId = await createCalendarEvent({
+ startsAt: hoursAgo(3),
+ endsAt: hoursAgo(2),
+ callRecorderPreference: 'ON',
+ });
+
+ const result = await deliverCalendarEventUpdate({
+ calendarEventId,
+ updatedFields: ['callRecorderPreference'],
+ before: { callRecorderPreference: null },
+ after: { callRecorderPreference: 'ON' },
+ });
+
+ expect(result).toEqual(expect.objectContaining({ reconciled: true }));
+ expect(await fetchCallRecorderPreference(calendarEventId)).toBeNull();
+ expect(
+ await findCallRecordings({
+ calendarEventId: { in: [calendarEventId] },
+ }),
+ ).toEqual([]);
+ });
+
+ it('clears an On set by hand while the workspace recording switch is off', async () => {
+ vi.stubEnv(
+ CALL_RECORDER_CALENDAR_BOT_SCHEDULING_ENABLED_ENV_VAR_NAME,
+ 'false',
+ );
+
+ const calendarEventId = await createCalendarEvent({
+ callRecorderPreference: 'ON',
+ });
+
+ const result = await deliverCalendarEventUpdate({
+ calendarEventId,
+ updatedFields: ['callRecorderPreference'],
+ before: { callRecorderPreference: null },
+ after: { callRecorderPreference: 'ON' },
+ });
+
+ expect(result).toEqual(expect.objectContaining({ reconciled: true }));
+ expect(await fetchCallRecorderPreference(calendarEventId)).toBeNull();
+ expect(
+ await findCallRecordings({
+ calendarEventId: { in: [calendarEventId] },
+ }),
+ ).toEqual([]);
+ });
+
it('schedules a bot and marks the event On when a user clears an Off', async () => {
const calendarEventId = await createCalendarEvent({
callRecorderPreference: 'OFF',
@@ -1394,6 +1467,49 @@ describe('call recorder app lifecycle (integration)', () => {
expect(await fetchCallRecorderPreference(calendarEventId)).toBeNull();
});
+ it('restores the bot when a user sets On after a pause left a canceled request', async () => {
+ const { calendarEventId, callRecordingId } =
+ await scheduleRecordingThroughCalendarReconciliation();
+
+ turnRecordingOff();
+ await syncCalendarBotSchedulingHandler();
+ await reconcileCallRecorderForCalendarEventIds({
+ client,
+ calendarEventIds: [calendarEventId],
+ });
+
+ expect(
+ (await fetchCallRecording(callRecordingId)).recordingRequestStatus,
+ ).toBe('CANCELED');
+
+ vi.unstubAllEnvs();
+
+ await client.mutation({
+ updateCalendarEvent: {
+ __args: {
+ id: calendarEventId,
+ data: { callRecorderPreference: 'ON' },
+ },
+ id: true,
+ },
+ });
+
+ const result = await deliverCalendarEventUpdate({
+ calendarEventId,
+ updatedFields: ['callRecorderPreference'],
+ before: { callRecorderPreference: null },
+ after: { callRecorderPreference: 'ON' },
+ });
+
+ expect(result).toEqual(expect.objectContaining({ reconciled: true }));
+
+ const callRecording = await fetchCallRecording(callRecordingId);
+
+ expect(callRecording.recordingRequestStatus).toBe('REQUESTED');
+ expect(callRecording.externalBotId).toBeTruthy();
+ expect(await fetchCallRecorderPreference(calendarEventId)).toBe('ON');
+ });
+
it('enqueues the upcoming-events sweep when turned back on', async () => {
vi.stubEnv(
CALL_RECORDER_CALENDAR_BOT_SCHEDULING_ENABLED_ENV_VAR_NAME,
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/reconcile-call-recorder-calendar-event.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/reconcile-call-recorder-calendar-event.ts
index 940c89d20eb..634f73d2a3d 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/reconcile-call-recorder-calendar-event.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/reconcile-call-recorder-calendar-event.ts
@@ -7,9 +7,14 @@ import {
} from 'twenty-sdk/define';
import { CallRecorderPreference } from 'src/constants/call-recorder-preference';
+import { CallRecordingRequestStatus } from 'src/logic-functions/constants/call-recording-request-status';
import { CALENDAR_EVENT_RECONCILIATION_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';
import { type RemovedCallRecorderOccurrence } from 'src/logic-functions/types/removed-call-recorder-occurrence.type';
+import { buildCallRecorderPolicyResult } from 'src/logic-functions/domain/build-call-recorder-policy-result.util';
+import { computeCallRecordingIdForMeeting } from 'src/logic-functions/domain/compute-call-recording-id-for-meeting.util';
import { computeRealMeetingKey } from 'src/logic-functions/domain/compute-real-meeting-key.util';
+import { fetchCalendarEventsByIds } from 'src/logic-functions/data/fetch-calendar-events-by-ids.util';
+import { findCallRecordingsByIds } from 'src/logic-functions/data/find-call-recordings-by-ids.util';
import { getUniqueSortedIds } from 'src/logic-functions/utils/get-unique-sorted-ids.util';
import { reconcileCallRecorderForCalendarEventIds } from 'src/logic-functions/flows/reconcile-call-recorder.util';
import { resolveConferenceLinkUrl } from 'src/logic-functions/domain/resolve-conference-link-url.util';
@@ -80,6 +85,18 @@ const handler = async (
}
const client = new CoreApiClient();
+
+ // Skips only the echo of the app's own On write; any other blank/On change reconciles, which schedules a missing bot or clears an On the policy cannot honor.
+ if (
+ isPreferenceChangeBetweenBlankAndOn(event) &&
+ (await isRecordingOnAlreadyHonored(client, event.recordId))
+ ) {
+ return {
+ skipped: true,
+ reason: 'preference change on a meeting whose bot is already requested',
+ };
+ }
+
const reconciliationResults = await reconcileCallRecorderForCalendarEventIds({
client,
calendarEventIds: reconciliationPayload.calendarEventIds,
@@ -114,14 +131,7 @@ const buildCalendarEventReconciliationPayload = ({
if (action === 'updated') {
const updatedFields = event.properties.updatedFields ?? [];
- if (
- !hasRelevantFieldChange(updatedFields) ||
- isPreferenceChangeWithoutPolicyEffect({
- updatedFields,
- before: event.properties.before,
- after: event.properties.after,
- })
- ) {
+ if (!hasRelevantFieldChange(updatedFields)) {
return { calendarEventIds: [], removedOccurrences: [] };
}
@@ -159,19 +169,50 @@ const hasRelevantFieldChange = (updatedFields: string[]): boolean =>
CALL_RECORDER_RELEVANT_CALENDAR_EVENT_FIELDS.includes(updatedField),
);
-const isPreferenceChangeWithoutPolicyEffect = ({
- updatedFields,
- before,
- after,
-}: {
- updatedFields: string[];
- before: CalendarEventForDatabaseEvent | undefined;
- after: CalendarEventForDatabaseEvent | undefined;
-}): boolean =>
- updatedFields.length === 1 &&
- updatedFields[0] === 'callRecorderPreference' &&
- before?.callRecorderPreference !== CallRecorderPreference.OFF &&
- after?.callRecorderPreference !== CallRecorderPreference.OFF;
+const isPreferenceChangeBetweenBlankAndOn = (
+ event: CalendarEventDatabaseEvent,
+): boolean => {
+ const updatedFields = event.properties.updatedFields ?? [];
+
+ return (
+ updatedFields.length === 1 &&
+ updatedFields[0] === 'callRecorderPreference' &&
+ event.properties.before?.callRecorderPreference !==
+ CallRecorderPreference.OFF &&
+ event.properties.after?.callRecorderPreference !==
+ CallRecorderPreference.OFF
+ );
+};
+
+const isRecordingOnAlreadyHonored = async (
+ client: CoreApiClient,
+ calendarEventId: string,
+): Promise<boolean> => {
+ const calendarEvent = (
+ await fetchCalendarEventsByIds(client, [calendarEventId])
+ )[0];
+
+ if (isUndefined(calendarEvent)) {
+ return false;
+ }
+
+ const policyResult = buildCallRecorderPolicyResult(calendarEvent, new Date());
+
+ if (!policyResult.shouldRequestBot) {
+ return false;
+ }
+
+ const policyManagedCallRecording = (
+ await findCallRecordingsByIds(client, [
+ computeCallRecordingIdForMeeting(policyResult.realMeetingKey),
+ ])
+ )[0];
+
+ return (
+ policyManagedCallRecording?.recordingRequestStatus ===
+ CallRecordingRequestStatus.REQUESTED
+ );
+};
const hasKeyFieldChange = (updatedFields: string[]): boolean =>
updatedFields.some((updatedField) =>