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.
Global-sales/wai-server/services/connections.service.js

47 lines
1.4 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, sesson_id: data.sesson_id },
defaults: { ...data, connect_domain: domain, connect_name: name },
});
return r;
};
const updateConnection = async (data, where = {}) => {
const r = await ConnectionsModel.update(
{
...data,
...(data.status === 'open' ? { opentime: Sequelize.fn('NOW') } : {}),
...(data.status === 'close' ? { closetime: Sequelize.fn('NOW') } : {}),
updatetime: Sequelize.fn('NOW'),
},
{ where: { ...where, sesson_id: data.sesson_id } },
);
return r;
};
const getConnection = async data => {
const r = await ConnectionsModel.findAll({ where: data });
return r;
};
module.exports = { addConnection, addCurrentConnection, updateConnection, getConnection };