| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | const { isFunction, isNil, prop } = require('lodash/fp'); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | const { createStrapiInstance } = require('./strapi'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  | const toUID = name => { | 
					
						
							| 
									
										
										
										
											2021-08-11 10:05:20 +02:00
										 |  |  |   return name.includes('::') ? name : `api::${name}.${name}`; | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const createHelpers = async ({ strapi: strapiInstance = null, ...options } = {}) => { | 
					
						
							|  |  |  |   const strapi = strapiInstance || (await createStrapiInstance(options)); | 
					
						
							| 
									
										
										
										
											2021-08-19 22:27:00 +02:00
										 |  |  |   const contentTypeService = strapi.plugin('content-type-builder').service('content-types'); | 
					
						
							|  |  |  |   const componentsService = strapi.plugin('content-type-builder').service('components'); | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   const cleanup = async () => { | 
					
						
							|  |  |  |     if (isNil(strapiInstance)) { | 
					
						
							|  |  |  |       await strapi.destroy(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |   return { | 
					
						
							|  |  |  |     strapi, | 
					
						
							|  |  |  |     contentTypeService, | 
					
						
							|  |  |  |     componentsService, | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     cleanup, | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-08-08 11:59:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const createContentType = async (model, { strapi } = {}) => { | 
					
						
							|  |  |  |   const { contentTypeService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2019-08-08 11:59:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const contentType = await contentTypeService.createContentType({ | 
					
						
							|  |  |  |     contentType: { | 
					
						
							|  |  |  |       ...model, | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-08 10:41:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return contentType; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const createContentTypes = async (models, { strapi } = {}) => { | 
					
						
							|  |  |  |   const { contentTypeService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |   const contentTypes = await contentTypeService.createContentTypes( | 
					
						
							|  |  |  |     models.map(model => ({ | 
					
						
							|  |  |  |       contentType: { | 
					
						
							|  |  |  |         ...model, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     })) | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return contentTypes; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const createComponent = async (component, { strapi } = {}) => { | 
					
						
							|  |  |  |   const { componentsService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const createdComponent = await componentsService.createComponent({ | 
					
						
							|  |  |  |     component: { | 
					
						
							|  |  |  |       category: 'default', | 
					
						
							|  |  |  |       icon: 'default', | 
					
						
							|  |  |  |       ...component, | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return createdComponent; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const createComponents = async (components, { strapi } = {}) => { | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const createdComponents = []; | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   for (const component of components) { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     createdComponents.push(await createComponent(component, { strapi })); | 
					
						
							| 
									
										
										
										
											2020-09-07 15:04:20 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return createdComponents; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const deleteComponent = async (componentUID, { strapi } = {}) => { | 
					
						
							|  |  |  |   const { componentsService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const component = await componentsService.deleteComponent(componentUID); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return component; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const deleteComponents = async (componentsUID, { strapi } = {}) => { | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const deletedComponents = []; | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   for (const componentUID of componentsUID) { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     deletedComponents.push(await deleteComponent(componentUID, { strapi })); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   return deletedComponents; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  | const deleteContentType = async (uid, { strapi } = {}) => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   const { contentTypeService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const contentType = await contentTypeService.deleteContentType(uid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return contentType; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  | const deleteContentTypes = async (modelsUIDs, { strapi } = {}) => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   const { contentTypeService, cleanup } = await createHelpers({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  |   const contentTypes = await contentTypeService.deleteContentTypes(modelsUIDs); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return contentTypes; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | async function cleanupModels(models, { strapi } = {}) { | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   for (const model of models) { | 
					
						
							| 
									
										
										
										
											2021-07-05 18:35:16 +02:00
										 |  |  |     await cleanupModel(model, { strapi }); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  | async function cleanupModel(uid, { strapi: strapiIst } = {}) { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   const { strapi, cleanup } = await createHelpers({ strapi: strapiIst }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 14:18:03 +02:00
										 |  |  |   await strapi.query(uid).deleteMany(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | async function createFixtures(dataMap, { strapi: strapiIst } = {}) { | 
					
						
							|  |  |  |   const { strapi, cleanup } = await createHelpers({ strapi: strapiIst }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const models = Object.keys(dataMap); | 
					
						
							|  |  |  |   const resultMap = {}; | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   for (const model of models) { | 
					
						
							|  |  |  |     const entries = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const data of dataMap[model]) { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |       entries.push(await strapi.entityService.create(toUID(model), { data })); | 
					
						
							| 
									
										
										
										
											2020-09-16 10:08:18 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resultMap[model] = entries; | 
					
						
							| 
									
										
										
										
											2020-09-16 10:08:18 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return resultMap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | async function createFixturesFor(model, entries, { strapi: strapiIst } = {}) { | 
					
						
							|  |  |  |   const { strapi, cleanup } = await createHelpers({ strapi: strapiIst }); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   const results = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const entry of entries) { | 
					
						
							| 
									
										
										
										
											2020-11-13 16:48:17 +01:00
										 |  |  |     const dataToCreate = isFunction(entry) ? entry(results) : entry; | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |     results.push(await strapi.entityService.create(toUID(model), { data: dataToCreate })); | 
					
						
							| 
									
										
										
										
											2020-09-16 10:08:18 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   await cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return results; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | async function deleteFixturesFor(model, entries, { strapi: strapiIst } = {}) { | 
					
						
							|  |  |  |   const { strapi, cleanup } = await createHelpers({ strapi: strapiIst }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |   await strapi.query(toUID(model)).deleteMany({ where: { id: entries.map(prop('id')) } }); | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   await cleanup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | async function modifyContentType(data, { strapi } = {}) { | 
					
						
							|  |  |  |   const { contentTypeService, cleanup } = await createHelpers({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const sanitizedData = { ...data }; | 
					
						
							|  |  |  |   delete sanitizedData.editable; | 
					
						
							|  |  |  |   delete sanitizedData.restrictRelationsTo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |   const uid = toUID(sanitizedData.name); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const ct = await contentTypeService.editContentType(uid, { | 
					
						
							|  |  |  |     contentType: { | 
					
						
							|  |  |  |       ...sanitizedData, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await cleanup(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ct; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getContentTypeSchema(modelName, { strapi: strapiIst } = {}) { | 
					
						
							|  |  |  |   const { strapi, contentTypeService, cleanup } = await createHelpers({ strapi: strapiIst }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |   const uid = toUID(modelName); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   const ct = contentTypeService.formatContentType(strapi.contentTypes[uid]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await cleanup(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (ct || {}).schema; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |   toUID, | 
					
						
							| 
									
										
										
										
											2020-11-10 14:15:31 +01:00
										 |  |  |   // Create Content-Types
 | 
					
						
							|  |  |  |   createContentType, | 
					
						
							|  |  |  |   createContentTypes, | 
					
						
							|  |  |  |   // Delete Content-Types
 | 
					
						
							|  |  |  |   deleteContentType, | 
					
						
							|  |  |  |   deleteContentTypes, | 
					
						
							|  |  |  |   // Cleanup Models
 | 
					
						
							|  |  |  |   cleanupModel, | 
					
						
							|  |  |  |   cleanupModels, | 
					
						
							|  |  |  |   // Create Components
 | 
					
						
							|  |  |  |   createComponent, | 
					
						
							|  |  |  |   createComponents, | 
					
						
							|  |  |  |   // Delete Components
 | 
					
						
							|  |  |  |   deleteComponent, | 
					
						
							|  |  |  |   deleteComponents, | 
					
						
							|  |  |  |   // Fixtures
 | 
					
						
							|  |  |  |   createFixtures, | 
					
						
							|  |  |  |   createFixturesFor, | 
					
						
							| 
									
										
										
										
											2021-01-04 11:32:43 +01:00
										 |  |  |   deleteFixturesFor, | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   // Update Content-Types
 | 
					
						
							|  |  |  |   modifyContentType, | 
					
						
							|  |  |  |   // Misc
 | 
					
						
							|  |  |  |   getContentTypeSchema, | 
					
						
							| 
									
										
										
										
											2019-05-06 15:33:25 +02:00
										 |  |  | }; |