sending images through messages works

This commit is contained in:
jackiettran
2025-12-18 19:37:16 -05:00
parent 996e815d57
commit 4b4584bc0f
6 changed files with 52 additions and 22 deletions

View File

@@ -0,0 +1,21 @@
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.changeColumn("Messages", "content", {
type: Sequelize.TEXT,
allowNull: true,
});
},
down: async (queryInterface, Sequelize) => {
// First update any null content to empty string before reverting
await queryInterface.sequelize.query(
`UPDATE "Messages" SET content = '' WHERE content IS NULL`
);
await queryInterface.changeColumn("Messages", "content", {
type: Sequelize.TEXT,
allowNull: false,
});
},
};

View File

@@ -25,7 +25,7 @@ const Message = sequelize.define('Message', {
},
content: {
type: DataTypes.TEXT,
allowNull: false
allowNull: true
},
isRead: {
type: DataTypes.BOOLEAN,
@@ -36,7 +36,15 @@ const Message = sequelize.define('Message', {
allowNull: true
}
}, {
timestamps: true
timestamps: true,
validate: {
contentOrImage() {
const hasContent = this.content && this.content.trim().length > 0;
if (!hasContent && !this.imageFilename) {
throw new Error('Message must have content or an image');
}
}
}
});
module.exports = Message;

View File

@@ -316,7 +316,7 @@ router.post('/', authenticateToken, async (req, res, next) => {
error: error.message,
stack: error.stack,
senderId: req.user.id,
receiverId: req.body.receiverId
receiverId: req.body?.receiverId
});
next(error);
}