tududi/backend/models/task.js
2025-06-16 23:01:28 +03:00

149 lines
No EOL
3 KiB
JavaScript

const { DataTypes } = require('sequelize');
module.exports = (sequelize) => {
const Task = sequelize.define('Task', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
due_date: {
type: DataTypes.DATE,
allowNull: true
},
today: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
priority: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0,
validate: {
min: 0,
max: 2
}
},
status: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
validate: {
min: 0,
max: 4
}
},
note: {
type: DataTypes.TEXT,
allowNull: true
},
recurrence_type: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'none'
},
recurrence_interval: {
type: DataTypes.INTEGER,
allowNull: true
},
recurrence_end_date: {
type: DataTypes.DATE,
allowNull: true
},
last_generated_date: {
type: DataTypes.DATE,
allowNull: true
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
project_id: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'projects',
key: 'id'
}
}
}, {
tableName: 'tasks',
indexes: [
{
fields: ['user_id']
},
{
fields: ['project_id']
},
{
fields: ['recurrence_type']
},
{
fields: ['last_generated_date']
}
]
});
// Define enum constants
Task.PRIORITY = {
LOW: 0,
MEDIUM: 1,
HIGH: 2
};
Task.STATUS = {
NOT_STARTED: 0,
IN_PROGRESS: 1,
DONE: 2,
ARCHIVED: 3,
WAITING: 4
};
// priority and status
const getPriorityName = (priorityValue) => {
const priorities = ['low', 'medium', 'high'];
return priorities[priorityValue] || 'low';
};
const getStatusName = (statusValue) => {
const statuses = ['not_started', 'in_progress', 'done', 'archived', 'waiting'];
return statuses[statusValue] || 'not_started';
};
const getPriorityValue = (priorityName) => {
const priorities = { 'low': 0, 'medium': 1, 'high': 2 };
return priorities[priorityName] !== undefined ? priorities[priorityName] : 0;
};
const getStatusValue = (statusName) => {
const statuses = {
'not_started': 0,
'in_progress': 1,
'done': 2,
'archived': 3,
'waiting': 4
};
return statuses[statusName] !== undefined ? statuses[statusName] : 0;
};
// Attach utility functions to model
Task.getPriorityName = getPriorityName;
Task.getStatusName = getStatusName;
Task.getPriorityValue = getPriorityValue;
Task.getStatusValue = getStatusValue;
return Task;
};