603 lines
14 KiB
JavaScript
603 lines
14 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 Production_lotsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const production_lots = await db.production_lots.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
produced_at: data.produced_at
|
|
||
|
|
null
|
|
,
|
|
|
|
quantity_produced: data.quantity_produced
|
|
||
|
|
null
|
|
,
|
|
|
|
uom: data.uom
|
|
||
|
|
null
|
|
,
|
|
|
|
disposition: data.disposition
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await production_lots.setWork_order( data.work_order || null, {
|
|
transaction,
|
|
});
|
|
|
|
await production_lots.setLot( data.lot || null, {
|
|
transaction,
|
|
});
|
|
|
|
await production_lots.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return production_lots;
|
|
}
|
|
|
|
|
|
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 production_lotsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
produced_at: item.produced_at
|
|
||
|
|
null
|
|
,
|
|
|
|
quantity_produced: item.quantity_produced
|
|
||
|
|
null
|
|
,
|
|
|
|
uom: item.uom
|
|
||
|
|
null
|
|
,
|
|
|
|
disposition: item.disposition
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const production_lots = await db.production_lots.bulkCreate(production_lotsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return production_lots;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const production_lots = await db.production_lots.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.produced_at !== undefined) updatePayload.produced_at = data.produced_at;
|
|
|
|
|
|
if (data.quantity_produced !== undefined) updatePayload.quantity_produced = data.quantity_produced;
|
|
|
|
|
|
if (data.uom !== undefined) updatePayload.uom = data.uom;
|
|
|
|
|
|
if (data.disposition !== undefined) updatePayload.disposition = data.disposition;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await production_lots.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.work_order !== undefined) {
|
|
await production_lots.setWork_order(
|
|
|
|
data.work_order,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.lot !== undefined) {
|
|
await production_lots.setLot(
|
|
|
|
data.lot,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await production_lots.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return production_lots;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const production_lots = await db.production_lots.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of production_lots) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of production_lots) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return production_lots;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const production_lots = await db.production_lots.findByPk(id, options);
|
|
|
|
await production_lots.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await production_lots.destroy({
|
|
transaction
|
|
});
|
|
|
|
return production_lots;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const production_lots = await db.production_lots.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!production_lots) {
|
|
return production_lots;
|
|
}
|
|
|
|
const output = production_lots.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.work_order = await production_lots.getWork_order({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.lot = await production_lots.getLot({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await production_lots.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.work_orders,
|
|
as: 'work_order',
|
|
|
|
where: filter.work_order ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.work_order.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
work_order_number: {
|
|
[Op.or]: filter.work_order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.lots,
|
|
as: 'lot',
|
|
|
|
where: filter.lot ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.lot.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
lot_number: {
|
|
[Op.or]: filter.lot.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.uom) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'production_lots',
|
|
'uom',
|
|
filter.uom,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'production_lots',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.produced_atRange) {
|
|
const [start, end] = filter.produced_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
produced_at: {
|
|
...where.produced_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
produced_at: {
|
|
...where.produced_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.quantity_producedRange) {
|
|
const [start, end] = filter.quantity_producedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
quantity_produced: {
|
|
...where.quantity_produced,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
quantity_produced: {
|
|
...where.quantity_produced,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.disposition) {
|
|
where = {
|
|
...where,
|
|
disposition: filter.disposition,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
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.production_lots.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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'production_lots',
|
|
'notes',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.production_lots.findAll({
|
|
attributes: [ 'id', 'notes' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['notes', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.notes,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|