I am trying to create a controller API with Sequelize in my backend. I want it to firstly load all the unverified dive logs belonging to a specific dive school. This would be the ones related to the logged in user who has a userID which matches the scubaStaff model.
Firstly how do I set the where op parameters to load the dive school logs that are set to false relating to the logged in users dive school in Sequelize? I have had experience using SQL and using joins before but Sequelize is new to me. The logged in user has a user token with the userRole details.
Secondly if the controller is just to return the unverified logs, can I use the same controller to update the log to verified or does this have to be a separate update API.
exports.schoolDiveLogApproval = (req, res) => {
diveLog.findAll({
include: { all: true },
where: {
[Op.and]: [
{assocScubaSchool: req.params.diveSchoolNameID},
{diveVerifiedBySchool: false}
]
},
})
.then((diveLog) => {
const schoolDiveLogList = [];
for (i = 0; i < diveLog.length; i++) {
schoolDiveLogList.push(diveLog[i].dataValues);
}
if (!diveLog) {
return res.status(404).send({ message: "No dive logs for approval for this scuba school." });
}
res.status(200).send({
data: schoolDiveLogList
});
})
.catch((err) => {
res.status(500).send({ message: err.message });
});
};
scubaStaff.model.js
module.exports = (sequelize, Sequelize) => {
const scubaStaff = sequelize.define("scubaStaff", {
staffID: {
type: Sequelize.INTEGER,
primaryKey: true
},
userID: {
type: Sequelize.INTEGER
},
assocScubaSchool: {
type: Sequelize.INTEGER
}},
{
timestamps: false
},{});
scubaStaff.associate = function(models){
userID.belongsTo(models.userLogin, {as: 'userid'})
assocScubaSchool.belongsTo(models.diveSchool, {as: 'diveSchoolID'})
};
return scubaStaff;
};
user.model.js
module.exports = (sequelize, Sequelize) => {
const userLogin = sequelize.define("userLogins", {
userID: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
username: {
type: Sequelize.TEXT
},
password: {
type: Sequelize.TEXT
},
userEmail: {
type: Sequelize.TEXT
},
userFirstName: {
type: Sequelize.TEXT
},
userSurname: {
type: Sequelize.TEXT
}
}},
{
timestamps: false
})
return userLogin;
};
diveLog.model.js
module.exports = (sequelize, Sequelize) => {
const diveLog = sequelize.define("diveLogs", {
diveID: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
diveTypeID: {
type: Sequelize.INTEGER
},
diveSchoolNameID: {
type: Sequelize.INTEGER
},
diveCurrentID: {
type: Sequelize.INTEGER
},
diveVisibilityID: {
type: Sequelize.INTEGER
},
diveMaxDepth: {
type: Sequelize.INTEGER
},
diverUserNumber: {
type: Sequelize.INTEGER
},
diveVerifiedBySchool: {
type: Sequelize.BOOLEAN
},
divePoint: {
type: Sequelize.INTEGER
}},
{
timestamps: false
}, {});
diveLog.associate = function(models){
diveLog.belongsTo(models.diveRegion, {foreignKey: 'diveSchoolRegionID', as: 'diveRegion'})
diveLog.belongsTo(models.diveCertification, {foreignKey: 'diveCertification', as: 'diveCertification'})
diveLog.belongsTo(models.current, {foreignKey: 'diveCurrentID', as: 'diveCurrent'})
diveLog.belongsTo(models.visibility, {foreignKey: 'diveVisibilityID', as: 'diveVisibility'})
diveLog.belongsTo(models.divePoint, {foreignKey: 'divePointID', as: 'divePoint'})
};
return diveLog;
};