mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 11:25:17 +00:00
Resolve issues on relationships
This commit is contained in:
parent
814d1c0211
commit
f32336370b
@ -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;
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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 => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user