Dealer_Onboarding_Backend/src/database/models/Document.ts

115 lines
3.2 KiB
TypeScript

import { Model, DataTypes, Sequelize } from 'sequelize';
import { REQUEST_TYPES, DOCUMENT_TYPES } from '../../common/config/constants.js';
export interface DocumentAttributes {
id: string;
applicationId: string | null;
dealerId: string | null;
requestId: string | null; // Compatibility
requestType: string | null; // Compatibility
documentType: string;
fileName: string;
filePath: string;
fileSize: number | null;
mimeType: string | null;
stage: string | null;
status: string;
uploadedBy: string | null;
}
export interface DocumentInstance extends Model<DocumentAttributes>, DocumentAttributes { }
export default (sequelize: Sequelize) => {
const Document = sequelize.define<DocumentInstance>('Document', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
applicationId: {
type: DataTypes.UUID,
allowNull: true,
references: {
model: 'applications',
key: 'id'
}
},
dealerId: {
type: DataTypes.UUID,
allowNull: true,
references: {
model: 'dealers',
key: 'id'
}
},
requestId: {
type: DataTypes.UUID,
allowNull: true
},
requestType: {
type: DataTypes.STRING,
allowNull: true
},
documentType: {
type: DataTypes.STRING,
allowNull: false
},
fileName: {
type: DataTypes.STRING,
allowNull: false
},
filePath: {
type: DataTypes.STRING,
allowNull: false
},
fileSize: {
type: DataTypes.INTEGER,
allowNull: true
},
mimeType: {
type: DataTypes.STRING,
allowNull: true
},
stage: {
type: DataTypes.STRING,
allowNull: true
},
status: {
type: DataTypes.STRING,
defaultValue: 'active'
},
uploadedBy: {
type: DataTypes.UUID,
allowNull: true,
references: {
model: 'users',
key: 'id'
}
}
}, {
tableName: 'documents',
timestamps: true,
indexes: [
{ fields: ['applicationId'] },
{ fields: ['dealerId'] },
{ fields: ['requestId'] }
]
});
(Document as any).associate = (models: any) => {
Document.belongsTo(models.User, { foreignKey: 'uploadedBy', as: 'uploader' });
Document.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
Document.belongsTo(models.Dealer, { foreignKey: 'dealerId', as: 'dealer' });
Document.hasMany(models.DocumentVersion, { foreignKey: 'documentId', as: 'versions' });
Document.belongsToMany(models.Worknote, {
through: models.WorkNoteAttachment,
foreignKey: 'documentId',
otherKey: 'noteId',
as: 'workNotes'
});
};
return Document;
};