File: //home/arjun/projects/good-life-be/models/newQuestions.js
import { Sequelize, DataTypes } from 'sequelize';
import sequelize from '../config/sequelize-config.js';
import Category from '../models/Category.js'; // Adjust the path if needed
import SubCategory from '../models/SubCategory.js'; // Adjust the path if needed
import NewSubQuestion from '../models/newSubQuestions.js'; // Adjust the path if needed
const NewQuestion = sequelize.define(
'newquestions',
{
id: {
primaryKey: true,
type: DataTypes.INTEGER,
autoIncrement: true,
},
category_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
sub_category_id: {
type: DataTypes.INTEGER,
allowNull: true,
},
question: {
type: DataTypes.TEXT,
allowNull: false,
},
answer_type: {
type: DataTypes.ENUM(
'text',
'dropdown',
'radio',
'checkbox',
'time_range',
'slider',
'time',
'duration',
'date',
'date_range',
'button'
),
allowNull: false,
},
sub_question: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: true,
},
answers: {
type: DataTypes.JSONB,
allowNull: true,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP'),
},
},
{
timestamps: true,
}
);
NewQuestion.associate = (models) => {
// Associating with the `categories` table
NewQuestion.belongsTo(models.category, {
foreignKey: 'category_id',
as: 'category',
});
// Associating with the `sub_categories` table
NewQuestion.belongsTo(models.sub_category, {
foreignKey: 'sub_category_id',
as: 'subCategory',
});
// One-to-many relationship with subquestions if applicable
NewQuestion.hasMany(models.new_sub_questions, {
foreignKey: 'question_id',
as: 'subQuestions',
});
};
export default NewQuestion;