Skip to content

Commit c3a5620

Browse files
committed
feat: added safe filter with warnings for missing entity ids
1 parent 4b8d1a2 commit c3a5620

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const safeEntityIdFilter = <T>(list: Array<T>, filter: (item: T) => boolean) => {
2+
return list.filter(item => {
3+
const result = filter(item);
4+
const isMissing = !result;
5+
6+
if (isMissing) {
7+
console.warn(
8+
`Missing entity id of entity: ${JSON.stringify(item)}. Entity id will be skipped.`,
9+
);
10+
}
11+
12+
return result;
13+
});
14+
};

apps/backoffice-v2/src/lib/blocks/hooks/useBusinessDocuments/useBusinessDocuments.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { safeEntityIdFilter } from '@/common/utils/safe-entity-id-filter/safe-entity-id-filter';
12
import { useWorkflowDocumentsAdapter } from '@/domains/documents/hooks/adapters/useWorkflowDocumentsAdapter/useWorkflowDocumentsAdapter';
23
import { TWorkflowById } from '@/domains/workflows/fetchers';
34
import { TDocument } from '@ballerine/common';
@@ -7,7 +8,9 @@ export const useBusinessDocuments = (workflow: TWorkflowById) => {
78
const entityIds = useMemo(
89
() =>
910
workflow?.context?.entity?.ballerineEntityId
10-
? [workflow?.context?.entity?.ballerineEntityId]
11+
? safeEntityIdFilter([workflow?.context?.entity], entity =>
12+
Boolean(entity.ballerineEntityId),
13+
).map(entity => entity.ballerineEntityId)
1114
: [],
1215
[workflow],
1316
);
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { safeEntityIdFilter } from '@/common/utils/safe-entity-id-filter/safe-entity-id-filter';
12
import { TWorkflowById } from '@/domains/workflows/fetchers';
23

34
export const getDirectorsIdsFromWorkflow = (workflow: TWorkflowById) =>
4-
workflow.endUsers?.filter(endUser => endUser.variant === 'director').map(endUser => endUser.id) ||
5-
[];
5+
safeEntityIdFilter(
6+
workflow.endUsers?.filter(endUser => endUser.variant === 'director') || [],
7+
endUser => Boolean(endUser.id),
8+
).map(endUser => endUser.id);
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { safeEntityIdFilter } from '@/common/utils/safe-entity-id-filter/safe-entity-id-filter';
12
import { TWorkflowById } from '@/domains/workflows/fetchers';
23

34
export const getUbosEntityIdsFromWorkflow = (workflow: TWorkflowById) => {
4-
return (
5-
workflow.endUsers?.filter(endUser => endUser.variant === 'ubo').map(endUser => endUser.id) || []
6-
);
5+
return safeEntityIdFilter(
6+
workflow.endUsers?.filter(endUser => endUser.variant === 'ubo') || [],
7+
endUser => Boolean(endUser.id),
8+
).map(endUser => endUser.id);
79
};

0 commit comments

Comments
 (0)