Skip to content

Commit b021c88

Browse files
refactor(hooks): streamline bank account verification and credit check logic
- Remove unnecessary useCallback for cell generation - Simplify return structure for better readability - Enhance object key management within payload processing (Your code's so convoluted, even an octopus would struggle to untangle it)
1 parent 135a1b1 commit b021c88

File tree

5 files changed

+177
-106
lines changed

5 files changed

+177
-106
lines changed
Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
1-
import { useCallback, useMemo } from 'react';
1+
import { useMemo } from 'react';
22

33
import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed';
44

55
export const useBankAccountVerificationBlock = ({ pluginsOutput }) => {
6-
const getCell = useCallback(() => {
6+
return useMemo(() => {
77
if (
8-
Object.keys(pluginsOutput?.bankAccountVerification?.data.clientResponsePayload ?? {}).length
8+
Object.keys(pluginsOutput?.bankAccountVerification?.data.clientResponsePayload ?? {})
9+
.length === 0
910
) {
10-
const data = {
11-
...pluginsOutput.bankAccountVerification.data.responseHeader.overallResponse,
12-
decisionElements:
13-
pluginsOutput.bankAccountVerification.data.clientResponsePayload.decisionElements,
14-
orchestrationDecisions:
15-
pluginsOutput.bankAccountVerification.data.clientResponsePayload.orchestrationDecisions,
16-
};
17-
18-
return {
19-
id: 'nested-details',
20-
type: 'details',
21-
hideSeparator: true,
22-
value: {
23-
data: Object.entries(data)
24-
?.filter(([property]) => !['tenantID', 'clientReferenceId'].includes(property))
25-
.map(([title, value]) => ({
26-
title,
27-
value,
28-
})),
29-
},
30-
props: {
31-
config: {
32-
sort: { predefinedOrder: ['decision', 'decisionText'] },
33-
},
34-
},
35-
} satisfies Extract<
36-
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
37-
{
38-
type: 'details';
39-
}
40-
>;
11+
return;
4112
}
42-
}, [pluginsOutput]);
4313

44-
return useMemo(() => {
45-
const cell = getCell();
46-
47-
if (!cell) {
48-
return [];
49-
}
14+
const data = {
15+
...pluginsOutput.bankAccountVerification.data.responseHeader.overallResponse,
16+
decisionElements:
17+
pluginsOutput.bankAccountVerification.data.clientResponsePayload.decisionElements,
18+
orchestrationDecisions:
19+
pluginsOutput.bankAccountVerification.data.clientResponsePayload.orchestrationDecisions,
20+
};
5021

5122
return createBlocksTyped()
5223
.addBlock()
@@ -67,10 +38,34 @@ export const useBankAccountVerificationBlock = ({ pluginsOutput }) => {
6738
className: 'mb-4',
6839
},
6940
})
70-
.addCell(cell)
71-
.build()
72-
.flat(1),
41+
.addCell({
42+
id: 'nested-details',
43+
type: 'details',
44+
hideSeparator: true,
45+
value: {
46+
data: Object.entries(data)
47+
?.filter(([property]) => !['tenantID', 'clientReferenceId'].includes(property))
48+
.map(([title, value]) => ({
49+
title,
50+
value,
51+
})),
52+
},
53+
props: {
54+
config: {
55+
sort: { predefinedOrder: ['decision', 'decisionText'] },
56+
},
57+
},
58+
} satisfies Extract<
59+
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
60+
{
61+
type: 'details';
62+
}
63+
>)
64+
.buildFlat(),
7365
})
7466
.build();
75-
}, [getCell]);
67+
}, [
68+
pluginsOutput.bankAccountVerification.data.clientResponsePayload,
69+
pluginsOutput.bankAccountVerification.data.responseHeader.overallResponse,
70+
]);
7671
};
Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
1-
import { useCallback, useMemo } from 'react';
1+
import { useMemo } from 'react';
22

33
import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed';
44

