719 lines
20 KiB
JavaScript
719 lines
20 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 Customer_health_snapshotsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_health_snapshots = await db.customer_health_snapshots.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
calculated_at: data.calculated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
health_score: data.health_score
|
|
||
|
|
null
|
|
,
|
|
|
|
health_label: data.health_label
|
|
||
|
|
null
|
|
,
|
|
|
|
churn_risk_score: data.churn_risk_score
|
|
||
|
|
null
|
|
,
|
|
|
|
churn_risk_label: data.churn_risk_label
|
|
||
|
|
null
|
|
,
|
|
|
|
sentiment_trend: data.sentiment_trend
|
|
||
|
|
null
|
|
,
|
|
|
|
ticket_volume_30d: data.ticket_volume_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
repeat_issue_count_30d: data.repeat_issue_count_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_first_response_minutes_30d: data.avg_first_response_minutes_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_resolution_hours_30d: data.avg_resolution_hours_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
drivers_summary: data.drivers_summary
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await customer_health_snapshots.setCustomer_account( data.customer_account || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
|
|
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 customer_health_snapshotsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
calculated_at: item.calculated_at
|
|
||
|
|
null
|
|
,
|
|
|
|
health_score: item.health_score
|
|
||
|
|
null
|
|
,
|
|
|
|
health_label: item.health_label
|
|
||
|
|
null
|
|
,
|
|
|
|
churn_risk_score: item.churn_risk_score
|
|
||
|
|
null
|
|
,
|
|
|
|
churn_risk_label: item.churn_risk_label
|
|
||
|
|
null
|
|
,
|
|
|
|
sentiment_trend: item.sentiment_trend
|
|
||
|
|
null
|
|
,
|
|
|
|
ticket_volume_30d: item.ticket_volume_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
repeat_issue_count_30d: item.repeat_issue_count_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_first_response_minutes_30d: item.avg_first_response_minutes_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
avg_resolution_hours_30d: item.avg_resolution_hours_30d
|
|
||
|
|
null
|
|
,
|
|
|
|
drivers_summary: item.drivers_summary
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const customer_health_snapshots = await db.customer_health_snapshots.bulkCreate(customer_health_snapshotsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const customer_health_snapshots = await db.customer_health_snapshots.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.calculated_at !== undefined) updatePayload.calculated_at = data.calculated_at;
|
|
|
|
|
|
if (data.health_score !== undefined) updatePayload.health_score = data.health_score;
|
|
|
|
|
|
if (data.health_label !== undefined) updatePayload.health_label = data.health_label;
|
|
|
|
|
|
if (data.churn_risk_score !== undefined) updatePayload.churn_risk_score = data.churn_risk_score;
|
|
|
|
|
|
if (data.churn_risk_label !== undefined) updatePayload.churn_risk_label = data.churn_risk_label;
|
|
|
|
|
|
if (data.sentiment_trend !== undefined) updatePayload.sentiment_trend = data.sentiment_trend;
|
|
|
|
|
|
if (data.ticket_volume_30d !== undefined) updatePayload.ticket_volume_30d = data.ticket_volume_30d;
|
|
|
|
|
|
if (data.repeat_issue_count_30d !== undefined) updatePayload.repeat_issue_count_30d = data.repeat_issue_count_30d;
|
|
|
|
|
|
if (data.avg_first_response_minutes_30d !== undefined) updatePayload.avg_first_response_minutes_30d = data.avg_first_response_minutes_30d;
|
|
|
|
|
|
if (data.avg_resolution_hours_30d !== undefined) updatePayload.avg_resolution_hours_30d = data.avg_resolution_hours_30d;
|
|
|
|
|
|
if (data.drivers_summary !== undefined) updatePayload.drivers_summary = data.drivers_summary;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await customer_health_snapshots.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.customer_account !== undefined) {
|
|
await customer_health_snapshots.setCustomer_account(
|
|
|
|
data.customer_account,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_health_snapshots = await db.customer_health_snapshots.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of customer_health_snapshots) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of customer_health_snapshots) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_health_snapshots = await db.customer_health_snapshots.findByPk(id, options);
|
|
|
|
await customer_health_snapshots.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await customer_health_snapshots.destroy({
|
|
transaction
|
|
});
|
|
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_health_snapshots = await db.customer_health_snapshots.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!customer_health_snapshots) {
|
|
return customer_health_snapshots;
|
|
}
|
|
|
|
const output = customer_health_snapshots.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.customer_account = await customer_health_snapshots.getCustomer_account({
|
|
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.customer_accounts,
|
|
as: 'customer_account',
|
|
|
|
where: filter.customer_account ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.customer_account.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
account_name: {
|
|
[Op.or]: filter.customer_account.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.drivers_summary) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_health_snapshots',
|
|
'drivers_summary',
|
|
filter.drivers_summary,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.calculated_atRange) {
|
|
const [start, end] = filter.calculated_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
calculated_at: {
|
|
...where.calculated_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
calculated_at: {
|
|
...where.calculated_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.health_scoreRange) {
|
|
const [start, end] = filter.health_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
health_score: {
|
|
...where.health_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
health_score: {
|
|
...where.health_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.churn_risk_scoreRange) {
|
|
const [start, end] = filter.churn_risk_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
churn_risk_score: {
|
|
...where.churn_risk_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
churn_risk_score: {
|
|
...where.churn_risk_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.sentiment_trendRange) {
|
|
const [start, end] = filter.sentiment_trendRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
sentiment_trend: {
|
|
...where.sentiment_trend,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
sentiment_trend: {
|
|
...where.sentiment_trend,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.ticket_volume_30dRange) {
|
|
const [start, end] = filter.ticket_volume_30dRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
ticket_volume_30d: {
|
|
...where.ticket_volume_30d,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
ticket_volume_30d: {
|
|
...where.ticket_volume_30d,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.repeat_issue_count_30dRange) {
|
|
const [start, end] = filter.repeat_issue_count_30dRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
repeat_issue_count_30d: {
|
|
...where.repeat_issue_count_30d,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
repeat_issue_count_30d: {
|
|
...where.repeat_issue_count_30d,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_first_response_minutes_30dRange) {
|
|
const [start, end] = filter.avg_first_response_minutes_30dRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_first_response_minutes_30d: {
|
|
...where.avg_first_response_minutes_30d,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_first_response_minutes_30d: {
|
|
...where.avg_first_response_minutes_30d,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_resolution_hours_30dRange) {
|
|
const [start, end] = filter.avg_resolution_hours_30dRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_resolution_hours_30d: {
|
|
...where.avg_resolution_hours_30d,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_resolution_hours_30d: {
|
|
...where.avg_resolution_hours_30d,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.health_label) {
|
|
where = {
|
|
...where,
|
|
health_label: filter.health_label,
|
|
};
|
|
}
|
|
|
|
if (filter.churn_risk_label) {
|
|
where = {
|
|
...where,
|
|
churn_risk_label: filter.churn_risk_label,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.customer_health_snapshots.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(
|
|
'customer_health_snapshots',
|
|
'health_label',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.customer_health_snapshots.findAll({
|
|
attributes: [ 'id', 'health_label' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['health_label', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.health_label,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|