94 lines
3.4 KiB
JavaScript
94 lines
3.4 KiB
JavaScript
const { createNotification } = require("../../notifications/middlewares/proccess");
|
|
const { getNotificationCode } = require("../../notifications/middlewares/query");
|
|
const { getUserWithId } = require("../../users/middlewares/query");
|
|
const Blogs = require("../models/Blogs");
|
|
const { ReBlog } = require("../models/ReBlogs");
|
|
const SavedBlogs = require("../models/SavedBlogs");
|
|
const { getSaved, getBlog } = require("./query");
|
|
const { checkLenText } = require("./tools");
|
|
|
|
exports.createBlog = async function(data, user){
|
|
const checkTitle = checkLenText(data.title, process.env.BLOGS_MIN_LIMIT_TITLE, process.env.BLOGS_MAX_LIMIT_TITLE);
|
|
const checkText = checkLenText(data.text, process.env.BLOGS_MIN_LIMIT_TEXT, process.env.BLOGS_MAX_LIMIT_TEXT);
|
|
if(checkTitle && checkText){
|
|
console.log(data, user)
|
|
return await Blogs.create({
|
|
title: data.title,
|
|
text: data.text,
|
|
categoryId: data.categoryId,
|
|
userId: user.id
|
|
});
|
|
}else{
|
|
return false;
|
|
};
|
|
};
|
|
|
|
exports.createSaved = async function({blogId, userId}){
|
|
const checkQuery = await getSaved({blogId: blogId, userId: userId});
|
|
|
|
if(!checkQuery){
|
|
const result = await SavedBlogs.create({
|
|
blogId: blogId,
|
|
userId: userId,
|
|
statu: true
|
|
}).then(async get => {
|
|
const myUser = await getUserWithId({ userId: userId });
|
|
await createNotification({
|
|
data: {
|
|
user: userId,
|
|
blog: blogId
|
|
},
|
|
userId: await getBlog({ blogId: blogId }).then(blog => blog.userId),
|
|
notificationCodeId: await getNotificationCode({ draftCode: "blog-saved" }).then(draft => draft.id)
|
|
});
|
|
return (get) ? true : false
|
|
});
|
|
return {Status: "Success", data: result}
|
|
}else if(checkQuery){
|
|
const result = await SavedBlogs.update(
|
|
{statu: !checkQuery.statu},
|
|
{
|
|
where:{
|
|
blogId: blogId,
|
|
userId: userId
|
|
}
|
|
}
|
|
).then(async get => {;
|
|
if(!checkQuery.statu == true){
|
|
// await createNotification({
|
|
// data: {
|
|
// user: userId,
|
|
// blog: blogId
|
|
// },
|
|
// userId: await getBlog({ blogId: blogId }).then(blog => blog.userId),
|
|
// notificationCodeId: await getNotificationCode({ draftCode: "blog-saved" }).then(draft => draft.id)
|
|
// });
|
|
};
|
|
return (get) && !checkQuery.statu;
|
|
});
|
|
return {Status: "Success", data: result};
|
|
}else{
|
|
return {Status: "Failed"};
|
|
};
|
|
};
|
|
|
|
exports.createReBlog = async function(blogId, userId){
|
|
return await ReBlog.create({
|
|
userId: userId,
|
|
blogId: blogId
|
|
}).then(async res => {
|
|
if(res){
|
|
await createNotification({
|
|
data: {
|
|
user: userId,
|
|
blog: blogId
|
|
},
|
|
userId: await getBlog({ blogId: blogId }).then(blog => blog.userId),
|
|
notificationCodeId: await getNotificationCode({ draftCode: "blog-reblog" }).then(draft => draft.id)
|
|
});
|
|
return {Status: "Success", data: res};
|
|
}else{
|
|
return {Status: "Failed"};
|
|
};
|
|
});
|
|
}; |