37943-vm/backend/src/db/api/optimization_runs.js
2026-01-29 13:53:43 +00:00

691 lines
18 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Optimization_runsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const optimization_runs = await db.optimization_runs.create(
{
id: data.id || undefined,
name: data.name
||
null
,
run_start: data.run_start
||
null
,
run_end: data.run_end
||
null
,
assigned_count: data.assigned_count
||
null
,
efficiency: data.efficiency
||
null
,
full_pieces_count: data.full_pieces_count
||
null
,
cut_pieces_count: data.cut_pieces_count
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await optimization_runs.setCreated_by( data.created_by || null, {
transaction,
});
await optimization_runs.setPanel( data.panel || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.optimization_runs.getTableName(),
belongsToColumn: 'scrap_snapshot',
belongsToId: optimization_runs.id,
},
data.scrap_snapshot,
options,
);
return optimization_runs;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const optimization_runsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
run_start: item.run_start
||
null
,
run_end: item.run_end
||
null
,
assigned_count: item.assigned_count
||
null
,
efficiency: item.efficiency
||
null
,
full_pieces_count: item.full_pieces_count
||
null
,
cut_pieces_count: item.cut_pieces_count
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const optimization_runs = await db.optimization_runs.bulkCreate(optimization_runsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < optimization_runs.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.optimization_runs.getTableName(),
belongsToColumn: 'scrap_snapshot',
belongsToId: optimization_runs[i].id,
},
data[i].scrap_snapshot,
options,
);
}
return optimization_runs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const optimization_runs = await db.optimization_runs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.run_start !== undefined) updatePayload.run_start = data.run_start;
if (data.run_end !== undefined) updatePayload.run_end = data.run_end;
if (data.assigned_count !== undefined) updatePayload.assigned_count = data.assigned_count;
if (data.efficiency !== undefined) updatePayload.efficiency = data.efficiency;
if (data.full_pieces_count !== undefined) updatePayload.full_pieces_count = data.full_pieces_count;
if (data.cut_pieces_count !== undefined) updatePayload.cut_pieces_count = data.cut_pieces_count;
updatePayload.updatedById = currentUser.id;
await optimization_runs.update(updatePayload, {transaction});
if (data.created_by !== undefined) {
await optimization_runs.setCreated_by(
data.created_by,
{ transaction }
);
}
if (data.panel !== undefined) {
await optimization_runs.setPanel(
data.panel,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.optimization_runs.getTableName(),
belongsToColumn: 'scrap_snapshot',
belongsToId: optimization_runs.id,
},
data.scrap_snapshot,
options,
);
return optimization_runs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const optimization_runs = await db.optimization_runs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of optimization_runs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of optimization_runs) {
await record.destroy({transaction});
}
});
return optimization_runs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const optimization_runs = await db.optimization_runs.findByPk(id, options);
await optimization_runs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await optimization_runs.destroy({
transaction
});
return optimization_runs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const optimization_runs = await db.optimization_runs.findOne(
{ where },
{ transaction },
);
if (!optimization_runs) {
return optimization_runs;
}
const output = optimization_runs.get({plain: true});
output.created_by = await optimization_runs.getCreated_by({
transaction
});
output.panel = await optimization_runs.getPanel({
transaction
});
output.scrap_snapshot = await optimization_runs.getScrap_snapshot({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.users,
as: 'created_by',
where: filter.created_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.created_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.created_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.panels,
as: 'panel',
where: filter.panel ? {
[Op.or]: [
{ id: { [Op.in]: filter.panel.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.panel.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'scrap_snapshot',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'optimization_runs',
'name',
filter.name,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
run_start: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
run_end: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.run_startRange) {
const [start, end] = filter.run_startRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
run_start: {
...where.run_start,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
run_start: {
...where.run_start,
[Op.lte]: end,
},
};
}
}
if (filter.run_endRange) {
const [start, end] = filter.run_endRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
run_end: {
...where.run_end,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
run_end: {
...where.run_end,
[Op.lte]: end,
},
};
}
}
if (filter.assigned_countRange) {
const [start, end] = filter.assigned_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
assigned_count: {
...where.assigned_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
assigned_count: {
...where.assigned_count,
[Op.lte]: end,
},
};
}
}
if (filter.efficiencyRange) {
const [start, end] = filter.efficiencyRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
efficiency: {
...where.efficiency,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
efficiency: {
...where.efficiency,
[Op.lte]: end,
},
};
}
}
if (filter.full_pieces_countRange) {
const [start, end] = filter.full_pieces_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
full_pieces_count: {
...where.full_pieces_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
full_pieces_count: {
...where.full_pieces_count,
[Op.lte]: end,
},
};
}
}
if (filter.cut_pieces_countRange) {
const [start, end] = filter.cut_pieces_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
cut_pieces_count: {
...where.cut_pieces_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
cut_pieces_count: {
...where.cut_pieces_count,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.optimization_runs.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'optimization_runs',
'name',
query,
),
],
};
}
const records = await db.optimization_runs.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};