85 lines
3.5 KiB
JavaScript
85 lines
3.5 KiB
JavaScript
const { isEmptyForString } = require("../../../middlewares/general/checks");
|
|
const { createCustomSetting, updateCustomSetting, destroyCustomSetting } = require("../../settings/middlewares/post");
|
|
const { getCustomSettings } = require("../../settings/middlewares/query");
|
|
const { User } = require("../models/user");
|
|
|
|
exports.returnData = async function({data, dataset, user, dataForReturn}){
|
|
const returnData = dataForReturn;
|
|
|
|
for (let key in data){
|
|
const databaseKey = dataset[key].title;
|
|
const datasetType = dataset[key].type || "input";
|
|
const databaseVal = user[databaseKey];
|
|
|
|
|
|
if(["input", "select"].includes(datasetType)){
|
|
const value = (data[key]).trim();
|
|
if(databaseVal != value){
|
|
if(
|
|
(dataset[key].isNull === true && [null, undefined, ""].includes(value)) ||
|
|
(![null, undefined, ""].includes(value))
|
|
){
|
|
try {
|
|
await User.update(
|
|
{[databaseKey]: value},
|
|
{
|
|
where:{
|
|
id: user.id
|
|
}
|
|
}
|
|
).then(res => {
|
|
if(res){
|
|
returnData[key] = true;
|
|
};
|
|
});
|
|
} catch (error) {
|
|
returnData[key] = false;
|
|
};
|
|
}
|
|
}else{
|
|
returnData[key] = true;
|
|
};
|
|
}else if(["array"].includes(datasetType)){
|
|
const datas = data[key];
|
|
const query = await getCustomSettings({userId: user.id});
|
|
console.log(datas.length, query.length, 9854894);
|
|
if(query.length < 5){
|
|
datas.map(async (get) => {
|
|
const key = !isEmptyForString(get.key);
|
|
const value = !isEmptyForString(get.value);
|
|
if(!(key && value)){
|
|
if(query.length === 0){
|
|
await createCustomSetting({
|
|
key: get.key,
|
|
value: get.value,
|
|
userId: user.id
|
|
});
|
|
}else{
|
|
const isAvailable = await query.filter(q => (
|
|
get.key === q.key &&
|
|
get.value === q.value
|
|
));
|
|
|
|
const isID = await query.filter(q => get.id === q.id);
|
|
|
|
if(isAvailable.length === 0 && isID.length === 0){
|
|
await createCustomSetting({
|
|
key: get.key,
|
|
value: get.value,
|
|
userId: user.id
|
|
});
|
|
}else if(isAvailable.length === 0 && isID.length > 0){
|
|
await updateCustomSetting({
|
|
id: get.id,
|
|
key: get.key,
|
|
value: get.value
|
|
});
|
|
};
|
|
};
|
|
};
|
|
});
|
|
};
|
|
};
|
|
};
|
|
return returnData;
|
|
}; |