30152/backend/src/db/api/influencers.js
2025-03-24 13:49:30 +00:00

573 lines
14 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 InfluencersDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const influencers = await db.influencers.create(
{
id: data.id || undefined,
name: data.name || null,
username: data.username || null,
platform: data.platform || null,
profile_pic_url: data.profile_pic_url || null,
bio: data.bio || null,
category: data.category || null,
location: data.location || null,
followers_count: data.followers_count || null,
following_count: data.following_count || null,
engagement_rate: data.engagement_rate || null,
avg_likes: data.avg_likes || null,
avg_comments: data.avg_comments || null,
posts_count: data.posts_count || null,
platform_verified: data.platform_verified || false,
email: data.email || null,
contact_links: data.contact_links || null,
is_opted_in: data.is_opted_in || false,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return influencers;
}
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 influencersData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name || null,
username: item.username || null,
platform: item.platform || null,
profile_pic_url: item.profile_pic_url || null,
bio: item.bio || null,
category: item.category || null,
location: item.location || null,
followers_count: item.followers_count || null,
following_count: item.following_count || null,
engagement_rate: item.engagement_rate || null,
avg_likes: item.avg_likes || null,
avg_comments: item.avg_comments || null,
posts_count: item.posts_count || null,
platform_verified: item.platform_verified || false,
email: item.email || null,
contact_links: item.contact_links || null,
is_opted_in: item.is_opted_in || false,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const influencers = await db.influencers.bulkCreate(influencersData, {
transaction,
});
// For each item created, replace relation files
return influencers;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const influencers = await db.influencers.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.username !== undefined) updatePayload.username = data.username;
if (data.platform !== undefined) updatePayload.platform = data.platform;
if (data.profile_pic_url !== undefined)
updatePayload.profile_pic_url = data.profile_pic_url;
if (data.bio !== undefined) updatePayload.bio = data.bio;
if (data.category !== undefined) updatePayload.category = data.category;
if (data.location !== undefined) updatePayload.location = data.location;
if (data.followers_count !== undefined)
updatePayload.followers_count = data.followers_count;
if (data.following_count !== undefined)
updatePayload.following_count = data.following_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;
if (data.posts_count !== undefined)
updatePayload.posts_count = data.posts_count;
if (data.platform_verified !== undefined)
updatePayload.platform_verified = data.platform_verified;
if (data.email !== undefined) updatePayload.email = data.email;
if (data.contact_links !== undefined)
updatePayload.contact_links = data.contact_links;
if (data.is_opted_in !== undefined)
updatePayload.is_opted_in = data.is_opted_in;
updatePayload.updatedById = currentUser.id;
await influencers.update(updatePayload, { transaction });
return influencers;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const influencers = await db.influencers.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of influencers) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of influencers) {
await record.destroy({ transaction });
}
});
return influencers;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const influencers = await db.influencers.findByPk(id, options);
await influencers.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await influencers.destroy({
transaction,
});
return influencers;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const influencers = await db.influencers.findOne(
{ where },
{ transaction },
);
if (!influencers) {
return influencers;
}
const output = influencers.get({ plain: true });
output.brand_mentions_influencer =
await influencers.getBrand_mentions_influencer({
transaction,
});
output.influencer_audiences_influencer =
await influencers.getInfluencer_audiences_influencer({
transaction,
});
output.influencer_metrics_influencer =
await influencers.getInfluencer_metrics_influencer({
transaction,
});
output.list_influencers_influencer =
await influencers.getList_influencers_influencer({
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 = [];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'name', filter.name),
};
}
if (filter.username) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'username', filter.username),
};
}
if (filter.profile_pic_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'influencers',
'profile_pic_url',
filter.profile_pic_url,
),
};
}
if (filter.bio) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'bio', filter.bio),
};
}
if (filter.category) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'category', filter.category),
};
}
if (filter.location) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'location', filter.location),
};
}
if (filter.email) {
where = {
...where,
[Op.and]: Utils.ilike('influencers', 'email', filter.email),
};
}
if (filter.contact_links) {
where = {
...where,
[Op.and]: Utils.ilike(
'influencers',
'contact_links',
filter.contact_links,
),
};
}
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.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.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.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.platform) {
where = {
...where,
platform: filter.platform,
};
}
if (filter.platform_verified) {
where = {
...where,
platform_verified: filter.platform_verified,
};
}
if (filter.is_opted_in) {
where = {
...where,
is_opted_in: filter.is_opted_in,
};
}
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.influencers.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('influencers', 'name', query),
],
};
}
const records = await db.influencers.findAll({
attributes: ['id', 'name'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};