forked from jaypae95/nh_back
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
169 lines (147 loc) · 4.54 KB
/
utils.js
File metadata and controls
169 lines (147 loc) · 4.54 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
165
166
167
168
169
const path = require('path');
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '.', 'config', 'config.json'))[env];
const jwt = require('jsonwebtoken');
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey');
const serviceId = config.serviceId;
const serviceKey = config.serviceKey;
const ncsAccessKey = config.ncsAccessKey;
const ncsSecretKey = config.ncsSecretKey;
const apiURL = `https://sens.apigw.ntruss.com/sms/v2/services/${serviceId}/messages`;
const crypto = require('crypto');
const axios = require('axios');
const {Event, EventAdmin, Guest} = require('./models');
const value = {};
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://kkotgil-bdb2e.firebaseio.com',
});
value.masterCheck = async function(userId, eventId) {
const check = await Event.findOne({
where: {user_id: userId, id: eventId},
});
return check !== null;
};
value.guestCheck = async function(userId, eventId) {
const check = await Guest.findOne({
where: {user_id: userId, event_id: eventId},
});
return check !== null;
};
value.adminCheck = async function(userId, eventId) {
const check = await EventAdmin.findOne({
where: {user_id: userId, event_id: eventId},
});
return check !== null;
};
value.code = {
'UNKNOWN_ERROR': -1,
'SUCCESS': 0,
'NO_USER': 1,
'INCORRECT_PASSWORD': 2,
'USERNAME_ALREADY_EXIST': 3,
'USERNAME_INVALID': 4,
'PHONE_NUMBER_ALREADY_EXIST': 5,
'PHONE_NUMBER_INVALID': 6,
'NAME_INVALID': 7,
'NO_DATA': 8,
'INVALID_QUERY': 9,
'NO_AUTH': 10,
'NH_API_ERROR': 11,
'DB_ERROR': 12,
};
value.phoneNumberCheck = function (phone) {
const regExp = /(01[016789])([1-9]{1}[0-9]{2,3})([0-9]{4})$/;
if(!regExp.test(phone)) {
return false;
}
return true;
};
value.getUser = function (req) {
if (req.headers && req.headers.authorization) {
const authorization = req.headers.authorization;
let decoded = '';
try {
decoded = jwt.verify(authorization, config.jwtSecret);
}
catch (e) {
return {detail: 'unauthorized'};
}
return {detail: 'success', user: decoded.data};
// Fetch the user by id
}
return {detail: 'no header'};
};
value.saltRounds = 10;
value.sendFcm = async function(title, body, link, fbTokenList) {
const fcmMessage = {
data: {
title: title,
body: body,
link: link,
},
tokens: fbTokenList,
};
try {
const result = await admin.messaging().sendMulticast(fcmMessage);
for(let i = 0; i < result.responses.length; i++) {
if(!result.responses[i].success) {
console.log(result.responses[i].error);
}
}
console.log('Send FCM Success');
return value.code.SUCCESS;
}
catch(exception) {
console.log(exception);
return value.code.UNKNOWN_ERROR;
}
};
value.sendSms = async function (phoneList, content) {
const hmac = crypto.createHmac('sha256', ncsSecretKey);
const space = ' '; // one space
const newLine = '\n'; // new line
const method = 'POST'; // method
const timestamp = Date.now().toString();
const url2 = `/sms/v2/services/${serviceId}/messages`;
const message = [];
message.push(method);
message.push(space);
message.push(url2);
message.push(newLine);
message.push(timestamp);
message.push(newLine);
message.push(ncsAccessKey);
const signature = hmac.update(message.join('')).digest('base64');
const destination = [];
for (let i = 0; i < phoneList.length; i++) {
const temp = {
'to': phoneList[i],
};
destination.push(temp);
}
const reqHeader = {
'Content-Type': 'application/json; charset=utf-8',
'x-ncp-iam-access-key': ncsAccessKey,
'x-ncp-apigw-timestamp': timestamp,
'x-ncp-apigw-signature-v2': signature.toString(),
};
const reqBody = {
'type': 'SMS', // LMS, MMS
'contentType': 'COMM', // 일반 메시지, AD : 광고
'countryCode': '82', // 한국
'from': '01047335304', // 사전 등록된 발신번호
'content': content, // 메시지 내용
'messages': destination,
};
try {
await axios.post(apiURL, reqBody, {
headers: reqHeader,
});
}
catch (exception) {
console.log(exception);
}
};
module.exports = value;