Skip to content

Commit

Permalink
fix(refs): unblock extract implicit refs in multi candidate suffix si…
Browse files Browse the repository at this point in the history
…tuations
  • Loading branch information
uladkasach committed Jun 7, 2024
1 parent 5622cc8 commit 4aa9891
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"chalk": "2.4.2",
"change-case": "4.1.1",
"domain-objects": "0.20.0",
"domain-objects-metadata": "0.5.0",
"domain-objects-metadata": "0.5.1",
"fast-glob": "3.2.2",
"joi": "17.4.0",
"lodash.omit": "4.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe('getDomainObjectNameThatPropertyIsUnambigiouslyNaturallyNamedAfter', ()
allDomainObjectNames: ['HomeAddress', 'WorkAddress', 'Geocode', 'User'],
expectedErrorMessageContains: '["HomeAddress","WorkAddress"]',
},
{
propertyName: 'homeAddress',
allDomainObjectNames: ['HomeAddress', 'WorkAddress', 'Geocode', 'User'],
expectedResult: 'HomeAddress',
},
{
propertyName: 'externalId',
allDomainObjectNames: ['PlaneExternalId', 'Airport', 'PlaneManufacturer'],
Expand All @@ -41,6 +46,17 @@ describe('getDomainObjectNameThatPropertyIsUnambigiouslyNaturallyNamedAfter', ()
allDomainObjectNames: ['PlaneExternalId', 'Airport', 'PlaneManufacturer'],
expectedResult: 'PlaneExternalId',
},
{
propertyName: 'eventUuid',
allDomainObjectNames: ['OutreachCaptureEvent', 'RelationshipEvent'],
expectedErrorMessageContains:
'["OutreachCaptureEvent","RelationshipEvent"]',
},
{
propertyName: 'outreachCaptureEventUuid',
allDomainObjectNames: ['OutreachCaptureEvent', 'RelationshipEvent'],
expectedResult: 'OutreachCaptureEvent',
},
];

cases.forEach((testCase) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UnexpectedCodePathError } from '@ehmpathy/error-fns';
import { isPropertyNameAReferenceIntuitively } from 'domain-objects';

export class AmbiguouslyNamedDomainObjectReferencePropertyError extends Error {
Expand Down Expand Up @@ -39,6 +40,9 @@ Please update the name of this property or the names of your domain objects in o
* - address, ['HomeAddress', 'Geocode', 'User'] => 'HomeAddress'
* - address, ['HomeAddress', 'WorkAddress', 'Geocode', 'User'] => throws AmbiguouslyImplicitUuidReferencePropertyNameError('could reference either WorkAddress or HomeAddress. please remove this ambiguity');
* - externalId, ['PlaneExternalId', 'Airport', 'PlaneManufacturer'] => 'PlaneExternalId'
*
* todo:
* - eliminate this entirely, in favor of using explicit references, via `Ref<Dobj>` or `public static refs = { }`
*/
export const getDomainObjectNameThatPropertyIsUnambigiouslyNaturallyNamedAfter =
({
Expand All @@ -50,6 +54,24 @@ export const getDomainObjectNameThatPropertyIsUnambigiouslyNaturallyNamedAfter =
propertyName: string;
allDomainObjectNames: string[];
}): string | null => {
// determine whether there is a domain object with that exact same name
const domainObjectWithExactSameName = allDomainObjectNames.filter(
(dobjName) =>
propertyName.toLowerCase() === dobjName.toLowerCase() ||
propertyName.toLowerCase() ===
[dobjName, 'uuid'].join('').toLowerCase() || // todo: getPrimaryKey of dobj class, dont just assume its uuid
propertyName.toLowerCase() ===
[dobjName, 'uuids'].join('').toLowerCase(), // todo: getPrimaryKey of dobj class, dont just assume its uuid
);
if (domainObjectWithExactSameName.length > 1)
throw new AmbiguouslyNamedDomainObjectReferencePropertyError({
parentDomainObjectName,
propertyName,
ambiguousOptions: domainObjectWithExactSameName,
});
if (domainObjectWithExactSameName.length === 1)
return domainObjectWithExactSameName[0]!;

// determine all of the domain objects that are intuitively referenced by this property name
const intuitivelyReferencedDomainObjectNames = allDomainObjectNames.filter(
(domainObjectName) =>
Expand Down

0 comments on commit 4aa9891

Please sign in to comment.