32683/backend/src/db/api/analytics.js
2025-07-07 21:31:52 +00:00

439 lines
11 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 AnalyticsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const analytics = await db.analytics.create(
{
id: data.id || undefined,
date: data.date || null,
new_leads: data.new_leads || null,
processed_leads: data.processed_leads || null,
rejected_leads: data.rejected_leads || null,
deferred_leads: data.deferred_leads || null,
average_response_time: data.average_response_time || null,
conversion_rate: data.conversion_rate || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return analytics;
}
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 analyticsData = data.map((item, index) => ({
id: item.id || undefined,
date: item.date || null,
new_leads: item.new_leads || null,
processed_leads: item.processed_leads || null,
rejected_leads: item.rejected_leads || null,
deferred_leads: item.deferred_leads || null,
average_response_time: item.average_response_time || null,
conversion_rate: item.conversion_rate || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const analytics = await db.analytics.bulkCreate(analyticsData, {
transaction,
});
// For each item created, replace relation files
return analytics;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const analytics = await db.analytics.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.date !== undefined) updatePayload.date = data.date;
if (data.new_leads !== undefined) updatePayload.new_leads = data.new_leads;
if (data.processed_leads !== undefined)
updatePayload.processed_leads = data.processed_leads;
if (data.rejected_leads !== undefined)
updatePayload.rejected_leads = data.rejected_leads;
if (data.deferred_leads !== undefined)
updatePayload.deferred_leads = data.deferred_leads;
if (data.average_response_time !== undefined)
updatePayload.average_response_time = data.average_response_time;
if (data.conversion_rate !== undefined)
updatePayload.conversion_rate = data.conversion_rate;
updatePayload.updatedById = currentUser.id;
await analytics.update(updatePayload, { transaction });
return analytics;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const analytics = await db.analytics.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of analytics) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of analytics) {
await record.destroy({ transaction });
}
});
return analytics;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const analytics = await db.analytics.findByPk(id, options);
await analytics.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await analytics.destroy({
transaction,
});
return analytics;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const analytics = await db.analytics.findOne({ where }, { transaction });
if (!analytics) {
return analytics;
}
const output = analytics.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.dateRange) {
const [start, end] = filter.dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
date: {
...where.date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
date: {
...where.date,
[Op.lte]: end,
},
};
}
}
if (filter.new_leadsRange) {
const [start, end] = filter.new_leadsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
new_leads: {
...where.new_leads,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
new_leads: {
...where.new_leads,
[Op.lte]: end,
},
};
}
}
if (filter.processed_leadsRange) {
const [start, end] = filter.processed_leadsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
processed_leads: {
...where.processed_leads,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
processed_leads: {
...where.processed_leads,
[Op.lte]: end,
},
};
}
}
if (filter.rejected_leadsRange) {
const [start, end] = filter.rejected_leadsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rejected_leads: {
...where.rejected_leads,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rejected_leads: {
...where.rejected_leads,
[Op.lte]: end,
},
};
}
}
if (filter.deferred_leadsRange) {
const [start, end] = filter.deferred_leadsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
deferred_leads: {
...where.deferred_leads,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
deferred_leads: {
...where.deferred_leads,
[Op.lte]: end,
},
};
}
}
if (filter.average_response_timeRange) {
const [start, end] = filter.average_response_timeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
average_response_time: {
...where.average_response_time,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
average_response_time: {
...where.average_response_time,
[Op.lte]: end,
},
};
}
}
if (filter.conversion_rateRange) {
const [start, end] = filter.conversion_rateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
conversion_rate: {
...where.conversion_rate,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
conversion_rate: {
...where.conversion_rate,
[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.analytics.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('analytics', 'date', query),
],
};
}
const records = await db.analytics.findAll({
attributes: ['id', 'date'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['date', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.date,
}));
}
};