From 341328fdf8a8faf7834d84d02ab3d1c74ee0b327 Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Tue, 16 Jul 2024 11:45:29 +0200 Subject: [PATCH] Fixed mass deletion page size. (#6275) Fixed when page size was not the same as the one used for calculating number of pages --- .../hooks/__mocks__/useFetchAllRecordIds.ts | 6 +++--- .../object-record/hooks/useDeleteManyRecords.ts | 15 ++++++++++----- .../object-record/hooks/useFetchAllRecordIds.ts | 16 +++++++++++++--- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/twenty-front/src/modules/object-record/hooks/__mocks__/useFetchAllRecordIds.ts b/packages/twenty-front/src/modules/object-record/hooks/__mocks__/useFetchAllRecordIds.ts index 8b25676b6a20..6309686fb9e2 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/__mocks__/useFetchAllRecordIds.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/__mocks__/useFetchAllRecordIds.ts @@ -34,20 +34,20 @@ export const thirdRequestLastCursor = peopleMockWithIdsOnly.edges[mockPageSize * export const variablesFirstRequest = { filter: undefined, - limit: undefined, + limit: mockPageSize, orderBy: undefined }; export const variablesSecondRequest = { filter: undefined, - limit: undefined, + limit: mockPageSize, orderBy: undefined, lastCursor: firstRequestLastCursor }; export const variablesThirdRequest = { filter: undefined, - limit: undefined, + limit: mockPageSize, orderBy: undefined, lastCursor: secondRequestLastCursor } diff --git a/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts index a9af8e812318..1d3d82f3fe60 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useDeleteManyRecords.ts @@ -1,12 +1,14 @@ import { useApolloClient } from '@apollo/client'; import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect'; +import { apiConfigState } from '@/client-config/states/apiConfigState'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize'; import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation'; import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField'; +import { useRecoilValue } from 'recoil'; import { isDefined } from '~/utils/isDefined'; import { sleep } from '~/utils/sleep'; import { capitalize } from '~/utils/string/capitalize'; @@ -24,6 +26,11 @@ type DeleteManyRecordsOptions = { export const useDeleteManyRecords = ({ objectNameSingular, }: useDeleteOneRecordProps) => { + const apiConfig = useRecoilValue(apiConfigState); + + const mutationPageSize = + apiConfig?.mutationMaximumAffectedRecords ?? DEFAULT_MUTATION_BATCH_SIZE; + const apolloClient = useApolloClient(); const { objectMetadataItem } = useObjectMetadataItem({ @@ -48,16 +55,14 @@ export const useDeleteManyRecords = ({ idsToDelete: string[], options?: DeleteManyRecordsOptions, ) => { - const numberOfBatches = Math.ceil( - idsToDelete.length / DEFAULT_MUTATION_BATCH_SIZE, - ); + const numberOfBatches = Math.ceil(idsToDelete.length / mutationPageSize); const deletedRecords = []; for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) { const batchIds = idsToDelete.slice( - batchIndex * DEFAULT_MUTATION_BATCH_SIZE, - (batchIndex + 1) * DEFAULT_MUTATION_BATCH_SIZE, + batchIndex * mutationPageSize, + (batchIndex + 1) * mutationPageSize, ); const deletedRecordsResponse = await apolloClient.mutate({ diff --git a/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts b/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts index 0e70a8ed7b68..937548814f12 100644 --- a/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts +++ b/packages/twenty-front/src/modules/object-record/hooks/useFetchAllRecordIds.ts @@ -20,6 +20,7 @@ export const useFetchAllRecordIds = ({ objectNameSingular, filter, orderBy, + limit: pageSize, recordGqlFields: { id: true }, }); @@ -49,12 +50,17 @@ export const useFetchAllRecordIds = ({ const remainingPages = Math.ceil(remainingCount / pageSize); - let lastCursor = firstQueryResult?.pageInfo.endCursor ?? ''; + let lastCursor = firstQueryResult?.pageInfo.endCursor ?? null; + + for (let pageIndex = 0; pageIndex < remainingPages; pageIndex++) { + if (lastCursor === null) { + break; + } - for (let i = 0; i < remainingPages; i++) { const rawResult = await fetchMore?.({ variables: { lastCursor: lastCursor, + limit: pageSize, }, }); @@ -64,7 +70,11 @@ export const useFetchAllRecordIds = ({ recordIdSet.add(edge.node.id); } - lastCursor = fetchMoreResult.pageInfo.endCursor ?? ''; + if (fetchMoreResult.pageInfo.hasNextPage === false) { + break; + } + + lastCursor = fetchMoreResult.pageInfo.endCursor ?? null; } const recordIds = Array.from(recordIdSet);