-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_prof.js
More file actions
86 lines (51 loc) · 2.09 KB
/
update_prof.js
File metadata and controls
86 lines (51 loc) · 2.09 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
require('dotenv').config();
var crypto = require("crypto");
const mongoose = require("mongoose");
// const { path } = require('./schema/user_detail');
const path = require('path');
let link = process.env.DB_LINK;
// var json_data;
var user_detail_schema = require("./schema/user_detail");
function connect_to_db() {
mongoose.connect(link, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }).catch(error => { });
}
async function update_profile(json_data) {
let result;
let model = mongoose.models["user_detail"] === undefined ? mongoose.model("user_detail", user_detail_schema) : mongoose.model("user_detail");
result = await model.findOne({ email: json_data.email, token: json_data.token, u_id: json_data.u_id });
if (result == null || result.account_status != "active") {
return { status: "error", message: "Not a valid user" }
}
let response = { data: [] };
let result2;
if(json_data.is_file==1){
let new_name ;
let file_ext = path.extname(json_data.file_name)
while (true) {
new_name = "pi" + crypto.randomBytes(10).toString('hex') + file_ext;
result2 = await model.findOne({ profile_img:new_name });
if (result2 == null) {
break;
}
}
result2= await model.updateOne({u_id:json_data.u_id},{ $set:{profile_img:new_name ,pro_mess:json_data.pro_mess,account_type:json_data.account_type}});
if(result2.nModified==1){
return {status:"ok",curr_file_name: new_name ,prev_file_name:result.profile_img }
}
}else{
//only save update message and accouunt type
result2= await model.updateOne({u_id:json_data.u_id},{ $set:{pro_mess:json_data.pro_mess,account_type:json_data.account_type}});
if(result2.nModified==1){
return {status:"ok" }
}
}
return {status:"ok",}
}
async function main(data) {
connect_to_db();
let result;
result = await update_profile(data);
// mongoose.connection.close();
return result;
}
module.exports = main;