From 67ad73a8bdb1b6966ea343696ec9ce68f0a0ab86 Mon Sep 17 00:00:00 2001 From: Ricardo Paes Date: Wed, 23 Feb 2022 11:06:31 -0300 Subject: [PATCH] :sparkles: add user association with whatsapps --- .../20220223095932-add-whatsapp-to-user.ts | 17 +++++++++++++++++ backend/src/models/User.ts | 12 +++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 backend/src/database/migrations/20220223095932-add-whatsapp-to-user.ts diff --git a/backend/src/database/migrations/20220223095932-add-whatsapp-to-user.ts b/backend/src/database/migrations/20220223095932-add-whatsapp-to-user.ts new file mode 100644 index 0000000..fc53178 --- /dev/null +++ b/backend/src/database/migrations/20220223095932-add-whatsapp-to-user.ts @@ -0,0 +1,17 @@ +import { QueryInterface, DataTypes } from "sequelize"; + +module.exports = { + up: (queryInterface: QueryInterface) => { + return queryInterface.addColumn("Users", "whatsappId", { + type: DataTypes.INTEGER, + references: { model: "Whatsapps", key: "id" }, + onUpdate: "CASCADE", + onDelete: "SET NULL", + allowNull: true + },); + }, + + down: (queryInterface: QueryInterface) => { + return queryInterface.removeColumn("Users", "whatsappId"); + } +}; diff --git a/backend/src/models/User.ts b/backend/src/models/User.ts index 9be664b..73edff4 100644 --- a/backend/src/models/User.ts +++ b/backend/src/models/User.ts @@ -11,12 +11,15 @@ import { AutoIncrement, Default, HasMany, - BelongsToMany + BelongsToMany, + ForeignKey, + BelongsTo } from "sequelize-typescript"; import { hash, compare } from "bcryptjs"; import Ticket from "./Ticket"; import Queue from "./Queue"; import UserQueue from "./UserQueue"; +import Whatsapp from "./Whatsapp"; @Table class User extends Model { @@ -45,6 +48,13 @@ class User extends Model { @Column profile: string; + @ForeignKey(() => Whatsapp) + @Column + whatsappId: number; + + @BelongsTo(() => Whatsapp) + whatsapp: Whatsapp; + @CreatedAt createdAt: Date;