-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccept_friend_request.js
202 lines (124 loc) · 5.82 KB
/
accept_friend_request.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require('dotenv').config();
const { json } = require('express');
const mongoose = require("mongoose");
let link = process.env.DB_LINK;
var profile_schema = require("./schema/profile");
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 add_to_friend_list(json_data) {
let result;
let result2;
let model0 = mongoose.models["user_detail"] === undefined ? mongoose.model("user_detail", user_detail_schema) : mongoose.model("user_detail");
// pr("Finding data is; ", { email: json_data.email, token: json_data.token, u_id: json_data.u_id });
result = await model0.findOne({ email: json_data.email, token: json_data.token, u_id: json_data.u_id });
// pr("reslut of model 0 is checking: ", result);
if (result == null || result.account_status != "active") {
return { status: "error", message: "Not a valid user" }
}
//find u_id of sender
if(json_data.signal==1){
result2 = await model0.findOne({p_id: json_data.friend_p_id });
// pr("reslut of model 0 is: ", result);
if(!result2){
return {status: "error",message:"Friend Not Exists"};
}
json_data.friend_u_id = result2.u_id;
//notify friend that you have accepted request
let result3 = await model0.updateOne({u_id:result2.u_id} , {$push:{
notification: {message: json_data.name + " Accepted your Friend Request ",
"current_time":json_data.time,
img:result.profile_img,
sender_name:json_data.name ,
pro_mess:result.pro_mess,
"date":json_data.date,
direction:"ser"
}
}} );
// pull that friend from request list ;
// /pz9010673a097de53c9811
let result4 = await model0.updateOne({u_id:json_data.u_id} , {"$pull":{
friend_request: { sender_p_id:json_data.friend_p_id}} } );
// pr("result 4 is: ",result4);
// pr("result2 is: ",result2) ;
//add sender p_id to your friendlist [p_id]
let result5 = await model0.updateOne({u_id:json_data.u_id} , {"$push":{
friend_list: { sender_p_id:json_data.friend_p_id}} } );
// also add to friend list[ p_id]
let result6 = await model0.updateOne({u_id:result2.u_id} , {"$push":{
friend_list: { sender_p_id:json_data.p_id}} } );
// also remove from friend's sender request ;
let result7 = await model0.updateOne({u_id:result2.u_id} , {"$pull":{
sended_request: { sender_p_id:json_data.p_id}} } );
//add yourself to your friend collection
let model1 = mongoose.models[json_data.friend_u_id] === undefined ? mongoose.model (json_data.friend_u_id,profile_schema) : mongoose.model (json_data.friend_u_id);
//check if you are added to your friend list or not
result = await model1.findOne({ friend_u_id: json_data.u_id} );
// if No then add yourself to your friendl list
if (!result) {
let document1 = new model1 ( {
friend_name:json_data.name,
friend_u_id: json_data.u_id,
chat_message:[
{message: json_data.name + " Accepted your Friend Request ",
"current_time":json_data.time,
"date":json_data.date,
direction:"ser"
}],
is_blocked:false
})
try {
// pr("documetn is: ", document1);
result = await document1.save();
// console.log("result of save is; ");
} catch (error) {
// console.log((error))
return {status: "error", message: "something went wrong " };
}
//
}
else {
// return{ status: "ok", message: "Already added to friend list "};
}
// also add your friend to your collection
let model2 = mongoose.models[json_data.u_id] === undefined ? mongoose.model (json_data.u_id,profile_schema) : mongoose.model (json_data.u_id);
let document2 = new model2 ( {
friend_u_id:json_data.friend_u_id,
friend_name:result2.name,
chat_message: [
{message: result2.name + " Now Added as your Friend ",
"current_time":json_data.time,
"date":json_data.date,
direction:"ser"
}],
is_blocked:false
})
try {
// pr("documetn is: ", document2);
result = await document2.save();
// console.log("result of save is; ",);
// return{ status: "ok", message: "Successfully added to friend list"};
} catch (error) {
// console.log((error))
return {status: "error", message: "something went wrong " };
}
//
}
//endif
//find all other request
let result_last = await model0.findOne({u_id:json_data.u_id} );
// pr("----finding of result_last is: ",result_last);
return {status:"ok",data:result_last.friend_request};
}
async function main(data) {
connect_to_db();
let result;
result = await add_to_friend_list(data);
// mongoose.connection.close();
return result;
}
// main({ name: "mohan",friend_name:"mad_max" , u_id: "czdf72dfce1710b41d4fa9" , friend_u_id: "czf96c5e50d312d5309048" ,date:"27/12/2020", time: "11:43" }).then(data=>{
// pr("main result is; ",data);
// });
module.exports = main;