import { DataTypes, Model, type CreationOptional, type InferAttributes, type InferCreationAttributes, type Sequelize, } from 'sequelize'; import type { Db } from '@/db/types'; import type { BelongsToGetAssociationMixin, BelongsToSetAssociationMixin, HasManyGetAssociationsMixin, HasManySetAssociationsMixin, } from 'sequelize'; import type { Campuses } from './campuses'; import type { Organizations } from './organizations'; import type { Users } from './users'; /** * School tier (American Organization → School → Campus hierarchy). A school * belongs to one organization and owns one or more campuses. The school head is * the Principal (school scope); campuses keep their own per-campus `timezone` * (a school may span timezones), so timezone is intentionally NOT on this model. */ export class Schools extends Model< InferAttributes, InferCreationAttributes > { declare id: CreationOptional; declare name: string; declare code: string; declare address: string | null; declare phone: string | null; declare email: string | null; declare description: string | null; declare active: CreationOptional; declare importHash: CreationOptional; declare organizationId: CreationOptional; declare createdById: CreationOptional; declare updatedById: CreationOptional; declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare deletedAt: CreationOptional; declare getCampuses_school: HasManyGetAssociationsMixin; declare setCampuses_school: HasManySetAssociationsMixin; declare getOrganization: BelongsToGetAssociationMixin; declare setOrganization: BelongsToSetAssociationMixin; declare getCreatedBy: BelongsToGetAssociationMixin; declare setCreatedBy: BelongsToSetAssociationMixin; declare getUpdatedBy: BelongsToGetAssociationMixin; declare setUpdatedBy: BelongsToSetAssociationMixin; static associate(db: Db): void { db.schools.hasMany(db.campuses, { as: 'campuses_school', foreignKey: { name: 'schoolId' }, constraints: false, }); db.schools.belongsTo(db.organizations, { as: 'organization', foreignKey: { name: 'organizationId' }, constraints: false, }); db.schools.belongsTo(db.users, { as: 'createdBy' }); db.schools.belongsTo(db.users, { as: 'updatedBy' }); } } export default function (sequelize: Sequelize): typeof Schools { Schools.init( { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, allowNull: false }, code: { type: DataTypes.TEXT, allowNull: false }, address: { type: DataTypes.TEXT }, phone: { type: DataTypes.TEXT }, email: { type: DataTypes.TEXT }, description: { type: DataTypes.TEXT }, active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, organizationId: { type: DataTypes.UUID, allowNull: true }, createdById: { type: DataTypes.UUID, allowNull: true }, updatedById: { type: DataTypes.UUID, allowNull: true }, createdAt: { type: DataTypes.DATE }, updatedAt: { type: DataTypes.DATE }, deletedAt: { type: DataTypes.DATE }, }, { sequelize, modelName: 'schools', timestamps: true, paranoid: true, freezeTableName: true, }, ); return Schools; }