54 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
const { hasDraftAndPublish } = require('@strapi/utils').contentTypes;
const enableDraftAndPublish = async ({ oldContentTypes, contentTypes }) => {
if (!oldContentTypes) {
return;
}
// run the after content types migrations
for (const uid in contentTypes) {
if (!oldContentTypes[uid]) {
continue;
}
const oldContentType = oldContentTypes[uid];
const contentType = contentTypes[uid];
2021-09-22 16:01:46 +02:00
// if d&p was enabled set publishedAt to eq createdAt
if (!hasDraftAndPublish(oldContentType) && hasDraftAndPublish(contentType)) {
const qb = strapi.db.queryBuilder(uid);
await qb
2021-09-29 17:31:01 +02:00
.update({ published_at: qb.ref('created_at') })
.where({ published_at: null })
.execute();
}
}
};
const disableDraftAndPublish = async ({ oldContentTypes, contentTypes }) => {
if (!oldContentTypes) {
return;
}
for (const uid in contentTypes) {
if (!oldContentTypes[uid]) {
continue;
}
const oldContentType = oldContentTypes[uid];
const contentType = contentTypes[uid];
// if d&p was disabled remove unpublish content before sync
if (hasDraftAndPublish(oldContentType) && !hasDraftAndPublish(contentType)) {
2022-08-08 23:33:39 +02:00
await strapi.db.queryBuilder(uid).delete().where({ published_at: null }).execute();
}
}
};
module.exports = {
enable: enableDraftAndPublish,
disable: disableDraftAndPublish,
};