Guest User

Untitled

a guest
Mar 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. // Input
  2. const data = [
  3. {
  4. calcFieldValue: 3,
  5. id: '50346a2e-8421-e811-a2bc-000c29878c3f',
  6. idFieldValue: null,
  7. primaryCurrencyCalcFieldValue: 4,
  8. productFolder: 'Personal',
  9. productTypeName: 'DDAs',
  10. fields: [],
  11. relationshipTypes: [{
  12. id: '5901e243-db0c-e811-a2ba-000c29878c3f',
  13. name: 'Primary'
  14. }]
  15. },
  16. {
  17. calcFieldValue: 3,
  18. id: '50346a2e-8421-e811-a2bc-000c29878c3f',
  19. idFieldValue: null,
  20. primaryCurrencyCalcFieldValue: 4,
  21. productFolder: 'Personal',
  22. productTypeName: 'DDAs',
  23. fields: [],
  24. relationshipTypes: [{
  25. id: '5901e243-db0c-e811-a2ba-000c29878c3f',
  26. name: 'Primary'
  27. }]
  28. },
  29. {
  30. calcFieldValue: 3,
  31. id: '50346a2f-8421-e811-a2bc-000c29878c3f',
  32. idFieldValue: null,
  33. primaryCurrencyCalcFieldValue: 4,
  34. productFolder: 'Personal',
  35. productTypeName: 'DDAs',
  36. fields: [],
  37. relationshipTypes: [{
  38. id: '5901e242-db0c-e811-a2ba-000c29878c3f',
  39. name: 'Primaries'
  40. }]
  41. }
  42. ]
  43.  
  44. /*
  45. TARGET
  46. [ {
  47. id: '5901e243-db0c-e811-a2ba-000c29878c3f', // relationshipType.id
  48. name: 'Primary', // relationshipType.name
  49. count: 1, // number of products with this relationshipType
  50. groups: [{
  51. name: 'Personal', // productFolder
  52. total: 4, // sum of all product primaryCurrencyCalcFieldValues in this group
  53. products: [{
  54. name: 'DDAs', // product type name
  55. id: '50346a2e-8421-e811-a2bc-000c29878c3f', // product id
  56. fields: [] // whatever dynamic fields the API gave us
  57. }]
  58. }],
  59. }]
  60. */
  61.  
  62. // Given our `data` array, return a productHash
  63. // EXAMPLE:
  64. // [{ id: 1 }] => { 1: { id: 1 } }
  65. const createProductHashFromData = reduce((acc, curr) => ({
  66. ...acc,
  67. [curr.id]: curr
  68. }), {})
  69.  
  70. const createRelationshipHash = reduce((acc, curr) => {
  71. const { relationshipTypes, id } = curr
  72. const ids = relationshipTypes.map(pick(['id','name']))
  73. const newVal = { ...acc }
  74. ids.forEach(({ id: relId, name }) => {
  75. if (has(relId, acc)) {
  76. newVal[relId] = {
  77. ...newVal[relId],
  78. list: newVal[relId].list.concat(id)
  79. }
  80. } else {
  81. newVal[relId] = {
  82. name,
  83. id,
  84. list: [id]
  85. }
  86. }
  87. })
  88.  
  89. return newVal
  90. }, {})
  91.  
  92.  
  93. const productHash = createProductHashFromData(data)
  94. const relationshipHash = createRelationshipHash(data);
  95.  
  96. // Grabs the id, fields, and productTypeName
  97. // and changes productTypeName key to name
  98. const getProudctForGroup = ({ productTypeName: name, id, fields }) => ({
  99. id,
  100. fields,
  101. name,
  102. })
  103.  
  104. // Transform a list of Product ( main object in input array )
  105. // into the groups needed per spec
  106. const createGroups = reduce((acc, curr) => {
  107. // we are calling the productFolder key the groupId
  108. // because we like it
  109. const groupId = curr.productFolder
  110. // If at some point before now we have seen this
  111. // specific group id
  112. if (has(groupId, acc)) {
  113. // We know that we already have some "state" to update
  114. return ({
  115. // copy everything previously
  116. ...acc,
  117. // and at the key equal to groupId
  118. [groupId]: {
  119. // copy over the previous values
  120. ...acc[groupId],
  121. // update it's count by adding the correct key
  122. count: acc[groupId].count + curr.primaryCurrencyCalcFieldValue,
  123. // and we want to create a new array and add a special form of
  124. // Product, made for our Group
  125. products: acc[groupId].products.concat(getProudctForGroup(curr))
  126. }
  127. })
  128. }
  129. // If we are here, it means that we have never
  130. // seen this specific groupId
  131. return ({
  132. // so we just copy over all the previous "state"
  133. ...acc,
  134. // and then set the groupId key
  135. [groupId]: {
  136. // to the general structure of our Product
  137. count: curr.primaryCurrencyCalcFieldValue,
  138. products: [getProudctForGroup(curr)],
  139. name: groupId
  140. }
  141. })
  142. }, {})
  143.  
  144. // Creates the final shape of the `groups` key
  145. // given a previously transformed `relationship`
  146. const getGroupHash = compose(
  147. values,
  148. createGroups
  149. )
  150.  
  151. // This is where we transform the data into our final shape
  152. keys(relationshipHash)
  153. .map(relId => {
  154. // we take the relationship from the relationshipHash
  155. const relationship = prop(relId, relationshipHash)
  156. // and we create our final relationship shape
  157. return ({
  158. id: relId,
  159. name: relationship.name,
  160. count: relationship.list.length,
  161. // We need to transform `groups` after this but
  162. // we can make that an easier problem by grabbing
  163. // the value from productHash that is at the key
  164. // at each index
  165. //
  166. // EXAMPLE:
  167. // productHash = { 'a': { id: 'a' } }
  168. // ['a'] => [({ id: 'a' })]
  169. groups: relationship.list.map(flip(prop)(productHash))
  170. })
  171. })
  172. .map(({ groups, ...rest }) => ({
  173. // Once we get everything right
  174. ...rest,
  175. // we can transform groups for final consumer
  176. groups: getGroupHash(groups)
  177. }))
Add Comment
Please, Sign In to add comment