Refactor backend folder structure (#4505)
* Refactor backend folder structure Co-authored-by: Charles Bochet <charles@twenty.com> * fix tests * fix * move yoga hooks --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,44 @@
|
||||
import { deleteUsingPagination } from './delete-using-pagination.util';
|
||||
|
||||
describe('deleteUsingPagination', () => {
|
||||
it('should delete items using pagination', async () => {
|
||||
const workspaceId = 'workspace123';
|
||||
const batchSize = 10;
|
||||
const getterPaginated = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce(['id1', 'id2'])
|
||||
.mockResolvedValueOnce([]);
|
||||
const deleter = jest.fn();
|
||||
const transactionManager = undefined;
|
||||
|
||||
await deleteUsingPagination(
|
||||
workspaceId,
|
||||
batchSize,
|
||||
getterPaginated,
|
||||
deleter,
|
||||
transactionManager,
|
||||
);
|
||||
|
||||
expect(getterPaginated).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
batchSize,
|
||||
0,
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
);
|
||||
expect(getterPaginated).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
batchSize,
|
||||
batchSize,
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
);
|
||||
expect(deleter).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
['id1', 'id2'],
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
);
|
||||
expect(deleter).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,38 @@
|
||||
import { EntityManager } from 'typeorm';
|
||||
|
||||
export const deleteUsingPagination = async (
|
||||
workspaceId: string,
|
||||
batchSize: number,
|
||||
getterPaginated: (
|
||||
limit: number,
|
||||
offset: number,
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
) => Promise<string[]>,
|
||||
deleter: (
|
||||
ids: string[],
|
||||
workspaceId: string,
|
||||
transactionManager?: EntityManager,
|
||||
) => Promise<void>,
|
||||
transactionManager?: EntityManager,
|
||||
) => {
|
||||
let offset = 0;
|
||||
let hasMoreData = true;
|
||||
|
||||
while (hasMoreData) {
|
||||
const idsToDelete = await getterPaginated(
|
||||
batchSize,
|
||||
offset,
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
);
|
||||
|
||||
if (idsToDelete.length > 0) {
|
||||
await deleter(idsToDelete, workspaceId, transactionManager);
|
||||
} else {
|
||||
hasMoreData = false;
|
||||
}
|
||||
|
||||
offset += batchSize;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user