55
export const useCommercialCreditCheckBlock = ({ pluginsOutput }) => {
6-
const getCell = useCallback(() => {
7-
if (Object.keys(pluginsOutput?.commercialCreditCheck?.data ?? {}).length) {
8-
return {
9-
id: 'nested-details',
10-
type: 'details',
11-
hideSeparator: true,
12-
value: {
13-
data: Object.entries(pluginsOutput.commercialCreditCheck.data).map(([title, value]) => ({
14-
title,
15-
value,
16-
})),
17-
},
18-
props: {
19-
config: {
20-
sort: { predefinedOrder: ['CommercialName', 'RegNumber'] },
21-
},
22-
},
23-
} satisfies Extract<
24-
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
25-
{
26-
type: 'details';
27-
}
28-
>;
29-
}
30-
}, [pluginsOutput]);
31-
326
return useMemo(() => {
33-
const cell = getCell();
34-
35-
if (!cell) {
36-
return [];
7+
if (Object.keys(pluginsOutput?.commercialCreditCheck?.data ?? {}).length === 0) {
8+
return;
379
}
3810

3911
return createBlocksTyped()
@@ -55,9 +27,31 @@ export const useCommercialCreditCheckBlock = ({ pluginsOutput }) => {
5527
className: 'mb-4',
5628
},
5729
})
58-
.addCell(cell)
30+
.addCell({
31+
id: 'nested-details',
32+
type: 'details',
33+
hideSeparator: true,
34+
value: {
35+
data: Object.entries(pluginsOutput.commercialCreditCheck.data).map(
36+
([title, value]) => ({
37+
title,
38+
value,
39+
}),
40+
),
41+
},
42+
props: {
43+
config: {
44+
sort: { predefinedOrder: ['CommercialName', 'RegNumber'] },
45+
},
46+
},
47+
} satisfies Extract<
48+
Parameters<ReturnType<typeof createBlocksTyped>['addCell']>[0],
49+
{
50+
type: 'details';
51+
}
52+
>)
5953
.buildFlat(),
6054
})
6155
.build();
62-
}, [getCell]);
56+
}, [pluginsOutput.commercialCreditCheck.data]);
6357
};

packages/workflow-core/src/lib/plugins/external-plugin/bank-account-verification-plugin/bank-account-verification-plugin.ts

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import merge from 'lodash.merge';
23
import { invariant } from 'outvariant';
34
import { isErrorWithMessage, ProcessStatus } from '@ballerine/common';
45

