Skip to content

Commit

Permalink
feat: full text search (ballerine-io#2021)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-shvadron authored Jan 29, 2024
1 parent 92cbe47 commit 23dd2f5
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
import { useMemo } from 'react';
import { useCallback, useMemo } from 'react';

import { WarningFilledSvg } from '@/common/components/atoms/icons';
import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed';

export const useKybRegistryInfoBlock = ({ pluginsOutput, workflow }) =>
useMemo(() => {
const cell = Object.keys(pluginsOutput?.businessInformation?.data?.[0] ?? {}).length
? ({
id: 'nested-details',
type: 'details',
hideSeparator: true,
value: {
data: Object.entries(pluginsOutput?.businessInformation?.data?.[0])?.map(
([title, value]) => ({
title,
value,
}),
),
},
workflowId: workflow?.id,
documents: workflow?.context?.documents,
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'details';
}
>)
: ({
type: 'paragraph',
value: (
<span className="flex text-sm text-black/60">
<WarningFilledSvg
className={'mr-[8px] mt-px text-black/20'}
width={'20'}
height={'20'}
/>
<span>{pluginsOutput?.businessInformation?.message}</span>
</span>
export const useKybRegistryInfoBlock = ({ pluginsOutput, workflow }) => {
const getCell = useCallback(() => {
if (Object.keys(pluginsOutput?.businessInformation?.data?.[0] ?? {}).length) {
return {
id: 'nested-details',
type: 'details',
hideSeparator: true,
value: {
data: Object.entries(pluginsOutput?.businessInformation?.data?.[0])?.map(
([title, value]) => ({
title,
value,
}),
),
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'paragraph';
}
>);
},
workflowId: workflow?.id,
documents: workflow?.context?.documents,
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'details';
}
>;
}

if (pluginsOutput?.businessInformation?.message) {
return {
type: 'paragraph',
value: (
<span className="flex text-sm text-black/60">
<WarningFilledSvg
className={'mr-[8px] mt-px text-black/20'}
width={'20'}
height={'20'}
/>
<span>{pluginsOutput?.businessInformation?.message}</span>
</span>
),
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'paragraph';
}
>;
}
}, [pluginsOutput, workflow]);

return useMemo(() => {
const cell = getCell();

if (!cell) {
return [];
}

return createBlocksTyped()
.addBlock()
Expand All @@ -69,4 +81,5 @@ export const useKybRegistryInfoBlock = ({ pluginsOutput, workflow }) =>
.flat(1),
})
.build();
}, [pluginsOutput, workflow]);
}, [getCell]);
};
112 changes: 62 additions & 50 deletions apps/backoffice-v2/src/lib/blocks/hooks/useUbosBlock/useUbosBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useCallback, useMemo } from 'react';
import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed';
import { WarningFilledSvg } from '@/common/components/atoms/icons';

Expand All @@ -9,57 +9,68 @@ type Ubo = {
percentage?: number;
};

export const useUbosBlock = (ubos: Ubo[] | undefined, message: string | undefined) =>
useMemo(() => {
const cell =
Array.isArray(ubos) && ubos?.length
? ({
type: 'table',
value: {
columns: [
{
accessorKey: 'name',
header: 'Name',
},
{
accessorKey: 'percentage',
header: 'Percentage (25% or higher)',
},
{
accessorKey: 'type',
header: 'Type',
},
{
accessorKey: 'level',
header: 'Level',
},
],
data: ubos,
export const useUbosBlock = (ubos: Ubo[] | undefined, message: string | undefined) => {
const getCell = useCallback(() => {
if (Array.isArray(ubos) && ubos?.length) {
return {
type: 'table',
value: {
columns: [
{
accessorKey: 'name',
header: 'Name',
},
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'table';
}
>)
: ({
type: 'paragraph',
value: (
<span className="flex text-sm text-black/60">
<WarningFilledSvg
className={'mr-[8px] mt-px text-black/20'}
width={'20'}
height={'20'}
/>
<span>{message}</span>
</span>
),
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
accessorKey: 'percentage',
header: 'Percentage (25% or higher)',
},
{
type: 'paragraph';
}
>);
accessorKey: 'type',
header: 'Type',
},
{
accessorKey: 'level',
header: 'Level',
},
],
data: ubos,
},
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'table';
}
>;
}

if (message) {
return {
type: 'paragraph',
value: (
<span className="flex text-sm text-black/60">
<WarningFilledSvg
className={'mr-[8px] mt-px text-black/20'}
width={'20'}
height={'20'}
/>
<span>{message}</span>
</span>
),
} satisfies Extract<
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
{
type: 'paragraph';
}
>;
}
}, [ubos]);

return useMemo(() => {
const cell = getCell();

if (!cell) {
return [];
}

return createBlocksTyped()
.addBlock()
Expand All @@ -83,4 +94,5 @@ export const useUbosBlock = (ubos: Ubo[] | undefined, message: string | undefine
.flat(1),
})
.build();
}, [message, ubos]);
}, [getCell]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
CREATE OR REPLACE
FUNCTION public.search_workflow_data(
search_text TEXT,
entity_type TEXT,
workflow_ids TEXT[],
statuses TEXT[],
project_ids TEXT[],
assignee_ids TEXT[],
case_statuses TEXT[],
include_unassigned boolean
)
RETURNS TABLE (
id TEXT
) AS $$
DECLARE
search_term TEXT := regexp_replace(
trim(
regexp_replace(
regexp_replace(search_text, '[^a-zA-Z0-9\s]', '', 'g'),
'\s{1,}', ' ', 'g'
)
),
'\s+', ':*&', 'g'
);

BEGIN
IF search_term != '' THEN
search_term := concat(
search_term,
':*'
);
END IF;
RETURN QUERY
SELECT
wrd."id"
FROM
public."WorkflowRuntimeData" wrd
LEFT JOIN LATERAL jsonb_array_elements_text(wrd.tags) AS tag ON
TRUE
WHERE
(
search_term = ''
OR
(
search_term <> ''
AND
to_tsquery('simple', search_term) @@
to_tsvector('simple',
regexp_replace(
lower(CONCAT_WS(' ',
wrd.context->'entity'->>'id',
wrd.context->'entity'->>'ballerineEntityId',
wrd.context->'entity'->'data'->>'firstName',
wrd.context->'entity'->'data'->>'middleName',
wrd.context->'entity'->'data'->>'lastName',
wrd.context->'entity'->'data'->>'email',
wrd.context->'entity'->'data'->>'companyName',
wrd.context->'entity'->'data'->>'phone')),
'[^a-zA-Z0-9\s]',
'',
'g'
)
)
)
)
AND
(
"workflowDefinitionId" = ANY(workflow_ids)
OR
array_length(workflow_ids, 1) IS NULL
)
AND
(
"status"::TEXT = ANY(statuses)
OR
array_length(statuses, 1) IS NULL
)
AND
(
"projectId" = ANY(project_ids)
OR
array_length(project_ids, 1) IS NULL
)
AND
(
CASE
WHEN entity_type = 'individuals' THEN "endUserId" IS NOT NULL
ELSE "businessId" IS NOT NULL
END
)
AND
(
"assigneeId" = ANY(assignee_ids)
OR
(
include_unassigned
AND "assigneeId" IS NULL
)
OR
(
include_unassigned IS NOT TRUE
AND array_length(assignee_ids, 1) IS NULL
)
)
AND
(
tag = ANY(case_statuses)
OR
array_length(case_statuses, 1) IS NULL
);
END;

$$ LANGUAGE plpgsql;

0 comments on commit 23dd2f5

Please sign in to comment.