feat: inicializacion de Help Desk segun estandar Telcotronics con bitacora de errores e integracion MCP
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
const db = require('../config/db');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const User = {
|
||||
findAll: async (companyId) => {
|
||||
const [rows] = await db.query(
|
||||
'SELECT id, company_id, nombre, email, rol, is_active, created_at FROM users WHERE company_id = ? ORDER BY created_at DESC',
|
||||
[companyId]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
findById: async (id) => {
|
||||
const [rows] = await db.query(
|
||||
'SELECT u.*, c.name as company_name, c.plan as company_plan, c.max_users, c.max_tickets FROM users u LEFT JOIN companies c ON u.company_id = c.id WHERE u.id = ?',
|
||||
[id]
|
||||
);
|
||||
return rows[0];
|
||||
},
|
||||
findByEmail: async (email) => {
|
||||
const [rows] = await db.query(
|
||||
'SELECT u.*, c.name as company_name, c.plan as company_plan, c.max_users, c.max_tickets FROM users u LEFT JOIN companies c ON u.company_id = c.id WHERE u.email = ?',
|
||||
[email]
|
||||
);
|
||||
return rows[0];
|
||||
},
|
||||
create: async (companyId, nombre, email, password, rol = 'client') => {
|
||||
const [result] = await db.query(
|
||||
'INSERT INTO users (company_id, nombre, email, password, rol) VALUES (?, ?, ?, ?, ?)',
|
||||
[companyId, nombre, email, password, rol]
|
||||
);
|
||||
return result.insertId;
|
||||
},
|
||||
update: async (id, data) => {
|
||||
const fields = [];
|
||||
const values = [];
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
fields.push(`${key} = ?`);
|
||||
values.push(value);
|
||||
}
|
||||
if (fields.length === 0) return;
|
||||
values.push(id);
|
||||
await db.query(`UPDATE users SET ${fields.join(', ')} WHERE id = ?`, values);
|
||||
},
|
||||
delete: async (id) => {
|
||||
await db.query('DELETE FROM users WHERE id = ?', [id]);
|
||||
},
|
||||
findByInviteToken: async (token) => {
|
||||
const [rows] = await db.query(
|
||||
'SELECT * FROM users WHERE invite_token = ? AND invite_token_expires > NOW()',
|
||||
[token]
|
||||
);
|
||||
return rows[0];
|
||||
},
|
||||
generateInviteToken: async (userId) => {
|
||||
const token = crypto.randomBytes(32).toString('hex');
|
||||
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
||||
await db.query(
|
||||
'UPDATE users SET invite_token = ?, invite_token_expires = ? WHERE id = ?',
|
||||
[token, expires, userId]
|
||||
);
|
||||
return token;
|
||||
},
|
||||
countByCompany: async (companyId) => {
|
||||
const [rows] = await db.query(
|
||||
'SELECT COUNT(*) as total FROM users WHERE company_id = ? AND is_active = 1',
|
||||
[companyId]
|
||||
);
|
||||
return rows[0].total;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = User;
|
||||
Reference in New Issue
Block a user