Skip to content

Commit

Permalink
feat(general): unique indexes + testing
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 10, 2020
1 parent 2fa1577 commit f3e0f62
Show file tree
Hide file tree
Showing 16 changed files with 632 additions and 10 deletions.
3 changes: 2 additions & 1 deletion migrations/20200129163706-create-mail-confirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = {
},
value: {
type: Sequelize.TEXT,
allowNull: false
allowNull: false,
unique: true
},
expires_at: {
allowNull: false,
Expand Down
3 changes: 2 additions & 1 deletion migrations/20200131145144-create-refresh-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = {
},
value: {
type: Sequelize.TEXT,
allowNull: false
allowNull: false,
unique: true
},
created_at: {
allowNull: false,
Expand Down
3 changes: 2 additions & 1 deletion migrations/20200131145244-create-access-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = {
},
value: {
type: Sequelize.TEXT,
allowNull: false
allowNull: false,
unique: true
},
created_at: {
allowNull: false,
Expand Down
3 changes: 2 additions & 1 deletion migrations/20200201191802-create-bodies.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = {
},
email: {
type: Sequelize.STRING,
allowNull: true
allowNull: true,
unique: true
},
phone: {
type: Sequelize.STRING,
Expand Down
6 changes: 6 additions & 0 deletions migrations/20200203160355-create-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
}, {
uniqueKeys: {
permission_scope_action_object_unique: {
fields: ['scope', 'action', 'object']
}
}
}),
down: (queryInterface) => queryInterface.dropTable('permissions')
};
6 changes: 6 additions & 0 deletions migrations/20200203171809-create-circle-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
}, {
uniqueKeys: {
circle_permission_unique: {
fields: ['circle_id', 'permission_id']
}
}
}),
down: (queryInterface) => queryInterface.dropTable('circle_permissions')
};
6 changes: 6 additions & 0 deletions migrations/20200203182511-create-circle-memberships.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
}, {
uniqueKeys: {
circle_user_unique: {
fields: ['circle_id', 'user_id']
}
}
}),
down: (queryInterface) => queryInterface.dropTable('circle_memberships')
};
47 changes: 47 additions & 0 deletions migrations/20200210101815-create-body-memberships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('body_memberships', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
body_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'bodies',
key: 'id'
},
onDelete: 'CASCADE'
},
user_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
},
onDelete: 'CASCADE'
},
comment: {
type: Sequelize.TEXT,
allowNull: true
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}, {
uniqueKeys: {
circle_permission_unique: {
fields: ['body_id', 'user_id']
}
}
}),
down: (queryInterface) => queryInterface.dropTable('body_memberships')
};
9 changes: 7 additions & 2 deletions models/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const Body = sequelize.define('body', {
},
email: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
allowNull: true,
validate: {
notEmpty: { msg: 'Email should be set.' },
isEmail: { msg: 'Email should be valid.' }
Expand Down Expand Up @@ -83,4 +82,10 @@ const Body = sequelize.define('body', {
updatedAt: 'updated_at',
});

Body.beforeValidate(async (body) => {
// skipping these fields if they are unset, will catch it later.
if (typeof body.code === 'string') body.code = body.code.toUpperCase().trim();
if (typeof body.email === 'string') body.email = body.email.toLowerCase().trim();
});

module.exports = Body;
15 changes: 15 additions & 0 deletions models/BodyMembership.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const BodyMembership = sequelize.define('body_membership', {
comment: {
type: Sequelize.TEXT,
allowNull: true
}
}, {
underscored: true,
tableName: 'body_memberships',
createdAt: 'created_at',
updatedAt: 'updated_at',
});

module.exports = BodyMembership;
11 changes: 10 additions & 1 deletion models/Permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Permission = sequelize.define('permission', {
args: [['global', 'local']],
msg: 'Permission scope should be one of these: "global", "local".'
}
}
},
unique: { args: true, msg: 'There\'s already a permission with such scope, action and object.' }
},
action: {
type: Sequelize.STRING,
Expand All @@ -19,6 +20,7 @@ const Permission = sequelize.define('permission', {
validate: {
notEmpty: { msg: 'Action should be set.' },
},
unique: { args: true, msg: 'There\'s already a permission with such scope, action and object.' }
},
object: {
type: Sequelize.STRING,
Expand All @@ -27,6 +29,7 @@ const Permission = sequelize.define('permission', {
validate: {
notEmpty: { msg: 'Object should be set.' },
},
unique: { args: true, msg: 'There\'s already a permission with such scope, action and object.' }
},
combined: {
type: Sequelize.STRING,
Expand All @@ -52,6 +55,12 @@ const Permission = sequelize.define('permission', {
updatedAt: 'updated_at',
});

Permission.beforeValidate(async (permission) => {
// skipping these fields if they are unset, will catch it later.
if (typeof permission.action === 'string') permission.action = permission.action.toLowerCase().trim();
if (typeof permission.object === 'string') permission.object = permission.object.toLowerCase().trim();
});

Permission.afterValidate((permission) => {
permission.combined = [
permission.scope,
Expand Down
13 changes: 12 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Circle = require('./Circle');
const Permission = require('./Permission');
const CirclePermission = require('./CirclePermission');
const CircleMembership = require('./CircleMembership');
const BodyMembership = require('./BodyMembership');

Campaign.hasMany(User, { foreignKey: 'campaign_id' });
User.belongsTo(Campaign, { foreignKey: 'campaign_id' });
Expand Down Expand Up @@ -48,6 +49,15 @@ User.hasMany(CircleMembership, { foreignKey: 'user_id' });
CircleMembership.belongsTo(Circle, { foreignKey: 'circle_id' });
Circle.hasMany(CircleMembership, { foreignKey: 'circle_id' });

Body.belongsToMany(User, { through: BodyMembership, foreignKey: 'body_id', as: 'users' });
User.belongsToMany(Body, { through: BodyMembership, foreignKey: 'user_id', as: 'bodies' });

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

BodyMembership.belongsTo(Body, { foreignKey: 'body_id' });
Body.hasMany(BodyMembership, { foreignKey: 'body_id' });

module.exports = {
User,
Campaign,
Expand All @@ -58,5 +68,6 @@ module.exports = {
Circle,
Permission,
CirclePermission,
CircleMembership
CircleMembership,
BodyMembership
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"request-promise-native": "^1.0.8"
},
"scripts": {
"test": "NODE_ENV=test npm run db:setup && jest test/api/*.js --runInBand --forceExit",
"test": "NODE_ENV=test npm run db:setup && jest test/**/*.test.js --runInBand --forceExit",
"lint": "node_modules/.bin/eslint .",
"db:create": "sequelize db:create",
"db:setup": "sequelize db:drop; sequelize db:create; sequelize db:migrate",
Expand Down
11 changes: 10 additions & 1 deletion test/scripts/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
Circle,
Permission,
CirclePermission,
CircleMembership
CircleMembership,
BodyMembership
} = require('../../models');

const notSet = (field) => typeof field === 'undefined';
Expand Down Expand Up @@ -137,7 +138,15 @@ exports.createCirclePermission = (circle, permission) => {
});
};

exports.createBodyMembership = (body, user) => {
return BodyMembership.create({
body_id: body.id,
user_id: user.id
});
};

exports.clearAll = async () => {
await BodyMembership.destroy({ where: {}, truncate: { cascade: true } });
await Permission.destroy({ where: {}, truncate: { cascade: true } });
await CirclePermission.destroy({ where: {}, truncate: { cascade: true } });
await CircleMembership.destroy({ where: {}, truncate: { cascade: true } });
Expand Down
Loading

0 comments on commit f3e0f62

Please sign in to comment.