@andrey4emk/npm-app-back-b24 0.5.20 → 0.5.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bitrix24/b24.js +14 -60
- package/package.json +1 -1
- package/sendMessage/chatApp.js +0 -3
- package/sendMessage/smsgold.js +1 -5
package/bitrix24/b24.js
CHANGED
|
@@ -4,97 +4,51 @@ import dotEnv from "dotenv";
|
|
|
4
4
|
|
|
5
5
|
dotEnv.config();
|
|
6
6
|
|
|
7
|
-
// Environment variables (copied here to make code easier to test/mocking)
|
|
8
|
-
const APP_B24_DOMEN = process.env.APP_B24_DOMEN;
|
|
9
|
-
const APP_ENV = process.env.APP_ENV;
|
|
10
|
-
const APP_B24_CLIENT_ID_DEV = process.env.APP_B24_CLIENT_ID_DEV;
|
|
11
|
-
const APP_B24_CLIENT_ID = process.env.APP_B24_CLIENT_ID;
|
|
12
|
-
const APP_B24_CLIENT_SECRET_DEV = process.env.APP_B24_CLIENT_SECRET_DEV;
|
|
13
|
-
const APP_B24_CLIENT_SECRET = process.env.APP_B24_CLIENT_SECRET;
|
|
14
7
|
const APP_NAME = process.env.APP_NAME;
|
|
15
8
|
|
|
16
|
-
async function makeAuthParams(
|
|
17
|
-
let authSql = await AuthB24Model.findOne({
|
|
18
|
-
where: {
|
|
19
|
-
name: `${APP_B24_DOMEN}_${APP_ENV}`,
|
|
20
|
-
},
|
|
21
|
-
raw: true,
|
|
22
|
-
});
|
|
23
|
-
if (!authSql) {
|
|
24
|
-
// Явно выбрасываем ошибку — caller может её поймать и вернуть понятный ответ
|
|
25
|
-
throw new Error(`Auth record not found for domain: ${APP_B24_DOMEN}`);
|
|
26
|
-
}
|
|
9
|
+
async function makeAuthParams(authParamB24, secretParamB24) {
|
|
27
10
|
// Формируем параметры авторизации для SDK
|
|
28
11
|
let AuthParams = {
|
|
29
12
|
applicationToken: "", // если нет — можно '' (см. примечание ниже)
|
|
30
13
|
userId: 0, // если неизвестен — 0
|
|
31
|
-
memberId:
|
|
32
|
-
accessToken:
|
|
33
|
-
refreshToken:
|
|
34
|
-
expires:
|
|
14
|
+
memberId: authParamB24.member_id,
|
|
15
|
+
accessToken: authParamB24.access_token,
|
|
16
|
+
refreshToken: authParamB24.refresh_token,
|
|
17
|
+
expires: authParamB24.expires_in,
|
|
35
18
|
expiresIn: 1800,
|
|
36
19
|
scope: "", // если нет строки scope — оставьте пусто
|
|
37
|
-
domain:
|
|
38
|
-
clientEndpoint: `https://${
|
|
20
|
+
domain: authParamB24.domain,
|
|
21
|
+
clientEndpoint: `https://${authParamB24.domain}/rest/`,
|
|
39
22
|
serverEndpoint: "https://oauth.bitrix.info/rest/",
|
|
40
23
|
status: "L", // Local — для локальных приложений; подставьте свой вариант
|
|
41
24
|
issuer: "store", // опционально
|
|
42
25
|
};
|
|
43
26
|
let secret = {
|
|
44
|
-
clientId:
|
|
45
|
-
clientSecret:
|
|
27
|
+
clientId: secretParamB24.clientId,
|
|
28
|
+
clientSecret: secretParamB24.clientSecret,
|
|
46
29
|
};
|
|
47
30
|
return { AuthParams, secret };
|
|
48
31
|
}
|
|
49
32
|
|
|
50
|
-
export async function create(
|
|
51
|
-
let { AuthParams, secret } = await makeAuthParams(
|
|
33
|
+
export async function create(authParamB24, secretParamB24) {
|
|
34
|
+
let { AuthParams, secret } = await makeAuthParams(authParamB24, secretParamB24);
|
|
52
35
|
return new B24OAuth(AuthParams, secret);
|
|
53
36
|
}
|
|
54
37
|
|
|
55
38
|
// Функция продления токена битрикс24 и сохранения в БД
|
|
56
|
-
export async function refresh(
|
|
39
|
+
export async function refresh(authParamB24, secretParamB24) {
|
|
57
40
|
try {
|
|
58
|
-
let { AuthParams, secret } = await makeAuthParams(
|
|
41
|
+
let { AuthParams, secret } = await makeAuthParams(authParamB24, secretParamB24);
|
|
59
42
|
let newToken = await new AuthOAuthManager(AuthParams, secret).refreshAuth();
|
|
60
43
|
if (!newToken) {
|
|
61
44
|
return { error: true, message: "В refresh не получили токен" };
|
|
62
45
|
}
|
|
63
|
-
|
|
64
|
-
await AuthB24Model.upsert({
|
|
65
|
-
access_token: newToken.access_token,
|
|
66
|
-
refresh_token: newToken.refresh_token,
|
|
67
|
-
domain: newToken.domain,
|
|
68
|
-
expires_in: newToken.expires,
|
|
69
|
-
member_id: newToken.member_id,
|
|
70
|
-
name: `${newToken.domain}_${APP_ENV}`,
|
|
71
|
-
});
|
|
72
|
-
return { error: false, message: "Токены битрикс24 успешно обновлены." };
|
|
46
|
+
return { error: false, message: "Токены битрикс24 успешно обновлены.", data: newToken };
|
|
73
47
|
} catch (error) {
|
|
74
48
|
return { error: true, message: "Не удалось продлить авторизацию." };
|
|
75
49
|
}
|
|
76
50
|
}
|
|
77
51
|
|
|
78
|
-
export async function save(req, res, AuthB24Model) {
|
|
79
|
-
try {
|
|
80
|
-
const { access_token, refresh_token, domain, expires_in, member_id } = req.body;
|
|
81
|
-
if (!access_token || !refresh_token || !domain || !expires_in || !member_id) {
|
|
82
|
-
return res.status(400).json({ status: "error", message: "Не заполнены обязательные поля." });
|
|
83
|
-
}
|
|
84
|
-
const authData = await AuthB24Model.upsert({
|
|
85
|
-
access_token: access_token,
|
|
86
|
-
refresh_token: refresh_token,
|
|
87
|
-
domain: domain,
|
|
88
|
-
expires_in: expires_in,
|
|
89
|
-
member_id: member_id,
|
|
90
|
-
name: `${domain}_${APP_ENV}`,
|
|
91
|
-
});
|
|
92
|
-
res.status(201).json({ status: "ok", message: "Сохранили токены" });
|
|
93
|
-
} catch (error) {
|
|
94
|
-
res.status(500).json({ status: "error", message: "Не удалось сохранить токен." });
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
52
|
export async function errTask(b24, dataTask) {
|
|
99
53
|
try {
|
|
100
54
|
let appName = APP_NAME || "Задай название приложения в .env";
|
package/package.json
CHANGED
package/sendMessage/chatApp.js
CHANGED
package/sendMessage/smsgold.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import dotEnv from "dotenv";
|
|
2
|
-
|
|
3
|
-
dotEnv.config();
|
|
4
|
-
|
|
5
1
|
export class Smsgold {
|
|
6
2
|
constructor(authParam, b24 = null) {
|
|
7
3
|
this.user = authParam.user;
|
|
@@ -50,7 +46,7 @@ export class Smsgold {
|
|
|
50
46
|
user: this.user,
|
|
51
47
|
pass: this.pass,
|
|
52
48
|
action: "send",
|
|
53
|
-
text: message + (publicLink ? `\n${publicLink}` : ""),
|
|
49
|
+
text: message + (publicLink ? `\n\n${publicLink}` : ""),
|
|
54
50
|
number: phone,
|
|
55
51
|
sender: "potolkiRepa",
|
|
56
52
|
}).toString();
|