Skip to content

Commit

Permalink
Migration ES6: Artifacts
Browse files Browse the repository at this point in the history
Related OWASP#152
  • Loading branch information
UlisesGascon committed Jan 26, 2020
1 parent a779d68 commit a8ddafd
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions artifacts/db-reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
// before running it (default: development). ie:
// NODE_ENV=production node artifacts/db-reset.js

var _ = require("underscore");
var MongoClient = require("mongodb").MongoClient;
const _ = require("underscore");
const { MongoClient } = require("mongodb");
const { db } = require("../config/config");

var USERS_TO_INSERT = [
const USERS_TO_INSERT = [
{
"_id": 1,
"userName": "admin",
Expand All @@ -25,21 +26,20 @@ var USERS_TO_INSERT = [
"lastName": "Doe",
"benefitStartDate": "2030-01-10",
"password": "User1_123"
// "password" : "$2a$10$RNFhiNmt2TTpVO9cqZElb.LQM9e1mzDoggEHufLjAnAKImc6FNE86",// User1_123
// "password" : "$2a$10$RNFhiNmt2TTpVO9cqZElb.LQM9e1mzDoggEHufLjAnAKImc6FNE86",// User1_123
}, {
"_id": 3,
"userName": "user2",
"firstName": "Will",
"lastName": "Smith",
"benefitStartDate": "2025-11-30",
"password": "User2_123"
//"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123
//"password" : "$2a$10$Tlx2cNv15M0Aia7wyItjsepeA8Y6PyBYaNdQqvpxkIUlcONf1ZHyq", // User2_123
}];

// Getting the global config taking in account he environment (proc)
var config = require("../config/config");

function parseResponse(err, res, comm) {
const parseResponse = (err, res, comm) => {
if (err) {
console.log("ERROR:");
console.log(comm);
Expand All @@ -52,15 +52,13 @@ function parseResponse(err, res, comm) {


// Starting here
MongoClient.connect(config.db, function(err, db) {
var usersCol, allocationsCol, countersCol;

MongoClient.connect(db, (err, db) => {
if (err) {
console.log("ERROR: connect");
console.log(JSON.stringify(err));
process.exit(1);
}
console.log("Connected to the database: " + config.db);
console.log("Connected to the database: " + db);

// remove existing data (if any), we don't want to look for errors here
db.dropCollection("users");
Expand All @@ -69,9 +67,9 @@ MongoClient.connect(config.db, function(err, db) {
db.dropCollection("memos");
db.dropCollection("counters");

usersCol = db.collection("users");
allocationsCol = db.collection("allocations");
countersCol = db.collection("counters");
const usersCol = db.collection("users");
const allocationsCol = db.collection("allocations");
const countersCol = db.collection("counters");

// reset unique id counter
countersCol.insert({
Expand All @@ -81,13 +79,10 @@ MongoClient.connect(config.db, function(err, db) {

// insert admin and test users
console.log("Users to insert:");
USERS_TO_INSERT.forEach(function(user) {
console.log(JSON.stringify(user));
});
USERS_TO_INSERT.forEach((user) => console.log(JSON.stringify(user)));

usersCol.insertMany(USERS_TO_INSERT, function(err, data) {
var finalAllocations = [];
var ids;
usersCol.insertMany(USERS_TO_INSERT, (err, data) => {
const finalAllocations = [];

// We can't continue if error here
if (err) {
Expand All @@ -97,7 +92,7 @@ MongoClient.connect(config.db, function(err, db) {
}
parseResponse(err, data, "users.insertMany");

data.ops.forEach(function(user) {
data.ops.forEach((user) => {
var stocks = Math.floor((Math.random() * 40) + 1);
var funds = Math.floor((Math.random() * 40) + 1);

Expand All @@ -110,11 +105,9 @@ MongoClient.connect(config.db, function(err, db) {
});

console.log("Allocations to insert:");
finalAllocations.forEach(function(allocation) {
console.log(JSON.stringify(allocation));
});
finalAllocations.forEach(allocation => console.log(JSON.stringify(allocation)));

allocationsCol.insertMany(finalAllocations, function(err, data) {
allocationsCol.insertMany(finalAllocations, (err, data) => {
parseResponse(err, data, "allocations.insertMany");
console.log("Database reset performed successfully")
process.exit(0);
Expand Down

0 comments on commit a8ddafd

Please sign in to comment.