-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.controller.js
More file actions
64 lines (54 loc) · 2.44 KB
/
Copy pathclient.controller.js
File metadata and controls
64 lines (54 loc) · 2.44 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
// =================================================================
// Файл: controllers/client.controller.js
// Описание: Контроллер для обработки логики, связанной с клиентами.
// =================================================================
const Client = require('../../database/models/client.model');
const Instance = require('../../database/models/instance.model');
const greenApiService = require('../../services/greenApi.service');
const getClientStatus = async (req, res, next) => {
const { portal_url } = req.body;
if (!portal_url) {
return res.status(400).json({ error: true, message: 'portal_url является обязательным полем.' });
}
try {
const client = await Client.findOne({
where: { portal_url },
include: [{ model: Instance, as: 'instances' }]
});
if (!client) {
return res.status(200).json({
client_exists: false,
instances: []
});
}
// Обновляем статусы инстансов "на лету"
const instancesWithStatus = await Promise.all(
client.instances.map(async (instance) => {
const decryptedToken = instance.getDecryptedToken();
const currentStatus = await greenApiService.getStateInstance(instance.idInstance, decryptedToken);
// Опционально: можно обновлять статус и номер телефона в БД, если они изменились
if (currentStatus !== instance.status && currentStatus !== 'error') {
instance.status = currentStatus;
// Здесь можно добавить логику получения номера телефона, если статус стал 'authorized'
await instance.save();
}
return {
id: instance.id,
idInstance: instance.idInstance,
name: instance.name,
phone_number: instance.phone_number,
status: currentStatus
};
})
);
res.status(200).json({
client_exists: true,
instances: instancesWithStatus
});
} catch (error) {
next(error);
}
};
module.exports = {
getClientStatus
};