Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions src/faker/user_update.faker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { fakerFR } = require('@faker-js/faker');
require('dotenv').config();
const { getRandomGif, getRandomMovie, updateInterest, getCustumBio, getRandomMusic, getRandomModules, getPreferences } = require('./utils.js');
const { getRandomAge, getRandomGif, getRandomMovie, updateInterest, getCustumBio, getRandomMusic, getRandomModules, getPreferences, getRandomQuestion } = require('./utils.js');

const faker = fakerFR

Expand Down Expand Up @@ -90,39 +90,24 @@ const updateUserList = async () => {
}
console.log("user : ", user._id)
await delay(10000);
getRandomGif(user).then((user) => {
getRandomMovie(user).then((user) => {
updateInterest(user).then((user) => {
getRandomModules(user).then((user) => {
getRandomMusic(user).then((user) => {
getPreferences(user).then((user) => {
getCustumBio(user).then((user) => {
updateUser(user).then((user) => {
console.log("user updated : ", user)
}).catch((error) => {
console.log("error while updating user : ", error);
})
}).catch((error) => {
console.log("error while getting custom bio : ", error);
})
}).catch((error) => {
console.log("error while getting preferences : ", error);
})
}).catch((error) => {
console.log("error while getting random music : ", error);
})
}).catch((error) => {
console.log("error while getting random modules : ", error);
})
}).catch((error) => {
console.log("error while updating interest : ", error);
})
}).catch((error) => {
console.log("error while getting random movie : ", error);
})
}).catch((error) => {
console.log("error while getting random gif : ", error);
})
try {
// user = await getRandomQuestion(user)
user = await getRandomAge(user)
// user = await getRandomGif(user)
// user = await getRandomMovie(user)
user = await updateInterest(user)
user = await getRandomModules(user)
// user = await getRandomMusic(user)
// user = await getPreferences(user)
// user = await getCustumBio(user)
user = await updateUser(user)
console.log("user updated : ", user)

} catch (error) {
console.log(error)
}


}
}
}
Expand Down
60 changes: 58 additions & 2 deletions src/faker/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ const getInterestList = async (user) => {
}
}

const getQuestionsList = async (user) => {
try {
const requestOptions = {
headers: { 'Content-Type': 'application/json' },
method: 'GET'
};
let link = `${process.env.API_LINK}/question`;
const response = await fetch(link, requestOptions)
const dataJson = await response.json();
let status_code = response.status;
if (status_code !== 200) {
throw new Error(dataJson);
}
let questions_list = dataJson;
return questions_list;
}
catch (error) {
throw error;
}
}

const updateInterest = async (user) => {
try {
const interest_list = await getInterestList();
Expand Down Expand Up @@ -274,9 +295,11 @@ const getRandomMusic = async (user) => {

const getRandomModules = async (user) => {
try {
let mainModule = ["gif", "movie", "music"]
let modules = ["gif", "movie", "music", "biographie", "interests", "questions"];
let user_modules = [];
for (let i = 0; i < 4; i++) {
user_modules.push(mainModule[Math.floor(Math.random() * mainModule.length)]);
for (let i = 1; i < 4; i++) {
let module = modules[Math.floor(Math.random() * modules.length)];
if (user_modules.includes(module)) {
i--;
Expand All @@ -302,5 +325,38 @@ const getRandomBoolean = () => {
return Math.random() < 0.5;
}

const getRandomQuestion = async (user) => {
try {
const questions_list = await getQuestionsList();
console.log("questions_list : ", questions_list)
let user_questions = [];
for (let i = 0; i < 3; i++) {
let question = questions_list[Math.floor(Math.random() * questions_list.length)];
if (user_questions.includes(question)) {
i--;
} else {
const answer = fakerFR.lorem.sentence();
user_questions.push({ question: question._id, answer: answer });

}
}
user.questions = user_questions;
console.log("user_questions : ", user_questions)
return user;
}
catch (error) {
throw error;
}
}

const getRandomAge = (user) => {

const age = Math.floor(Math.random() * 30) + 18;
let date = new Date();
date.setFullYear(date.getFullYear() - age);
user.birthday = date;
console.log("user.birthday : ", user.birthday)
return user;
}

module.exports = { getRandomGif, getRandomMovie, updateInterest, getCustumBio, getRandomMusic, getRandomModules, getRandomBoolean, getPreferences }
module.exports = { getRandomAge, getRandomQuestion, getRandomGif, getRandomMovie, updateInterest, getCustumBio, getRandomMusic, getRandomModules, getRandomBoolean, getPreferences }