39379-vm/backend/src/db/models/bulk_runs.js
2026-03-30 02:08:44 +00:00

183 lines
2.2 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const bulk_runs = sequelize.define(
'bulk_runs',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
input_text: {
type: DataTypes.TEXT,
},
started_at: {
type: DataTypes.DATE,
},
finished_at: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: [
"queued",
"running",
"completed",
"failed",
"cancelled"
],
},
total_inputs: {
type: DataTypes.INTEGER,
},
processed_count: {
type: DataTypes.INTEGER,
},
success_count: {
type: DataTypes.INTEGER,
},
error_count: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
bulk_runs.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.bulk_runs.hasMany(db.bulk_run_items, {
as: 'bulk_run_items_bulk_run',
foreignKey: {
name: 'bulk_runId',
},
constraints: false,
});
//end loop
db.bulk_runs.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.bulk_runs.hasMany(db.file, {
as: 'export_file',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.bulk_runs.getTableName(),
belongsToColumn: 'export_file',
},
});
db.bulk_runs.belongsTo(db.users, {
as: 'createdBy',
});
db.bulk_runs.belongsTo(db.users, {
as: 'updatedBy',
});
};
return bulk_runs;
};