Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 278 additions & 6 deletions resources-domain/lambdas/s3-importer/src/uschMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,85 @@ import type { AccountSID } from '@tech-matters/types';
import type { FlatResource } from '@tech-matters/resources-types';
import { parse } from 'date-fns';

// https://gist.github.com/mshafrir/2646763
const US_STATE_CODE_MAPPING = {
AL: 'Alabama',
AK: 'Alaska',
AS: 'American Samoa',
AZ: 'Arizona',
AR: 'Arkansas',
CA: 'California',
CO: 'Colorado',
CT: 'Connecticut',
DE: 'Delaware',
DC: 'District Of Columbia',
FM: 'Federated States Of Micronesia',
FL: 'Florida',
GA: 'Georgia',
GU: 'Guam',
HI: 'Hawaii',
ID: 'Idaho',
IL: 'Illinois',
IN: 'Indiana',
IA: 'Iowa',
KS: 'Kansas',
KY: 'Kentucky',
LA: 'Louisiana',
ME: 'Maine',
MH: 'Marshall Islands',
MD: 'Maryland',
MA: 'Massachusetts',
MI: 'Michigan',
MN: 'Minnesota',
MS: 'Mississippi',
MO: 'Missouri',
MT: 'Montana',
NE: 'Nebraska',
NV: 'Nevada',
NH: 'New Hampshire',
NJ: 'New Jersey',
NM: 'New Mexico',
NY: 'New York',
NC: 'North Carolina',
ND: 'North Dakota',
MP: 'Northern Mariana Islands',
OH: 'Ohio',
OK: 'Oklahoma',
OR: 'Oregon',
PW: 'Palau',
PA: 'Pennsylvania',
PR: 'Puerto Rico',
RI: 'Rhode Island',
SC: 'South Carolina',
SD: 'South Dakota',
TN: 'Tennessee',
TX: 'Texas',
UT: 'Utah',
VT: 'Vermont',
VI: 'Virgin Islands',
VA: 'Virginia',
WA: 'Washington',
WV: 'West Virginia',
WI: 'Wisconsin',
WY: 'Wyoming',
} as Record<string, string>;

const CANADIAN_PROVINCE_CODE_MAPPING = {
AB: 'Alberta',
BC: 'British Columbia',
NL: 'Newfoundland and Labrador',
PE: 'Île-du-Prince-Édouard',
NS: 'Nouvelle-Écosse',
NB: 'New Brunswick',
ON: 'Ontario',
MB: 'Manitoba',
SK: 'Saskatchewan',
YT: 'Yukon',
NT: 'Northwest Territories',
NU: 'Nunavut',
QC: 'Québec',
} as Record<string, string>;

