From c623344d9811ac475c6b2002995b2a961e11aa97 Mon Sep 17 00:00:00 2001 From: Jason Anton Date: Tue, 21 Jul 2020 11:19:11 -0400 Subject: [PATCH] Adding type mapping This aims to add a method to progmatically track entity type access to questionairres. resolves #219 --- src/models/entity-type.js | 43 +++++++++++ src/models/entity.js | 150 +++++++++++++++++++------------------- 2 files changed, 118 insertions(+), 75 deletions(-) create mode 100644 src/models/entity-type.js diff --git a/src/models/entity-type.js b/src/models/entity-type.js new file mode 100644 index 00000000..ff8f759a --- /dev/null +++ b/src/models/entity-type.js @@ -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 diff --git a/src/models/entity.js b/src/models/entity.js index dfb84a12..7de6d64c 100644 --- a/src/models/entity.js +++ b/src/models/entity.js @@ -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; \ No newline at end of file +export default entity