2020-09-07 15:04:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const { contentTypes: contentTypesUtils } = require('strapi-utils');
|
2020-10-27 11:27:17 +01:00
|
|
|
|
2020-09-07 15:04:20 +02:00
|
|
|
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
2021-02-15 08:43:59 +01:00
|
|
|
const { getDefinitionFromStore } = require('../utils/store-definition');
|
2020-09-07 15:04:20 +02:00
|
|
|
|
|
|
|
const getDraftAndPublishMigrationWay = async (definition, ORM) => {
|
|
|
|
const previousDefRow = await getDefinitionFromStore(definition, ORM);
|
|
|
|
const previousDef = JSON.parse(_.get(previousDefRow, 'value', null));
|
|
|
|
const previousDraftAndPublish = contentTypesUtils.hasDraftAndPublish(previousDef);
|
|
|
|
const actualDraftAndPublish = contentTypesUtils.hasDraftAndPublish(definition);
|
|
|
|
|
2020-10-13 14:30:17 +02:00
|
|
|
if (!previousDefRow || previousDraftAndPublish === actualDraftAndPublish) {
|
2020-09-07 15:04:20 +02:00
|
|
|
return 'none';
|
|
|
|
}
|
|
|
|
if (!previousDraftAndPublish && actualDraftAndPublish) {
|
|
|
|
return 'enable';
|
|
|
|
}
|
|
|
|
if (previousDraftAndPublish && !actualDraftAndPublish) {
|
|
|
|
return 'disable';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const migrateDraftAndPublish = async ({ definition, model, ORM }) => {
|
|
|
|
let way = await getDraftAndPublishMigrationWay(definition, ORM);
|
|
|
|
|
|
|
|
if (way === 'enable') {
|
2021-02-15 08:43:59 +01:00
|
|
|
console.log('D&P enable');
|
2020-09-07 15:04:20 +02:00
|
|
|
const createdAtCol = _.get(definition, 'timestamps.createdAt', 'createdAt');
|
|
|
|
await model
|
|
|
|
.aggregate([
|
|
|
|
{
|
|
|
|
$addFields: {
|
2020-09-16 10:08:18 +02:00
|
|
|
[PUBLISHED_AT_ATTRIBUTE]: { $ifNull: [`$${createdAtCol}`, new Date()] },
|
2020-09-07 15:04:20 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$out: definition.collectionName,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
.exec();
|
|
|
|
} else if (way === 'disable') {
|
2021-02-15 08:43:59 +01:00
|
|
|
console.log('D&P disable');
|
2020-09-07 15:04:20 +02:00
|
|
|
await model.deleteMany({ [PUBLISHED_AT_ATTRIBUTE]: null });
|
|
|
|
await model.updateMany({}, { $unset: { [PUBLISHED_AT_ATTRIBUTE]: '' } }, { strict: false });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2021-02-15 08:43:59 +01:00
|
|
|
before: migrateDraftAndPublish,
|
2020-09-07 15:04:20 +02:00
|
|
|
};
|