mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 05:09:18 +00:00
started migration of user domain to ts
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
class Contact extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
name: { type: Sequelize.STRING },
|
||||
number: { type: Sequelize.STRING, allowNull: false, unique: true },
|
||||
email: { type: Sequelize.STRING, allowNull: false, defaultValue: "" },
|
||||
profilePicUrl: { type: Sequelize.STRING },
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static associate(models) {
|
||||
this.hasMany(models.Ticket, { foreignKey: "contactId", as: "contact" });
|
||||
this.hasMany(models.ContactCustomField, {
|
||||
foreignKey: "contactId",
|
||||
as: "extraInfo",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Contact;
|
||||
@@ -1,26 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
class ContactCustomField extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
name: { type: Sequelize.STRING },
|
||||
value: { type: Sequelize.STRING },
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static associate(models) {
|
||||
this.belongsTo(models.Contact, {
|
||||
foreignKey: "contactId",
|
||||
as: "extraInfo",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ContactCustomField;
|
||||
@@ -1,35 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
class Message extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
ack: { type: Sequelize.INTEGER, defaultValue: 0 },
|
||||
read: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||
fromMe: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||
body: { type: Sequelize.TEXT },
|
||||
mediaUrl: { type: Sequelize.STRING },
|
||||
mediaType: { type: Sequelize.STRING },
|
||||
createdAt: {
|
||||
type: Sequelize.DATE(6),
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE(6),
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static associate(models) {
|
||||
this.belongsTo(models.Ticket, { foreignKey: "ticketId", as: "messages" });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
@@ -1,24 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
class Setting extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
key: {
|
||||
type: Sequelize.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
value: { type: Sequelize.TEXT, allowNull: false },
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Setting;
|
||||
@@ -1,50 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
const Message = require("./Message");
|
||||
|
||||
class Ticket extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
status: { type: Sequelize.STRING, defaultValue: "pending" },
|
||||
userId: { type: Sequelize.INTEGER, defaultValue: null },
|
||||
unreadMessages: { type: Sequelize.VIRTUAL },
|
||||
lastMessage: { type: Sequelize.STRING },
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
this.addHook("afterFind", async result => {
|
||||
if (result && result.length > 0) {
|
||||
await Promise.all(
|
||||
result.map(async ticket => {
|
||||
ticket.unreadMessages = await Message.count({
|
||||
where: { ticketId: ticket.id, read: false },
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
this.addHook("beforeUpdate", async ticket => {
|
||||
ticket.unreadMessages = await Message.count({
|
||||
where: { ticketId: ticket.id, read: false },
|
||||
});
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static associate(models) {
|
||||
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
|
||||
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
||||
this.belongsTo(models.Whatsapp, {
|
||||
foreignKey: "whatsappId",
|
||||
as: "whatsapp",
|
||||
});
|
||||
this.hasMany(models.Message, { foreignKey: "ticketId", as: "messages" });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Ticket;
|
||||
@@ -1,32 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
const bcrypt = require("bcryptjs");
|
||||
|
||||
class User extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
name: { type: Sequelize.STRING },
|
||||
password: { type: Sequelize.VIRTUAL },
|
||||
profile: { type: Sequelize.STRING, defaultValue: "admin" },
|
||||
passwordHash: { type: Sequelize.STRING },
|
||||
email: { type: Sequelize.STRING },
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
this.addHook("beforeSave", async user => {
|
||||
if (user.password) {
|
||||
user.passwordHash = await bcrypt.hash(user.password, 8);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
checkPassword(password) {
|
||||
return bcrypt.compare(password, this.passwordHash);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User;
|
||||
82
backend/src/models/User.ts
Normal file
82
backend/src/models/User.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
Table,
|
||||
Column,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
Model,
|
||||
DataType
|
||||
} from "sequelize-typescript";
|
||||
|
||||
@Table
|
||||
class User extends Model<User> {
|
||||
@Column({
|
||||
defaultValue: DataType.UUIDV4,
|
||||
primaryKey: true,
|
||||
type: DataType.UUID
|
||||
})
|
||||
id: string;
|
||||
|
||||
@Column
|
||||
name: string;
|
||||
|
||||
@Column
|
||||
email: string;
|
||||
|
||||
@Column
|
||||
passwordHash: string;
|
||||
|
||||
@Column({
|
||||
defaultValue: "admin"
|
||||
})
|
||||
profile: string;
|
||||
|
||||
@CreatedAt
|
||||
createdAt: Date;
|
||||
|
||||
@UpdatedAt
|
||||
updatedAt: Date;
|
||||
|
||||
// @BeforeUpdate
|
||||
// @BeforeInsert
|
||||
// hashPassword = async () => {
|
||||
// if (this.passwordHash) {
|
||||
// this.passwordHash = await hash(this.passwordHash, 8);
|
||||
// }
|
||||
// };
|
||||
|
||||
// checkPassword = async (password: string) => {
|
||||
// return await compare(password, this.passwordHash);
|
||||
// };
|
||||
}
|
||||
|
||||
export default User;
|
||||
|
||||
// const bcrypt = require("bcryptjs");
|
||||
// @Table
|
||||
// class User extends Model<User> {
|
||||
// static init(sequelize) {
|
||||
// super.init(
|
||||
// {
|
||||
// name: { type: Sequelize.STRING },
|
||||
// password: { type: Sequelize.VIRTUAL },
|
||||
// profile: { type: Sequelize.STRING, defaultValue: "admin" },
|
||||
// passwordHash: { type: Sequelize.STRING },
|
||||
// email: { type: Sequelize.STRING }
|
||||
// },
|
||||
// {
|
||||
// sequelize
|
||||
// }
|
||||
// );
|
||||
|
||||
// this.addHook("beforeSave", async user => {
|
||||
// if (user.password) {
|
||||
// user.passwordHash = await bcrypt.hash(user.password, 8);
|
||||
// }
|
||||
// });
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// checkPassword(password) {
|
||||
// return bcrypt.compare(password, this.passwordHash);
|
||||
// }
|
||||
// }
|
||||
@@ -1,32 +0,0 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
class Whatsapp extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
super.init(
|
||||
{
|
||||
session: { type: Sequelize.TEXT },
|
||||
qrcode: { type: Sequelize.TEXT },
|
||||
name: { type: Sequelize.STRING, unique: true, allowNull: false },
|
||||
status: { type: Sequelize.STRING },
|
||||
battery: { type: Sequelize.STRING },
|
||||
plugged: { type: Sequelize.BOOLEAN },
|
||||
default: {
|
||||
type: Sequelize.BOOLEAN,
|
||||
defaultValue: false,
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static associate(models) {
|
||||
this.hasMany(models.Ticket, { foreignKey: "whatsappId", as: "tickets" });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Whatsapp;
|
||||
Reference in New Issue
Block a user