83 lines
1.9 KiB
TypeScript
83 lines
1.9 KiB
TypeScript
import { sequelize } from '@config/database';
|
|
|
|
// Import all models
|
|
import { User } from './User';
|
|
import { WorkflowRequest } from './WorkflowRequest';
|
|
import { ApprovalLevel } from './ApprovalLevel';
|
|
import { Participant } from './Participant';
|
|
import { Document } from './Document';
|
|
import { Subscription } from './Subscription';
|
|
import { Activity } from './Activity';
|
|
import { WorkNote } from './WorkNote';
|
|
import { WorkNoteAttachment } from './WorkNoteAttachment';
|
|
|
|
// Define associations
|
|
const defineAssociations = () => {
|
|
// User associations
|
|
User.hasMany(WorkflowRequest, {
|
|
as: 'initiatedRequests',
|
|
foreignKey: 'initiatorId',
|
|
sourceKey: 'userId'
|
|
});
|
|
|
|
User.hasMany(ApprovalLevel, {
|
|
as: 'approvalLevels',
|
|
foreignKey: 'approverId',
|
|
sourceKey: 'userId'
|
|
});
|
|
|
|
User.hasMany(Participant, {
|
|
as: 'participations',
|
|
foreignKey: 'userId',
|
|
sourceKey: 'userId'
|
|
});
|
|
|
|
User.hasMany(Document, {
|
|
as: 'uploadedDocuments',
|
|
foreignKey: 'uploadedBy',
|
|
sourceKey: 'userId'
|
|
});
|
|
|
|
// WorkflowRequest associations
|
|
WorkflowRequest.hasMany(ApprovalLevel, {
|
|
as: 'approvalLevels',
|
|
foreignKey: 'requestId',
|
|
sourceKey: 'requestId'
|
|
});
|
|
|
|
WorkflowRequest.hasMany(Participant, {
|
|
as: 'participants',
|
|
foreignKey: 'requestId',
|
|
sourceKey: 'requestId'
|
|
});
|
|
|
|
WorkflowRequest.hasMany(Document, {
|
|
as: 'documents',
|
|
foreignKey: 'requestId',
|
|
sourceKey: 'requestId'
|
|
});
|
|
|
|
// Note: belongsTo associations are defined in individual model files to avoid duplicate alias conflicts
|
|
// Only hasMany associations from WorkflowRequest are defined here since they're one-way
|
|
};
|
|
|
|
// Initialize associations
|
|
defineAssociations();
|
|
|
|
// Export models and sequelize instance
|
|
export {
|
|
sequelize,
|
|
User,
|
|
WorkflowRequest,
|
|
ApprovalLevel,
|
|
Participant,
|
|
Document,
|
|
Subscription,
|
|
Activity,
|
|
WorkNote,
|
|
WorkNoteAttachment
|
|
};
|
|
|
|
// Export default sequelize instance
|
|
export default sequelize;
|