From f32336370b9ba0bb7e2a34b1bfb7f641d8b3b780 Mon Sep 17 00:00:00 2001 From: Aurelsicoko Date: Mon, 11 Sep 2017 18:17:09 +0200 Subject: [PATCH] Resolve issues on relationships --- .../lib/src/utils/cleanData.js | 6 +- .../lib/src/utils/templateObject.js | 2 +- .../admin/src/containers/Edit/sagas.js | 4 +- .../config/queries/mongoose.js | 47 +--------------- .../controllers/ContentManager.js | 55 +++++++++++++++++-- 5 files changed, 60 insertions(+), 54 deletions(-) diff --git a/packages/strapi-helper-plugin/lib/src/utils/cleanData.js b/packages/strapi-helper-plugin/lib/src/utils/cleanData.js index affeb3892f..997598a949 100644 --- a/packages/strapi-helper-plugin/lib/src/utils/cleanData.js +++ b/packages/strapi-helper-plugin/lib/src/utils/cleanData.js @@ -1,16 +1,18 @@ import { isArray } from 'lodash'; -function cleanData(value, key) { +function cleanData(value, key, secondKey) { if (isArray(value)) { return value.map(obj => { if (obj[key]) { return obj[key]; + } else if (obj[secondKey]) { + return obj[secondKey]; } return obj; }); } else if (_.isObject(value)) { - return value[key]; + return value[key] || value[secondKey]; } return value; diff --git a/packages/strapi-helper-plugin/lib/src/utils/templateObject.js b/packages/strapi-helper-plugin/lib/src/utils/templateObject.js index f628d49e79..ba870b74b1 100644 --- a/packages/strapi-helper-plugin/lib/src/utils/templateObject.js +++ b/packages/strapi-helper-plugin/lib/src/utils/templateObject.js @@ -12,7 +12,7 @@ const templateObject = function (obj, variables) { return Object.keys(obj).reduce((acc, key) => { if (isPlainObject(obj[key]) || isArray(obj[key])) { - acc[key] = templateObject(obj[key]); + acc[key] = templateObject(obj[key], variables[key]); } else if (isString(obj[key]) && regex.test(obj[key])) { acc[key] = obj[key].replace(regex, replacer); } else { diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/Edit/sagas.js b/packages/strapi-plugin-content-manager/admin/src/containers/Edit/sagas.js index 8663f99280..8b40b41abc 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/Edit/sagas.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/Edit/sagas.js @@ -42,13 +42,11 @@ export function* editRecord() { const recordJSON = record.toJSON(); const recordCleaned = Object.keys(recordJSON).reduce((acc, current) => { - acc[current] = cleanData(recordJSON[current], 'value'); + acc[current] = cleanData(recordJSON[current], 'value', 'id'); return acc; }, {}); - console.log(recordCleaned); - const isCreating = yield select(makeSelectIsCreating()); const id = isCreating ? '' : recordCleaned.id; diff --git a/packages/strapi-plugin-content-manager/config/queries/mongoose.js b/packages/strapi-plugin-content-manager/config/queries/mongoose.js index acea9c3ad1..f6a545ac98 100644 --- a/packages/strapi-plugin-content-manager/config/queries/mongoose.js +++ b/packages/strapi-plugin-content-manager/config/queries/mongoose.js @@ -28,53 +28,12 @@ module.exports = { }, update: async function (params) { - const virtualFields = []; - - const record = await module.exports.findOne.call(this, params); - const response = record ? record.toJSON() : {}; - - // Only update fields which are on this document. - const values = Object.keys(params.values).reduce((acc, current) => { - if (!this.schema.virtuals.hasOwnProperty(current)) { - acc[current] = params.values[current]; - } else if (response.hasOwnProperty(current) && current !== 'id'){ - const details = this.attributes[current]; - - const toAdd = _.differenceWith(params.values[current], response[current], _.isEqual); - const toRemove = _.differenceWith(response[current], params.values[current], _.isEqual); - - toAdd.forEach(value => { - value[details.via] = params.values[this.primaryKey]; - - virtualFields.push(strapi.query(details.model || details.collection).update({ - id: value.id || value[this.primaryKey] || '_id', - values: value - })); - }); - - toRemove.forEach(value => { - value[details.via] = null; - - virtualFields.push(strapi.query(details.model || details.collection).update({ - id: value.id || value[this.primaryKey] || '_id', - values: value - })); - }); - } - - return acc; - }, {}); - - // Add current model to the flow of updates. - virtualFields.push(this + return await this .update({ [this.primaryKey]: params.id - }, values, { + }, params.values, { strict: false - })); - - // Update virtuals fields. - return await Promise.all(virtualFields); + }); }, delete: async function (params) { diff --git a/packages/strapi-plugin-content-manager/controllers/ContentManager.js b/packages/strapi-plugin-content-manager/controllers/ContentManager.js index 05ad4ed2df..43fb29c830 100644 --- a/packages/strapi-plugin-content-manager/controllers/ContentManager.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentManager.js @@ -73,13 +73,60 @@ module.exports = { }, update: async ctx => { - // Update an entry using `queries` system - const entryUpdated = await strapi.query(ctx.params.model).update({ + const virtualFields = []; + const params = { id: ctx.params.id, values: ctx.request.body - }); + }; - ctx.body = entryUpdated; + // Retrieve current record. + const response = await strapi.query(ctx.params.model).findOne(params) || {}; + // Save current model into variable to get virtual and p + const model = strapi.models[ctx.params.model]; + + // Only update fields which are on this document. + const values = Object.keys(params.values).reduce((acc, current) => { + if (!model.schema.virtuals.hasOwnProperty(current)) { + acc[current] = params.values[current]; + } else if (response[current] && _.isArray(response[current]) && current !== 'id'){ + const details = model.attributes[current]; + + const toAdd = _.differenceWith(params.values[current], response[current], _.isEqual); + const toRemove = _.differenceWith(response[current], params.values[current], _.isEqual); + + toAdd.forEach(value => { + value[details.via] = params.values[model.primaryKey]; + + virtualFields.push(strapi.query(details.model || details.collection).update({ + id: value.id || value[model.primaryKey] || '_id', + values: value + })); + }); + + toRemove.forEach(value => { + value[details.via] = null; + + virtualFields.push(strapi.query(details.model || details.collection).update({ + id: value.id || value[model.primaryKey] || '_id', + values: value + })); + }); + } + + return acc; + }, {}); + + // Add current model to the flow of updates. + virtualFields.push(strapi.query(ctx.params.model).update({ + id: params.id, + values + })); + + // Update virtuals fields. + const process = await Promise.all(virtualFields); + + // Return the last one which is the current model. + ctx.body = process[process.length - 1]; }, delete: async ctx => {