????
Current Path : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/quantri/ |
Current File : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/quantri/nguoidung.js |
var Database = require('../../models/Database'); var bcrypt = require('bcryptjs'); class NguoiDungController extends Database { constructor(params) { super(params); } static create(params) { return new NguoiDungController(params); } async selectUser() { return await this.select('SELECT [Account].*,[Role].Name FROM [Account] INNER JOIN [Role] ON [Account].Role = [Role].Id WHERE [Account].Role != 1 AND [State] > 0'); } async addUser(user) { if ((await this.select('SELECT UUID FROM Account WHERE Username = @username', { username: user['username'] })).length) throw new Error(`Đã tồn tại tài khoản với tên đăng nhập [${user['username']}]`); if (user['idnumber'] && (await this.select('SELECT UUID FROM Account WHERE IdNumber = @idnumber', { idnumber: user['idnumber'] })).length) throw new Error(`Đã tồn tại tài khoản với số CMND hoặc hộ chiếu [${user['idnumber']}]`); if (user['email'] && (await this.select('SELECT UUID FROM Account WHERE Email = @email', { email: user['email'] })).length) throw new Error(`Đã tồn tại tài khoản với email [${user['email']}]`); if (user['phone'] && (await this.select('SELECT UUID FROM Account WHERE Telephone = @phone', { phone: user['phone'] })).length) throw new Error(`Số điện thoại [${user['phone']}] đã được tài khoản khác sử dụng`); let input = { username: user['username'], password: await bcrypt.hash(user['newpassword'], 10), fullname: user['fullname'] || null, gender: user['gender'] || 1, idnumber: user['idnumber'] || null, email: user['email'] || null, phone: user['phone'] || null, role: user['role'] || 2 } var query = 'INSERT INTO Account(Username,Password,FullName,Gender,IdNumber,Email,Telephone,Role,State) VALUES(@username,@password,@fullname,@gender,@idnumber,@email,@phone,@role,1)'; return await this.query(query, input); } async editUser(user) { if ((await this.select('SELECT UUID FROM Account WHERE UUID != @uuid AND Username = @username', { uuid: user['id'], username: user['username'] })).length) throw new Error(`Đã tồn tại tài khoản khác với tên đăng nhập [${user['username']}]`); if (user['idnumber'] && (await this.select('SELECT UUID FROM Account WHERE UUID != @uuid AND IdNumber = @idnumber', { uuid: user['id'], idnumber: user['idnumber'] })).length) throw new Error(`Đã tồn tại tài khoản khác với số CMND hoặc hộ chiếu [${user['idnumber']}]`); if (user['email'] && (await this.select('SELECT UUID FROM Account WHERE UUID != @uuid AND Email = @email', { uuid: user['id'], email: user['email'] })).length) throw new Error(`Đã tồn tại tài khoản khác với email [${user['email']}]`); if (user['phone'] && (await this.select('SELECT UUID FROM Account WHERE UUID != @uuid AND Telephone = @phone', { uuid: user['id'], phone: user['phone'] })).length) throw new Error(`Số điện thoại [${user['phone']}] đã được tài khoản khác sử dụng`); let updates = []; let input = { id: user['id'], username: user['username'], password: user['newpassword'] ? await bcrypt.hash(user['newpassword'], 10) : null, fullname: user['fullname'], gender: user['gender'] || 1, idnumber: user['idnumber'] || null, email: user['email'] || null, phone: user['phone'] || null, role: user['role'] || 2 } if (!input['id']) throw new Error('Không xác định được mã tài khoản cần chỉnh sửa'); if (typeof input['username'] != 'undefined') updates.push('Username = @username'); if (input['password']) updates.push('Password = @password'); if (typeof input['fullname'] != 'undefined') updates.push('FullName = @fullname'); if (typeof input['gender'] != 'undefined') updates.push('Gender = @gender'); if (typeof input['idnumber'] != 'undefined') updates.push('IdNumber = @idnumber'); if (typeof input['email'] != 'undefined') updates.push('Email = @email'); if (typeof input['phone'] != 'undefined') updates.push('Telephone = @phone'); if (typeof input['role'] != 'undefined') updates.push('Role = @role'); if (!updates.length) return null; return await this.query(`UPDATE Account SET ${updates.join(',')} WHERE UUID = @id`, input) } async deleteUser(id) { return await this.query('UPDATE Account SET [State] = 0 WHERE UUID = @uuid', { uuid: id }); } } module.exports = NguoiDungController;