Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/client.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
isJson,
stringify,
sanitizeParameters,
parseScriptResult
parseScriptResult,
setData
} = require('./utilities');

/**
Expand Down Expand Up @@ -382,9 +383,7 @@ class Client extends Document {
'script.presort.param',
'request'
]),
{
fieldData: this.data.incoming(stringify(data))
}
this.data.incoming(setData(data))
)
})
)
Expand Down Expand Up @@ -436,9 +435,7 @@ class Client extends Document {
'script.presort.param',
'request'
]),
{
fieldData: this.data.incoming(stringify(data))
}
this.data.incoming(setData(data))
)
})
)
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/conversion.utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
30 changes: 29 additions & 1 deletion src/utilities/filemaker.utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const {
recordId,
namespace,
parseScriptResult,
sanitizeParameters
sanitizeParameters,
setData
} = require('./filemaker.utilities');

const { omit, stringify, toArray, isJson } = require('./conversion.utilities');
Expand All @@ -17,6 +18,7 @@ module.exports = {
omit,
recordId,
stringify,
setData,
toArray,
isJson,
namespace,
Expand Down
37 changes: 36 additions & 1 deletion tests/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 44 additions & 1 deletion tests/edit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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
Expand Down