Skip to content

Commit

Permalink
feat(general): added circle memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 3, 2020
1 parent 7825467 commit 1dd6f6a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
39 changes: 39 additions & 0 deletions migrations/20200203182511-create-circle-memberships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('circle_memberships', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
circle_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'circles',
key: 'id'
}
},
user_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
position: {
type: Sequelize.TEXT,
allowNull: true
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('circle_memberships')
};
15 changes: 15 additions & 0 deletions models/CircleMembership.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const CircleMembership = sequelize.define('circle_membership', {
position: {
type: Sequelize.TEXT,
allowNull: true
}
}, {
underscored: true,
tableName: 'circle_memberships',
createdAt: 'created_at',
updatedAt: 'updated_at',
});

module.exports = CircleMembership;
13 changes: 12 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Body = require('./Body');
const Circle = require('./Circle');
const Permission = require('./Permission');
const CirclePermission = require('./CirclePermission');
const CircleMembership = require('./CircleMembership');

Campaign.hasMany(User, { foreignKey: 'campaign_id' });
User.belongsTo(Campaign, { foreignKey: 'campaign_id' });
Expand All @@ -29,6 +30,15 @@ Circle.belongsTo(Circle, { foreignKey: 'parent_circle_id', as: 'parent_circle' }
Circle.belongsToMany(Permission, { through: CirclePermission, foreignKey: 'circle_id', as: 'permissions' });
Permission.belongsToMany(Circle, { through: CirclePermission, foreignKey: 'permission_id', as: 'circles' });

Circle.belongsToMany(User, { through: CircleMembership, foreignKey: 'circle_id', as: 'users' });
User.belongsToMany(Circle, { through: CircleMembership, foreignKey: 'user_id', as: 'circles' });

CircleMembership.belongsTo(User, { foreignKey: 'user_id' });
User.hasMany(CircleMembership, { foreignKey: 'user_id' });

CircleMembership.belongsTo(Circle, { foreignKey: 'circle_id' });
Circle.hasMany(CircleMembership, { foreignKey: 'circle_id' });

module.exports = {
User,
Campaign,
Expand All @@ -37,5 +47,6 @@ module.exports = {
RefreshToken,
Body,
Circle,
Permission
Permission,
CircleMembership
};

0 comments on commit 1dd6f6a

Please sign in to comment.