38846-vm/backend/src/db/api/social_posts.js
2026-02-28 11:55:39 +00:00

578 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 Social_postsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const social_posts = await db.social_posts.create(
{
id: data.id || undefined,
platform: data.platform
||
null
,
author_handle: data.author_handle
||
null
,
posted_at: data.posted_at
||
null
,
content: data.content
||
null
,
engagement_score: data.engagement_score
||
null
,
sentiment_label: data.sentiment_label
||
null
,
sentiment_score: data.sentiment_score
||
null
,
noise_level: data.noise_level
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await social_posts.setTicker( data.ticker || null, {
transaction,
});
return social_posts;
}
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 social_postsData = data.map((item, index) => ({
id: item.id || undefined,
platform: item.platform
||
null
,
author_handle: item.author_handle
||
null
,
posted_at: item.posted_at
||
null
,
content: item.content
||
null
,
engagement_score: item.engagement_score
||
null
,
sentiment_label: item.sentiment_label
||
null
,
sentiment_score: item.sentiment_score
||
null
,
noise_level: item.noise_level
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const social_posts = await db.social_posts.bulkCreate(social_postsData, { transaction });
// For each item created, replace relation files
return social_posts;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const social_posts = await db.social_posts.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.platform !== undefined) updatePayload.platform = data.platform;
if (data.author_handle !== undefined) updatePayload.author_handle = data.author_handle;
if (data.posted_at !== undefined) updatePayload.posted_at = data.posted_at;
if (data.content !== undefined) updatePayload.content = data.content;
if (data.engagement_score !== undefined) updatePayload.engagement_score = data.engagement_score;
if (data.sentiment_label !== undefined) updatePayload.sentiment_label = data.sentiment_label;
if (data.sentiment_score !== undefined) updatePayload.sentiment_score = data.sentiment_score;
if (data.noise_level !== undefined) updatePayload.noise_level = data.noise_level;
updatePayload.updatedById = currentUser.id;
await social_posts.update(updatePayload, {transaction});
if (data.ticker !== undefined) {
await social_posts.setTicker(
data.ticker,
{ transaction }
);
}
return social_posts;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const social_posts = await db.social_posts.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of social_posts) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of social_posts) {
await record.destroy({transaction});
}
});
return social_posts;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const social_posts = await db.social_posts.findByPk(id, options);
await social_posts.update({
deletedBy: currentUser.id
}, {
transaction,
});
await social_posts.destroy({
transaction
});
return social_posts;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const social_posts = await db.social_posts.findOne(
{ where },
{ transaction },
);
if (!social_posts) {
return social_posts;
}
const output = social_posts.get({plain: true});
output.ticker = await social_posts.getTicker({
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.tickers,
as: 'ticker',
where: filter.ticker ? {
[Op.or]: [
{ id: { [Op.in]: filter.ticker.split('|').map(term => Utils.uuid(term)) } },
{
symbol: {
[Op.or]: filter.ticker.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.author_handle) {
where = {
...where,
[Op.and]: Utils.ilike(
'social_posts',
'author_handle',
filter.author_handle,
),
};
}
if (filter.content) {
where = {
...where,
[Op.and]: Utils.ilike(
'social_posts',
'content',
filter.content,
),
};
}
if (filter.posted_atRange) {
const [start, end] = filter.posted_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
posted_at: {
...where.posted_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
posted_at: {
...where.posted_at,
[Op.lte]: end,
},
};
}
}
if (filter.engagement_scoreRange) {
const [start, end] = filter.engagement_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
engagement_score: {
...where.engagement_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
engagement_score: {
...where.engagement_score,
[Op.lte]: end,
},
};
}
}
if (filter.sentiment_scoreRange) {
const [start, end] = filter.sentiment_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sentiment_score: {
...where.sentiment_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sentiment_score: {
...where.sentiment_score,
[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.sentiment_label) {
where = {
...where,
sentiment_label: filter.sentiment_label,
};
}
if (filter.noise_level) {
where = {
...where,
noise_level: filter.noise_level,
};
}
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.social_posts.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(
'social_posts',
'author_handle',
query,
),
],
};
}
const records = await db.social_posts.findAll({
attributes: [ 'id', 'author_handle' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['author_handle', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.author_handle,
}));
}
};