Skip to content

The open alternative to Salesforce, designed for AI.

1.3 KB
Raw Blame
import { type Attachment } from '@/activities/files/types/Attachment';
import { filterAttachmentsWithFile } from '@/activities/files/utils/filterAttachmentsWithFile';
import { compareUrls } from '@/activities/utils/compareUrls';
import { getActivityAttachmentPathsAndName } from '@/activities/utils/getActivityAttachmentPathsAndName';
import { getAttachmentUrl } from '@/activities/utils/getAttachmentUrl';

export const getActivityAttachmentIdsToDelete = (
  newActivityBody: string,
  oldActivityAttachments: Attachment[] = [],
  oldActivityBody: string,
) => {
  if (oldActivityAttachments.length === 0) return [];

  const newActivityAttachmentPaths =
    getActivityAttachmentPathsAndName(newActivityBody);

  const oldActivityAttachmentPaths =
    getActivityAttachmentPathsAndName(oldActivityBody);

  const pathsToDelete = oldActivityAttachmentPaths
    .filter(
      (oldActivity) =>
        !newActivityAttachmentPaths.some(
          (newActivity) => newActivity.path === oldActivity.path,
        ),
    )
    .map((activity) => activity.path);

  return filterAttachmentsWithFile(oldActivityAttachments)
    .filter((attachment) =>
      pathsToDelete.some((pathToDelete) =>
        compareUrls(getAttachmentUrl({ attachment }), pathToDelete),
      ),
    )
    .map((attachment) => attachment.id);
};