Skip to content

Commit 039946b

Browse files
authored
Merge pull request #601 from companieshouse/feature/lp-1013-capital-contribution
Feature/lp 1013 capital contribution
2 parents 6a2d095 + 0ebf4f0 commit 039946b

File tree

10 files changed

+1291
-14
lines changed

10 files changed

+1291
-14
lines changed

locales/cy/errors.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"errorMessages": {
3+
"capitalContribution": {
4+
"valueRequired": "WELSH - Contribution currency type is required",
5+
"twoDecimalPlaces": "WELSH - Value must be a number with 2 decimal places",
6+
"moreThanZero": "WELSH - Value must be 0.01 or more",
7+
"noSymbols": "WELSH - Value must not include currency symbols like £, $ and €",
8+
"noComma": "WELSH - Value must not include a comma",
9+
"atLeastOneType": "WELSH - Select at least one type of contribution",
10+
"currencyRequired": "WELSH - Select the currency of the capital contribution"
11+
}
12+
}
13+
}

locales/en/errors.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"errorMessages": {
3+
"capitalContribution": {
4+
"valueRequired": "Contribution currency type is required",
5+
"twoDecimalPlaces": "Value must be a number with 2 decimal places",
6+
"moreThanZero": "Value must be 0.01 or more",
7+
"noSymbols": "Value must not include currency symbols like £, $ and €",
8+
"noComma": "Value must not include a comma",
9+
"atLeastOneType": "Select at least one type of contribution",
10+
"currencyRequired": "Select the currency of the capital contribution"
11+
}
12+
}
13+
}

src/application/service/LimitedPartnerService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { logger } from "../../utils";
44
import UIErrors from "../../domain/entities/UIErrors";
55
import { extractAPIErrors, incompletePartnerErrorList } from "./utils";
66
import { Tokens } from "../../domain/types";
7+
import { capitalContributionValidation } from "./utils/capitalContributionValidation";
78

89
class LimitedPartnerService {
910
i18n: any;
@@ -23,6 +24,10 @@ class LimitedPartnerService {
2324
errors?: UIErrors;
2425
}> {
2526
try {
27+
if (data.contribution_currency_type || data.contribution_currency_value || data.contribution_sub_types) {
28+
capitalContributionValidation(data, this.i18n);
29+
}
30+
2631
const limitedPartnerId = await this.limitedPartnerGateway.createLimitedPartner(opt, transactionId, data);
2732

2833
return { limitedPartnerId };
@@ -88,6 +93,10 @@ class LimitedPartnerService {
8893
errors?: UIErrors;
8994
}> {
9095
try {
96+
if (data.contribution_currency_type || data.contribution_currency_value || data.contribution_sub_types) {
97+
capitalContributionValidation(data, this.i18n);
98+
}
99+
91100
await this.limitedPartnerGateway.sendPageData(opt, transactionId, limitedPartnerId, data);
92101
} catch (errors: any) {
93102
const { apiErrors, isValidationErrors } = extractAPIErrors(errors);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import UIErrors from "../../../domain/entities/UIErrors";
2+
import { symbols } from "./currencies";
3+
4+
const capitalContributionValidation = (data: Record<string, string | string[]>, i18n: any): void => {
5+
const uiErrors = new UIErrors();
6+
7+
if (!data.contribution_currency_type) {
8+
uiErrors.setWebError("contribution_currency_type", i18n?.errorMessages?.capitalContribution?.currencyRequired);
9+
}
10+
11+
if (!data.contribution_sub_types?.length) {
12+
uiErrors.setWebError("contribution_sub_types", i18n?.errorMessages?.capitalContribution?.atLeastOneType);
13+
}
14+
15+
contributionCurrencyValueValidation(data, uiErrors, i18n);
16+
17+
if (uiErrors.hasErrors()) {
18+
throw uiErrors;
19+
}
20+
};
21+
22+
const contributionCurrencyValueValidation = (data: Record<string, any>, uiErrors: UIErrors, i18n: any) => {
23+
const field = "contribution_currency_value";
24+
25+
if (!data.contribution_currency_value) {
26+
uiErrors.setWebError(field, i18n?.errorMessages?.capitalContribution?.valueRequired);
27+
} else if (hasSymbol(data.contribution_currency_value, symbols)) {
28+
uiErrors.setWebError(field, i18n?.errorMessages?.capitalContribution?.noSymbols);
29+
} else if (hasComma(data.contribution_currency_value)) {
30+
uiErrors.setWebError(field, i18n?.errorMessages?.capitalContribution?.noComma);
31+
} else if (!isNumber(data.contribution_currency_value) || !has2Decimal(data.contribution_currency_value)) {
32+
uiErrors.setWebError(field, i18n?.errorMessages?.capitalContribution?.twoDecimalPlaces);
33+
} else if (!isGreaterThanZero(data.contribution_currency_value)) {
34+
uiErrors.setWebError(field, i18n?.errorMessages?.capitalContribution?.moreThanZero);
35+
}
36+
};
37+
38+
const isNumber = (value: string) => {
39+
return !isNaN(Number(value));
40+
};
41+
42+
const has2Decimal = (value: string) => {
43+
return value.split(".")[1]?.length === 2;
44+
};
45+
46+
const isGreaterThanZero = (value: string) => {
47+
return parseFloat(value) > 0;
48+
};
49+
50+
const hasSymbol = (str: string, symbols: string[]): boolean => {
51+
return symbols.some((symbol) => str.includes(symbol));
52+
};
53+
54+
const hasComma = (str: string): boolean => {
55+
return str.includes(",");
56+
};
57+
58+
export { capitalContributionValidation };

0 commit comments

Comments
 (0)