Skip to content

Issue #128 #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/models/phaseProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function definePhaseProduct(sequelize, DataTypes) {
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
tableName: 'phase_products',
paranoid: false,
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
Expand Down
2 changes: 2 additions & 0 deletions src/models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ module.exports = function defineProject(sequelize, DataTypes) {
deletedAt: { type: DataTypes.DATE, allowNull: true },
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
updatedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
deletedBy: DataTypes.BIGINT,
createdBy: { type: DataTypes.INTEGER, allowNull: false },
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
version: { type: DataTypes.STRING(3), allowNull: false, defaultValue: 'v3' },
}, {
tableName: 'projects',
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
Expand Down
3 changes: 2 additions & 1 deletion src/models/projectAttachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ module.exports = function defineProjectAttachment(sequelize, DataTypes) {
deletedAt: { type: DataTypes.DATE, allowNull: true },
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
updatedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
deletedBy: DataTypes.BIGINT,
createdBy: { type: DataTypes.INTEGER, allowNull: false },
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
tableName: 'project_attachments',
paranoid: false,
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
Expand Down
2 changes: 1 addition & 1 deletion src/models/projectHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function defineProjectHistory(sequelize, DataTypes) {
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
tableName: 'project_history',
paranoid: false,
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
Expand Down
1 change: 1 addition & 0 deletions src/models/projectMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function defineProjectMember(sequelize, DataTypes) {
deletedAt: { type: DataTypes.DATE, allowNull: true },
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
updatedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
deletedBy: DataTypes.BIGINT,
createdBy: { type: DataTypes.INTEGER, allowNull: false },
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
Expand Down
2 changes: 1 addition & 1 deletion src/models/projectPhase.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function defineProjectPhase(sequelize, DataTypes) {
updatedBy: { type: DataTypes.INTEGER, allowNull: false },
}, {
tableName: 'project_phases',
paranoid: false,
paranoid: true,
timestamps: true,
updatedAt: 'updatedAt',
createdAt: 'createdAt',
Expand Down
7 changes: 4 additions & 3 deletions src/routes/attachments/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ module.exports = [
return Promise.reject(err);
}
attachment = _attachment;
return _attachment.destroy();
})
return _attachment.update({ deletedBy: req.authUser.userId })
.then(() => _attachment.destroy());
}))
.then((_attachment) => {
if (process.env.NODE_ENV !== 'development') {
return fileService.deleteFile(req, _attachment.filePath);
Expand All @@ -56,6 +57,6 @@ module.exports = [
req.app.emit(EVENT.ROUTING_KEY.PROJECT_ATTACHMENT_REMOVED, { req, pattachment });
res.status(204).json({});
})
.catch(err => next(err)));
.catch(err => next(err));
},
];
28 changes: 26 additions & 2 deletions src/routes/attachments/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import models from '../../models';
import util from '../../util';
import server from '../../app';
import testUtil from '../../tests/util';
import chai from 'chai';


describe('Project Attachments delete', () => {
Expand Down Expand Up @@ -113,8 +114,31 @@ describe('Project Attachments delete', () => {
if (err) {
done(err);
} else {
deleteSpy.should.have.been.calledOnce;
done();
setTimeout(() =>
models.ProjectAttachment.findOne({
where: {
projectId: project1.id,
id: attachment.id,
},
paranoid: false,
})
.then((res) => {
if (!res) {
throw new Error('Should found the entity');
} else {
deleteSpy.should.have.been.calledOnce;

chai.assert.isNotNull(res.deletedAt);
chai.assert.isNotNull(res.deletedBy);

request(server)
.get(`/v4/projects/${project1.id}/attachments/${attachment.id}`)
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.expect(404, done);
}
}), 500);
}
});
});
Expand Down
37 changes: 14 additions & 23 deletions src/routes/milestoneTemplates/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,24 @@ module.exports = [
productTemplateId: req.params.productTemplateId,
};

return models.sequelize.transaction(tx =>
// Update the deletedBy
models.ProductMilestoneTemplate.update({ deletedBy: req.authUser.userId }, {
return models.sequelize.transaction(() =>
// soft delete the record
models.ProductMilestoneTemplate.findOne({
where,
returning: true,
raw: true,
transaction: tx,
}).then((existing) => {
if (!existing) {
// handle 404
const err = new Error(
`Milestone template not found for milestone template id ${req.params.milestoneTemplateId}`);
err.status = 404;
return Promise.reject(err);
}
return existing.update({ deletedBy: req.authUser.userId });
})
.then((updatedResults) => {
// Not found
if (updatedResults[0] === 0) {
const apiErr = new Error(
`Milestone template not found for milestone template id ${req.params.milestoneTemplateId}`);
apiErr.status = 404;
return Promise.reject(apiErr);
}

// Soft delete
return models.ProductMilestoneTemplate.destroy({
where,
transaction: tx,
});
})
.then(entity => entity.destroy()))
.then(() => {
res.status(204).end();
})
.catch(next),
);
.catch(next);
},
];
31 changes: 29 additions & 2 deletions src/routes/milestoneTemplates/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,34 @@ import request from 'supertest';
import models from '../../models';
import server from '../../app';
import testUtil from '../../tests/util';
import chai from 'chai';

const expectAfterDelete = (productTemplateId, id, err, next) => {
if (err) throw err;
setTimeout(() =>
models.ProductMilestoneTemplate.findOne({
where: {
id,
productTemplateId,
},
paranoid: false,
})
.then((res) => {
if (!res) {
throw new Error('Should found the entity');
} else {
chai.assert.isNotNull(res.deletedAt);
chai.assert.isNotNull(res.deletedBy);

request(server)
.get(`/v4/productTemplates/${productTemplateId}/milestones/${id}`)
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.expect(404, next);
}
}), 500);
};
const productTemplates = [
{
name: 'name 1',
Expand Down Expand Up @@ -180,7 +207,7 @@ describe('DELETE milestone template', () => {
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.expect(204)
.end(done);
.end(err => expectAfterDelete(1, 1, err, done));
});

it('should return 204, for connect admin, if template was successfully removed', (done) => {
Expand All @@ -190,7 +217,7 @@ describe('DELETE milestone template', () => {
Authorization: `Bearer ${testUtil.jwts.connectAdmin}`,
})
.expect(204)
.end(done);
.end(err => expectAfterDelete(1, 1, err, done));
});
});
});
Loading