You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.7 KiB
JavaScript
59 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const db = require('../config').database;
|
|
const { domain, name } = require('../config').server;
|
|
const initModels = require('../models/init-models');
|
|
|
|
const Sequelize = db.sequelize;
|
|
const models = initModels(Sequelize);
|
|
|
|
const ConnectionsModel = models.connections;
|
|
|
|
const addConnection = async data => {
|
|
const r = await ConnectionsModel.create({ ...data, connect_domain: domain, connect_name: name });
|
|
return r;
|
|
};
|
|
|
|
/**
|
|
* Find or create connection to current server
|
|
*/
|
|
const addCurrentConnection = async data => {
|
|
const [r, createdId] = await ConnectionsModel.findOrCreate({
|
|
where: { connect_domain: domain, connect_name: name, wa_id: data.wa_id },
|
|
defaults: { ...data, connect_domain: domain, connect_name: name, closetime: null },
|
|
});
|
|
return r;
|
|
};
|
|
|
|
const updateConnection = async (data, where = {}) => {
|
|
const r = await ConnectionsModel.update(
|
|
{
|
|
...data,
|
|
...(data.status === 'open' ? { opentime: Sequelize.fn('NOW') } : {}),
|
|
...(['close', 'offline'].includes(data.status) ? { closetime: Sequelize.fn('NOW') } : {}),
|
|
updatetime: Sequelize.fn('NOW'),
|
|
},
|
|
{ where: { ...where, wa_id: data.wa_id } },
|
|
);
|
|
return r;
|
|
};
|
|
|
|
const getConnection = async data => {
|
|
const r = await ConnectionsModel.findAll({ where: data, raw: true });
|
|
return r || [];
|
|
};
|
|
|
|
const resetConnection = async () => {
|
|
const r = await ConnectionsModel.update(
|
|
{
|
|
status: 'close',
|
|
closetime: Sequelize.fn('NOW'),
|
|
updatetime: Sequelize.fn('NOW'),
|
|
},
|
|
{ where: { connect_domain: domain, connect_name: name } },
|
|
);
|
|
return r;
|
|
};
|
|
|
|
module.exports = { addConnection, addCurrentConnection, updateConnection, getConnection, resetConnection };
|