11import { UUIDV4 } from "sequelize" ;
2+ import models from './index' ;
23
34const contact = ( sequelize , DataTypes ) => {
45 // Defining our contact table and setting Contact object.
@@ -17,13 +18,6 @@ const contact = (sequelize, DataTypes) => {
1718 } ,
1819 unique : true
1920 } ,
20- EntityId : {
21- type : DataTypes . UUID ,
22- references : {
23- model : 'Entity' ,
24- key : 'id'
25- }
26- } ,
2721 name : {
2822 type : DataTypes . STRING ,
2923 required : true
@@ -44,7 +38,12 @@ const contact = (sequelize, DataTypes) => {
4438
4539 Contact . associate = models => {
4640 Contact . belongsTo ( models . User ) ;
47- Contact . belongsTo ( models . Entity ) ;
41+ Contact . belongsToMany ( models . Entity , {
42+ through : "EntityContact" ,
43+ as : "entities" ,
44+ foreignKey : "contactId" ,
45+ otherKey : "entityId"
46+ } ) ;
4847 }
4948
5049 Contact . findById = async ( id ) => {
@@ -71,6 +70,25 @@ const contact = (sequelize, DataTypes) => {
7170 return contact ;
7271 } ;
7372
73+ Contact . findContactWithAssociatedEntities = async ( contactId ) => {
74+ const contactEntities = await Contact . findOne ( {
75+ where : { id : contactId } ,
76+ include : [ {
77+ model : models . Entity ,
78+ as : 'entities' ,
79+ required : false ,
80+ attributes : [ "id" , "name" , "type" , "address" , "phone" , "email" , "checkIn" , "description" , "attributes" ] ,
81+ through : {
82+ model : models . EntityContact ,
83+ as : 'entityContacts' ,
84+ attributes : [ "relationshipTitle" ]
85+ }
86+ } ]
87+ } ) ;
88+
89+ return contactEntities ;
90+ } ;
91+
7492 return Contact ;
7593} ;
7694
0 commit comments