diff --git a/src/client.model.js b/src/client.model.js index aa5680b..34dcc6f 100644 --- a/src/client.model.js +++ b/src/client.model.js @@ -11,7 +11,8 @@ const { isJson, stringify, sanitizeParameters, - parseScriptResult + parseScriptResult, + setData } = require('./utilities'); /** @@ -382,9 +383,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - { - fieldData: this.data.incoming(stringify(data)) - } + this.data.incoming(setData(data)) ) }) ) @@ -436,9 +435,7 @@ class Client extends Document { 'script.presort.param', 'request' ]), - { - fieldData: this.data.incoming(stringify(data)) - } + this.data.incoming(setData(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..e98aad5 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 setData + * @public + * @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. + * @return {Object} A modified object containing with the fieldData property + */ + +const setData = data => + Object.assign( + {}, + { + fieldData: !_.has(data, 'fieldData') + ? stringify(_.omit(data, 'portalData')) + : stringify(data.fieldData) + }, + _.has(data, 'portalData') + ? { + portalData: _.mapValues(data.portalData, data => + _.map(data, object => stringify(object)) + ) + } + : {} + ); + module.exports = { fieldData, recordId, namespace, parseScriptResult, - sanitizeParameters + sanitizeParameters, + setData }; diff --git a/src/utilities/index.js b/src/utilities/index.js index 5b987b7..689030b 100644 --- a/src/utilities/index.js +++ b/src/utilities/index.js @@ -5,7 +5,8 @@ const { recordId, namespace, parseScriptResult, - sanitizeParameters + sanitizeParameters, + setData } = require('./filemaker.utilities'); const { omit, stringify, toArray, isJson } = require('./conversion.utilities'); @@ -17,6 +18,7 @@ module.exports = { omit, recordId, stringify, + setData, 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..9e0c288 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' } }, + { 'Vehicles::name': 5 } + ] + } + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('modId') + ); + }); + it('should reject bad data with an error', () => expect( client