Skip to content

Commit

Permalink
Merge pull request #183 from UK-Export-Finance/chore/EMS-653
Browse files Browse the repository at this point in the history
chore(EMS-653): UI helpers documentation, improve directory structure
  • Loading branch information
abhi-markan authored Dec 12, 2022
2 parents b4a3faa + bba6a8e commit 6e15dc3
Show file tree
Hide file tree
Showing 47 changed files with 372 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { canGetAQuoteOnline, canGetAQuoteByEmail, cannotGetAQuote } from './country-support';
import { canGetAQuoteOnline, canGetAQuoteByEmail, cannotGetAQuote } from '.';

describe('server/helpers/country-support', () => {
const mockCountryBase = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Country } from '../../types';
import { Country } from '../../../types';

/**
* canGetAQuoteOnline
* Check if a country is able to get a quote online
* @param {Object} Country from CIS API
* @returns {Boolean}
*/
export const canGetAQuoteOnline = (c: Country) => {
if (c.riskCategory && c.shortTermCoverAvailable && c.nbiIssueAvailable) {
return true;
Expand All @@ -8,6 +14,12 @@ export const canGetAQuoteOnline = (c: Country) => {
return false;
};

/**
* canGetAQuoteByEmail
* Check if a country is able to get a quote by email
* @param {Object} Country from CIS API
* @returns {Boolean}
*/
export const canGetAQuoteByEmail = (c: Country) => {
if (c.riskCategory && c.shortTermCoverAvailable && !c.nbiIssueAvailable) {
return true;
Expand All @@ -16,6 +28,12 @@ export const canGetAQuoteByEmail = (c: Country) => {
return false;
};

/**
* cannotGetAQuote
* Check if a country cannot get a quote online or offline
* @param {Object} Country from CIS API
* @returns {Boolean}
*/
export const cannotGetAQuote = (c: Country) => {
if (!c.riskCategory || (!c.shortTermCoverAvailable && !c.nbiIssueAvailable)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const {
VALID_COMPANY_BASE,
} = FIELD_IDS;

/**
* mapPolicyType
* Map policy type into an object for GOV summary list structure
* @param {String} Policy type answer
* @returns {Object} Answer in an object
*/
const mapPolicyType = (answer: string) => {
if (isSinglePolicyType(answer)) {
return {
Expand All @@ -38,8 +44,20 @@ const mapPolicyType = (answer: string) => {
return {};
};

/**
* mapPercentageOfCover
* Map percentage of cover answer to a string with percentage symbol for GOVUK summary list structure
* @param {Number} Percentage of cover answer
* @returns {String} Percentage of cover with percentage symbol
*/
const mapPercentageOfCover = (answer: number) => `${answer}%`;

/**
* mapAnswersToContent
* Map all answers/submitted data into an object structure for GOVUK summary list structure
* @param {Object} All submitted data
* @returns {Object} All answers in an object structure with 'text' field
*/
const mapAnswersToContent = (answers: SubmittedData) => {
const mapped = {
[VALID_COMPANY_BASE]: {
Expand Down
6 changes: 6 additions & 0 deletions src/ui/server/helpers/data-content-mappings/map-cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { Quote, SubmittedData } from '../../../types';

const { CONTRACT_VALUE, CURRENCY, POLICY_TYPE, MAX_AMOUNT_OWED } = FIELD_IDS;

/**
* mapCost
* Map cost answer into an object for GOV summary list structure
* @param {Object} All submitted data
* @returns {Object} Answer in an object
*/
const mapCost = (answers: SubmittedData | Quote) => {
let mapped;

Expand Down
6 changes: 6 additions & 0 deletions src/ui/server/helpers/data-content-mappings/map-country.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Country } from '../../../types';

/**
* mapCountry
* Map country answer to a simple name string
* @param {Object} Country
* @returns {String} Country name
*/
const mapCountry = (countryObj: Country) => countryObj.name;

export default mapCountry;
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* mapPeriodMonths
* Map credit period answer to a month/s string
* @param {Number} Credit period
* @returns {String} Credit period with month/s string
*/
const mapPeriodMonths = (answer: number) => {
if (answer === 1) {
return `${answer} month`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { FIELD_IDS } from '../../constants';

const { POLICY_TYPE, POLICY_LENGTH, SINGLE_POLICY_LENGTH, MULTI_POLICY_LENGTH } = FIELD_IDS;

/**
* mapPolicyLength
* Map policy length answer into an object for GOV summary list structure
* @param {Number} Credit period
* @returns {String} Answer in an object
*/
const mapPolicyLength = (data: object) => {
let mapped;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const { BUYER_COUNTRY, CURRENCY, PERCENTAGE_OF_COVER, QUOTE } = FIELD_IDS;

const { ESTIMATED_COST, INSURED_FOR, PREMIUM_RATE_PERCENTAGE } = QUOTE;

/**
* mapQuoteToContent
* Map all quote values (generated from submitted data and calculator) into an object structure for GOVUK summary list structure
* @param {Object} Quote
* @returns {Object} All quote values in an object structure with 'text' field
*/
const mapQuoteToContent = (quote: Quote): QuoteContent => {
const currencyCode = quote[CURRENCY].isoCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatCurrency from './format-currency';
import formatCurrency from '.';

describe('server/helpers/format-currency', () => {
it('shouild return a formatted currency', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* formatCurrency
* Transform a number into a currency string
* @param {Number} Amount
* @param {String} Currency code
* @param {Number} Decimal points
* @returns {String}
*/
const formatCurrency = (number: number, currencyCode: string, decimalPoints: number): string =>
number.toLocaleString('en', {
style: 'currency',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getCountryByName from './get-country-by-name';
import { Country } from '../../types';
import getCountryByName from '.';
import { Country } from '../../../types';

describe('server/helpers/get-country-by-name', () => {
it('should return a mapped object', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Country } from '../../types';
import { Country } from '../../../types';

/**
* getCountryByName
* Get a country by name
* @param {Array} Countries
* @param {String} Country name
* @returns {Object} Country
*/
const getCountryByName = (countries: Array<Country>, countryName: string) => {
const country = countries.find((c) => c.name === countryName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getCurrencyByCode from './get-currency-by-code';
import getCurrencyByCode from '.';

describe('server/helpers/get-currency-by-code', () => {
it('should return a mapped object', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Currency } from '../../types';
import { Currency } from '../../../types';

/**
* getCurrencyByCode
* Get a currency by ISO code
* @param {Array} Currencies
* @param {String} Currency ISO code
* @returns {Object} Currency
*/
const getCurrencyByCode = (currencies: Array<Currency>, isoCode: string) => {
const currency = currencies.find((c) => c.isoCode === isoCode);

Expand Down
11 changes: 0 additions & 11 deletions src/ui/server/helpers/get-last-substring.test.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/ui/server/helpers/get-last-substring.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/ui/server/helpers/get-last-substring/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import getLastSubstring from '.';

describe('server/helpers/get-last-substring', () => {
it('should return the last sub string', () => {
const mockStr = 'a/b/c';

const result = getLastSubstring(mockStr);

expect(result).toEqual('c');
});

describe('when no string is provided', () => {
it('should return null', () => {
// @ts-ignore
const result = getLastSubstring();

expect(result).toEqual(null);
});
});
});
15 changes: 15 additions & 0 deletions src/ui/server/helpers/get-last-substring/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* getLastSubstring
* Get the last sub string in a string
* @param {String}
* @returns {String} Last sub string
*/
const getLastSubstring = (str: string) => {
if (str) {
return str.substring(str.lastIndexOf('/') + 1);
}

return null;
};

export default getLastSubstring;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isChangeRoute from './is-change-route';
import isChangeRoute from '.';

describe('server/helpers/is-change-route', () => {
describe("when the url's last substring is `change`", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import getLastSubstring from './get-last-substring';
import getLastSubstring from '../get-last-substring';

/**
* isChangeRoute
* Check if the last part of a string/URL is 'change'
* @param {String} URL
* @returns {Boolean}
*/
const isChangeRoute = (url: string) => {
const lastSubstring = getLastSubstring(url);

Expand Down
38 changes: 38 additions & 0 deletions src/ui/server/helpers/mappings/map-countries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { API } from '../../constants';
import sortArrayAlphabetically from '../sort-array-alphabetically';
import { CisCountry, Country } from '../../../types';

/**
* mapRiskCategory
* Transform a countries risk category to a consistent string
* @param {String} Risk category
* @returns {String} Consistent risk category
*/
export const mapRiskCategory = (str: string) => {
if (str === API.CIS.RISK.STANDARD) {
return API.MAPPINGS.RISK.STANDARD;
Expand All @@ -18,6 +24,12 @@ export const mapRiskCategory = (str: string) => {
return null;
};

/**
* mapShortTermCoverAvailabe
* Transform a countries 'short term cover available' string to a boolean
* @param {String} Risk category
* @returns {Boolean}
*/
export const mapShortTermCoverAvailable = (str: string): boolean => {
if (str === API.CIS.SHORT_TERM_COVER_AVAILABLE.YES) {
return true;
Expand All @@ -38,6 +50,12 @@ export const mapShortTermCoverAvailable = (str: string): boolean => {
return false;
};

/**
* mapNbiIssueAvailable
* Transform a countries 'NBI issue available' string to a boolean
* @param {String} NBI flag
* @returns {Boolean}
*/
export const mapNbiIssueAvailable = (str: string): boolean => {
if (str === API.CIS.NBI_ISSUE_AVAILABLE.YES) {
return true;
Expand All @@ -46,8 +64,21 @@ export const mapNbiIssueAvailable = (str: string): boolean => {
return false;
};

/**
* filterCisCountries
* Filter out countries from CIS API that have an invalid name
* @param {Array} All CIS countries
* @returns {Array} CIS countries without invalid country names
*/
export const filterCisCountries = (countries: Array<CisCountry>) => countries.filter((country) => !API.CIS.INVALID_COUNTRIES.includes(country.marketName));

/**
* mapCountry
* Map a CIS country to cleaner structure
* @param {Object} CIS Country
* @returns {String} Selected country ISO code
* @returns {Object} Mapped country
*/
export const mapCountry = (country: CisCountry, selectedIsoCode?: string): Country => {
const mapped = {
name: country.marketName,
Expand All @@ -65,6 +96,13 @@ export const mapCountry = (country: CisCountry, selectedIsoCode?: string): Count
return mapped;
};

/**
* mapCountries
* Map all CIS countries to cleaner structure
* @param {Array} Array of CIS Countries
* @returns {String} Selected country ISO code
* @returns {Array} Array of mapped countries
*/
export const mapCountries = (countries: Array<CisCountry>, selectedIsoCode?: string) => {
const filteredCountries = filterCisCountries(countries);

Expand Down
7 changes: 7 additions & 0 deletions src/ui/server/helpers/mappings/map-credit-period.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { SelectOption } from '../../../types';

/**
* mapCreditPeriod
* Map all credit periods and mark if it's selected
* @param {Array} Array of credit period objects
* @returns {String} Selected credit period value
* @returns {Array} Array of mapped credit periods
*/
const mapCreditPeriod = (options: Array<SelectOption>, selectedValue?: string) => {
const mapped = options.map((opt: SelectOption) => {
const mappedOption = {
Expand Down
13 changes: 13 additions & 0 deletions src/ui/server/helpers/mappings/map-currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ import { SUPPORTED_CURRENCIES } from '../../constants';
import sortArrayAlphabetically from '../sort-array-alphabetically';
import { Currency } from '../../../types';

/**
* getSupportedCurrencies
* Get all supported currencies
* @param {Array} Array of all possible currencies
* @returns {Array} Array of currencies that EXIP supports
*/
const getSupportedCurrencies = (currencies: Array<Currency>) => {
const supported = currencies.filter((currency) => SUPPORTED_CURRENCIES.find((currencyCode: string) => currency.isoCode === currencyCode));

return supported;
};

/**
* mapCurrencies
* Map all currencies and mark if it's selected
* @param {Array} Array of currency objects
* @returns {String} Selected credit period value
* @returns {Array} Array of mapped currencies
*/
const mapCurrencies = (currencies: Array<Currency>, selectedValue?: string) => {
const supportedCurrencies = getSupportedCurrencies(currencies);

Expand Down
7 changes: 7 additions & 0 deletions src/ui/server/helpers/mappings/map-percentage-of-cover.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* mapPercentageOfCover
* Map all percentages of cover and mark if it's selected
* @param {Array} Array of percentages of cover
* @returns {String} Selected percentage
* @returns {Array} Array of mapped percentages of cover
*/
const mapPercentageOfCover = (percentages: Array<number>, selectedValue?: number) => {
const mapped = percentages.map((percentage) => {
if (selectedValue && selectedValue === percentage) {
Expand Down
Loading

0 comments on commit 6e15dc3

Please sign in to comment.