Reputation: 369
I am trying to search user that not in the team yet to invite them but right now my function is searching user that in the team only. So now how can I implement the function to search user that not in the team yet.
Here is my following code:
exports.searchUserToInvite = async (req, res) => {
// Grab query
const query = req.params.q;
const publicTeamId = req.params.id;
const teamId = await getTeamId(publicTeamId);
// Get team data based on id
// Search for users
const usersFound = await models.User.findAll({
where: {
[Op.or]: [
{
fullname: {
[Op.iLike]: "%" + query + "%",
},
},
],
},
attributes: [
"fullname",
"public_user_id",
"institution",
"location",
"webpage",
"linkedin",
"major",
"picture",
"verifiedDT",
],
include: [
{
model: models.Rating,
attributes: ["skillset_rating", "team_member_rating"],
},
{
model: models.Skill,
attributes: ["skill"],
},
{
model: models.Team,
attributes: ["status"],
where: {
id: teamId,
},
},
],
});
// Run searches
const searchData = await Promise.all([usersFound]);
// Return results
res.status(200).json(searchData);
};
Update add following code model User:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
fullname: {
type: DataTypes.STRING,
allowNull: false,
},
passwordhash: DataTypes.STRING,
institution: DataTypes.STRING,
bio: DataTypes.STRING,
creator_user_id: DataTypes.UUID,
public_user_id: DataTypes.STRING,
picture: DataTypes.STRING(300),
email: { type: DataTypes.STRING, unique: true },
service: DataTypes.STRING,
gender: DataTypes.STRING,
age: DataTypes.INTEGER ,
altId: DataTypes.STRING,
location: DataTypes.STRING,
mergedTo: DataTypes.UUID,
document: DataTypes.STRING,
webpage: DataTypes.STRING,
linkedin: DataTypes.STRING,
signupIP: DataTypes.STRING,
major: DataTypes.STRING,
verifiedDT: DataTypes.DATE,
couponsUsed: DataTypes.ARRAY(DataTypes.STRING),
teamCredits: DataTypes.INTEGER ,
resetToken: DataTypes.STRING,
resetTokenExpires: DataTypes.DATE,
uniqueToken: DataTypes.STRING,
expireToken: DataTypes.DATE,
},
{
tableName: "Users",
timestamps: true,
indexes: [
{
unique: false,
fields: ["email", "id", "fullname", "public_user_id"],
},
],
}
);
User.associate = function (models) {
User.belongsToMany(models.Skill, { through: models.UserSkills });
User.hasMany(models.Team, {
foreignKey: "creatorId",
});
User.hasMany(models.Membership, {
foreignKey: "memberId",
});
User.hasMany(models.Rating, {
foreignKey: "raterId",
});
User.hasMany(models.Rating, {
foreignKey: "rateeId",
});
User.hasMany(models.InvitesApplications, {
foreignKey: "senderId",
});
User.hasMany(models.Message, {
foreignKey: "userID"
});
};
return User;
};
How can I implement my function?
Updated add model Membership in question:
"use strict";
module.exports = (sequelize, DataTypes) => {
const Membership = sequelize.define(
"Membership",
{
interests: DataTypes.STRING,
contributions: DataTypes.STRING,
authorization: DataTypes.STRING,
status: DataTypes.STRING,
application_letter: DataTypes.STRING,
date_applied: DataTypes.DATE,
date_joined: DataTypes.DATE,
date_departed: DataTypes.DATE
},
{ tableName: "Memberships", timestamps: true }
);
Membership.associate = function(models) {
Membership.belongsTo(models.User, {
foreignKey: "memberId"
});
Membership.belongsTo(models.Team, {
foreignKey: "teamId"
});
Membership.belongsTo(models.Role, {
foreignKey: "roleId"
});
Membership.hasMany(models.Rating, {
foreignKey: "membershipId"
});
};
return Membership;
};
Updated add model User in question:
Upvotes: 0
Views: 635
Reputation: 713
you need to apply not equal to in where clause.
where: {
id: {[Op.ne]: teamId},
},
Upvotes: 1