-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatsapp_func.js
110 lines (80 loc) · 2.58 KB
/
whatsapp_func.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
const {
checkUsersForWhatsAPPConnection,
findMember,
findPosition,
findQueryResponses,
updateQueryResponse
} = require("./backEnd_api_func.js");
async function checkIfFirstMessageWhatsAPPConnection(msgWhatsAPP) {
//msgWhatsAPP{text, whatsAppNumber}
const message = msgWhatsAPP.text;
const whatsAppNumber = msgWhatsAPP.whatsAppNumber
// if message is only 3 letters
// if all letters of message are numbers
if (message.length == 3 && message.match(/^[0-9]+$/) != null) {
let resCheck = await checkUsersForWhatsAPPConnection({
authNumberWhatsAppMessage: message,
whatsappNumber: whatsAppNumber
})
console.log("resCheck = ", resCheck)
let messageBack = "This is not the right code try again please"
if (resCheck._id) {
messageBack = `hello ${resCheck.name}!!! you are connected I can't wait to find the perfect opportunity for you`
}
return {
done: true,
messageBack: messageBack,
}
} else {
return {
done: false,
messageBack: "",
}
}
}
async function findIfMessageIsAResponse(msg) {
const {
text,
whatsAppNumber
} = msg
let memberData = await findMember({
whatsAppNumber: whatsAppNumber
});
let findQueryResponsesRes
if (!memberData) {
let positionData = await findPosition({
whatsAppNumber: whatsAppNumber
});
findQueryResponsesRes = await findQueryResponses({
sentFlag: true,
phase: "QUERY",
responderType: "POSITION",
responderID: positionData._id,
});
} else {
console.log("change = -d-d-d-d-d-d" )
findQueryResponsesRes = await findQueryResponses({
sentFlag: true,
phase: "QUERY",
responderType: "USER",
responderID: memberData._id,
});
}
if (findQueryResponsesRes.length > 0) {
let queryResponse = findQueryResponsesRes[0];
console.log("queryResponse = " , queryResponse)
// --------------- update backend that message was sent ----------------
let res = await updateQueryResponse({
_id: queryResponse._id,
sentFlag: false,
answer: msg,
phase: "RESPONDED",
})
console.log("res = " , res)
// --------------- update backend that message was sent ----------------
}
}
module.exports = {
checkIfFirstMessageWhatsAPPConnection,
findIfMessageIsAResponse
}