38546-vm/backend/src/db/api/reorder_rules.js
2026-02-17 23:49:42 +00:00

637 lines
16 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 Reorder_rulesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reorder_rules = await db.reorder_rules.create(
{
id: data.id || undefined,
min_quantity: data.min_quantity
||
null
,
max_quantity: data.max_quantity
||
null
,
reorder_point: data.reorder_point
||
null
,
reorder_quantity: data.reorder_quantity
||
null
,
lead_time_days: data.lead_time_days
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await reorder_rules.setProduct( data.product || null, {
transaction,
});
await reorder_rules.setWarehouse( data.warehouse || null, {
transaction,
});
await reorder_rules.setStorage_location( data.storage_location || null, {
transaction,
});
return reorder_rules;
}
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 reorder_rulesData = data.map((item, index) => ({
id: item.id || undefined,
min_quantity: item.min_quantity
||
null
,
max_quantity: item.max_quantity
||
null
,
reorder_point: item.reorder_point
||
null
,
reorder_quantity: item.reorder_quantity
||
null
,
lead_time_days: item.lead_time_days
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const reorder_rules = await db.reorder_rules.bulkCreate(reorder_rulesData, { transaction });
// For each item created, replace relation files
return reorder_rules;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reorder_rules = await db.reorder_rules.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.min_quantity !== undefined) updatePayload.min_quantity = data.min_quantity;
if (data.max_quantity !== undefined) updatePayload.max_quantity = data.max_quantity;
if (data.reorder_point !== undefined) updatePayload.reorder_point = data.reorder_point;
if (data.reorder_quantity !== undefined) updatePayload.reorder_quantity = data.reorder_quantity;
if (data.lead_time_days !== undefined) updatePayload.lead_time_days = data.lead_time_days;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await reorder_rules.update(updatePayload, {transaction});
if (data.product !== undefined) {
await reorder_rules.setProduct(
data.product,
{ transaction }
);
}
if (data.warehouse !== undefined) {
await reorder_rules.setWarehouse(
data.warehouse,
{ transaction }
);
}
if (data.storage_location !== undefined) {
await reorder_rules.setStorage_location(
data.storage_location,
{ transaction }
);
}
return reorder_rules;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reorder_rules = await db.reorder_rules.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of reorder_rules) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of reorder_rules) {
await record.destroy({transaction});
}
});
return reorder_rules;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reorder_rules = await db.reorder_rules.findByPk(id, options);
await reorder_rules.update({
deletedBy: currentUser.id
}, {
transaction,
});
await reorder_rules.destroy({
transaction
});
return reorder_rules;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const reorder_rules = await db.reorder_rules.findOne(
{ where },
{ transaction },
);
if (!reorder_rules) {
return reorder_rules;
}
const output = reorder_rules.get({plain: true});
output.product = await reorder_rules.getProduct({
transaction
});
output.warehouse = await reorder_rules.getWarehouse({
transaction
});
output.storage_location = await reorder_rules.getStorage_location({
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.products,
as: 'product',
where: filter.product ? {
[Op.or]: [
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.warehouses,
as: 'warehouse',
where: filter.warehouse ? {
[Op.or]: [
{ id: { [Op.in]: filter.warehouse.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.warehouse.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.storage_locations,
as: 'storage_location',
where: filter.storage_location ? {
[Op.or]: [
{ id: { [Op.in]: filter.storage_location.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.storage_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.min_quantityRange) {
const [start, end] = filter.min_quantityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
min_quantity: {
...where.min_quantity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
min_quantity: {
...where.min_quantity,
[Op.lte]: end,
},
};
}
}
if (filter.max_quantityRange) {
const [start, end] = filter.max_quantityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_quantity: {
...where.max_quantity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_quantity: {
...where.max_quantity,
[Op.lte]: end,
},
};
}
}
if (filter.reorder_pointRange) {
const [start, end] = filter.reorder_pointRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
reorder_point: {
...where.reorder_point,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
reorder_point: {
...where.reorder_point,
[Op.lte]: end,
},
};
}
}
if (filter.reorder_quantityRange) {
const [start, end] = filter.reorder_quantityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
reorder_quantity: {
...where.reorder_quantity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
reorder_quantity: {
...where.reorder_quantity,
[Op.lte]: end,
},
};
}
}
if (filter.lead_time_daysRange) {
const [start, end] = filter.lead_time_daysRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
lead_time_days: {
...where.lead_time_days,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
lead_time_days: {
...where.lead_time_days,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.reorder_rules.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(
'reorder_rules',
'product',
query,
),
],
};
}
const records = await db.reorder_rules.findAll({
attributes: [ 'id', 'product' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['product', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.product,
}));
}
};