Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
timtamimi committed Aug 11, 2021
1 parent b6e6e03 commit f05818c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/digitise/ar-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const addData = (state, action) => {
content: {}
};

const ids = (state.licence.arData || []).map(item => item.id);
const ids = (state.licence.arData || []).map(subitem => subitem.id);
if (ids.includes(id)) {
throw new Error(`Cannot add data with ID ${id}, already exists`);
}
Expand Down
50 changes: 18 additions & 32 deletions src/digitise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ const { getWR22 } = require('./schema');
* @param {Object} data
* @return {Object} with any 'null' converted to null
*/
const transformNulls = (data) => {
return deepMap(data, (val) => {
// Convert string null to real null
if (typeof (val) === 'string' && (val === '' || val === 'null')) {
return null;
}
return val;
});
};
const transformNulls = (data) => deepMap(data, (val) => {
// Convert string null to real null
if (typeof (val) === 'string' && (val === '' || val === 'null')) {
return null;
}
return val;
});

/**
* A function that generates a simple JSON schema as a starting point for the supplied object
Expand All @@ -52,7 +50,7 @@ const transformNulls = (data) => {
*/
const generateJsonSchema = (obj) => ({
type: 'object',
properties: mapValues(obj, (value) => {
properties: mapValues(obj, () => {
return {
type: 'string'
};
Expand All @@ -62,14 +60,12 @@ const generateJsonSchema = (obj) => ({
/**
* Like lodash set, but always creates an object
* even if key is numeric
* @param {Object} object
* @param {Object} obj
* @param {String} path
* @param {Mixed} value
* @return {Object}
*/
const setObject = (obj, path, value) => {
return setWith(obj, path, value, (obj) => obj || {});
};
const setObject = (obj, path, value) => setWith(obj, path, value, (subObj) => subObj || {});

/**
* Checks for match for items with integer ids
Expand All @@ -78,9 +74,7 @@ const setObject = (obj, path, value) => {
* @param {Number} id - ID to check item ID against
* @return {Boolean}
*/
const isMatch = (item, id) => {
return parseInt(item.ID) === parseInt(id);
};
const isMatch = (item, id) => parseInt(item.ID) === parseInt(id);

/**
* Checks if version matches the supplied issue and increment number
Expand All @@ -89,9 +83,8 @@ const isMatch = (item, id) => {
* @param {Number} incrementNumber
* @return {Boolean}
*/
const isVersion = (version, issueNumber, incrementNumber) => {
return issueNumber === parseInt(version.ISSUE_NO) && incrementNumber === parseInt(version.INCR_NO);
};
const isVersion = (version, issueNumber, incrementNumber) =>
issueNumber === parseInt(version.ISSUE_NO) && incrementNumber === parseInt(version.INCR_NO);

/**
* Formats the supplied object and filters out any non-scalar values
Expand Down Expand Up @@ -126,9 +119,7 @@ const prepareItem = (licence, finalState, getter = x => x) => {
* @param {Object} schema - JSON schema
* @return {Object}
*/
const extractData = (object, schema) => {
return pick(object, Object.keys(schema.properties));
};
const extractData = (object, schema) => pick(object, Object.keys(schema.properties));

/**
* Maps an AR item in the AR licence to a format expected by the view
Expand Down Expand Up @@ -164,7 +155,6 @@ const prepareData = (licence, finalState) => {
const address = prepareItem(licence, finalState, getCurrentVersionAddress);

// Prepare purposes
// @TODO - we will need to compare to check for deleted/added items
const purposes = getPurposes(licence.licence_data_value).map((purpose, index) => {
return {
base: filterScalars(purpose),
Expand All @@ -191,15 +181,15 @@ const prepareData = (licence, finalState) => {
const arData = (finalState.licence.arData || []).map(mapARItem);

return {
licence: base,
currentVersion,
purposes,
points,
conditions,
notes: finalState.notes,
party,
address,
arData
arData,
licence: base,
notes: finalState.notes
};
};

Expand All @@ -208,11 +198,7 @@ const prepareData = (licence, finalState) => {
* @param {Object} obj
* @return {Object}
*/
const filterScalars = (obj) => {
return pickBy(obj, (val) => {
return !(isArray(val) || isObject(val));
});
};
const filterScalars = obj => pickBy(obj, val => !(isArray(val) || isObject(val)));

module.exports = {
getPurpose,
Expand Down
48 changes: 13 additions & 35 deletions src/digitise/licence-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ const { find, flatMap } = require('lodash');
* @param {Object} data - permit data for licence
* @return {Array} array of purposes
*/
const getPurposes = data => {
return data.data.current_version.purposes;
};
const getPurposes = data => data.data.current_version.purposes;

/**
* Get purpose
* @param {Object} data - permit data for licence
* @param
*/
const getPurpose = (data, purposeId) => {
return find(getPurposes(data), { ID: purposeId });
};
const getPurpose = (data, purposeId) => find(getPurposes(data), { ID: purposeId });

/**
* Get licence
* @param {Object} data - permit data for licence
* @param
*/
const getLicence = data => {
return data;
};
const getLicence = data => data;

const purposePointsMapper = purpose => purpose.purposePoints.map(row => row.point_detail);
const purposeConditionsMapper = purpose => purpose.licenceConditions;
Expand All @@ -41,38 +35,30 @@ const partyAddressesMapper = party => party.contacts.map(contact => contact.part
* @param {Object} data - permit data for licence
* @return {Array} array of licence points
*/
const getPoints = data => {
return flatMap(getPurposes(data), purposePointsMapper);
};
const getPoints = data => flatMap(getPurposes(data), purposePointsMapper);

/**
* Get a single point
* @param {Object} data - all licence data
* @param {String} pointId - the point ID
* @return {Object} point
*/
const getPoint = (data, pointId) => {
return find(getPoints(data), { ID: pointId });
};
const getPoint = (data, pointId) => find(getPoints(data), { ID: pointId });

/**
* Get conditions from licence
* @param {Object} data - permit data for licence
* @return {Array} array of licence conditions
*/
const getConditions = data => {
return flatMap(getPurposes(data), purposeConditionsMapper);
};
const getConditions = data => flatMap(getPurposes(data), purposeConditionsMapper);

/**
* Get a single condition
* @param {Object} data - all licence data
* @param {String} conditionId - the condition ID
* @return {Object} condition
*/
const getCondition = (data, conditionId) => {
return find(getConditions(data), { ID: conditionId });
};
const getCondition = (data, conditionId) => find(getConditions(data), { ID: conditionId });

/**
* Get the current licence version
Expand All @@ -91,30 +77,24 @@ const getCurrentVersion = data => {
* @param {String} incrementNumber - the increment number
* @return {Object}
*/
const getVersion = (data, issueNumber, incrementNumber) => {
return find(data.data.versions, (version) => {
return (version.ISSUE_NO === issueNumber) && (version.INCR_NO === incrementNumber);
});
};
const getVersion = (data, issueNumber, incrementNumber) =>
find(data.data.versions, (version) =>
(version.ISSUE_NO === issueNumber) && (version.INCR_NO === incrementNumber));

/**
* Get the licence holder party for the current licence version
* @param {Object} data - all licence data
* @return {Object} current version
*/
const getCurrentVersionParty = data => {
return data.data.current_version.party;
};
const getCurrentVersionParty = data => data.data.current_version.party;

/**
* Gets flat list of parties from supplied licence data
* There may be duplication
* @param {Object} data - licence data
* @return {Array} parties list
*/
const getParties = data => {
return flatMap(data.data.versions, versionPartiesMapper);
};
const getParties = data => flatMap(data.data.versions, versionPartiesMapper);

/**
* Gets party by ID from licence data
Expand Down Expand Up @@ -154,9 +134,7 @@ const getAddress = (data, addressId) => {
* @param {Object} data - all licence data
* @return {Object} current version
*/
const getCurrentVersionAddress = data => {
return data.data.current_version.address;
};
const getCurrentVersionAddress = data => data.data.current_version.address;

module.exports = {
getPurposes,
Expand Down
13 changes: 5 additions & 8 deletions src/digitise/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ const getBaseQuery = (action, status = STATUS_IN_PROGRESS) => {
const editPurpose = (state, action) => {
const { purposeId, data } = action.payload;

const index = findIndex(state.licence.data.current_version.purposes, (row) => {
return parseInt(row.ID) === parseInt(purposeId);
});
const index = findIndex(state.licence.data.current_version.purposes, (row) =>
parseInt(row.ID) === parseInt(purposeId));

if (index === -1) {
return state;
Expand Down Expand Up @@ -88,8 +87,7 @@ const editPoint = (state, action) => {
licence: {
data: {
current_version: {
purposes: {
}
purposes: {}
}
}
}
Expand Down Expand Up @@ -122,8 +120,7 @@ const editCondition = (state, action) => {
licence: {
data: {
current_version: {
purposes: {
}
purposes: {}
}
}
}
Expand Down Expand Up @@ -282,7 +279,7 @@ const reducer = (state, action) => {

if (commands[action.type]) {
return commands[action.type](state, action);
};
}

return state;
};
Expand Down
29 changes: 13 additions & 16 deletions src/digitise/schema-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const createCategory = schema => {
const { category } = schema;
const slug = slugify(category, { lower: true });
return {
title: category,
slug,
title: category,
subcategories: []
};
};
Expand Down Expand Up @@ -72,26 +72,23 @@ const findSubcategory = (arr, schema) => {
* @param {Array} schema - list of custom schemas
* @return {Object} - indexed by category, subcategory
*/
const getSchemaCategories = schema => {
return schema.reduce((acc, schema) => {
const category = findCategory(acc, schema);
const subcategory = findSubcategory(category.subcategories, schema);
subcategory.schemas.push(schema.id);
return acc;
}, []);
};
const getSchemaCategories = schema => schema.reduce((acc, schema) => {
const category = findCategory(acc, schema);
const subcategory = findSubcategory(category.subcategories, schema);
subcategory.schemas.push(schema.id);
return acc;
}, []);

/**
* Given a schema, finds the category which contains it
* @param {Object} schema - WR22 JSON schema
* @return {[type]} [description]
* @param categories
* @param id
*/
const getSchemaCategory = (categories, id) => {
return find(categories, category => {
const ids = flatMap(category.subcategories, subcat => subcat.schemas);
return ids.includes(id);
});
};
const getSchemaCategory = (categories, id) => find(categories, category => {
const ids = flatMap(category.subcategories, subcat => subcat.schemas);
return ids.includes(id);
});

module.exports = {
getSchemaCategories,
Expand Down

0 comments on commit f05818c

Please sign in to comment.