Skip to content

Commit

Permalink
Merge branch 'release-4.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Nov 28, 2018
2 parents 736e858 + c61ffbc commit 04760d1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [4.4.1] (2018-11-28)

### Fixed

- [#516](https://github.com/dadi/api/issues/516): make media field handle legacy values

## [4.4.0] (2018-11-22)

### Added
Expand Down
14 changes: 9 additions & 5 deletions dadi/lib/fields/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ module.exports.beforeOutput = function ({
return mediaObjects
}, {})
}).then(mediaObjects => {
return normalisedValue.map(value => {
if (mediaObjects[value._id]) {
let mergedValue = Object.assign({}, mediaObjects[value._id], value)
return mediaObjectIDs.map((id, index) => {
let value = typeof normalisedValue[index] === 'object'
? normalisedValue[index]
: {}

if (mediaObjects[id]) {
let mergedValue = Object.assign({}, mediaObjects[id], value)
let sortedValue = Object.keys(mergedValue).sort().reduce((sortedValue, field) => {
sortedValue[field] = mergedValue[field]

return sortedValue
}, {})

composedIDs.push(value._id)
composedIDs.push(id.toString())

return sortedValue
}

return value._id
return id
})
}).then(composedValue => {
let output = Object.assign(input, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dadi/api",
"version": "4.4.0",
"version": "4.4.1",
"main": "main.js",
"scripts": {
"create-client": "cd ../../.. && node ./node_modules/@dadi/api/utils/create-client.js",
Expand Down
83 changes: 83 additions & 0 deletions test/acceptance/fields/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,89 @@ describe('Media field', () => {
})
})
})

it('should resolve a legacy value created by a Reference field', done => {
client
.post('/media/upload')
.set('content-type', 'application/json')
.set('Authorization', `Bearer ${bearerToken}`)
.attach('avatar', 'test/acceptance/temp-workspace/media/1f525.png')
.end((err, res) => {
let mediaObject = res.body.results[0]
let payload = {
title: 'Media support in DADI API',
legacyImage: mediaObject._id
}

client
.post('/vtest/testdb/test-schema')
.set('content-type', 'application/json')
.set('Authorization', `Bearer ${bearerToken}`)
.send(payload)
.end((err, res) => {
let {results} = res.body

results.should.be.instanceOf(Array)
results.length.should.eql(1)
results[0].title.should.eql(payload.title)
results[0].legacyImage._id.should.eql(mediaObject._id)
results[0].legacyImage.fileName.should.eql('1f525.png')
results[0]._composed.legacyImage.should.eql(mediaObject._id)

client
.get(`/vtest/testdb/test-schema/${results[0]._id}?compose=true`)
.set('content-type', 'application/json')
.set('Authorization', `Bearer ${bearerToken}`)
.end((err, res) => {
let {results} = res.body

results.should.be.instanceOf(Array)
results.length.should.eql(1)
results[0].title.should.eql(payload.title)
results[0].legacyImage._id.should.eql(mediaObject._id)
results[0].legacyImage.fileName.should.eql('1f525.png')
results[0]._composed.legacyImage.should.eql(mediaObject._id)

let collectionSchemaPath = path.join(
__dirname,
'/../temp-workspace/collections/vtest/testdb/collection.test-schema.json'
)
let collectionSchema = require(collectionSchemaPath)

// Convert the field to use the Media type.
collectionSchema.fields.legacyImage.type = 'Media'
delete collectionSchema.fields.legacyImage.settings

help.writeTempFile(
collectionSchemaPath,
JSON.stringify(collectionSchema, null, 2),
restoreCollection => {
setTimeout(() => {
client
.get(`/vtest/testdb/test-schema/${results[0]._id}?cache=false`)
.set('content-type', 'application/json')
.set('Authorization', `Bearer ${bearerToken}`)
.end((err, res) => {
let {results} = res.body

results.should.be.instanceOf(Array)
results.length.should.eql(1)
results[0].title.should.eql(payload.title)
results[0].legacyImage._id.should.eql(mediaObject._id)
results[0].legacyImage.fileName.should.eql('1f525.png')
results[0]._composed.legacyImage.should.eql(mediaObject._id)

restoreCollection()

done(err)
})
}, 1000)
}
)
})
})
})
})
})

describe('PUT', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"validation": {
"mimeTypes": ["image/jpeg"]
}
},
"legacyImage": {
"type": "Reference",
"settings": {
"collection": "mediaStore"
}
}
},
"settings": {
Expand Down

0 comments on commit 04760d1

Please sign in to comment.