2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-06 15:33:25 +02:00
|
|
|
const waitRestart = require('./waitRestart');
|
|
|
|
|
|
|
|
module.exports = ({ rq }) => {
|
2019-10-22 18:01:03 +02:00
|
|
|
async function createComponent(data) {
|
2019-08-08 11:59:45 +02:00
|
|
|
await rq({
|
2019-10-22 18:01:03 +02:00
|
|
|
url: '/content-type-builder/components',
|
2019-08-08 11:59:45 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2019-11-14 16:37:57 +01:00
|
|
|
component: {
|
|
|
|
category: 'default',
|
|
|
|
icon: 'default',
|
|
|
|
connection: 'default',
|
|
|
|
...data,
|
|
|
|
},
|
2019-08-08 11:59:45 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitRestart();
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
async function deleteComponent(name) {
|
2019-08-08 11:59:45 +02:00
|
|
|
await rq({
|
2019-10-22 18:01:03 +02:00
|
|
|
url: `/content-type-builder/components/${name}`,
|
2019-08-08 11:59:45 +02:00
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitRestart();
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
function createContentTypeWithType(name, type, opts = {}) {
|
|
|
|
return createContentType({
|
2019-08-08 10:41:05 +02:00
|
|
|
connection: 'default',
|
|
|
|
name,
|
2019-11-14 16:37:57 +01:00
|
|
|
attributes: {
|
|
|
|
field: {
|
|
|
|
type,
|
|
|
|
...opts,
|
2019-08-08 10:41:05 +02:00
|
|
|
},
|
2019-11-14 16:37:57 +01:00
|
|
|
},
|
2019-08-08 10:41:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
async function createContentType(data) {
|
2019-05-06 15:33:25 +02:00
|
|
|
await rq({
|
2019-11-14 16:37:57 +01:00
|
|
|
url: '/content-type-builder/content-types',
|
2019-05-06 15:33:25 +02:00
|
|
|
method: 'POST',
|
2019-08-08 11:59:45 +02:00
|
|
|
body: {
|
2019-11-14 16:37:57 +01:00
|
|
|
contentType: {
|
|
|
|
connection: 'default',
|
|
|
|
...data,
|
|
|
|
},
|
2019-08-08 11:59:45 +02:00
|
|
|
},
|
2019-05-06 15:33:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await waitRestart();
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
async function createContentTypes(models) {
|
2019-05-06 15:33:25 +02:00
|
|
|
for (let model of models) {
|
2019-12-12 10:15:25 +01:00
|
|
|
await createContentType(model);
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:04:20 +02:00
|
|
|
async function modifyContentType(data) {
|
2020-09-10 09:40:26 +02:00
|
|
|
const sanitizedData = { ...data };
|
|
|
|
delete sanitizedData.editable;
|
|
|
|
delete sanitizedData.restrictRelationsTo;
|
|
|
|
|
2020-09-07 15:04:20 +02:00
|
|
|
await rq({
|
2020-09-10 09:40:26 +02:00
|
|
|
url: `/content-type-builder/content-types/application::${sanitizedData.name}.${sanitizedData.name}`,
|
2020-09-07 15:04:20 +02:00
|
|
|
method: 'PUT',
|
|
|
|
body: {
|
|
|
|
contentType: {
|
|
|
|
connection: 'default',
|
2020-09-10 09:40:26 +02:00
|
|
|
...sanitizedData,
|
2020-09-07 15:04:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitRestart();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function modifyContentTypes(models) {
|
|
|
|
for (let model of models) {
|
|
|
|
await modifyContentType(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getContentTypeSchema(model) {
|
|
|
|
const { body } = await rq({
|
|
|
|
url: '/content-type-builder/content-types',
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
const contentType = body.data.find(ct => ct.uid === `application::${model}.${model}`);
|
|
|
|
|
|
|
|
return (contentType || {}).schema;
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
async function deleteContentType(model) {
|
2019-05-06 15:33:25 +02:00
|
|
|
await rq({
|
2019-11-14 16:37:57 +01:00
|
|
|
url: `/content-type-builder/content-types/application::${model}.${model}`,
|
2019-05-06 15:33:25 +02:00
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitRestart();
|
|
|
|
}
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
async function deleteContentTypes(models) {
|
2019-05-06 15:33:25 +02:00
|
|
|
for (let model of models) {
|
2019-12-12 10:15:25 +01:00
|
|
|
await deleteContentType(model);
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:08:18 +02:00
|
|
|
async function cleanupContentTypes(models) {
|
|
|
|
for (const model of models) {
|
|
|
|
await cleanupContentType(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanupContentType(model) {
|
|
|
|
const { body } = await rq({
|
2020-11-06 19:25:09 +01:00
|
|
|
url: `/content-manager/collection-types/application::${model}.${model}`,
|
|
|
|
qs: {
|
|
|
|
pageSize: 1000,
|
|
|
|
},
|
2020-09-16 10:08:18 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
2020-11-06 19:25:09 +01:00
|
|
|
if (Array.isArray(body.results) && body.results.length > 0) {
|
2020-09-16 10:08:18 +02:00
|
|
|
await rq({
|
2020-11-02 17:19:42 +01:00
|
|
|
url: `/content-manager/collection-types/application::${model}.${model}/actions/bulkDelete`,
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2020-11-06 19:25:09 +01:00
|
|
|
ids: body.results.map(({ id }) => id),
|
2020-11-02 17:19:42 +01:00
|
|
|
},
|
2020-09-16 10:08:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 15:33:25 +02:00
|
|
|
return {
|
2019-10-22 18:01:03 +02:00
|
|
|
createComponent,
|
|
|
|
deleteComponent,
|
2019-12-12 10:15:25 +01:00
|
|
|
createContentType,
|
|
|
|
createContentTypes,
|
|
|
|
createContentTypeWithType,
|
|
|
|
deleteContentType,
|
|
|
|
deleteContentTypes,
|
2020-09-07 15:04:20 +02:00
|
|
|
modifyContentType,
|
|
|
|
modifyContentTypes,
|
|
|
|
getContentTypeSchema,
|
2020-09-16 10:08:18 +02:00
|
|
|
cleanupContentType,
|
|
|
|
cleanupContentTypes,
|
2019-05-06 15:33:25 +02:00
|
|
|
};
|
|
|
|
};
|