Last active
April 17, 2017 20:53
-
-
Save adrianmcli/f572e28ed89694c3c099551a0a3fe7dd to your computer and use it in GitHub Desktop.
When the data that the client wants to consume is different from the database model, transform it on the backend before sending it down.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (show, venueType) => { | |
const tiers = show.tierPrices.map((price, index) => ({ | |
title: venueType[`tier${index + 1}Title`], | |
description: venueType[`tier${index + 1}Description`], | |
price, | |
})); | |
return { | |
name: show.name, | |
venueType: venueType.name, | |
tiers, | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global describe, expect */ | |
import transformData from './tierTransform'; | |
const dbModel = { | |
venueType: { | |
name: 'Stadium', | |
tier1Title: 'Upper Balcony', | |
tier1Description: '...', | |
tier2Title: 'Balcony', | |
tier2Description: '...', | |
tier3Title: 'Floor', | |
tier3Description: '...', | |
}, | |
show: { | |
name: 'Awesome Show', | |
tierPrices: [5000, 7500, 10000], | |
}, | |
}; | |
describe('from db model to client consumption', () => { | |
it('should transform data correctly', () => { | |
const showData = { | |
name: 'Awesome Show', | |
venueType: 'Stadium', | |
tiers: [ | |
{ | |
title: 'Upper Balcony', | |
description: '...', | |
price: 5000, | |
}, | |
{ | |
title: 'Balcony', | |
description: '...', | |
price: 7500, | |
}, | |
{ | |
title: 'Floor', | |
description: '...', | |
price: 10000, | |
}, | |
], | |
}; | |
expect(transformData(dbModel.show, dbModel.venueType)).toEqual(showData); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment