From 08f92ff61ecdd781c04e0485fd87c5b2064b0258 Mon Sep 17 00:00:00 2001 From: Lui de la Parra Date: Sun, 14 Oct 2018 20:32:46 -0700 Subject: [PATCH 1/3] Adds Utility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a setFieldData Utility function. This function will allow portalData to be set on the client’s create and edit methods. the setFieldData will stringify objects and numbers in the portalData’s properties. Signed-off-by: Lui de la Parra --- src/client.model.js | 11 +++---- src/utilities/conversion.utilities.js | 2 +- src/utilities/filemaker.utilities.js | 30 +++++++++++++++++- src/utilities/index.js | 4 ++- tests/create.test.js | 37 +++++++++++++++++++++- tests/edit.test.js | 45 ++++++++++++++++++++++++++- 6 files changed, 117 insertions(+), 12 deletions(-) diff --git a/src/client.model.js b/src/client.model.js index aa5680b..90dd9e4 100644 --- a/src/client.model.js +++ b/src/client.model.js @@ -11,7 +11,8 @@ const { isJson, stringify, sanitizeParameters, - parseScriptResult + parseScriptResult, + setFieldData } = require('./utilities'); /** @@ -382,9 +383,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - { - fieldData: this.data.incoming(stringify(data)) - } + this.data.incoming(setFieldData(data)) ) }) ) @@ -436,9 +435,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - { - fieldData: this.data.incoming(stringify(data)) - } + this.data.incoming(setFieldData(data)) ) }) ) diff --git a/src/utilities/conversion.utilities.js b/src/utilities/conversion.utilities.js index 607c525..14c03d4 100644 --- a/src/utilities/conversion.utilities.js +++ b/src/utilities/conversion.utilities.js @@ -41,7 +41,7 @@ const toArray = data => (Array.isArray(data) ? data : [data]); * @method isJson * @public * @description The isJson method uses the a try / catch to parse incoming data safely as json. - * This method will return tru if it is able to cast the incoming data as json. + * This method will return true if it is able to cast the incoming data as json. * @param {Any} data The data to be evaluated as json. * @return {Boolean} A boolean result depending on if the data passed to it is valid JSON */ diff --git a/src/utilities/filemaker.utilities.js b/src/utilities/filemaker.utilities.js index 65ed9ab..76c9f46 100644 --- a/src/utilities/filemaker.utilities.js +++ b/src/utilities/filemaker.utilities.js @@ -188,10 +188,38 @@ const namespace = data => _.includes(['limit', 'offset', 'sort'], key) ? `_${key}` : key ); +/** + * @method setFieldData + * @public + * @description The setFieldData method checks the incoming data for a fieldData property. + * the fieldData property is not found it will create the property and add all properties + * except portalData to the fieldData property. + * @param {Object} data An object to use when creating or editing records. + * @return {Object} A modified object containing with the fieldData property + */ + +const setFieldData = data => + Object.assign( + {}, + { + fieldData: !_.has(data, 'fieldData') + ? stringify(_.omit(data, 'portalData')) + : stringify(data.fieldData) + }, + _.has(data, 'portalData') + ? { + portalData: _.mapValues(data.portalData, datum => + _.map(datum, object => stringify(object)) + ) + } + : {} + ); + module.exports = { fieldData, recordId, namespace, parseScriptResult, - sanitizeParameters + sanitizeParameters, + setFieldData }; diff --git a/src/utilities/index.js b/src/utilities/index.js index 5b987b7..b2e9f16 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -5,7 +5,8 @@ const { recordId, namespace, parseScriptResult, - sanitizeParameters + sanitizeParameters, + setFieldData } = require('./filemaker.utilities'); const { omit, stringify, toArray, isJson } = require('./conversion.utilities'); @@ -17,6 +18,7 @@ module.exports = { omit, recordId, stringify, + setFieldData, toArray, isJson, namespace, diff --git a/tests/create.test.js b/tests/create.test.js index 25fb08f..6e96767 100644 --- a/tests/create.test.js +++ b/tests/create.test.js @@ -50,12 +50,47 @@ describe('Create Capabilities', () => { .catch(error => done()); }); - it('should create FileMaker records.', () => { + it('should create FileMaker records without fieldData', () => { return expect(client.create(process.env.LAYOUT, { name: 'Han Solo' })) .to.eventually.be.a('object') .that.has.all.keys('recordId', 'modId'); }); + it('should create FileMaker records using fieldData', () => { + return expect( + client.create(process.env.LAYOUT, { fieldData: { name: 'Han Solo' } }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('recordId', 'modId'); + }); + + it('should create FileMaker records with portalData', () => { + return expect( + client.create(process.env.LAYOUT, { + fieldData: { name: 'Han Solo' }, + portalData: { Vehicles: [{ 'Vehicles::name': 'Millenium Falcon' }] } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('recordId', 'modId'); + }); + + it('should allow portalData to be an object or number', () => { + return expect( + client.create(process.env.LAYOUT, { + fieldData: { name: 'Han Solo' }, + portalData: { + Vehicles: [ + { 'Vehicles::name': { name: 'Millenium Falcon -test' } }, + { 'Vehicles::name': 5 } + ] + } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('recordId', 'modId'); + }); + it('should reject bad data with an error', () => { return expect( client.create(process.env.LAYOUT, 'junk data').catch(error => error) diff --git a/tests/edit.test.js b/tests/edit.test.js index 3cf7236..9ca9565 100644 --- a/tests/edit.test.js +++ b/tests/edit.test.js @@ -48,7 +48,7 @@ describe('Edit Capabilities', () => { .catch(error => done()); }); - it('should edit FileMaker records.', () => { + it('should edit FileMaker records without fieldData', () => { client.create(process.env.LAYOUT, { name: 'Obi-Wan' }).then(response => expect( client.edit(process.env.LAYOUT, response.recordId, { @@ -60,6 +60,49 @@ describe('Edit Capabilities', () => { ); }); + it('should edit FileMaker records using fieldData', () => { + client.create(process.env.LAYOUT, { name: 'Obi-Wan' }).then(response => + expect( + client.edit(process.env.LAYOUT, response.recordId, { + fieldData: { name: 'Luke Skywalker' } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('modId') + ); + }); + + it('should edit FileMaker records with portalData', () => { + client.create(process.env.LAYOUT, { name: 'Obi-Wan' }).then(response => + expect( + client.edit(process.env.LAYOUT, response.recordId, { + fieldData: { name: 'Han Solo' }, + portalData: { Vehicles: [{ 'Vehicles::name': 'Millenium Falcon' }] } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('modId') + ); + }); + + it('should edit FileMaker records with portalData and allow portalData to be an array.', () => { + client.create(process.env.LAYOUT, { name: 'Obi-Wan' }).then(response => + expect( + client.edit(process.env.LAYOUT, response.recordId, { + fieldData: { name: 'Han Solo' }, + portalData: { + Vehicles: [ + { 'Vehicles::name': { name: 'Millenium Falcon -test' } }, + { 'Vehicles::name': 5 } + ] + } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('modId') + ); + }); + it('should reject bad data with an error', () => expect( client From 145b7556e5208a4002b65cb62d546b5838df943b Mon Sep 17 00:00:00 2001 From: Lui de la Parra Date: Sun, 14 Oct 2018 20:42:33 -0700 Subject: [PATCH 2/3] Revises Utility Function Name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit revises the setFieldData utility function’s name to setData. Signed-off-by: Lui de la Parra --- src/client.model.js | 6 +++--- src/utilities/filemaker.utilities.js | 10 +++++----- src/utilities/index.js | 4 ++-- tests/edit.test.js | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/client.model.js b/src/client.model.js index 90dd9e4..34dcc6f 100644 --- a/src/client.model.js +++ b/src/client.model.js @@ -12,7 +12,7 @@ const { stringify, sanitizeParameters, parseScriptResult, - setFieldData + setData } = require('./utilities'); /** @@ -383,7 +383,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - this.data.incoming(setFieldData(data)) + this.data.incoming(setData(data)) ) }) ) @@ -435,7 +435,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - this.data.incoming(setFieldData(data)) + this.data.incoming(setData(data)) ) }) ) diff --git a/src/utilities/filemaker.utilities.js b/src/utilities/filemaker.utilities.js index 76c9f46..9d146b2 100644 --- a/src/utilities/filemaker.utilities.js +++ b/src/utilities/filemaker.utilities.js @@ -189,9 +189,9 @@ const namespace = data => ); /** - * @method setFieldData + * @method setData * @public - * @description The setFieldData method checks the incoming data for a fieldData property. + * @description The setData method checks the incoming data for a fieldData property. * the fieldData property is not found it will create the property and add all properties * except portalData to the fieldData property. * @param {Object} data An object to use when creating or editing records. @@ -208,8 +208,8 @@ const setFieldData = data => }, _.has(data, 'portalData') ? { - portalData: _.mapValues(data.portalData, datum => - _.map(datum, object => stringify(object)) + portalData: _.mapValues(data.portalData, data => + _.map(data, object => stringify(object)) ) } : {} @@ -221,5 +221,5 @@ module.exports = { namespace, parseScriptResult, sanitizeParameters, - setFieldData + setData }; diff --git a/src/utilities/index.js b/src/utilities/index.js index b2e9f16..689030b 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -6,7 +6,7 @@ const { namespace, parseScriptResult, sanitizeParameters, - setFieldData + setData } = require('./filemaker.utilities'); const { omit, stringify, toArray, isJson } = require('./conversion.utilities'); @@ -18,7 +18,7 @@ module.exports = { omit, recordId, stringify, - setFieldData, + setData, toArray, isJson, namespace, diff --git a/tests/edit.test.js b/tests/edit.test.js index 9ca9565..9e0c288 100644 --- a/tests/edit.test.js +++ b/tests/edit.test.js @@ -92,7 +92,7 @@ describe('Edit Capabilities', () => { fieldData: { name: 'Han Solo' }, portalData: { Vehicles: [ - { 'Vehicles::name': { name: 'Millenium Falcon -test' } }, + { 'Vehicles::name': { name: 'Millenium Falcon' } }, { 'Vehicles::name': 5 } ] } From 473c1efdb507ac3b500a944164860083f0fb1315 Mon Sep 17 00:00:00 2001 From: Lui de la Parra Date: Sun, 14 Oct 2018 20:45:03 -0700 Subject: [PATCH 3/3] Actually Rename Function This commit actually renames the function. Signed-off-by: Lui de la Parra --- src/utilities/filemaker.utilities.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/filemaker.utilities.js b/src/utilities/filemaker.utilities.js index 9d146b2..e98aad5 100644 --- a/src/utilities/filemaker.utilities.js +++ b/src/utilities/filemaker.utilities.js @@ -198,7 +198,7 @@ const namespace = data => * @return {Object} A modified object containing with the fieldData property */ -const setFieldData = data => +const setData = data => Object.assign( {}, {