/*
* This defines all the mapping logic to convert Childhelp resource to an Aselo resource.
* The mapping is defined as a tree of nodes.
Expand Down Expand Up @@ -93,6 +172,31 @@ export type UschExpandedResource = Partial<
}
>;

const isUnitedStates = (country: string | undefined) =>
['us', 'usa', 'unitedstates'].includes(
(country ?? '').toLowerCase().replaceAll(/[.\s]/g, ''),
);

const isUSStateOrTerritory = (country: string | undefined) =>
country &&
(Object.keys(US_STATE_CODE_MAPPING).includes(country) ||
Object.values(US_STATE_CODE_MAPPING).includes(country));

const isCanadianProvince = (country: string | undefined) =>
country &&
(Object.keys(CANADIAN_PROVINCE_CODE_MAPPING).includes(country) ||
Object.values(CANADIAN_PROVINCE_CODE_MAPPING).includes(country));

const lookupUsStateNameFromCode = ({
Country: country,
StateProvince: stateProvince,
}: UschExpandedResource): string | undefined => {
if (isUnitedStates(country)) {
return US_STATE_CODE_MAPPING[stateProvince ?? ''] ?? stateProvince;
}
return stateProvince;
};

export const expandCsvLine = (csv: UschCsvResource): UschExpandedResource => {
const expanded = {
...csv,
Expand All @@ -113,8 +217,27 @@ export const USCH_MAPPING_NODE: MappingNode = {
Name: resourceFieldMapping('name'),
AlternateName: translatableAttributeMapping('alternateName', { language: 'en' }),
Address: attributeMapping('stringAttributes', 'address/street'),
City: attributeMapping('stringAttributes', 'address/city'),
StateProvince: attributeMapping('stringAttributes', 'address/province'),
City: attributeMapping('stringAttributes', 'address/city', {
value: ({ currentValue, rootResource }) =>
[
(rootResource as UschExpandedResource).Country,
(rootResource as UschExpandedResource).StateProvince,
currentValue,
].join('/'),
info: ({ currentValue, rootResource }) => ({
country: (rootResource as UschExpandedResource).Country,
stateProvince: lookupUsStateNameFromCode(rootResource as UschExpandedResource),
name: currentValue,
}),
}),
StateProvince: attributeMapping('stringAttributes', 'address/province', {
value: ({ currentValue, rootResource }) =>
`${(rootResource as UschExpandedResource).Country}/${currentValue}`,
info: ({ rootResource }) => ({
country: (rootResource as UschExpandedResource).Country,
name: lookupUsStateNameFromCode(rootResource as UschExpandedResource),
}),
}),
PostalCode: attributeMapping('stringAttributes', 'address/postalCode'),
Country: attributeMapping('stringAttributes', 'address/country'),
HoursOfOperation: translatableAttributeMapping('hoursOfOperation'),
Expand Down Expand Up @@ -188,10 +311,159 @@ export const USCH_MAPPING_NODE: MappingNode = {
},
Coverage: {
children: {
'{coverageIndex}': translatableAttributeMapping('coverage/{coverageIndex}', {
value: ({ currentValue }) => currentValue,
language: 'en',
}),
'{coverageIndex}': {
mappings: [
translatableAttributeMapping('coverage/{coverageIndex}', {
value: ({ currentValue }) => {
const [countryOrState] = currentValue.toString().split(/\s+-\s+/);

if (isUSStateOrTerritory(countryOrState)) {
return `United States/${currentValue.replaceAll(/\s+-\s+/g, '/')}`;
} else {
return `${currentValue.replaceAll(/\s+-\s+/g, '/')}`;
}
},
info: ({ currentValue }) => {
const [countryOrState, provinceOrCity, internationalCity] = currentValue
.toString()
.split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
return {
country: 'United States',
stateProvince:
US_STATE_CODE_MAPPING[countryOrState ?? ''] ?? countryOrState,
city: provinceOrCity,
name: currentValue,
};
} else if (isCanadianProvince(countryOrState)) {
return {
country: 'Canada',
stateProvince:
CANADIAN_PROVINCE_CODE_MAPPING[countryOrState ?? ''] ??
countryOrState,
city: provinceOrCity,
name: currentValue,
};
} else {
return {
country: countryOrState,
stateProvince: provinceOrCity,
city: internationalCity,
name: currentValue,
};
}
},
language: 'en',
}),
// Not using coverage/country because that makes things bessy with root coverage values
translatableAttributeMapping('coverageCountry/{coverageIndex}', {
value: ({ currentValue }) => {
const [countryOrState] = currentValue.toString().split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
return 'United States';
} else if (isCanadianProvince(countryOrState)) {
return 'Canada';
} else {
return countryOrState;
}
},
language: 'en',
}),
// Not using coverage/province because that makes things bessy with root coverage values
translatableAttributeMapping('coverageStateProvince/{coverageIndex}', {
value: ({ currentValue }) => {
const [countryOrState, provinceOrCity] = currentValue
.toString()
.split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
return `United States/${countryOrState}`;
} else if (isCanadianProvince(countryOrState)) {
return `Canada/${countryOrState}`;
} else if (provinceOrCity) {
return `${countryOrState}/${provinceOrCity}`;
} else return '';
},
info: ({ currentValue }) => {
const [countryOrState, provinceOrCity] = currentValue
.toString()
.split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
const stateProvince =
US_STATE_CODE_MAPPING[countryOrState ?? ''] ?? countryOrState;
return {
country: 'United States',
stateProvince,
name: stateProvince,
};
} else if (isCanadianProvince(countryOrState)) {
const stateProvince =
CANADIAN_PROVINCE_CODE_MAPPING[countryOrState ?? ''] ?? countryOrState;
return {
country: 'Canada',
stateProvince,
name: stateProvince,
};
} else if (provinceOrCity) {
return {
country: countryOrState,
stateProvince: provinceOrCity,
name: provinceOrCity,
};
} else return null;
},
language: 'en',
}),
// Not using coverage/city because that makes things messy with root coverage values
translatableAttributeMapping('coverageCity/{coverageIndex}', {
value: ({ currentValue }) => {
const [countryOrState, provinceOrCity, internationalCity] = currentValue
.toString()
.split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
return `United States/${countryOrState}/${provinceOrCity}`;
} else if (isCanadianProvince(countryOrState)) {
return `Canada/${countryOrState}/${provinceOrCity}`;
} else if (internationalCity) {
return `${countryOrState}/${provinceOrCity}/${internationalCity}`;
} else return '';
},
info: ({ currentValue, rootResource }) => {
const [countryOrState, provinceOrCity, internationalCity] = currentValue
.toString()
.split(/\s+-\s+/);
if (isUSStateOrTerritory(countryOrState)) {
const stateProvince =
US_STATE_CODE_MAPPING[countryOrState ?? ''] ?? countryOrState;
return {
country: 'United States',
stateProvince,
city: provinceOrCity,
name: provinceOrCity,
};
} else if (isCanadianProvince(rootResource.Country)) {
const stateProvince =
CANADIAN_PROVINCE_CODE_MAPPING[countryOrState ?? ''] ?? countryOrState;
return {
country: 'Canada',
stateProvince,
city: provinceOrCity,
name: provinceOrCity,
};
} else if (internationalCity) {
return {
country: countryOrState,
stateProvince: provinceOrCity,
city: internationalCity,
name: internationalCity,
};
} else {
return null;
}
},
language: 'en',
}),
],
},
},
},
Comment: translatableAttributeMapping('comment', { language: 'en' }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import type { ReferrableResourceAttribute } from '@tech-matters/resources-types';
import { ResourceIndexDocumentMappings } from './resourceIndexDocumentMappings';
import { ResourcesSearchConfiguration } from '../searchConfiguration';

Expand Down Expand Up @@ -59,22 +58,16 @@ const resourceIndexDocumentMappings: ResourceIndexDocumentMappings = {
type: 'keyword',
isArrayField: true,
attributeKeyPattern: /(.*)([cC])ountry$/,
indexValueGenerator: ({ value, info }: ReferrableResourceAttribute<string>) =>
[info?.name, value].filter(i => i).join(' '),
},
province: {
type: 'keyword',
isArrayField: true,
attributeKeyPattern: /(.*)([pP])rovince$/,
indexValueGenerator: ({ value, info }: ReferrableResourceAttribute<string>) =>
[info?.name, value].filter(i => i).join(' '),
},
city: {
type: 'keyword',
isArrayField: true,
attributeKeyPattern: /(.*)[cC]ity$/,
indexValueGenerator: ({ value, info }: ReferrableResourceAttribute<string>) =>
[info?.name, value].filter(i => i).join(' '),
},
},
languageFields: {
Expand Down
2 changes: 1 addition & 1 deletion resources-domain/resources-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test:service": "cross-env POSTGRES_PORT=5433 RDS_USERNAME=hrm RDS_PASSWORD=postgres RESOURCES_PASSWORD=resources-password run-s -c docker:compose:test:up db:create:schema test:service:ci:migrate test:service:ci:run docker:compose:test:down",
"test:service:ci": "RDS_USERNAME=rdsadmin RDS_PASSWORD=postgres RESOURCES_PASSWORD=resources-password run-s db:create:schema test:service:ci:migrate test:service:ci:run",
"test:service:ci:migrate": "node ./db-migrate",
"test:service:ci:run": "cross-env AWS_REGION=us-east-1 CI=true TWILIO_ACCOUNT_SID=ACxxx TWILIO_AUTH_TOKEN=xxxxxx SSM_ENDPOINT=http://mock-ssm/ jest --verbose --maxWorkers=1 --forceExit --coverage tests/service/adminSearch.test.ts",
"test:service:ci:run": "cross-env AWS_REGION=us-east-1 CI=true TWILIO_ACCOUNT_SID=ACxxx TWILIO_AUTH_TOKEN=xxxxxx SSM_ENDPOINT=http://mock-ssm/ jest --verbose --maxWorkers=1 --forceExit --coverage tests/service",
"test:coverage": "run-s docker:compose:test:up test:service:migrate test:coverage:run docker:compose:test:down",
"test:coverage:run": "cross-env POSTGRES_PORT=5433 AWS_REGION=us-east-1 jest --verbose --maxWorkers=1 --coverage",
"test:migrate": "run-s test:service:migrate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const referenceAttributeRoutes = () => {
const router: IRouter = Router();
const { getResourceReferenceAttributeList } = referenceAttributeService();

router.get('/:list', async (req, res) => {
router.get('/*', async (req, res) => {
const { valueStartsWith, language } = req.query;
const { list } = req.params;
const list = (req as any).params[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we document what's this doing? Is params now a list or how is this working? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes /* returns a list, which is documented in Express, I'm not sure we should document standard API behaviour

const result = await getResourceReferenceAttributeList(
req.hrmAccountId as AccountSID,
list,
Expand Down
Loading
Loading