feat: update/create/delete entity stages methods

This commit is contained in:
Marc-Roig 2023-05-15 10:27:06 +02:00
parent 5bf82c08b4
commit d7b8224c37
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249

View File

@ -64,10 +64,11 @@ module.exports = ({ strapi }) => {
return strapi.entityService.count(STAGE_MODEL_UID); return strapi.entityService.count(STAGE_MODEL_UID);
}, },
async replaceStages(fromStages, toStages) { // TODO : improve contentTypesToUpdate name
const { created, updated, deleted } = getDiffBetweenStages(fromStages, toStages); async replaceStages(srcStages, destStages, contentTypesToUpdate) {
const { created, updated, deleted } = getDiffBetweenStages(srcStages, destStages);
assertAtLeastOneStageRemain(fromStages || [], { created, deleted }); assertAtLeastOneStageRemain(srcStages || [], { created, deleted });
// Update stages and assign entity stages // Update stages and assign entity stages
return strapi.db.transaction(async ({ trx }) => { return strapi.db.transaction(async ({ trx }) => {
@ -75,9 +76,6 @@ module.exports = ({ strapi }) => {
const createdStages = await this.createMany(created, { fields: ['id'] }); const createdStages = await this.createMany(created, { fields: ['id'] });
// Put all the newly created stages ids // Put all the newly created stages ids
const createdStagesIds = map('id', createdStages); const createdStagesIds = map('id', createdStages);
// TODO: Find content types assigned to the workflow
// const contentTypes = getContentTypeUIDsWithActivatedReviewWorkflows(strapi.contentTypes);
const contentTypes = [];
// Update the workflow stages // Update the workflow stages
await mapAsync(updated, (stage) => this.update(stage.id, stage)); await mapAsync(updated, (stage) => this.update(stage.id, stage));
@ -87,15 +85,15 @@ module.exports = ({ strapi }) => {
// Find the nearest stage in the workflow and newly created stages // Find the nearest stage in the workflow and newly created stages
// that is not deleted, prioritizing the previous stages // that is not deleted, prioritizing the previous stages
const nearestStage = findNearestMatchingStage( const nearestStage = findNearestMatchingStage(
[...fromStages, ...createdStages], [...srcStages, ...createdStages],
fromStages.findIndex((s) => s.id === stage.id), srcStages.findIndex((s) => s.id === stage.id),
(targetStage) => { (targetStage) => {
return !deleted.find((s) => s.id === targetStage.id); return !deleted.find((s) => s.id === targetStage.id);
} }
); );
// Assign the new stage to entities that had the deleted stage // Assign the new stage to entities that had the deleted stage
await mapAsync(contentTypes, (contentTypeUID) => { await mapAsync(contentTypesToUpdate, (contentTypeUID) => {
this.updateEntitiesStage(contentTypeUID, { this.updateEntitiesStage(contentTypeUID, {
fromStageId: stage.id, fromStageId: stage.id,
toStageId: nearestStage.id, toStageId: nearestStage.id,
@ -106,7 +104,7 @@ module.exports = ({ strapi }) => {
return this.delete(stage.id); return this.delete(stage.id);
}); });
return toStages.map((stage) => ({ ...stage, id: stage.id ?? createdStagesIds.shift() })); return destStages.map((stage) => ({ ...stage, id: stage.id ?? createdStagesIds.shift() }));
}); });
}, },
@ -137,18 +135,19 @@ module.exports = ({ strapi }) => {
/** /**
* Updates the stage of all entities of a content type that are in a specific stage * Updates the stage of all entities of a content type that are in a specific stage
* @param {string} contentTypeUID * @param {string} uid
* @param {number} fromStageId * @param {number} fromStageId
* @param {number} toStageId * @param {number} toStageId
* @param {KnexTransaction} trx * @param {KnexTransaction} trx
* @returns * @returns
*/ */
async updateEntitiesStage(contentTypeUID, { fromStageId, toStageId, trx = null }) { async updateEntitiesStage(uid, { fromStageId, toStageId }) {
const { attributes, tableName } = strapi.db.metadata.get(contentTypeUID); const { attributes, tableName } = strapi.db.metadata.get(uid);
const joinTable = attributes[ENTITY_STAGE_ATTRIBUTE].joinTable; const joinTable = attributes[ENTITY_STAGE_ATTRIBUTE].joinTable;
const joinColumn = joinTable.joinColumn.name; const joinColumn = joinTable.joinColumn.name;
const invJoinColumn = joinTable.inverseJoinColumn.name; const invJoinColumn = joinTable.inverseJoinColumn.name;
return strapi.db.transaction(async ({ trx }) => {
const selectStatement = strapi.db const selectStatement = strapi.db
.getConnection() .getConnection()
.select({ [joinColumn]: 't1.id', [invJoinColumn]: toStageId }) .select({ [joinColumn]: 't1.id', [invJoinColumn]: toStageId })
@ -157,22 +156,42 @@ module.exports = ({ strapi }) => {
.where(`t2.${invJoinColumn}`, fromStageId) .where(`t2.${invJoinColumn}`, fromStageId)
.toSQL(); .toSQL();
// Insert rows for all entries of the content type that do not have a // Insert rows for all entries of the content type that have the specified stage
// default stage return strapi.db
const query = strapi.db
.getConnection(joinTable.name) .getConnection(joinTable.name)
.insert( .insert(
strapi.db.connection.raw( strapi.db.connection.raw(
`(${joinColumn}, ${invJoinColumn}) ${selectStatement.sql}`, `(${joinColumn}, ${invJoinColumn}) ${selectStatement.sql}`,
selectStatement.bindings selectStatement.bindings
) )
)
.transacting(trx);
});
},
async updateAllEntitiesStage(uid, { toStageId }) {
const { attributes } = strapi.db.metadata.get(uid);
const joinTable = attributes[ENTITY_STAGE_ATTRIBUTE].joinTable;
const invJoinColumn = joinTable.inverseJoinColumn.name;
// Move all entries to the specified stage
return strapi.db.transaction(async ({ trx }) =>
strapi.db
.getConnection()
.from(joinTable.name)
.update({ [invJoinColumn]: toStageId })
.transacting(trx)
); );
},
if (trx) { async deleteAllEntitiesStage(uid) {
query.transacting(trx); const { attributes } = strapi.db.metadata.get(uid);
} const joinTable = attributes[ENTITY_STAGE_ATTRIBUTE].joinTable;
return query; // Delete all stage links for the content type
return strapi.db.transaction(async ({ trx }) =>
strapi.db.getConnection().from(joinTable.name).delete().transacting(trx)
);
}, },
}; };
}; };