Merge pull request #43 from boazpoolman/feature/multi-sort-relations
Update relation on importpull/47/head
commit
ef74b725cc
|
@ -1,6 +1,6 @@
|
||||||
const { isEmpty } = require('lodash');
|
const { isEmpty } = require('lodash');
|
||||||
const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCombinedUidWhereFilter, getUidParamsFromName } = require('../utils');
|
const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCombinedUidWhereFilter, getUidParamsFromName } = require('../utils');
|
||||||
const difference = require('../utils/getArrayDiff');
|
const { difference, same } = require('../utils/getArrayDiff');
|
||||||
|
|
||||||
const ConfigType = class ConfigType {
|
const ConfigType = class ConfigType {
|
||||||
constructor({ queryString, configName, uid, jsonFields, relations }) {
|
constructor({ queryString, configName, uid, jsonFields, relations }) {
|
||||||
|
@ -99,18 +99,23 @@ const ConfigType = class ConfigType {
|
||||||
const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query });
|
const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query });
|
||||||
|
|
||||||
// Delete/create relations.
|
// Delete/create relations.
|
||||||
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortField }) => {
|
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortFields }) => {
|
||||||
const relationQueryApi = strapi.query(queryString);
|
const relationQueryApi = strapi.query(queryString);
|
||||||
existingConfig = sanitizeConfig(existingConfig, relationName, relationSortField);
|
existingConfig = sanitizeConfig(existingConfig, relationName, relationSortFields);
|
||||||
configContent = sanitizeConfig(configContent, relationName, relationSortField);
|
configContent = sanitizeConfig(configContent, relationName, relationSortFields);
|
||||||
|
|
||||||
const configToAdd = difference(configContent[relationName], existingConfig[relationName], relationSortField);
|
const configToAdd = difference(configContent[relationName], existingConfig[relationName], relationSortFields);
|
||||||
const configToDelete = difference(existingConfig[relationName], configContent[relationName], relationSortField);
|
const configToDelete = difference(existingConfig[relationName], configContent[relationName], relationSortFields);
|
||||||
|
const configToUpdate = same(configContent[relationName], existingConfig[relationName], relationSortFields);
|
||||||
|
|
||||||
await Promise.all(configToDelete.map(async (config) => {
|
await Promise.all(configToDelete.map(async (config) => {
|
||||||
|
const whereClause = {};
|
||||||
|
relationSortFields.map((sortField) => {
|
||||||
|
whereClause[sortField] = config[sortField];
|
||||||
|
});
|
||||||
await relationQueryApi.delete({
|
await relationQueryApi.delete({
|
||||||
where: {
|
where: {
|
||||||
[relationSortField]: config[relationSortField],
|
...whereClause,
|
||||||
[parentName]: entity.id,
|
[parentName]: entity.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -121,6 +126,21 @@ const ConfigType = class ConfigType {
|
||||||
data: { ...config, [parentName]: entity.id },
|
data: { ...config, [parentName]: entity.id },
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
await Promise.all(configToUpdate.map(async (config, index) => {
|
||||||
|
const whereClause = {};
|
||||||
|
relationSortFields.map((sortField) => {
|
||||||
|
whereClause[sortField] = config[sortField];
|
||||||
|
});
|
||||||
|
|
||||||
|
await relationQueryApi.update({
|
||||||
|
where: {
|
||||||
|
...whereClause,
|
||||||
|
[parentName]: entity.id,
|
||||||
|
},
|
||||||
|
data: { ...config, [parentName]: entity.id },
|
||||||
|
});
|
||||||
|
}));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,13 +188,15 @@ const ConfigType = class ConfigType {
|
||||||
if (shouldExclude) return;
|
if (shouldExclude) return;
|
||||||
|
|
||||||
const formattedConfig = { ...sanitizeConfig(config) };
|
const formattedConfig = { ...sanitizeConfig(config) };
|
||||||
await Promise.all(this.relations.map(async ({ queryString, relationName, relationSortField, parentName }) => {
|
await Promise.all(this.relations.map(async ({ queryString, relationName, relationSortFields, parentName }) => {
|
||||||
const relations = await noLimit(strapi.query(queryString), {
|
const relations = await noLimit(strapi.query(queryString), {
|
||||||
where: { [parentName]: combinedUidWhereFilter },
|
where: { [parentName]: combinedUidWhereFilter },
|
||||||
});
|
});
|
||||||
|
|
||||||
relations.map((relation) => sanitizeConfig(relation));
|
relations.map((relation) => sanitizeConfig(relation));
|
||||||
relations.sort(dynamicSort(relationSortField));
|
relationSortFields.map((sortField) => {
|
||||||
|
relations.sort(dynamicSort(sortField));
|
||||||
|
});
|
||||||
formattedConfig[relationName] = relations;
|
formattedConfig[relationName] = relations;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ const types = (strapi) => {
|
||||||
queryString: 'admin::permission',
|
queryString: 'admin::permission',
|
||||||
relationName: 'permissions',
|
relationName: 'permissions',
|
||||||
parentName: 'role',
|
parentName: 'role',
|
||||||
relationSortField: 'action',
|
relationSortFields: ['action', 'subject'],
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@ -32,7 +32,7 @@ const types = (strapi) => {
|
||||||
queryString: 'plugin::users-permissions.permission',
|
queryString: 'plugin::users-permissions.permission',
|
||||||
relationName: 'permissions',
|
relationName: 'permissions',
|
||||||
parentName: 'role',
|
parentName: 'role',
|
||||||
relationSortField: 'action',
|
relationSortFields: ['action'],
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,32 @@
|
||||||
const difference = (arrayOne, arrayTwo, compareKey) => {
|
const difference = (arrayOne, arrayTwo, compareKeys) => {
|
||||||
return arrayOne.filter(({ [compareKey]: id1 }) => {
|
return arrayOne.filter(({
|
||||||
return !arrayTwo.some(({ [compareKey]: id2 }) => id2 === id1);
|
[compareKeys[0]]: id1,
|
||||||
|
[compareKeys[1]]: id2,
|
||||||
|
}) => {
|
||||||
|
return !arrayTwo.some(({
|
||||||
|
[compareKeys[0]]: id3,
|
||||||
|
[compareKeys[1]]: id4,
|
||||||
|
}) => id1 === id3 && id2 === id4);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = difference;
|
const same = (arrayOne, arrayTwo, compareKeys) => {
|
||||||
|
return arrayOne.filter(({
|
||||||
|
[compareKeys[0]]: id1,
|
||||||
|
[compareKeys[1]]: id2,
|
||||||
|
...restOne
|
||||||
|
}) => {
|
||||||
|
return !arrayTwo.some(({
|
||||||
|
[compareKeys[0]]: id3,
|
||||||
|
[compareKeys[1]]: id4,
|
||||||
|
...restTwo
|
||||||
|
}) => id1 === id3
|
||||||
|
&& id2 === id4
|
||||||
|
&& JSON.stringify(restOne) === JSON.stringify(restTwo));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
difference,
|
||||||
|
same,
|
||||||
|
};
|
||||||
|
|
|
@ -37,14 +37,16 @@ const dynamicSort = (property) => {
|
||||||
|
|
||||||
return (a, b) => {
|
return (a, b) => {
|
||||||
if (sortOrder === -1) {
|
if (sortOrder === -1) {
|
||||||
return b[property].localeCompare(a[property]);
|
if (b[property]) {
|
||||||
} else {
|
return b[property].localeCompare(a[property]);
|
||||||
|
}
|
||||||
|
} else if (a[property]) {
|
||||||
return a[property].localeCompare(b[property]);
|
return a[property].localeCompare(b[property]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const sanitizeConfig = (config, relation, relationSortField) => {
|
const sanitizeConfig = (config, relation, relationSortFields) => {
|
||||||
delete config._id;
|
delete config._id;
|
||||||
delete config.id;
|
delete config.id;
|
||||||
delete config.updatedAt;
|
delete config.updatedAt;
|
||||||
|
@ -63,8 +65,10 @@ const sanitizeConfig = (config, relation, relationSortField) => {
|
||||||
formattedRelations.push(relationEntity);
|
formattedRelations.push(relationEntity);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (relationSortField) {
|
if (relationSortFields) {
|
||||||
formattedRelations.sort(dynamicSort(relationSortField));
|
relationSortFields.map((sortField) => {
|
||||||
|
formattedRelations.sort(dynamicSort(sortField));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
config[relation] = formattedRelations;
|
config[relation] = formattedRelations;
|
||||||
|
|
Loading…
Reference in New Issue