Skip to content

The open alternative to Salesforce, designed for AI.

3.3 KB
Raw Blame
import { type Attachment } from '@/activities/files/types/Attachment';
import { filterAttachmentsToRestore } from '@/activities/utils/filterAttachmentsToRestore';
import { getActivityAttachmentIdsAndNameToUpdate } from '@/activities/utils/getActivityAttachmentIdsAndNameToUpdate';
import { getActivityAttachmentIdsToDelete } from '@/activities/utils/getActivityAttachmentIdsToDelete';
import { getActivityAttachmentPathsToRestore } from '@/activities/utils/getActivityAttachmentPathsToRestore';
import { getToastOptionsFromError } from '@/error-handler/utils/getToastOptionsFromError';
import { useDeleteManyRecords } from '@/object-record/hooks/useDeleteManyRecords';
import { useLazyFetchAllRecords } from '@/object-record/hooks/useLazyFetchAllRecords';
import { useRestoreManyRecords } from '@/object-record/hooks/useRestoreManyRecords';
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
import { CoreObjectNameSingular } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';

export const useAttachmentSync = (attachments: Attachment[]) => {
  const { deleteManyRecords: deleteAttachments } = useDeleteManyRecords({
    objectNameSingular: CoreObjectNameSingular.Attachment,
  });

  const { restoreManyRecords: restoreAttachments } = useRestoreManyRecords({
    objectNameSingular: CoreObjectNameSingular.Attachment,
  });

  const { fetchAllRecords: findSoftDeletedAttachments } =
    useLazyFetchAllRecords({
      objectNameSingular: CoreObjectNameSingular.Attachment,
      filter: {
        deletedAt: {
          is: 'NOT_NULL',
        },
      },
    });

  const { updateOneRecord } = useUpdateOneRecord();
  const { enqueueToast } = useToast();

  const syncAttachments = async (
    newBody: string,
    previousBody?: string | null,
  ) => {
    if (!newBody) return;

    const attachmentIdsToDelete = getActivityAttachmentIdsToDelete(
      newBody,
      attachments,
      previousBody ?? '',
    );

    if (attachmentIdsToDelete.length > 0) {
      await deleteAttachments({
        recordIdsToDelete: attachmentIdsToDelete,
      });
    }

    const attachmentPathsToRestore = getActivityAttachmentPathsToRestore(
      newBody,
      attachments,
    );

    if (attachmentPathsToRestore.length > 0) {
      const softDeletedAttachments = (await findSoftDeletedAttachments().catch(
        (error) => {
          enqueueToast(getToastOptionsFromError({ error }));

          return null;
        },
      )) as Attachment[] | null;

      if (isDefined(softDeletedAttachments)) {
        const attachmentIdsToRestore = filterAttachmentsToRestore({
          attachmentPathsToRestore,
          softDeletedAttachments,
        });

        await restoreAttachments({
          idsToRestore: attachmentIdsToRestore,
        });
      }
    }

    const attachmentsToUpdate = getActivityAttachmentIdsAndNameToUpdate(
      newBody,
      attachments,
    );

    for (const attachmentToUpdate of attachmentsToUpdate) {
      if (!attachmentToUpdate.id || !attachmentToUpdate.name) continue;
      await updateOneRecord({
        objectNameSingular: CoreObjectNameSingular.Attachment,
        idToUpdate: attachmentToUpdate.id,
        updateOneRecordInput: { name: attachmentToUpdate.name },
      });
    }
  };

  return { syncAttachments };
};