import { DataTypes, Model, type CreationOptional, type InferAttributes, type InferCreationAttributes, type Sequelize, } from 'sequelize'; import type { Db } from '@/db/types'; import type { BelongsToGetAssociationMixin } from 'sequelize'; import type { Organizations } from './organizations'; import type { Users } from './users'; /** * Guardian ↔ student link (many-to-many). Students and guardians are **users** * (roles), not SIS entities, so this junction relates two `users` rows: a * student may have several guardians and a guardian several students. * `relationship` is an optional label (parent / grandparent / guardian …). * Tenant-scoped by `organizationId`; queries narrow further via the student's * class/campus. */ export class GuardianStudents extends Model< InferAttributes, InferCreationAttributes > { declare id: CreationOptional; declare guardianId: string; declare studentId: string; declare relationship: CreationOptional; declare organizationId: CreationOptional; declare createdById: CreationOptional; declare updatedById: CreationOptional; declare createdAt: CreationOptional; declare updatedAt: CreationOptional; declare deletedAt: CreationOptional; declare getGuardian: BelongsToGetAssociationMixin; declare getStudent: BelongsToGetAssociationMixin; declare getOrganization: BelongsToGetAssociationMixin; static associate(db: Db): void { db.guardian_students.belongsTo(db.users, { as: 'guardian', foreignKey: { name: 'guardianId' }, constraints: false, }); db.guardian_students.belongsTo(db.users, { as: 'student', foreignKey: { name: 'studentId' }, constraints: false, }); db.guardian_students.belongsTo(db.organizations, { as: 'organization', foreignKey: { name: 'organizationId' }, constraints: false, }); db.guardian_students.belongsTo(db.users, { as: 'createdBy' }); db.guardian_students.belongsTo(db.users, { as: 'updatedBy' }); } } export default function (sequelize: Sequelize): typeof GuardianStudents { GuardianStudents.init( { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, guardianId: { type: DataTypes.UUID, allowNull: false }, studentId: { type: DataTypes.UUID, allowNull: false }, relationship: { type: DataTypes.TEXT, allowNull: 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: 'guardian_students', timestamps: true, paranoid: true, freezeTableName: true, }, ); return GuardianStudents; }