129 lines
2.6 KiB
TypeScript
129 lines
2.6 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';
|
|
|
|
// 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'
|
|
});
|
|
|
|
// ApprovalLevel associations
|
|
ApprovalLevel.belongsTo(WorkflowRequest, {
|
|
as: 'request',
|
|
foreignKey: 'requestId',
|
|
targetKey: 'requestId'
|
|
});
|
|
|
|
ApprovalLevel.belongsTo(User, {
|
|
as: 'approver',
|
|
foreignKey: 'approverId',
|
|
targetKey: 'userId'
|
|
});
|
|
|
|
// Participant associations
|
|
Participant.belongsTo(WorkflowRequest, {
|
|
as: 'request',
|
|
foreignKey: 'requestId',
|
|
targetKey: 'requestId'
|
|
});
|
|
|
|
Participant.belongsTo(User, {
|
|
as: 'user',
|
|
foreignKey: 'userId',
|
|
targetKey: 'userId'
|
|
});
|
|
|
|
Participant.belongsTo(User, {
|
|
as: 'addedByUser',
|
|
foreignKey: 'addedBy',
|
|
targetKey: 'userId'
|
|
});
|
|
|
|
// Document associations
|
|
Document.belongsTo(WorkflowRequest, {
|
|
as: 'request',
|
|
foreignKey: 'requestId',
|
|
targetKey: 'requestId'
|
|
});
|
|
|
|
Document.belongsTo(User, {
|
|
as: 'uploader',
|
|
foreignKey: 'uploadedBy',
|
|
targetKey: 'userId'
|
|
});
|
|
|
|
Document.belongsTo(Document, {
|
|
as: 'parentDocument',
|
|
foreignKey: 'parentDocumentId',
|
|
targetKey: 'documentId'
|
|
});
|
|
|
|
Document.hasMany(Document, {
|
|
as: 'childDocuments',
|
|
foreignKey: 'parentDocumentId',
|
|
sourceKey: 'documentId'
|
|
});
|
|
};
|
|
|
|
// Initialize associations
|
|
defineAssociations();
|
|
|
|
// Export models and sequelize instance
|
|
export {
|
|
sequelize,
|
|
User,
|
|
WorkflowRequest,
|
|
ApprovalLevel,
|
|
Participant,
|
|
Document
|
|
};
|
|
|
|
// Export default sequelize instance
|
|
export default sequelize;
|