Resolve issues on relationships

This commit is contained in:
Aurelsicoko 2017-09-11 18:17:09 +02:00
parent 814d1c0211
commit f32336370b
5 changed files with 60 additions and 54 deletions

View File

@ -1,16 +1,18 @@
import { isArray } from 'lodash'; import { isArray } from 'lodash';
function cleanData(value, key) { function cleanData(value, key, secondKey) {
if (isArray(value)) { if (isArray(value)) {
return value.map(obj => { return value.map(obj => {
if (obj[key]) { if (obj[key]) {
return obj[key]; return obj[key];
} else if (obj[secondKey]) {
return obj[secondKey];
} }
return obj; return obj;
}); });
} else if (_.isObject(value)) { } else if (_.isObject(value)) {
return value[key]; return value[key] || value[secondKey];
} }
return value; return value;

View File

@ -12,7 +12,7 @@ const templateObject = function (obj, variables) {
return Object.keys(obj).reduce((acc, key) => { return Object.keys(obj).reduce((acc, key) => {
if (isPlainObject(obj[key]) || isArray(obj[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])) { } else if (isString(obj[key]) && regex.test(obj[key])) {
acc[key] = obj[key].replace(regex, replacer); acc[key] = obj[key].replace(regex, replacer);
} else { } else {

View File

@ -42,13 +42,11 @@ export function* editRecord() {
const recordJSON = record.toJSON(); const recordJSON = record.toJSON();
const recordCleaned = Object.keys(recordJSON).reduce((acc, current) => { const recordCleaned = Object.keys(recordJSON).reduce((acc, current) => {
acc[current] = cleanData(recordJSON[current], 'value'); acc[current] = cleanData(recordJSON[current], 'value', 'id');
return acc; return acc;
}, {}); }, {});
console.log(recordCleaned);
const isCreating = yield select(makeSelectIsCreating()); const isCreating = yield select(makeSelectIsCreating());
const id = isCreating ? '' : recordCleaned.id; const id = isCreating ? '' : recordCleaned.id;

View File

@ -28,53 +28,12 @@ module.exports = {
}, },
update: async function (params) { update: async function (params) {
const virtualFields = []; return await this
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
.update({ .update({
[this.primaryKey]: params.id [this.primaryKey]: params.id
}, values, { }, params.values, {
strict: false strict: false
})); });
// Update virtuals fields.
return await Promise.all(virtualFields);
}, },
delete: async function (params) { delete: async function (params) {

View File

@ -73,13 +73,60 @@ module.exports = {
}, },
update: async ctx => { update: async ctx => {
// Update an entry using `queries` system const virtualFields = [];
const entryUpdated = await strapi.query(ctx.params.model).update({ const params = {
id: ctx.params.id, id: ctx.params.id,
values: ctx.request.body 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 => { delete: async ctx => {