From 77ef87d3adee4b88d32c1a21a3f01a212e0bcfad Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 13:40:30 -0400 Subject: [PATCH 01/14] Populate users and tokens in db --- api/db/populate_users.js | 121 +++++++++++++++++++++++++++++++++++++++ api/package-lock.json | 6 ++ api/package.json | 3 +- 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 api/db/populate_users.js diff --git a/api/db/populate_users.js b/api/db/populate_users.js new file mode 100644 index 00000000..7fa653c1 --- /dev/null +++ b/api/db/populate_users.js @@ -0,0 +1,121 @@ +#! /usr/bin/env node + +// TODO: This will all be in the general js to abide by DRY principal +const userArgs = process.argv.slice(2); + +if (!userArgs[0].startsWith('mongodb')) { + console.log( + 'Error: you need to specify a valid MongoDB URL as the first argument' + ); + + return; +} + +const User = require('../models/User'); +const Interest = require('../models/Interest') +const Token = require('../models/Token') +const crypto = require('crypto'); + +const async = require('async'); + +const mongoose = require('mongoose'); +const mongoDB = userArgs[0]; +mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); +mongoose.Promise = global.Promise; +const db = mongoose.connection; +db.on('error', console.error.bind(console, 'MongoDB connection error:')); + +const { uniqueNamesGenerator, names } = require('unique-names-generator'); +const countries = require('country-region-data'); +const locales = ['en', 'es']; +const users = []; + +const createUser = async ( + name, email, emailConfirmed, emailConfirmedAt, + dateOfBirth, country, region, + preferredLocale, interests, admin, + callback +) => { + const user = new User({ + name, email, emailConfirmed, emailConfirmedAt, + dateOfBirth, country, region, + preferredLocale, interests, admin + }); + user.setPassword('Password1'); + await user.save(); + const token = createTokenForUser(user); + await token.save(); + + console.log(`User: ${user}`); + users.push(user); + callback(null, user); +} + +function randomDate(start, end) { + return new Date(+start + Math.random() * (end - start)); +} + +createTokenForUser = (user) => { + return new Token({ + user, + type: 'Email', + token: crypto.randomBytes(16).toString('hex') + }); +} + +const generateUsersArray = async () => { + let interests = await Interest.distinct("_id"); + const config = { + dictionaries: [names] + } + + let arr = [] + for (let i = 0; i < 100; i++) { + const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator(config)}`; + const email = `${name.replace(/ /g, '_')}@test-tisn-email.com`.toLowerCase(); + const country = countries[Math.floor(Math.random() * countries.length)]; + const region = country.regions[Math.floor(Math.random() * country.regions.length)]; + const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); + const birthDate = randomDate(new Date(1950, 01, 01), new Date(2007, 01, 01)); + interests = interests.sort(() => 0.5 - Math.random()); + const selectedInterests = interests.slice(0, Math.floor(Math.random() * 10)); + + + arr.push( + (seriesCallback) => { + createUser( + name, + email, + true, + enrolledDate, + birthDate, + country.countryShortCode, + region.shortCode || 'N/A', + locales[Math.floor(Math.random() * 2)], + selectedInterests, + false, + seriesCallback + ); + }, + ) + } + + return arr +} + +const createUsers = async () => { + async.series( + await generateUsersArray(), + (error, results) => { + if (error) { + console.log(`Final error: ${error}`); + } else { + console.log(`Final results: ${results}`) + } + + mongoose.connection.close(); + } + ) +} + +createUsers(); diff --git a/api/package-lock.json b/api/package-lock.json index 6568799f..a7ece361 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -2767,6 +2767,12 @@ } } }, + "unique-names-generator": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.3.1.tgz", + "integrity": "sha512-oxkKrDXbwx6I5M963SdfmMH2t8n1OIfYyzoJ25BmDeETlyWAZjTbMXwIEMiQDex4lkD2pqe+9gxDJQAB2IOfDg==", + "dev": true + }, "unique-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-2.0.0.tgz", diff --git a/api/package.json b/api/package.json index 9f8d04a3..d23a884e 100644 --- a/api/package.json +++ b/api/package.json @@ -43,7 +43,8 @@ "husky": "^4.3.0", "lint-staged": "^10.4.0", "nodemon": "^2.0.4", - "prettier": "2.1.2" + "prettier": "2.1.2", + "unique-names-generator": "^4.3.1" }, "husky": { "hooks": { From 97a5265d834f238f7b76662fb9d1f713ccbf74cd Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 13:44:11 -0400 Subject: [PATCH 02/14] Populate users and tokens in db --- api/db/populate_users.js | 121 --------------------------------------- api/package-lock.json | 6 -- api/package.json | 3 +- 3 files changed, 1 insertion(+), 129 deletions(-) delete mode 100644 api/db/populate_users.js diff --git a/api/db/populate_users.js b/api/db/populate_users.js deleted file mode 100644 index 7fa653c1..00000000 --- a/api/db/populate_users.js +++ /dev/null @@ -1,121 +0,0 @@ -#! /usr/bin/env node - -// TODO: This will all be in the general js to abide by DRY principal -const userArgs = process.argv.slice(2); - -if (!userArgs[0].startsWith('mongodb')) { - console.log( - 'Error: you need to specify a valid MongoDB URL as the first argument' - ); - - return; -} - -const User = require('../models/User'); -const Interest = require('../models/Interest') -const Token = require('../models/Token') -const crypto = require('crypto'); - -const async = require('async'); - -const mongoose = require('mongoose'); -const mongoDB = userArgs[0]; -mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); -mongoose.Promise = global.Promise; -const db = mongoose.connection; -db.on('error', console.error.bind(console, 'MongoDB connection error:')); - -const { uniqueNamesGenerator, names } = require('unique-names-generator'); -const countries = require('country-region-data'); -const locales = ['en', 'es']; -const users = []; - -const createUser = async ( - name, email, emailConfirmed, emailConfirmedAt, - dateOfBirth, country, region, - preferredLocale, interests, admin, - callback -) => { - const user = new User({ - name, email, emailConfirmed, emailConfirmedAt, - dateOfBirth, country, region, - preferredLocale, interests, admin - }); - user.setPassword('Password1'); - await user.save(); - const token = createTokenForUser(user); - await token.save(); - - console.log(`User: ${user}`); - users.push(user); - callback(null, user); -} - -function randomDate(start, end) { - return new Date(+start + Math.random() * (end - start)); -} - -createTokenForUser = (user) => { - return new Token({ - user, - type: 'Email', - token: crypto.randomBytes(16).toString('hex') - }); -} - -const generateUsersArray = async () => { - let interests = await Interest.distinct("_id"); - const config = { - dictionaries: [names] - } - - let arr = [] - for (let i = 0; i < 100; i++) { - const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator(config)}`; - const email = `${name.replace(/ /g, '_')}@test-tisn-email.com`.toLowerCase(); - const country = countries[Math.floor(Math.random() * countries.length)]; - const region = country.regions[Math.floor(Math.random() * country.regions.length)]; - const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); - const birthDate = randomDate(new Date(1950, 01, 01), new Date(2007, 01, 01)); - interests = interests.sort(() => 0.5 - Math.random()); - const selectedInterests = interests.slice(0, Math.floor(Math.random() * 10)); - - - arr.push( - (seriesCallback) => { - createUser( - name, - email, - true, - enrolledDate, - birthDate, - country.countryShortCode, - region.shortCode || 'N/A', - locales[Math.floor(Math.random() * 2)], - selectedInterests, - false, - seriesCallback - ); - }, - ) - } - - return arr -} - -const createUsers = async () => { - async.series( - await generateUsersArray(), - (error, results) => { - if (error) { - console.log(`Final error: ${error}`); - } else { - console.log(`Final results: ${results}`) - } - - mongoose.connection.close(); - } - ) -} - -createUsers(); diff --git a/api/package-lock.json b/api/package-lock.json index a7ece361..6568799f 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -2767,12 +2767,6 @@ } } }, - "unique-names-generator": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.3.1.tgz", - "integrity": "sha512-oxkKrDXbwx6I5M963SdfmMH2t8n1OIfYyzoJ25BmDeETlyWAZjTbMXwIEMiQDex4lkD2pqe+9gxDJQAB2IOfDg==", - "dev": true - }, "unique-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-2.0.0.tgz", diff --git a/api/package.json b/api/package.json index d23a884e..9f8d04a3 100644 --- a/api/package.json +++ b/api/package.json @@ -43,8 +43,7 @@ "husky": "^4.3.0", "lint-staged": "^10.4.0", "nodemon": "^2.0.4", - "prettier": "2.1.2", - "unique-names-generator": "^4.3.1" + "prettier": "2.1.2" }, "husky": { "hooks": { From 0096906f0a9d3319da2080eee5b99b9676449160 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 14:15:22 -0400 Subject: [PATCH 03/14] Populate users and tokens in db --- api/db/populate_users.js | 121 +++++++++++++++++++++++++++++++++++++++ api/package-lock.json | 6 ++ api/package.json | 3 +- 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 api/db/populate_users.js diff --git a/api/db/populate_users.js b/api/db/populate_users.js new file mode 100644 index 00000000..7fa653c1 --- /dev/null +++ b/api/db/populate_users.js @@ -0,0 +1,121 @@ +#! /usr/bin/env node + +// TODO: This will all be in the general js to abide by DRY principal +const userArgs = process.argv.slice(2); + +if (!userArgs[0].startsWith('mongodb')) { + console.log( + 'Error: you need to specify a valid MongoDB URL as the first argument' + ); + + return; +} + +const User = require('../models/User'); +const Interest = require('../models/Interest') +const Token = require('../models/Token') +const crypto = require('crypto'); + +const async = require('async'); + +const mongoose = require('mongoose'); +const mongoDB = userArgs[0]; +mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); +mongoose.Promise = global.Promise; +const db = mongoose.connection; +db.on('error', console.error.bind(console, 'MongoDB connection error:')); + +const { uniqueNamesGenerator, names } = require('unique-names-generator'); +const countries = require('country-region-data'); +const locales = ['en', 'es']; +const users = []; + +const createUser = async ( + name, email, emailConfirmed, emailConfirmedAt, + dateOfBirth, country, region, + preferredLocale, interests, admin, + callback +) => { + const user = new User({ + name, email, emailConfirmed, emailConfirmedAt, + dateOfBirth, country, region, + preferredLocale, interests, admin + }); + user.setPassword('Password1'); + await user.save(); + const token = createTokenForUser(user); + await token.save(); + + console.log(`User: ${user}`); + users.push(user); + callback(null, user); +} + +function randomDate(start, end) { + return new Date(+start + Math.random() * (end - start)); +} + +createTokenForUser = (user) => { + return new Token({ + user, + type: 'Email', + token: crypto.randomBytes(16).toString('hex') + }); +} + +const generateUsersArray = async () => { + let interests = await Interest.distinct("_id"); + const config = { + dictionaries: [names] + } + + let arr = [] + for (let i = 0; i < 100; i++) { + const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator(config)}`; + const email = `${name.replace(/ /g, '_')}@test-tisn-email.com`.toLowerCase(); + const country = countries[Math.floor(Math.random() * countries.length)]; + const region = country.regions[Math.floor(Math.random() * country.regions.length)]; + const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); + const birthDate = randomDate(new Date(1950, 01, 01), new Date(2007, 01, 01)); + interests = interests.sort(() => 0.5 - Math.random()); + const selectedInterests = interests.slice(0, Math.floor(Math.random() * 10)); + + + arr.push( + (seriesCallback) => { + createUser( + name, + email, + true, + enrolledDate, + birthDate, + country.countryShortCode, + region.shortCode || 'N/A', + locales[Math.floor(Math.random() * 2)], + selectedInterests, + false, + seriesCallback + ); + }, + ) + } + + return arr +} + +const createUsers = async () => { + async.series( + await generateUsersArray(), + (error, results) => { + if (error) { + console.log(`Final error: ${error}`); + } else { + console.log(`Final results: ${results}`) + } + + mongoose.connection.close(); + } + ) +} + +createUsers(); diff --git a/api/package-lock.json b/api/package-lock.json index 6568799f..a7ece361 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -2767,6 +2767,12 @@ } } }, + "unique-names-generator": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.3.1.tgz", + "integrity": "sha512-oxkKrDXbwx6I5M963SdfmMH2t8n1OIfYyzoJ25BmDeETlyWAZjTbMXwIEMiQDex4lkD2pqe+9gxDJQAB2IOfDg==", + "dev": true + }, "unique-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-2.0.0.tgz", diff --git a/api/package.json b/api/package.json index 9f8d04a3..d23a884e 100644 --- a/api/package.json +++ b/api/package.json @@ -43,7 +43,8 @@ "husky": "^4.3.0", "lint-staged": "^10.4.0", "nodemon": "^2.0.4", - "prettier": "2.1.2" + "prettier": "2.1.2", + "unique-names-generator": "^4.3.1" }, "husky": { "hooks": { From cf73eedb4bc133fdaefc9b23141bf0713424bfc6 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 20:41:56 -0400 Subject: [PATCH 04/14] Ran prettier to format files and renamed populate files --- api/db/{populate.js => populate-interests.js} | 0 api/db/populate-users.js | 141 ++++++++++++++++++ api/db/populate_users.js | 121 --------------- 3 files changed, 141 insertions(+), 121 deletions(-) rename api/db/{populate.js => populate-interests.js} (100%) create mode 100644 api/db/populate-users.js delete mode 100644 api/db/populate_users.js diff --git a/api/db/populate.js b/api/db/populate-interests.js similarity index 100% rename from api/db/populate.js rename to api/db/populate-interests.js diff --git a/api/db/populate-users.js b/api/db/populate-users.js new file mode 100644 index 00000000..59b1473a --- /dev/null +++ b/api/db/populate-users.js @@ -0,0 +1,141 @@ +#! /usr/bin/env node + +// TODO: This will all be in the general js to abide by DRY principal +const userArgs = process.argv.slice(2); + +if (!userArgs[0].startsWith("mongodb")) { + console.log( + "Error: you need to specify a valid MongoDB URL as the first argument" + ); + + return; +} + +const User = require("../models/User"); +const Interest = require("../models/Interest"); +const Token = require("../models/Token"); +const crypto = require("crypto"); + +const async = require("async"); + +const mongoose = require("mongoose"); +const mongoDB = userArgs[0]; +mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); +mongoose.Promise = global.Promise; +const db = mongoose.connection; +db.on("error", console.error.bind(console, "MongoDB connection error:")); + +const { uniqueNamesGenerator, names } = require("unique-names-generator"); +const countries = require("country-region-data"); +const locales = ["en", "es"]; +const users = []; + +const createUser = async ( + name, + email, + emailConfirmed, + emailConfirmedAt, + dateOfBirth, + country, + region, + preferredLocale, + interests, + admin, + callback +) => { + const user = new User({ + name, + email, + emailConfirmed, + emailConfirmedAt, + dateOfBirth, + country, + region, + preferredLocale, + interests, + admin, + }); + user.setPassword("Password1"); + await user.save(); + const token = createTokenForUser(user); + await token.save(); + + console.log(`User: ${user}`); + users.push(user); + callback(null, user); +}; + +function randomDate(start, end) { + return new Date(+start + Math.random() * (end - start)); +} + +createTokenForUser = (user) => { + return new Token({ + user, + type: "Email", + token: crypto.randomBytes(16).toString("hex"), + }); +}; + +const generateUsersArray = async () => { + let interests = await Interest.distinct("_id"); + const config = { + dictionaries: [names], + }; + + let arr = []; + for (let i = 0; i < 100; i++) { + const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator( + config + )}`; + const email = `${name.replace( + / /g, + "_" + )}@test-tisn-email.com`.toLowerCase(); + const country = countries[Math.floor(Math.random() * countries.length)]; + const region = + country.regions[Math.floor(Math.random() * country.regions.length)]; + const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); + const birthDate = randomDate( + new Date(1950, 01, 01), + new Date(2007, 01, 01) + ); + interests = interests.sort(() => 0.5 - Math.random()); + const selectedInterests = interests.slice( + 0, + Math.floor(Math.random() * 10) + ); + + arr.push((seriesCallback) => { + createUser( + name, + email, + true, + enrolledDate, + birthDate, + country.countryShortCode, + region.shortCode || "N/A", + locales[Math.floor(Math.random() * 2)], + selectedInterests, + false, + seriesCallback + ); + }); + } + + return arr; +}; + +const createUsers = async () => { + async.series(await generateUsersArray(), (error, results) => { + if (error) { + console.log(`Final error: ${error}`); + } else { + console.log(`Final results: ${results}`); + } + + mongoose.connection.close(); + }); +}; + +createUsers(); diff --git a/api/db/populate_users.js b/api/db/populate_users.js deleted file mode 100644 index 7fa653c1..00000000 --- a/api/db/populate_users.js +++ /dev/null @@ -1,121 +0,0 @@ -#! /usr/bin/env node - -// TODO: This will all be in the general js to abide by DRY principal -const userArgs = process.argv.slice(2); - -if (!userArgs[0].startsWith('mongodb')) { - console.log( - 'Error: you need to specify a valid MongoDB URL as the first argument' - ); - - return; -} - -const User = require('../models/User'); -const Interest = require('../models/Interest') -const Token = require('../models/Token') -const crypto = require('crypto'); - -const async = require('async'); - -const mongoose = require('mongoose'); -const mongoDB = userArgs[0]; -mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); -mongoose.Promise = global.Promise; -const db = mongoose.connection; -db.on('error', console.error.bind(console, 'MongoDB connection error:')); - -const { uniqueNamesGenerator, names } = require('unique-names-generator'); -const countries = require('country-region-data'); -const locales = ['en', 'es']; -const users = []; - -const createUser = async ( - name, email, emailConfirmed, emailConfirmedAt, - dateOfBirth, country, region, - preferredLocale, interests, admin, - callback -) => { - const user = new User({ - name, email, emailConfirmed, emailConfirmedAt, - dateOfBirth, country, region, - preferredLocale, interests, admin - }); - user.setPassword('Password1'); - await user.save(); - const token = createTokenForUser(user); - await token.save(); - - console.log(`User: ${user}`); - users.push(user); - callback(null, user); -} - -function randomDate(start, end) { - return new Date(+start + Math.random() * (end - start)); -} - -createTokenForUser = (user) => { - return new Token({ - user, - type: 'Email', - token: crypto.randomBytes(16).toString('hex') - }); -} - -const generateUsersArray = async () => { - let interests = await Interest.distinct("_id"); - const config = { - dictionaries: [names] - } - - let arr = [] - for (let i = 0; i < 100; i++) { - const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator(config)}`; - const email = `${name.replace(/ /g, '_')}@test-tisn-email.com`.toLowerCase(); - const country = countries[Math.floor(Math.random() * countries.length)]; - const region = country.regions[Math.floor(Math.random() * country.regions.length)]; - const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); - const birthDate = randomDate(new Date(1950, 01, 01), new Date(2007, 01, 01)); - interests = interests.sort(() => 0.5 - Math.random()); - const selectedInterests = interests.slice(0, Math.floor(Math.random() * 10)); - - - arr.push( - (seriesCallback) => { - createUser( - name, - email, - true, - enrolledDate, - birthDate, - country.countryShortCode, - region.shortCode || 'N/A', - locales[Math.floor(Math.random() * 2)], - selectedInterests, - false, - seriesCallback - ); - }, - ) - } - - return arr -} - -const createUsers = async () => { - async.series( - await generateUsersArray(), - (error, results) => { - if (error) { - console.log(`Final error: ${error}`); - } else { - console.log(`Final results: ${results}`) - } - - mongoose.connection.close(); - } - ) -} - -createUsers(); From e62eaf829ce3554eab82b9e0d74ece3a85bca186 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 20:53:47 -0400 Subject: [PATCH 05/14] Renamed constant async to asynchronous --- api/db/populate-users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 59b1473a..f846f7b5 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -16,7 +16,7 @@ const Interest = require("../models/Interest"); const Token = require("../models/Token"); const crypto = require("crypto"); -const async = require("async"); +const asynchronous = require("async"); const mongoose = require("mongoose"); const mongoDB = userArgs[0]; @@ -127,7 +127,7 @@ const generateUsersArray = async () => { }; const createUsers = async () => { - async.series(await generateUsersArray(), (error, results) => { + asynchronous.series(await generateUsersArray(), (error, results) => { if (error) { console.log(`Final error: ${error}`); } else { From 0b99b011f2cd3c7b44fa13005203521b39fdfc65 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 21:02:07 -0400 Subject: [PATCH 06/14] Changed password and logs label --- api/db/populate-users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index f846f7b5..1832f9cc 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -55,12 +55,12 @@ const createUser = async ( interests, admin, }); - user.setPassword("Password1"); + user.setPassword("password"); await user.save(); const token = createTokenForUser(user); await token.save(); - console.log(`User: ${user}`); + console.log(`New User: ${user}`); users.push(user); callback(null, user); }; From eead31715159260fc6ffb8dc5286dbd1fe76e1e1 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 21:04:43 -0400 Subject: [PATCH 07/14] Renamed arr to usersArray for clarity and changed to const --- api/db/populate-users.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 1832f9cc..6062b45b 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -83,7 +83,7 @@ const generateUsersArray = async () => { dictionaries: [names], }; - let arr = []; + const usersArray = []; for (let i = 0; i < 100; i++) { const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator( config @@ -106,7 +106,7 @@ const generateUsersArray = async () => { Math.floor(Math.random() * 10) ); - arr.push((seriesCallback) => { + usersArray.push((seriesCallback) => { createUser( name, email, @@ -123,7 +123,7 @@ const generateUsersArray = async () => { }); } - return arr; + return usersArray; }; const createUsers = async () => { From 66a6282efa286b5551499a18c4efe2ca200278e8 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 21:07:41 -0400 Subject: [PATCH 08/14] Refactored random date method for clarity --- api/db/populate-users.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 6062b45b..dfb74bd0 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -65,9 +65,8 @@ const createUser = async ( callback(null, user); }; -function randomDate(start, end) { - return new Date(+start + Math.random() * (end - start)); -} +const randomDate = (startDate, endDate) => + new Date(+startDate + Math.random() * (endDate - startDate)); createTokenForUser = (user) => { return new Token({ From 286e9e257dee616f94160bfc78cc8fff4c09b77e Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Thu, 1 Oct 2020 21:13:32 -0400 Subject: [PATCH 09/14] Removed unnecessary token creation --- api/db/populate-users.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index dfb74bd0..ee4b9d20 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -13,8 +13,6 @@ if (!userArgs[0].startsWith("mongodb")) { const User = require("../models/User"); const Interest = require("../models/Interest"); -const Token = require("../models/Token"); -const crypto = require("crypto"); const asynchronous = require("async"); @@ -57,8 +55,6 @@ const createUser = async ( }); user.setPassword("password"); await user.save(); - const token = createTokenForUser(user); - await token.save(); console.log(`New User: ${user}`); users.push(user); @@ -68,14 +64,6 @@ const createUser = async ( const randomDate = (startDate, endDate) => new Date(+startDate + Math.random() * (endDate - startDate)); -createTokenForUser = (user) => { - return new Token({ - user, - type: "Email", - token: crypto.randomBytes(16).toString("hex"), - }); -}; - const generateUsersArray = async () => { let interests = await Interest.distinct("_id"); const config = { From fb2c92b16ff4c1a51a2f500d888f406bd3647f55 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Fri, 2 Oct 2020 10:58:51 -0400 Subject: [PATCH 10/14] Refactoring and add number of records argument --- api/db/populate-users.js | 50 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index ee4b9d20..74294bba 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -18,7 +18,12 @@ const asynchronous = require("async"); const mongoose = require("mongoose"); const mongoDB = userArgs[0]; -mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); +const numberOfRecords = userArgs[1] || 100; +mongoose.connect(mongoDB, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, +}); mongoose.Promise = global.Promise; const db = mongoose.connection; db.on("error", console.error.bind(console, "MongoDB connection error:")); @@ -61,50 +66,43 @@ const createUser = async ( callback(null, user); }; -const randomDate = (startDate, endDate) => +const getRandomDate = (startDate, endDate) => new Date(+startDate + Math.random() * (endDate - startDate)); const generateUsersArray = async () => { let interests = await Interest.distinct("_id"); - const config = { - dictionaries: [names], - }; + const now = new Date(); const usersArray = []; - for (let i = 0; i < 100; i++) { - const name = `${uniqueNamesGenerator(config)} ${uniqueNamesGenerator( - config - )}`; - const email = `${name.replace( - / /g, - "_" - )}@test-tisn-email.com`.toLowerCase(); + for (let i = 0; i < numberOfRecords; i++) { + const name = uniqueNamesGenerator({ + dictionaries: [names, names], + separator: ' ' + }) + const email = `${name.replace(/ /g, "_")}@tisn.app`.toLowerCase(); + const emailConfirmed = true; + const emailConfirmedAt = getRandomDate(new Date(2020, 05, 05), now); const country = countries[Math.floor(Math.random() * countries.length)]; const region = country.regions[Math.floor(Math.random() * country.regions.length)]; - const enrolledDate = randomDate(new Date(2019, 01, 01), new Date()); - const birthDate = randomDate( - new Date(1950, 01, 01), - new Date(2007, 01, 01) - ); - interests = interests.sort(() => 0.5 - Math.random()); - const selectedInterests = interests.slice( - 0, - Math.floor(Math.random() * 10) - ); + const birthDate = getRandomDate(new Date(1970, 01, 01), new Date().setFullYear(now.getFullYear() - 13)); + const selectedInterests = interests + .sort(() => 0.5 - Math.random()) + .slice(0, Math.floor(Math.random() * interests.length)); + const admin = false; usersArray.push((seriesCallback) => { createUser( name, email, - true, - enrolledDate, + emailConfirmed, + emailConfirmedAt, birthDate, country.countryShortCode, region.shortCode || "N/A", locales[Math.floor(Math.random() * 2)], selectedInterests, - false, + admin, seriesCallback ); }); From 18614f9c62ea93d83839e40cde2f6a04512aa649 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Fri, 2 Oct 2020 20:34:39 -0400 Subject: [PATCH 11/14] Resolved majority of issues discussed --- api/db/populate-interests.js | 6 ++- api/db/populate-users.js | 101 +++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/api/db/populate-interests.js b/api/db/populate-interests.js index c004009c..bce2b98e 100644 --- a/api/db/populate-interests.js +++ b/api/db/populate-interests.js @@ -16,7 +16,11 @@ const async = require('async'); const mongoose = require('mongoose'); const mongoDB = userArgs[0]; -mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); +mongoose.connect(mongoDB, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, +}); mongoose.Promise = global.Promise; const db = mongoose.connection; db.on('error', console.error.bind(console, 'MongoDB connection error:')); diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 74294bba..579e9b96 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -33,30 +33,18 @@ const countries = require("country-region-data"); const locales = ["en", "es"]; const users = []; -const createUser = async ( - name, - email, - emailConfirmed, - emailConfirmedAt, - dateOfBirth, - country, - region, - preferredLocale, - interests, - admin, - callback -) => { +const createUser = async (userParams, callback) => { const user = new User({ - name, - email, - emailConfirmed, - emailConfirmedAt, - dateOfBirth, - country, - region, - preferredLocale, - interests, - admin, + name: userParams.name, + email: userParams.email, + emailConfirmed: userParams.emailConfirmed, + emailConfirmedAt: userParams.emailConfirmedAt, + dateOfBirth: userParams.dateOfBirth, + country: userParams.country, + region: userParams.region, + preferredLocale: userParams.preferredLocale, + interests: userParams.interests, + admin: userParams.admin, }); user.setPassword("password"); await user.save(); @@ -69,42 +57,61 @@ const createUser = async ( const getRandomDate = (startDate, endDate) => new Date(+startDate + Math.random() * (endDate - startDate)); +const createAdminUser = () => (seriesCallback) => { + createUser( + { + name: "Admin", + email: `admin@tisn.app`, + emailConfirmed: true, + emailConfirmedAt: new Date(), + country: "US", + region: "FL", + preferredLocale: "en", + dateOfBirth: new Date(), + interests: [], + admin: true, + }, + seriesCallback + ); +}; + const generateUsersArray = async () => { - let interests = await Interest.distinct("_id"); + let interestsList = await Interest.distinct("_id"); const now = new Date(); const usersArray = []; + + usersArray.push(createAdminUser()); + for (let i = 0; i < numberOfRecords; i++) { const name = uniqueNamesGenerator({ dictionaries: [names, names], - separator: ' ' - }) - const email = `${name.replace(/ /g, "_")}@tisn.app`.toLowerCase(); - const emailConfirmed = true; - const emailConfirmedAt = getRandomDate(new Date(2020, 05, 05), now); + separator: " ", + }); const country = countries[Math.floor(Math.random() * countries.length)]; const region = country.regions[Math.floor(Math.random() * country.regions.length)]; - const birthDate = getRandomDate(new Date(1970, 01, 01), new Date().setFullYear(now.getFullYear() - 13)); - const selectedInterests = interests - .sort(() => 0.5 - Math.random()) - .slice(0, Math.floor(Math.random() * interests.length)); - const admin = false; + + const userParams = { + name, + email: `${name.replace(/ /g, "_")}@tisn.app`.toLowerCase(), + emailConfirmed: true, + emailConfirmedAt: getRandomDate(new Date(2020, 05, 05), now), + country: country.countryShortCode, + region: region.shortCode || "N/A", + preferredLocale: locales[Math.floor(Math.random() * 2)], + dateOfBirth: getRandomDate( + new Date(1970, 01, 01), + new Date().setFullYear(now.getFullYear() - 13) + ), + interests: interestsList + .sort(() => 0.5 - Math.random()) + .slice(0, Math.floor(Math.random() * interestsList.length)), + admin: false, + }; usersArray.push((seriesCallback) => { - createUser( - name, - email, - emailConfirmed, - emailConfirmedAt, - birthDate, - country.countryShortCode, - region.shortCode || "N/A", - locales[Math.floor(Math.random() * 2)], - selectedInterests, - admin, - seriesCallback - ); + createUser(userParams, seriesCallback); }); } From 460a4daf54701d2fc8b7f4420462a7cb9e7efcb0 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Sat, 3 Oct 2020 15:28:29 -0400 Subject: [PATCH 12/14] Reformatted with prettier --- api/db/populate-users.js | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 579e9b96..97aba5f8 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -3,20 +3,20 @@ // TODO: This will all be in the general js to abide by DRY principal const userArgs = process.argv.slice(2); -if (!userArgs[0].startsWith("mongodb")) { +if (!userArgs[0].startsWith('mongodb')) { console.log( - "Error: you need to specify a valid MongoDB URL as the first argument" + 'Error: you need to specify a valid MongoDB URL as the first argument' ); return; -} +}; -const User = require("../models/User"); -const Interest = require("../models/Interest"); +const User = require('../models/User'); +const Interest = require('../models/Interest'); -const asynchronous = require("async"); +const asynchronous = require('async'); -const mongoose = require("mongoose"); +const mongoose = require('mongoose'); const mongoDB = userArgs[0]; const numberOfRecords = userArgs[1] || 100; mongoose.connect(mongoDB, { @@ -26,11 +26,11 @@ mongoose.connect(mongoDB, { }); mongoose.Promise = global.Promise; const db = mongoose.connection; -db.on("error", console.error.bind(console, "MongoDB connection error:")); +db.on('error', console.error.bind(console, 'MongoDB connection error:')); -const { uniqueNamesGenerator, names } = require("unique-names-generator"); -const countries = require("country-region-data"); -const locales = ["en", "es"]; +const { uniqueNamesGenerator, names } = require('unique-names-generator'); +const countries = require('country-region-data'); +const locales = ['en', 'es']; const users = []; const createUser = async (userParams, callback) => { @@ -46,7 +46,7 @@ const createUser = async (userParams, callback) => { interests: userParams.interests, admin: userParams.admin, }); - user.setPassword("password"); + user.setPassword('password'); await user.save(); console.log(`New User: ${user}`); @@ -60,13 +60,13 @@ const getRandomDate = (startDate, endDate) => const createAdminUser = () => (seriesCallback) => { createUser( { - name: "Admin", + name: 'Admin', email: `admin@tisn.app`, emailConfirmed: true, emailConfirmedAt: new Date(), - country: "US", - region: "FL", - preferredLocale: "en", + country: 'US', + region: 'FL', + preferredLocale: 'en', dateOfBirth: new Date(), interests: [], admin: true, @@ -76,7 +76,7 @@ const createAdminUser = () => (seriesCallback) => { }; const generateUsersArray = async () => { - let interestsList = await Interest.distinct("_id"); + let interestsList = await Interest.distinct('_id'); const now = new Date(); const usersArray = []; @@ -86,7 +86,7 @@ const generateUsersArray = async () => { for (let i = 0; i < numberOfRecords; i++) { const name = uniqueNamesGenerator({ dictionaries: [names, names], - separator: " ", + separator: ' ', }); const country = countries[Math.floor(Math.random() * countries.length)]; const region = @@ -94,11 +94,11 @@ const generateUsersArray = async () => { const userParams = { name, - email: `${name.replace(/ /g, "_")}@tisn.app`.toLowerCase(), + email: `${name.replace(/ /g, '_')}@tisn.app`.toLowerCase(), emailConfirmed: true, emailConfirmedAt: getRandomDate(new Date(2020, 05, 05), now), country: country.countryShortCode, - region: region.shortCode || "N/A", + region: region.shortCode || 'N/A', preferredLocale: locales[Math.floor(Math.random() * 2)], dateOfBirth: getRandomDate( new Date(1970, 01, 01), @@ -113,7 +113,7 @@ const generateUsersArray = async () => { usersArray.push((seriesCallback) => { createUser(userParams, seriesCallback); }); - } + }; return usersArray; }; From 3242e9ce93d8b847c668c95ac1f2956b0a57bc65 Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Sat, 3 Oct 2020 15:33:57 -0400 Subject: [PATCH 13/14] Resolved some minor issues --- api/db/populate-users.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 97aba5f8..55154571 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -18,7 +18,6 @@ const asynchronous = require('async'); const mongoose = require('mongoose'); const mongoDB = userArgs[0]; -const numberOfRecords = userArgs[1] || 100; mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, @@ -28,6 +27,7 @@ mongoose.Promise = global.Promise; const db = mongoose.connection; db.on('error', console.error.bind(console, 'MongoDB connection error:')); +const numberOfRecords = userArgs[1] || 100; const { uniqueNamesGenerator, names } = require('unique-names-generator'); const countries = require('country-region-data'); const locales = ['en', 'es']; @@ -67,7 +67,7 @@ const createAdminUser = () => (seriesCallback) => { country: 'US', region: 'FL', preferredLocale: 'en', - dateOfBirth: new Date(), + dateOfBirth: new Date(2000, 01, 01), interests: [], admin: true, }, From bb81cdeee6e54a1cbd4fbb0aed1ee9046cebc9bf Mon Sep 17 00:00:00 2001 From: Jacob Simpson Date: Sat, 3 Oct 2020 15:36:28 -0400 Subject: [PATCH 14/14] Removed the todo and fixed some further prettier linting erros --- api/db/populate-users.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/db/populate-users.js b/api/db/populate-users.js index 55154571..0042e8fa 100644 --- a/api/db/populate-users.js +++ b/api/db/populate-users.js @@ -1,6 +1,5 @@ #! /usr/bin/env node -// TODO: This will all be in the general js to abide by DRY principal const userArgs = process.argv.slice(2); if (!userArgs[0].startsWith('mongodb')) { @@ -9,7 +8,7 @@ if (!userArgs[0].startsWith('mongodb')) { ); return; -}; +} const User = require('../models/User'); const Interest = require('../models/Interest'); @@ -113,7 +112,7 @@ const generateUsersArray = async () => { usersArray.push((seriesCallback) => { createUser(userParams, seriesCallback); }); - }; + } return usersArray; };