Skip to content

Commit

Permalink
First iteration of relationships version 2 prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
saedwards committed Apr 27, 2018
1 parent 1b79b9e commit 441213a
Show file tree
Hide file tree
Showing 13 changed files with 1,128 additions and 705 deletions.
69 changes: 54 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
{
"parser" : "babel-eslint",
"extends" : [
"parser": "babel-eslint",
"extends": [
"standard"
],
"ecmaFeatures": {
"modules": true
},
"env": {
"browser" : true
"browser": true
},
"globals": {
"globals": {
"$": true,
"jQuery": true,
"browser": true,
"ga": true
},
"rules": {
"no-unused-vars" : [1],
"semi" : [0, "always"],
"space-before-function-paren": [2, "never"],
"no-unused-vars": [
1
],
"semi": [
0,
"always"
],
"space-before-function-paren": [
2,
"never"
],
"no-alert": 0,
"quotes": [2, "single"],
"strict": [2, "never"],
"new-cap": [0],
"quotes": [
2,
"single"
],
"strict": [
2,
"never"
],
"new-cap": [
0
],
"consistent-return": 0,
"no-underscore-dangle": 0,
"indent": [2, 2, {"SwitchCase": 1}],
"space-before-blocks": [2, "always"],
"no-var": [1],
"one-var": [0],
"no-new": [0]
"indent": [
2,
2,
{
"SwitchCase": 1
}
],
"space-before-blocks": [
2,
"always"
],
"no-var": [
1
],
"one-var": [
0
],
"no-new": [
0
],
"padded-blocks": [
0,
"never"
],
"computed-property-even-spacing": [
0,
"never"
]
}
}
152 changes: 141 additions & 11 deletions _prototypes/your-household-v2/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export const RELATIONSHIPS_STORAGE_KEY = 'relationships';
export const VISITOR_TYPE = 'visitor';

let relationshipTypes = {
'spouse': {},
'child-parent': {},
'step-child-parent': {},
'grandchild-grandparent': {},
'sibling': {},
'step-brother-sister': {},
'partner': {},
'unrelated': {},
'other-relation': {}
'spouse': {id: 'spouse'},
'child-parent': {id: 'child-parent'},
'step-child-parent': {id: 'step-child-parent'},
'grandchild-grandparent': {id: 'grandchild-grandparent'},
'sibling': {id: 'sibling'},
'step-brother-sister': {id: 'step-brother-sister'},
'partner': {id: 'partner'},
'unrelated': {id: 'unrelated'},
'other-relation': {id: 'other-relation'}
};

let relationshipDescriptionMap = {
Expand Down Expand Up @@ -68,6 +68,11 @@ let relationshipDescriptionMap = {
}
};

/**
* Augment Underscore library
*/
const _ = window._ || {};

export function getAddress() {
let addressLines = sessionStorage.getItem('address').split(',');

Expand Down Expand Up @@ -193,8 +198,9 @@ export function isOtherHouseholdMember(member) {
/**
* Relationships
*/
export function addRelationship(relationshipObj) {
let householdRelationships = getAllRelationships() || [],
export function addRelationship(relationshipObj, opts) {
let options = opts || {},
householdRelationships = getAllRelationships() || [],
item = {
...relationshipObj,
id: autoIncrementId('relationships')
Expand Down Expand Up @@ -242,6 +248,129 @@ export function relationship(description, personIsId, personToId) {
};
}

/**
* Find relationships
*/
export function isInRelationship(personId, relationship) {
return relationship.personToId === personId || relationship.personIsId === personId;
}

export function isAChildInRelationship(personId, relationship) {
/**
* Guard
*/
if (!isInRelationship(personId, relationship)) {
return false;
}

/**
* TODO - child-parent relationship can be found from reversing
* personIs & personTo checks and changing description to son-daughter
*/
return (
relationship.personIsDescription === 'mother-father' &&
relationship.personToId === personId
) || (
relationship.personIsDescription === 'son-daughter' &&
relationship.personIsId === personId
);
}

export function isASiblingInRelationship(personId, relationship) {
return isInRelationship(personId, relationship) &&
relationshipDescriptionMap[relationship.personIsDescription].type.id === 'sibling';
}

export function getParentIdFromRelationship(relationship) {
let parentId;

if (relationship.personIsDescription === 'mother-father') {
parentId = relationship.personIsId;
}

if (relationship.personIsDescription === 'son-daughter') {
parentId = relationship.personToId;
}

if (!parentId) {
console.log('Parent not found in relationship: ', relationship);
return false;
}

return parentId;
}

export function getSiblingIdFromRelationship(personId, relationship) {
if (!isInRelationship(personId, relationship)) {
console.log('Person ' + personId + ' not found in relationship: ', relationship);
return false;
}

return relationship[relationship.personIsId === personId ? 'personToId' : 'personIsId'];
}

export function getAllParentsOf(personId) {
return getAllRelationships()
.filter(isAChildInRelationship.bind(null, personId))
.map(relationship => getHouseholdMemberByPersonId(getParentIdFromRelationship(relationship))['@person']);
}

export const missingRelationshipInference = {
siblingsOf(person) {

const missingRelationships = [],
allRelationships = getAllRelationships(),
personId = person.id,

parents = getAllParentsOf(personId),

siblingIds = allRelationships
.filter(isASiblingInRelationship.bind(null, personId))
.map(getSiblingIdFromRelationship.bind(null, personId));

/**
* If 2 parent relationships of 'person' are found we can attempt to infer
* sibling relationships
*/
if (parents.length === 2) {

getAllHouseholdMembers().forEach((member) => {

const memberPersonId = member['@person'].id;

/**
* If member already has a sibling relationship with 'person', skip
*/
if (siblingIds.indexOf(memberPersonId) > -1) {
return;
}

const memberParents = getAllParentsOf(memberPersonId);

/**
* If 2 parents of 'member' are found
* and they are the same parents of 'person'
* we have identified a missing inferred relationship
*/
if (memberParents.length === 2 &&
_.difference(parents, memberParents).length === 0) {

/**
* Add to missingRelationships
*/
missingRelationships.push(relationship(
'brother-sister',
person.id,
memberPersonId
));
}
});
}

return missingRelationships;
}
};

/**
* Helpers
*/
Expand Down Expand Up @@ -369,6 +498,7 @@ window.ONS.storage = {
deleteAllRelationshipsForMember,

relationshipDescriptionMap,
missingRelationshipInference,

KEYS: {
HOUSEHOLD_MEMBERS_STORAGE_KEY,
Expand Down
Loading

0 comments on commit 441213a

Please sign in to comment.