33428/backend/src/db/api/sos_triggers.js
2025-08-16 12:42:05 +00:00

301 lines
7.2 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 Sos_triggersDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const sos_triggers = await db.sos_triggers.create(
{
id: data.id || undefined,
trigger_type: data.trigger_type || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await sos_triggers.setSos_history(data.sos_history || null, {
transaction,
});
return sos_triggers;
}
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 sos_triggersData = data.map((item, index) => ({
id: item.id || undefined,
trigger_type: item.trigger_type || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const sos_triggers = await db.sos_triggers.bulkCreate(sos_triggersData, {
transaction,
});
// For each item created, replace relation files
return sos_triggers;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const sos_triggers = await db.sos_triggers.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.trigger_type !== undefined)
updatePayload.trigger_type = data.trigger_type;
updatePayload.updatedById = currentUser.id;
await sos_triggers.update(updatePayload, { transaction });
if (data.sos_history !== undefined) {
await sos_triggers.setSos_history(
data.sos_history,
{ transaction },
);
}
return sos_triggers;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const sos_triggers = await db.sos_triggers.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of sos_triggers) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of sos_triggers) {
await record.destroy({ transaction });
}
});
return sos_triggers;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const sos_triggers = await db.sos_triggers.findByPk(id, options);
await sos_triggers.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await sos_triggers.destroy({
transaction,
});
return sos_triggers;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const sos_triggers = await db.sos_triggers.findOne(
{ where },
{ transaction },
);
if (!sos_triggers) {
return sos_triggers;
}
const output = sos_triggers.get({ plain: true });
output.sos_history = await sos_triggers.getSos_history({
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.sos_histories,
as: 'sos_history',
where: filter.sos_history
? {
[Op.or]: [
{
id: {
[Op.in]: filter.sos_history
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
location: {
[Op.or]: filter.sos_history
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.trigger_type) {
where = {
...where,
trigger_type: filter.trigger_type,
};
}
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.sos_triggers.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('sos_triggers', 'trigger_type', query),
],
};
}
const records = await db.sos_triggers.findAll({
attributes: ['id', 'trigger_type'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['trigger_type', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.trigger_type,
}));
}
};