-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacyDB.js
More file actions
164 lines (134 loc) · 4.5 KB
/
legacyDB.js
File metadata and controls
164 lines (134 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const {MongoClient, ExplainVerbosity} = require('mongodb');
const prompt = require('prompt-sync')({sigint: true});
const email_validator = require('email-validator');
const {phone} = require('phone');
const uri = 'mongodb://127.0.0.1:27017/';
const client = new MongoClient(uri);
// This project is a smiple node project that deals with starting a membership
// Add function for checking if vaild email - done
// Add function for checking if correct phone number -done
// Delete function user - update active status as false
// Update function user
// Plans collection
// Story collection - add, update, delete, list(report)
function choosePlan(){
console.log("Please select a plan:\n b: Basic\n p: Permium");
let plan = prompt(">>>");
while(plan !== 'b' && plan !== 'p'){
console.log("Please enter b or p ");
plan = prompt(">>>");
}
return plan;
}
async function listDatabases(client){
try{
databases_list = await client.db().admin().listDatabases();
console.log("Databases:");
databases_list.databases.forEach(db => console.log(` - ${db.name}`));
}catch(e){
console.log("Something went wrong");
console.error(e);
}
};
async function openDatabase(client){
try{
await client.connect();
console.log("Database is open");
}catch(e){
console.log("Sorry could not open database....exiting program");
console.error(e);
process.exit(0);
}
}
function timeStamp(){
const date_object = new Date();
const day = (`0 ${date_object.getDate()}`).slice(-2);
const month = (`0 ${date_object.getMonth() + 1}`).slice(-1);
const year = date_object.getFullYear();
const hours = date_object.getHours();
const minutes = date_object.getMinutes();
const seconds = date_object.getSeconds();
const date = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
return date
}
async function insertUser(client){
const database_object = client.db("Legacy");
const collection_object = database_object.collection("users");
console.log("Inserting user");
console.log("Enter first name");
let first_name = prompt(">>>");
console.log("Enter last name");
let last_name = prompt(">>>");
console.log("Enter email address")
let email = prompt(">>>");
while (!email_validator.validate(email)){
console.log("Invalid email\n Please enter a vaild email");
email = prompt(">>>")
}
console.log("Enter password");
let password = prompt(">>>");
console.log("Enter Phone Number");
let phone_number = prompt(">>>");
while (!phone(phone_number).isValid){
console.log("Invaild phone number\n Please enter vaild phone number");
phone_number = prompt(">>>")
}
const date = timeStamp();
const plan = choosePlan();
const user_object = {
first_name: first_name,
last_name: last_name,
email: email,
password: password,
phone_number: phone_number,
avatar: "generic",
active: "true",
plan_id: plan,
story_share_link: "generic link",
story_shar_link_password: "generic password",
created_at: date,
updated_at: "null"
};
try{
let result = await collection_object.insertOne(user_object);
console.log( `A document was inserted with the _id: ${result.insertedId}`);
}catch(e){
console.log("Could not add new user");
console.error(e);
}
}
async function closeDatabase(){
try{
await client.close();
console.log("Database closed");
}catch(e){
console.log("Could not close database");
console.error(e);
}
}
async function main() {
let dbopen = true;
console.log("Hi, this is the db fucntionality for the Legacy App!");
while(dbopen == true){
console.log("Please choose from our menu:\n a: Open database\n i: Insert User\n q: Close database");
let choice = prompt(">>>");
if (choice === 'a'){
await openDatabase(client);
dbopen = true;
}
if(choice == 'i'){
// insert user
await insertUser(client);
}
if(choice == 'l'){
// list dbs
await listDatabases(client);
}
if(choice == 'q'){
dbopen = false;
await closeDatabase();
process.exit(0);
}
}
}
main().catch(console.error);