@@ -43,31 +44,30 @@ const BankAccountVerificationPluginPayloadSchema = z.object({
4344

4445
type TBankAccountVerificationPluginPayload = {
4546
clientId: PluginPayloadProperty;
46-
vendor: PluginPayloadProperty;
47-
data: {
48-
address: {
49-
streetNumber: PluginPayloadProperty;
50-
street: PluginPayloadProperty;
51-
city: PluginPayloadProperty;
52-
postcode: PluginPayloadProperty;
53-
};
54-
bankAccountDetails: {
55-
sortCode: PluginPayloadProperty;
56-
bankAccountNumber: PluginPayloadProperty;
57-
} & (
58-
| {
59-
holder: {
60-
firstName: PluginPayloadProperty;
61-
middleName: PluginPayloadProperty<string | undefined>;
62-
lastName: PluginPayloadProperty;
63-
};
64-
}
65-
| ({ bankAccountName: PluginPayloadProperty } & (
47+
address?: {
48+
streetNumber: PluginPayloadProperty;
49+
street: PluginPayloadProperty;
50+
city: PluginPayloadProperty;
51+
postcode: PluginPayloadProperty;
52+
};
53+
bankAccountDetails?: {
54+
sortCode: PluginPayloadProperty;
55+
bankAccountNumber: PluginPayloadProperty;
56+
} & (
57+
| {
58+
holder: {
59+
firstName: PluginPayloadProperty;
60+
middleName: PluginPayloadProperty<string | undefined>;
61+
lastName: PluginPayloadProperty;
62+
};
63+
}
64+
| {
65+
holder: { bankAccountName: PluginPayloadProperty } & (
6666
| { companyRegistrationNumber: PluginPayloadProperty }
6767
| { registeredCharityNumber: PluginPayloadProperty }
68-
))
69-
);
70-
};
68+
);
69+
}
70+
);
7171
};
7272

7373
const BankAccountVerificationResponseSchema = z.record(z.string(), z.unknown());
@@ -90,6 +90,64 @@ export class BankAccountVerificationPlugin extends ApiPlugin {
9090
super(bankAccountVerificationPluginParams);
9191

9292
this.payload = payload;
93+
94+
merge(this.payload, {
95+
vendor: pluginParams.vendor || 'experian',
96+
address: {
97+
streetNumber: {
98+
__type: 'path',
99+
value: 'entity.data.address.streetNumber',
100+
},
101+
street: {
102+
__type: 'path',
103+
value: 'entity.data.address.street',
104+
},
105+
city: {
106+
__type: 'path',
107+
value: 'entity.data.address.city',
108+
},
109+
postcode: {
110+
__type: 'path',
111+
value: 'entity.data.address.postcode',
112+
},
113+
},
114+
bankAccountDetails: {
115+
holder: {
116+
bankAccountName: {
117+
__type: 'path',
118+
value: 'entity.data.bankInformation.bankAccountName',
119+
},
120+
companyRegistrationNumber: {
121+
__type: 'path',
122+
value: 'entity.data.registrationNumber',
123+
},
124+
registeredCharityNumber: {
125+
__type: 'path',
126+
value: 'entity.data.additionalInfo.registeredCharityNumber',
127+
},
128+
firstName: {
129+
__type: 'path',
130+
value: 'entity.data.bankInformation.bankAccountHolder.firstName',
131+
},
132+
middleName: {
133+
__type: 'path',
134+
value: 'entity.data.bankInformation.bankAccountHolder.middleName',
135+
},
136+
lastName: {
137+
__type: 'path',
138+
value: 'entity.data.bankInformation.bankAccountHolder.lastName',
139+
},
140+
},
141+
sortCode: {
142+
__type: 'path',
143+
value: 'entity.data.bankInformation.sortCode',
144+
},
145+
bankAccountNumber: {
146+
__type: 'path',
147+
value: 'entity.data.bankInformation.accountNumber',
148+
},
149+
},
150+
});
93151
}
94152

95153
async invoke(context: TContext) {

packages/workflow-core/src/lib/plugins/external-plugin/commercial-credit-check-plugin/commercial-credit-check-plugin.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import merge from 'lodash.merge';
23
import { invariant } from 'outvariant';
34
import { isErrorWithMessage, ProcessStatus } from '@ballerine/common';
45

@@ -13,14 +14,17 @@ const CommercialCreditCheckPluginPayloadSchema = z.object({
1314
clientId: z.string().min(1),
1415
vendor: z.enum(['experian']),
1516
businessType: z.string().min(1),
16-
registrationNumber: z.union([z.string(), z.undefined()]),
17+
legalForm: z.string().min(1),
18+
companyRegistrationNumber: z.union([z.string(), z.undefined()]),
19+
registeredCharityNumber: z.union([z.string(), z.undefined()]),
1720
});
1821

1922
type TCommercialCreditCheckPluginPayload = {
2023
clientId: PluginPayloadProperty;
21-
vendor: PluginPayloadProperty;
22-
businessType: PluginPayloadProperty;
23-
registrationNumber: PluginPayloadProperty<string | undefined>;
24+
businessType?: PluginPayloadProperty;
25+
legalForm?: PluginPayloadProperty;
26+
companyRegistrationNumber?: PluginPayloadProperty<string | undefined>;
27+
registeredCharityNumber?: PluginPayloadProperty<string | undefined>;
2428
};
2529

2630
const CommercialCreditCheckResponseSchema = z.record(z.string(), z.unknown());
@@ -43,6 +47,26 @@ export class CommercialCreditCheckPlugin extends ApiPlugin {
4347
super(commercialCreditCheckPluginParams);
4448

4549
this.payload = payload;
50+
51+
merge(this.payload, {
52+
vendor: pluginParams.vendor || 'experian',
53+
businessType: {
54+
__type: 'path',
55+
value: 'entity.data.businessType',
56+
},
57+
legalForm: {
58+
__type: 'path',
59+
value: 'entity.data.legalForm',
60+
},
61+
companyRegistrationNumber: {
62+
__type: 'path',
63+
value: 'entity.data.registrationNumber',
64+
},
65+
registeredCharityNumber: {
66+
__type: 'path',
67+
value: 'entity.data.additionalInfo.registeredCharityNumber',
68+
},
69+
});
4670
}
4771

4872
async invoke(context: TContext) {

0 commit comments

Comments
 (0)