38439-vm/backend/src/db/api/visitor_messages.js
2026-02-15 01:11:13 +00:00

500 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 Visitor_messagesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const visitor_messages = await db.visitor_messages.create(
{
id: data.id || undefined,
name: data.name
||
null
,
email: data.email
||
null
,
subject: data.subject
||
null
,
message: data.message
||
null
,
status: data.status
||
null
,
received_at: data.received_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await visitor_messages.setHandled_by( data.handled_by || null, {
transaction,
});
return visitor_messages;
}
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 visitor_messagesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
email: item.email
||
null
,
subject: item.subject
||
null
,
message: item.message
||
null
,
status: item.status
||
null
,
received_at: item.received_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const visitor_messages = await db.visitor_messages.bulkCreate(visitor_messagesData, { transaction });
// For each item created, replace relation files
return visitor_messages;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const visitor_messages = await db.visitor_messages.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.email !== undefined) updatePayload.email = data.email;
if (data.subject !== undefined) updatePayload.subject = data.subject;
if (data.message !== undefined) updatePayload.message = data.message;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.received_at !== undefined) updatePayload.received_at = data.received_at;
updatePayload.updatedById = currentUser.id;
await visitor_messages.update(updatePayload, {transaction});
if (data.handled_by !== undefined) {
await visitor_messages.setHandled_by(
data.handled_by,
{ transaction }
);
}
return visitor_messages;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const visitor_messages = await db.visitor_messages.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of visitor_messages) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of visitor_messages) {
await record.destroy({transaction});
}
});
return visitor_messages;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const visitor_messages = await db.visitor_messages.findByPk(id, options);
await visitor_messages.update({
deletedBy: currentUser.id
}, {
transaction,
});
await visitor_messages.destroy({
transaction
});
return visitor_messages;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const visitor_messages = await db.visitor_messages.findOne(
{ where },
{ transaction },
);
if (!visitor_messages) {
return visitor_messages;
}
const output = visitor_messages.get({plain: true});
output.handled_by = await visitor_messages.getHandled_by({
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.users,
as: 'handled_by',
where: filter.handled_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.handled_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.handled_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'visitor_messages',
'name',
filter.name,
),
};
}
if (filter.email) {
where = {
...where,
[Op.and]: Utils.ilike(
'visitor_messages',
'email',
filter.email,
),
};
}
if (filter.subject) {
where = {
...where,
[Op.and]: Utils.ilike(
'visitor_messages',
'subject',
filter.subject,
),
};
}
if (filter.message) {
where = {
...where,
[Op.and]: Utils.ilike(
'visitor_messages',
'message',
filter.message,
),
};
}
if (filter.received_atRange) {
const [start, end] = filter.received_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
received_at: {
...where.received_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
received_at: {
...where.received_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.visitor_messages.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(
'visitor_messages',
'subject',
query,
),
],
};
}
const records = await db.visitor_messages.findAll({
attributes: [ 'id', 'subject' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['subject', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.subject,
}));
}
};