feat: Update relations entities on import
parent
dbe3ea8c88
commit
dbbdeb5f5b
|
@ -1,6 +1,6 @@
|
|||
const { isEmpty } = require('lodash');
|
||||
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 {
|
||||
constructor({ queryString, configName, uid, jsonFields, relations }) {
|
||||
|
@ -106,6 +106,7 @@ const ConfigType = class ConfigType {
|
|||
|
||||
const configToAdd = difference(configContent[relationName], existingConfig[relationName], relationSortFields);
|
||||
const configToDelete = difference(existingConfig[relationName], configContent[relationName], relationSortFields);
|
||||
const configToUpdate = same(configContent[relationName], existingConfig[relationName], relationSortFields);
|
||||
|
||||
await Promise.all(configToDelete.map(async (config) => {
|
||||
const whereClause = {};
|
||||
|
@ -125,6 +126,21 @@ const ConfigType = class ConfigType {
|
|||
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 },
|
||||
});
|
||||
}));
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,4 +10,23 @@ const difference = (arrayOne, arrayTwo, compareKeys) => {
|
|||
});
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue