| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Retrieve the path of each API | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  |  * @param {Object}} data | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |  * @returns {Array} Array of API path ['plugins.upload.file', 'plugins.users-permissions.user', ...] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const getApis = (data) => Object.keys(data).reduce((acc, curr) => { | 
					
						
							|  |  |  |   if (data[curr].fields) { | 
					
						
							|  |  |  |     return acc.concat([curr]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (curr === 'plugins') { | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  |     Object.keys(data[curr]).forEach(plugin => { | 
					
						
							|  |  |  |       Object.keys(data[curr][plugin]).forEach(api => { | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |         acc = acc.concat([`${curr}.${plugin}.${api}`]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return acc; | 
					
						
							|  |  |  | }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Retrieve all the fields from an api | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  |  * @param {Object} data | 
					
						
							|  |  |  |  * @param {Array} apis | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |  * @returns {Array} Array composed of fields path for instance : [['plugins.users-permissions.user.fields.username', 'plugins.users-permissions.user.fields.email', 'plugins.users-permissions.user.fields.password'], [...]] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const getApisKeys = (data, apis) => apis.map(apiPath => { | 
					
						
							|  |  |  |   const fields = Object.keys(_.get(data.models, apiPath.concat(['fields']))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return fields.map(field => `${apiPath.join('.')}.fields.${field}`); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Same as above but only for the relations since it's custom | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const getApisUploadRelations = (data, sameArray) => sameArray.map(apiPath => { | 
					
						
							|  |  |  |   const relationPath = [...apiPath, 'relations']; | 
					
						
							|  |  |  |   const relationsObject = _.get(data.models, relationPath, {}); | 
					
						
							|  |  |  |   const relations = Object.keys(relationsObject) | 
					
						
							|  |  |  |     .filter(relationName => { | 
					
						
							|  |  |  |       return _.get(data.models, [...relationPath, relationName, 'plugin' ]) === 'upload'; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |   return relations.map(relation => `${apiPath.join('.')}.editDisplay.availableFields.${relation}`); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-11-15 15:41:08 -06:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |  * @param {String} attrPath | 
					
						
							|  |  |  |  * @returns {Array} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const getEditDisplayAvailableFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'availableFields', attrPath[attrPath.length - 1]]; | 
					
						
							| 
									
										
										
										
											2019-02-04 17:03:51 +01:00
										 |  |  | const getEditDisplayDisplayedField = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'displayedField']; | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  | const getEditDisplayFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'fields']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   getApis, | 
					
						
							|  |  |  |   getApisKeys, | 
					
						
							|  |  |  |   getApisUploadRelations, | 
					
						
							|  |  |  |   getEditDisplayAvailableFieldsPath, | 
					
						
							| 
									
										
										
										
											2019-02-04 17:03:51 +01:00
										 |  |  |   getEditDisplayDisplayedField, | 
					
						
							| 
									
										
										
										
											2018-08-21 17:43:17 +02:00
										 |  |  |   getEditDisplayFieldsPath | 
					
						
							|  |  |  | }; |