diff --git a/src/services/matching.services.js b/src/services/matching.services.js index 70d60d5..7c0e9e7 100644 --- a/src/services/matching.services.js +++ b/src/services/matching.services.js @@ -19,31 +19,46 @@ function getcrossSkill(str) { async function searchForMatch(crossSkill, notToProvide) { return new Promise(resolve => { let results = {}; + //set interval for firing the search every 1/2 secend const intervalId = setInterval(async () => { + //test---------------- // console.log('searching for match'); + //-------------------- const crossSkillUsers = await getMany(crossSkill); - //remove any user that is in notToProvide + //test---------------- // console.log( // crossSkillUsers, // 'crossSkill', // notToProvide, // 'notToProvide' // ); + //-------------------- + + //remove any user that is in notToProvide crossSkillUsers.filter(user => { if (!notToProvide.includes(user)) { + //test---------------- // console.log('user not in notToProvide', user); + //-------------------- return user; } }); + //test---------------- // console.log(crossSkillUsers, notToProvide); + //-------------------- + if (crossSkillUsers.length > 0) { results = { found: true, MatchedUserId: crossSkillUsers[0] }; + //test---------------- // console.log('match found', results); + //-------------------- } else { results = { found: false, MatchedUserId: null }; + //test---------------- // console.log('no match found', results); + //-------------------- } }, 1 * 1000); @@ -59,7 +74,9 @@ async function searchForMatch(crossSkill, notToProvide) { function createSkills(wantedSkill, userSkills) { const userSkillsArray = [...new Set(userSkills)]; let strings = []; + //test---------------- // console.log(userSkillsArray, wantedSkill); + //-------------------- userSkillsArray.map(skill => { strings.push(`${skill}/${wantedSkill}`); }); @@ -67,59 +84,117 @@ function createSkills(wantedSkill, userSkills) { } exports.findMatch = async (userId, wantedInnerSkill, userSocketId) => { + //get the user const userInner = await User.findById(userId); - //test + + //test---------------- // console.log(userInner); + //-------------------- + const userInnerSkills = userInner.skillsLearned.map(skillLearned => { + //test---------------- // console.log(skillLearned); + //-------------------- return skillLearned.skill?.name; }); - //test + + //test---------------- // console.log(userInnerSkills); + //-------------------- + + //setting the user session state to started await setOne(`${userInner._id}/state`, 'started'); + + //preseting the user socket id in redis await setMany([`${userInner._id}`], `${userSocketId}`); - // await setMany([`${userInner._id}/not-to-provide`], 'Starter'); + // 4- create search tags const tags = createSkills(wantedInnerSkill, userInnerSkills); + + //test---------------- // console.log(tags); // console.log(await getMany(tags[0])); + //-------------------- + + //setting the user tags in redis with initial user id value of first user to have this skill tag await setMany(tags, `${userInner._id}`); - // 5- search for match - // console.log(tags, '-------------------tags'); + + //test---------------- + // console.log(tags, 'tags'); + //-------------------- + + //start searching for match for only the first user skill tag const tag = tags[0]; + + //get the opposite skill tag to match with the first user skill tag const crossSkill = getcrossSkill(tag); + + //get the not to provide list (a list of all rejected users) of user const notToProvide = await getMany(`${userInner._id}/not-to-provide`); + + //test---------------- // console.log(crossSkill); // console.log(notToProvide); // console.log(`${tag}`, await getMany(tag), 'tag conten'); + //-------------------- + + //start searching for match as long as the user session state is not stopped or found while (true) { const match = await searchForMatch(crossSkill, notToProvide); + //get the state of the user/matchedUser session const state = await getOne(`${userInner._id}/${match.MatchedUserId}`); + //get the state of the user session const singleUserState = await getOne(`${userInner._id}/state`); + //test---------------- // console.log(singleUserState, 'singleUserState'); // console.log(state, 'state'); + //-------------------- + //if the user session state is stopped or found if (singleUserState === 'stopped' || singleUserState === 'found') { - console.log('hi from stopped or found'); + //test---------------- + //console.log('hi from stopped or found'); + //-------------------- + return { emitMatchFound: false }; + + //if the user/matchedUser session state is stated } else if (match.found || state === 'started') { - console.log('hi from match-found event'); + //test---------------- + // console.log('hi from match-found event'); // console.log( // `${userInner._id}/${match.MatchedUserId}`, // 'buggg' // ); + //-------------------- + + //get the socket id of the matched user const matchSocketId = await getMany(`${match.MatchedUserId}`); + + //get the socket ids of the user const userSockets = await getMany(`${userInner._id}`); + + //get the matched user const matchedUser = await User.findById(match.MatchedUserId); + //remove the user from tag (skill) matching queue await removeFromSet(tag, `${userInner._id}`); + + //remove the matched user from tag (skill) matching queue await removeFromSet(crossSkill, `${match.MatchedUserId}`); + + //set the user/matchedUser session state to started await setOne(`${userInner._id}/${match.MatchedUserId}`, 'started'); + + //set the user session state to found await setOne(`${userInner._id}/state`, 'found'); + + //set the matchedUser session state to found await setOne(`${matchedUser._id}/state`, 'found'); + return { emitMatchFound: true, matchedUser, @@ -132,26 +207,45 @@ exports.findMatch = async (userId, wantedInnerSkill, userSocketId) => { }; exports.clinetApproval = async (userId, MatchedUserId) => { + //get the user const user = await User.findById(userId); + + //get the matched user const matchedUser = await User.findById(MatchedUserId); - console.log('hi from global approval'); + //test---------------- + // console.log('hi from global approval'); + //-------------------- + //get the state of the user/matchedUser session const status1 = await getOne(`${userId}/${MatchedUserId}`); const status2 = await getOne(`${MatchedUserId}/${userId}`); const status = status1 || status2; - console.log(status, status1, status2, 'status'); + //test---------------- + // console.log(status, status1, status2, 'status'); + //-------------------- + + //if the user/matchedUser session state is started set matching session state to pending if (status === 'started') { - console.log('hi from started'); + //test---------------- + // console.log('hi from started'); + //-------------------- await setOne(`${userId}/${MatchedUserId}`, 'pending'); - } else if (status === 'pending') { - console.log('hi from pending'); - await setOne(`${userId}/${MatchedUserId}`, 'approved'); + //if the user/matchedUser session state is pending set matching session state to approved and create meeting then add the two participant to meeting + } else if (status === 'pending') { + //test---------------- + // console.log('hi from pending'); // console.log(this.id); + //-------------------- + + await setOne(`${userId}/${MatchedUserId}`, 'approved'); + //get the socket ids of the matched user const matchSocketIds = await getMany(`${MatchedUserId}`); + + //get the socket ids of the user const userSocketIds = await getMany(`${userId}`); //1- create meeting @@ -185,19 +279,29 @@ exports.clinetApproval = async (userId, MatchedUserId) => { }; exports.clientRejection = async (userId, MatchedUserId) => { - console.log('hi from global rejection'); + //test---------------- + // console.log('hi from global rejection'); // const socketId = this.id; + //-------------------- + //get the user const user = await User.findById(userId); + + //get the matched user const matchedUser = await User.findById(MatchedUserId); + //get the state of the user/matchedUser session const status1 = await getOne(`${userId}/${MatchedUserId}`); const status2 = await getOne(`${MatchedUserId}/${userId}`); const status = status1 || status2; + //test---------------- // console.log(socketId); - console.log(status, status1, status2); + // console.log(status, status1, status2); + //-------------------- + //if the user/matchedUser session state is started or pending set matching session state to rejected + //and add the rejected user to the blocked list of the other user if (status === 'started' || status === 'pending') { await setOne(`${userId}/${MatchedUserId}`, 'rejected'); @@ -205,10 +309,15 @@ exports.clientRejection = async (userId, MatchedUserId) => { await setMany([`${MatchedUserId}/not-to-provide`], `${userId}`); + //get the socket ids of the matched user const matchSocketIds = await getMany(`${MatchedUserId}`); + + //get the socket ids of the user const userSocketIds = await getMany(`${userId}`); - console.log(matchSocketIds, userSocketIds); + //test---------------- + // console.log(matchSocketIds, userSocketIds); + //-------------------- return { emitServerRejection: true, @@ -222,22 +331,38 @@ exports.clientRejection = async (userId, MatchedUserId) => { }; exports.clientCancelation = async (userId, wantedInnerSkill) => { - console.log('hi from cancel-search'); - + //test---------------- + // console.log('hi from cancel-search'); // const state = await getOne(`${userId}/state`); // console.log(state); + //-------------------- + + //get the user const userInner = await User.findById(userId); + + //get all user learned skills const userInnerSkills = userInner.skillsLearned.map(skillLearned => { return skillLearned.skill?.name; }); + + //create search tags const tags = createSkills(wantedInnerSkill, userInnerSkills); + //test---------------- // console.log(tags); + //-------------------- + //set the user session state to stopped await setOne(`${userId}/state`, 'stopped'); + + //remove the user from all tags (skills) matching queues tags.forEach(async tag => { + //test---------------- // console.log(`${tag}`, await getMany(tag), 'this tag content'); + //-------------------- await removeFromSet(tag, `${userInner._id}`); + //test---------------- // console.log(`${tag}`, await getMany(tag), 'this tag content'); + //-------------------- }); };