Skip to content

Commit

Permalink
Adding type mapping
Browse files Browse the repository at this point in the history
This aims to add a method to progmatically track entity type access to
questionairres.
resolves #219
  • Loading branch information
stoopidJSON committed Jul 21, 2020
1 parent 4211618 commit c623344
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 75 deletions.
43 changes: 43 additions & 0 deletions src/models/entity-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const entityType = (sequelize, DataTypes) => {
const EntityType = sequelize.define('EntityType', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING,
required: true
},
description: {
type: DataTypes.STRING
},
questionnaires: {
type: DataTypes.JSON
}
},
{
schema: process.env.DATABASE_SCHEMA
})

EntityType.findById = async (id) => {
const entityType = await EntityType.findOne({
where: { id }
})

return entityType
}

EntityType.findByName = async (name) => {
const entityType = await EntityType.findOne({
where: { name }
})

return entityType
}

return EntityType
}

export default entityType
150 changes: 75 additions & 75 deletions src/models/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,89 @@ import { UUIDV4 } from "sequelize"
import models from "./index"

const entity = (sequelize, DataTypes) => {
// Defining our entity table and setting Entity object.
const Entity = sequelize.define('Entity', {
id: {
type: DataTypes.UUID,
unique: true,
primaryKey: true,
defaultValue: UUIDV4
},
name : {
type: DataTypes.STRING,
required: true
},
type : {
type: DataTypes.STRING
},
address: {
type: DataTypes.JSON,
},
phone: {
type: DataTypes.JSON,
},
email: {
type: DataTypes.JSON,
},
checkIn: {
type: DataTypes.JSON,
},
description: {
type: DataTypes.STRING
},
attributes: {
type: DataTypes.JSON,
}
// Defining our entity table and setting Entity object.
const Entity = sequelize.define('Entity', {
id: {
type: DataTypes.UUID,
unique: true,
primaryKey: true,
defaultValue: UUIDV4
},
name: {
type: DataTypes.STRING,
required: true
},
type: {
type: DataTypes.STRING
},
address: {
type: DataTypes.JSON,
},
phone: {
type: DataTypes.JSON,
},
email: {
type: DataTypes.JSON,
},
checkIn: {
type: DataTypes.JSON,
},
description: {
type: DataTypes.STRING
},
attributes: {
type: DataTypes.JSON,
}
},
{
schema: process.env.DATABASE_SCHEMA
schema: process.env.DATABASE_SCHEMA
})

Entity.associate = models => {
Entity.belongsToMany(models.Contact, {
through: "EntityContact",
as: "contacts",
foreignKey: "entityId",
otherKey: "contactId"
})
}
Entity.associate = models => {
Entity.belongsToMany(models.Contact, {
through: "EntityContact",
as: "contacts",
foreignKey: "entityId",
otherKey: "contactId"
})
}

Entity.findById = async (id) => {
const entity = await Entity.findOne({
where: { id }
})

return entity
}
Entity.findById = async (id) => {
const entity = await Entity.findOne({
where: { id }
})

Entity.findByName = async (name) => {
const entity = await Entity.findOne({
where: { name }
})
return entity
}
return entity
}

Entity.findByName = async (name) => {
const entity = await Entity.findOne({
where: { name }
})

Entity.findEntityWithAssociatedContacts = async (entityId) => {
const entityContacts = await Entity.findOne({
where: { id: entityId },
include: [{
model: models.Contact,
as: 'contacts',
required: false,
attributes: ["id", "name", "phone", "email", "attributes", "email"],
through: {
model: models.EntityContact,
as: "entityContacts",
attributes: ["relationshipTitle"]
}
}]
})
return entity
}

return entityContacts
}
Entity.findEntityWithAssociatedContacts = async (entityId) => {
const entityContacts = await Entity.findOne({
where: { id: entityId },
include: [{
model: models.Contact,
as: 'contacts',
required: false,
attributes: ["id", "name", "phone", "email", "attributes", "email"],
through: {
model: models.EntityContact,
as: "entityContacts",
attributes: ["relationshipTitle"]
}
}]
})

return entityContacts
}

return Entity
return Entity
}

export default entity;
export default entity

0 comments on commit c623344

Please sign in to comment.