513 lines
13 KiB
JavaScript
513 lines
13 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 Influencer_metricsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const influencer_metrics = await db.influencer_metrics.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
date_recorded: data.date_recorded || null,
|
|
followers_count: data.followers_count || null,
|
|
following_count: data.following_count || null,
|
|
posts_count: data.posts_count || null,
|
|
engagement_rate: data.engagement_rate || null,
|
|
avg_likes: data.avg_likes || null,
|
|
avg_comments: data.avg_comments || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await influencer_metrics.setInfluencer(data.influencer || null, {
|
|
transaction,
|
|
});
|
|
|
|
return influencer_metrics;
|
|
}
|
|
|
|
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 influencer_metricsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
date_recorded: item.date_recorded || null,
|
|
followers_count: item.followers_count || null,
|
|
following_count: item.following_count || null,
|
|
posts_count: item.posts_count || null,
|
|
engagement_rate: item.engagement_rate || null,
|
|
avg_likes: item.avg_likes || null,
|
|
avg_comments: item.avg_comments || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const influencer_metrics = await db.influencer_metrics.bulkCreate(
|
|
influencer_metricsData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return influencer_metrics;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const influencer_metrics = await db.influencer_metrics.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.date_recorded !== undefined)
|
|
updatePayload.date_recorded = data.date_recorded;
|
|
|
|
if (data.followers_count !== undefined)
|
|
updatePayload.followers_count = data.followers_count;
|
|
|
|
if (data.following_count !== undefined)
|
|
updatePayload.following_count = data.following_count;
|
|
|
|
if (data.posts_count !== undefined)
|
|
updatePayload.posts_count = data.posts_count;
|
|
|
|
if (data.engagement_rate !== undefined)
|
|
updatePayload.engagement_rate = data.engagement_rate;
|
|
|
|
if (data.avg_likes !== undefined) updatePayload.avg_likes = data.avg_likes;
|
|
|
|
if (data.avg_comments !== undefined)
|
|
updatePayload.avg_comments = data.avg_comments;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await influencer_metrics.update(updatePayload, { transaction });
|
|
|
|
if (data.influencer !== undefined) {
|
|
await influencer_metrics.setInfluencer(
|
|
data.influencer,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return influencer_metrics;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const influencer_metrics = await db.influencer_metrics.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of influencer_metrics) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of influencer_metrics) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return influencer_metrics;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const influencer_metrics = await db.influencer_metrics.findByPk(
|
|
id,
|
|
options,
|
|
);
|
|
|
|
await influencer_metrics.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await influencer_metrics.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return influencer_metrics;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const influencer_metrics = await db.influencer_metrics.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!influencer_metrics) {
|
|
return influencer_metrics;
|
|
}
|
|
|
|
const output = influencer_metrics.get({ plain: true });
|
|
|
|
output.influencer = await influencer_metrics.getInfluencer({
|
|
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.influencers,
|
|
as: 'influencer',
|
|
|
|
where: filter.influencer
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.influencer
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.influencer
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
date_recorded: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
date_recorded: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (filter.date_recordedRange) {
|
|
const [start, end] = filter.date_recordedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
date_recorded: {
|
|
...where.date_recorded,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
date_recorded: {
|
|
...where.date_recorded,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.followers_countRange) {
|
|
const [start, end] = filter.followers_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
followers_count: {
|
|
...where.followers_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
followers_count: {
|
|
...where.followers_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.following_countRange) {
|
|
const [start, end] = filter.following_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
following_count: {
|
|
...where.following_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
following_count: {
|
|
...where.following_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.posts_countRange) {
|
|
const [start, end] = filter.posts_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
posts_count: {
|
|
...where.posts_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
posts_count: {
|
|
...where.posts_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.engagement_rateRange) {
|
|
const [start, end] = filter.engagement_rateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
engagement_rate: {
|
|
...where.engagement_rate,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
engagement_rate: {
|
|
...where.engagement_rate,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_likesRange) {
|
|
const [start, end] = filter.avg_likesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_likes: {
|
|
...where.avg_likes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_likes: {
|
|
...where.avg_likes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.avg_commentsRange) {
|
|
const [start, end] = filter.avg_commentsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
avg_comments: {
|
|
...where.avg_comments,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
avg_comments: {
|
|
...where.avg_comments,
|
|
[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.influencer_metrics.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('influencer_metrics', 'date_recorded', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.influencer_metrics.findAll({
|
|
attributes: ['id', 'date_recorded'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['date_recorded', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.date_recorded,
|
|
}));
|
|
}
|
|
};
|