Added activity model to keep track of changes to items
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Gregory Ballantine 2022-12-04 01:05:24 -05:00
parent 6024a1fe7f
commit b8700c1ba3
4 changed files with 59 additions and 0 deletions

26
src/models/activity.js Normal file
View File

@ -0,0 +1,26 @@
module.exports = (sequelize, Sequelize) => {
const Activity = sequelize.define('activity', {
user: {
type: Sequelize.STRING,
allowNull: false,
},
action: {
type: Sequelize.STRING,
allowNull: false,
},
itemId: {
type: Sequelize.NUMBER,
allowNull: false,
},
});
Activity.render = function() {
return this.user + ' has ' + this.action + ' this item at ' + this.createdAt + '.';
};
return Activity;
};

View File

@ -10,6 +10,14 @@ db.sequelize = sequelize;
db.items = require('./item.js')(sequelize, Sequelize); db.items = require('./item.js')(sequelize, Sequelize);
db.licenses = require('./license.js')(sequelize, Sequelize); db.licenses = require('./license.js')(sequelize, Sequelize);
db.activities = require('./activity.js')(sequelize, Sequelize);
// Model associations
db.items.hasMany(db.activities, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
db.activities.belongsTo(db.items);
module.exports = db; module.exports = db;

View File

@ -1,5 +1,6 @@
const db = require('../models'); const db = require('../models');
const Item = db.items; const Item = db.items;
const Activity = db.activities;
// GET - /item/add // GET - /item/add
exports.getAdd = async function(req, res) { exports.getAdd = async function(req, res) {
@ -20,6 +21,12 @@ exports.postAdd = async function(req, res) {
console.log(`Saved item ${item.name} to the database.`); console.log(`Saved item ${item.name} to the database.`);
const activity = await Activity.create({
user: 'Admin',
action: 'create',
itemId: item.id,
});
res.redirect('/'); res.redirect('/');
}; };
@ -29,8 +36,11 @@ exports.getItem = async function(req, res) {
where: { where: {
id: req.params.id, id: req.params.id,
}, },
include: Activity,
}); });
console.log(item[0].activities[0].render());
res.render('item/view.twig', { res.render('item/view.twig', {
item: item[0], item: item[0],
}); });

View File

@ -44,4 +44,19 @@
</table> </table>
</section> </section>
<hr>
<section id="item-activity" class="row">
<h2>Recorded activity</h2>
{% if item.activities | length > 0 %}
<ul class="twelve columns">
{% for a in item.activities %}
<li><p>{{ a.render() }}</p></li>
{% endfor %}
</ul>
{% else %}
<p>There hasn't been any activity recorded for this item.</p>
{% endif %}
</section>
{% endblock %} {% endblock %}