const { Follow } = require("../../follows/models"); const { Gender, Education } = require("../models/user"); const { followButton } = require("../../follows/middlewares/process"); const { getFollowers, getFollowings } = require("../../follows/middlewares/query"); const { Op } = require("sequelize"); const { CustomSettings } = require("../../settings/models/Settings"); const Blogs = require("../../blogs/models/Blogs"); const User = require("../models/user").User; exports.getUsers = async function(){ return await User.findAll({ include: [Blogs, CustomSettings] }); }; exports.getUserWithId = async function({userId}) { return await User.findOne({ where:{ id: userId }, include: [Gender, Education, CustomSettings] }); } exports.getUserWithUsername = async function({username, myUserId=null}){ const result = await User.findOne({ where:{ username: username }, include: [Gender, Education, { model: Follow, where: { user: myUserId }, include: { model: User, as: 'following', where: { username: username } }, required: false }] }); if(result){ result.dataValues.followers = (await getFollowers({userId: result.id, statu: true})).data; result.dataValues.followings = (await getFollowings({userId: result.id, statu: true})).data; if(result.follows.length > 0){ followButton({data: result.follows[0], myUserId: myUserId}); }else{ result.dataValues.button = new Object({ text: "Takip Et" }); }; } return result; }; exports.getUsersDiscover = async function({userId}) { const followings = await Follow.findAll({ where:{ user: userId } }); const followingsIds = followings.map(res => res.target_user); console.log(followingsIds) return User.findAll({ where: { [Op.not]: { id: [...followingsIds, userId] } } }); };