tududi/backend/models/calendar_token.js
Chris 2103f633eb
fix: wire CalendarToken model into models/index.js to prevent MCP crashes (#1069)
Fixes #1050

The calendar_token.js model file was creating a circular dependency by
importing sequelize from ./models instead of receiving it as a parameter.
This caused InboxItem and other models to be undefined when accessed by
the MCP inbox tools, resulting in crashes.

Changes:
- Refactored calendar_token.js to follow the standard model pattern
  (export function that receives sequelize parameter)
- Added CalendarToken import in models/index.js
- Set up User<->CalendarToken associations
- Exported CalendarToken from models/index.js

This eliminates the circular dependency and ensures all models are
properly initialized before use.
2026-04-25 01:44:56 +03:00

81 lines
2.2 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { uid } = require('../utils/uid');
module.exports = (sequelize) => {
const CalendarToken = sequelize.define(
'CalendarToken',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
uid: {
type: DataTypes.STRING(),
allowNull: false,
unique: true,
defaultValue: uid,
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Users',
key: 'id',
},
onDelete: 'CASCADE',
},
provider: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'google',
},
access_token: {
type: DataTypes.TEXT,
allowNull: false,
},
refresh_token: {
type: DataTypes.TEXT,
allowNull: true,
},
token_type: {
type: DataTypes.STRING,
defaultValue: 'Bearer',
},
expires_at: {
type: DataTypes.DATE,
allowNull: true,
},
scope: {
type: DataTypes.TEXT,
allowNull: true,
},
connected_email: {
type: DataTypes.STRING,
allowNull: true,
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
},
{
tableName: 'calendar_tokens',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
unique: true,
fields: ['user_id', 'provider'],
},
],
}
);
return CalendarToken;
};