38948-vm/backend/src/db/api/standards_references.js
2026-03-03 13:23:09 +00:00

474 lines
12 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 Standards_referencesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const standards_references = await db.standards_references.create(
{
id: data.id || undefined,
standard_name: data.standard_name
||
null
,
standard_code: data.standard_code
||
null
,
relevance_summary: data.relevance_summary
||
null
,
link_url: data.link_url
||
null
,
effective_date: data.effective_date
||
null
,
is_featured: data.is_featured
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return standards_references;
}
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 standards_referencesData = data.map((item, index) => ({
id: item.id || undefined,
standard_name: item.standard_name
||
null
,
standard_code: item.standard_code
||
null
,
relevance_summary: item.relevance_summary
||
null
,
link_url: item.link_url
||
null
,
effective_date: item.effective_date
||
null
,
is_featured: item.is_featured
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const standards_references = await db.standards_references.bulkCreate(standards_referencesData, { transaction });
// For each item created, replace relation files
return standards_references;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const standards_references = await db.standards_references.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.standard_name !== undefined) updatePayload.standard_name = data.standard_name;
if (data.standard_code !== undefined) updatePayload.standard_code = data.standard_code;
if (data.relevance_summary !== undefined) updatePayload.relevance_summary = data.relevance_summary;
if (data.link_url !== undefined) updatePayload.link_url = data.link_url;
if (data.effective_date !== undefined) updatePayload.effective_date = data.effective_date;
if (data.is_featured !== undefined) updatePayload.is_featured = data.is_featured;
updatePayload.updatedById = currentUser.id;
await standards_references.update(updatePayload, {transaction});
return standards_references;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const standards_references = await db.standards_references.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of standards_references) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of standards_references) {
await record.destroy({transaction});
}
});
return standards_references;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const standards_references = await db.standards_references.findByPk(id, options);
await standards_references.update({
deletedBy: currentUser.id
}, {
transaction,
});
await standards_references.destroy({
transaction
});
return standards_references;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const standards_references = await db.standards_references.findOne(
{ where },
{ transaction },
);
if (!standards_references) {
return standards_references;
}
const output = standards_references.get({plain: true});
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.standard_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'standards_references',
'standard_name',
filter.standard_name,
),
};
}
if (filter.standard_code) {
where = {
...where,
[Op.and]: Utils.ilike(
'standards_references',
'standard_code',
filter.standard_code,
),
};
}
if (filter.relevance_summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'standards_references',
'relevance_summary',
filter.relevance_summary,
),
};
}
if (filter.link_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'standards_references',
'link_url',
filter.link_url,
),
};
}
if (filter.effective_dateRange) {
const [start, end] = filter.effective_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
effective_date: {
...where.effective_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
effective_date: {
...where.effective_date,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_featured) {
where = {
...where,
is_featured: filter.is_featured,
};
}
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.standards_references.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(
'standards_references',
'standard_code',
query,
),
],
};
}
const records = await db.standards_references.findAll({
attributes: [ 'id', 'standard_code' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['standard_code', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.standard_code,
}));
}
};