Skip to content

Commit

Permalink
fix: refactor body memberships logic
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Apr 9, 2020
1 parent 5c2669b commit 94c30fe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
25 changes: 0 additions & 25 deletions middlewares/memberships.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const {
Permission,
BodyMembership,
CircleMembership,
JoinRequest,
User
} = require('../models');
const helpers = require('../lib/helpers');
Expand Down Expand Up @@ -99,18 +98,6 @@ exports.deleteMembership = async (req, res) => {
return errors.makeForbiddenError(res, 'Permission delete_member:body is required, but not present.');
}

// delete all join requests if any, so a person can reapply
await JoinRequest.destroy({
where: {
user_id: req.currentBodyMembership.user_id,
body_id: req.currentBody.id
}
});

if (req.currentBodyMembership.user.primary_body_id === req.currentBody.id) {
await req.currentBodyMembership.user.update({ primary_body_id: null });
}

await req.currentBodyMembership.destroy();

return res.json({
Expand All @@ -128,18 +115,6 @@ exports.deleteOwnMembership = async (req, res) => {
return errors.makeNotFoundError(res, 'You are not a member.');
}

// delete all join requests if any, so a person can reapply
await JoinRequest.destroy({
where: {
user_id: req.user.id,
body_id: req.currentBody.id
}
});

if (req.user.primary_body_id === req.currentBody.id) {
await req.user.update({ primary_body_id: null });
}

await bodyMembership.destroy();

return res.json({
Expand Down
16 changes: 16 additions & 0 deletions migrations/20200408165836-add-primary-body-id-to-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'users',
'primary_body_id',
{
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: 'bodies',
key: 'id'
},
onDelete: 'SET NULL'
},
),
down: (queryInterface) => queryInterface.removeColumn('users', 'primary_body_id')
};
22 changes: 22 additions & 0 deletions models/BodyMembership.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { Sequelize, sequelize } = require('../lib/sequelize');
const logger = require('../lib/logger');
const Body = require('./Body');
const User = require('./User');
const Circle = require('./Circle');
const JoinRequest = require('./JoinRequest');
const CircleMembership = require('./CircleMembership');

const BodyMembership = sequelize.define('body_membership', {
Expand Down Expand Up @@ -56,4 +58,24 @@ BodyMembership.afterDestroy(async (membership) => {
});
});

// Deleting all the join requests after leaving the body.
BodyMembership.afterDestroy(async (membership) => {
await JoinRequest.destroy({
where: {
user_id: membership.user_id,
body_id: membership.body_id
}
});
});

// Unsetting primary body for user if needed.
BodyMembership.afterDestroy(async (membership) => {
const user = await User.findByPk(membership.user_id);

if (user.primary_body_id === membership.body_id) {
await user.update({ primary_body_id: null });
}
});


module.exports = BodyMembership;

0 comments on commit 94c30fe

Please sign in to comment.