32101/backend/src/db/api/testdescs.js
2025-06-08 17:31:35 +00:00

852 lines
22 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 TestdescsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const testdescs = await db.testdescs.create(
{
id: data.id || undefined,
testno: data.testno || null,
testdesc: data.testdesc || null,
testrangem: data.testrangem || null,
testrangef: data.testrangef || null,
testunit: data.testunit || null,
comments: data.comments || null,
testcost: data.testcost || null,
testcost2: data.testcost2 || null,
testcost3: data.testcost3 || null,
min_m: data.min_m || null,
max_m: data.max_m || null,
min_f: data.min_f || null,
max_f: data.max_f || null,
min_m_note: data.min_m_note || null,
max_m_note: data.max_m_note || null,
min_f_note: data.min_f_note || null,
max_f_note: data.max_f_note || null,
test_status: data.test_status || null,
is_title: data.is_title || null,
is_describtion: data.is_describtion || null,
is_ratio: data.is_ratio || null,
var1: data.var1 || null,
var2: data.var2 || null,
convert_factor: data.convert_factor || null,
g_testno: data.g_testno || null,
remain_flg: data.remain_flg || null,
report_type: data.report_type || null,
acc_no: data.acc_no || null,
time_needed_cnt: data.time_needed_cnt || null,
time_needed_unit: data.time_needed_unit || null,
out_test: data.out_test || null,
estimated_time: data.estimated_time || null,
time_unit: data.time_unit || null,
isratio: data.isratio || null,
branch_no: data.branch_no || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await testdescs.setTesttype(data.testtype || null, {
transaction,
});
await testdescs.setTestgroup(data.testgroup || null, {
transaction,
});
return testdescs;
}
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 testdescsData = data.map((item, index) => ({
id: item.id || undefined,
testno: item.testno || null,
testdesc: item.testdesc || null,
testrangem: item.testrangem || null,
testrangef: item.testrangef || null,
testunit: item.testunit || null,
comments: item.comments || null,
testcost: item.testcost || null,
testcost2: item.testcost2 || null,
testcost3: item.testcost3 || null,
min_m: item.min_m || null,
max_m: item.max_m || null,
min_f: item.min_f || null,
max_f: item.max_f || null,
min_m_note: item.min_m_note || null,
max_m_note: item.max_m_note || null,
min_f_note: item.min_f_note || null,
max_f_note: item.max_f_note || null,
test_status: item.test_status || null,
is_title: item.is_title || null,
is_describtion: item.is_describtion || null,
is_ratio: item.is_ratio || null,
var1: item.var1 || null,
var2: item.var2 || null,
convert_factor: item.convert_factor || null,
g_testno: item.g_testno || null,
remain_flg: item.remain_flg || null,
report_type: item.report_type || null,
acc_no: item.acc_no || null,
time_needed_cnt: item.time_needed_cnt || null,
time_needed_unit: item.time_needed_unit || null,
out_test: item.out_test || null,
estimated_time: item.estimated_time || null,
time_unit: item.time_unit || null,
isratio: item.isratio || null,
branch_no: item.branch_no || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const testdescs = await db.testdescs.bulkCreate(testdescsData, {
transaction,
});
// For each item created, replace relation files
return testdescs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const testdescs = await db.testdescs.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.testno !== undefined) updatePayload.testno = data.testno;
if (data.testdesc !== undefined) updatePayload.testdesc = data.testdesc;
if (data.testrangem !== undefined)
updatePayload.testrangem = data.testrangem;
if (data.testrangef !== undefined)
updatePayload.testrangef = data.testrangef;
if (data.testunit !== undefined) updatePayload.testunit = data.testunit;
if (data.comments !== undefined) updatePayload.comments = data.comments;
if (data.testcost !== undefined) updatePayload.testcost = data.testcost;
if (data.testcost2 !== undefined) updatePayload.testcost2 = data.testcost2;
if (data.testcost3 !== undefined) updatePayload.testcost3 = data.testcost3;
if (data.min_m !== undefined) updatePayload.min_m = data.min_m;
if (data.max_m !== undefined) updatePayload.max_m = data.max_m;
if (data.min_f !== undefined) updatePayload.min_f = data.min_f;
if (data.max_f !== undefined) updatePayload.max_f = data.max_f;
if (data.min_m_note !== undefined)
updatePayload.min_m_note = data.min_m_note;
if (data.max_m_note !== undefined)
updatePayload.max_m_note = data.max_m_note;
if (data.min_f_note !== undefined)
updatePayload.min_f_note = data.min_f_note;
if (data.max_f_note !== undefined)
updatePayload.max_f_note = data.max_f_note;
if (data.test_status !== undefined)
updatePayload.test_status = data.test_status;
if (data.is_title !== undefined) updatePayload.is_title = data.is_title;
if (data.is_describtion !== undefined)
updatePayload.is_describtion = data.is_describtion;
if (data.is_ratio !== undefined) updatePayload.is_ratio = data.is_ratio;
if (data.var1 !== undefined) updatePayload.var1 = data.var1;
if (data.var2 !== undefined) updatePayload.var2 = data.var2;
if (data.convert_factor !== undefined)
updatePayload.convert_factor = data.convert_factor;
if (data.g_testno !== undefined) updatePayload.g_testno = data.g_testno;
if (data.remain_flg !== undefined)
updatePayload.remain_flg = data.remain_flg;
if (data.report_type !== undefined)
updatePayload.report_type = data.report_type;
if (data.acc_no !== undefined) updatePayload.acc_no = data.acc_no;
if (data.time_needed_cnt !== undefined)
updatePayload.time_needed_cnt = data.time_needed_cnt;
if (data.time_needed_unit !== undefined)
updatePayload.time_needed_unit = data.time_needed_unit;
if (data.out_test !== undefined) updatePayload.out_test = data.out_test;
if (data.estimated_time !== undefined)
updatePayload.estimated_time = data.estimated_time;
if (data.time_unit !== undefined) updatePayload.time_unit = data.time_unit;
if (data.isratio !== undefined) updatePayload.isratio = data.isratio;
if (data.branch_no !== undefined) updatePayload.branch_no = data.branch_no;
updatePayload.updatedById = currentUser.id;
await testdescs.update(updatePayload, { transaction });
if (data.testtype !== undefined) {
await testdescs.setTesttype(
data.testtype,
{ transaction },
);
}
if (data.testgroup !== undefined) {
await testdescs.setTestgroup(
data.testgroup,
{ transaction },
);
}
return testdescs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const testdescs = await db.testdescs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of testdescs) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of testdescs) {
await record.destroy({ transaction });
}
});
return testdescs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const testdescs = await db.testdescs.findByPk(id, options);
await testdescs.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await testdescs.destroy({
transaction,
});
return testdescs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const testdescs = await db.testdescs.findOne({ where }, { transaction });
if (!testdescs) {
return testdescs;
}
const output = testdescs.get({ plain: true });
output.testtype = await testdescs.getTesttype({
transaction,
});
output.testgroup = await testdescs.getTestgroup({
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.testtypes,
as: 'testtype',
where: filter.testtype
? {
[Op.or]: [
{
id: {
[Op.in]: filter.testtype
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
type_name: {
[Op.or]: filter.testtype
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
{
model: db.group_types,
as: 'testgroup',
where: filter.testgroup
? {
[Op.or]: [
{
id: {
[Op.in]: filter.testgroup
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
g_name: {
[Op.or]: filter.testgroup
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.testdesc) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'testdesc', filter.testdesc),
};
}
if (filter.testrangem) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'testrangem', filter.testrangem),
};
}
if (filter.testrangef) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'testrangef', filter.testrangef),
};
}
if (filter.testunit) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'testunit', filter.testunit),
};
}
if (filter.comments) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'comments', filter.comments),
};
}
if (filter.min_m) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'min_m', filter.min_m),
};
}
if (filter.max_m) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'max_m', filter.max_m),
};
}
if (filter.min_f) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'min_f', filter.min_f),
};
}
if (filter.max_f) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'max_f', filter.max_f),
};
}
if (filter.min_m_note) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'min_m_note', filter.min_m_note),
};
}
if (filter.max_m_note) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'max_m_note', filter.max_m_note),
};
}
if (filter.min_f_note) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'min_f_note', filter.min_f_note),
};
}
if (filter.max_f_note) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'max_f_note', filter.max_f_note),
};
}
if (filter.test_status) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'test_status', filter.test_status),
};
}
if (filter.is_title) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'is_title', filter.is_title),
};
}
if (filter.is_describtion) {
where = {
...where,
[Op.and]: Utils.ilike(
'testdescs',
'is_describtion',
filter.is_describtion,
),
};
}
if (filter.is_ratio) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'is_ratio', filter.is_ratio),
};
}
if (filter.var1) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'var1', filter.var1),
};
}
if (filter.var2) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'var2', filter.var2),
};
}
if (filter.remain_flg) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'remain_flg', filter.remain_flg),
};
}
if (filter.report_type) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'report_type', filter.report_type),
};
}
if (filter.acc_no) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'acc_no', filter.acc_no),
};
}
if (filter.time_needed_unit) {
where = {
...where,
[Op.and]: Utils.ilike(
'testdescs',
'time_needed_unit',
filter.time_needed_unit,
),
};
}
if (filter.out_test) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'out_test', filter.out_test),
};
}
if (filter.estimated_time) {
where = {
...where,
[Op.and]: Utils.ilike(
'testdescs',
'estimated_time',
filter.estimated_time,
),
};
}
if (filter.time_unit) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'time_unit', filter.time_unit),
};
}
if (filter.isratio) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'isratio', filter.isratio),
};
}
if (filter.branch_no) {
where = {
...where,
[Op.and]: Utils.ilike('testdescs', 'branch_no', filter.branch_no),
};
}
if (filter.testnoRange) {
const [start, end] = filter.testnoRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
testno: {
...where.testno,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
testno: {
...where.testno,
[Op.lte]: end,
},
};
}
}
if (filter.testcostRange) {
const [start, end] = filter.testcostRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
testcost: {
...where.testcost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
testcost: {
...where.testcost,
[Op.lte]: end,
},
};
}
}
if (filter.testcost2Range) {
const [start, end] = filter.testcost2Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
testcost2: {
...where.testcost2,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
testcost2: {
...where.testcost2,
[Op.lte]: end,
},
};
}
}
if (filter.testcost3Range) {
const [start, end] = filter.testcost3Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
testcost3: {
...where.testcost3,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
testcost3: {
...where.testcost3,
[Op.lte]: end,
},
};
}
}
if (filter.convert_factorRange) {
const [start, end] = filter.convert_factorRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
convert_factor: {
...where.convert_factor,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
convert_factor: {
...where.convert_factor,
[Op.lte]: end,
},
};
}
}
if (filter.g_testnoRange) {
const [start, end] = filter.g_testnoRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
g_testno: {
...where.g_testno,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
g_testno: {
...where.g_testno,
[Op.lte]: end,
},
};
}
}
if (filter.time_needed_cntRange) {
const [start, end] = filter.time_needed_cntRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
time_needed_cnt: {
...where.time_needed_cnt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
time_needed_cnt: {
...where.time_needed_cnt,
[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.testdescs.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('testdescs', 'testdesc', query),
],
};
}
const records = await db.testdescs.findAll({
attributes: ['id', 'testdesc'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['testdesc', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.testdesc,
}));
}
};