39404-vm/backend/src/db/api/post_ideas.js
2026-03-30 19:27:21 +00:00

608 lines
15 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 Post_ideasDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const post_ideas = await db.post_ideas.create(
{
id: data.id || undefined,
format: data.format
||
null
,
status: data.status
||
null
,
text: data.text
||
null
,
scheduled_for: data.scheduled_for
||
null
,
published_at: data.published_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await post_ideas.setPersona_profile( data.persona_profile || null, {
transaction,
});
await post_ideas.setGeneration( data.generation || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'media',
belongsToId: post_ideas.id,
},
data.media,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'attachments',
belongsToId: post_ideas.id,
},
data.attachments,
options,
);
return post_ideas;
}
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 post_ideasData = data.map((item, index) => ({
id: item.id || undefined,
format: item.format
||
null
,
status: item.status
||
null
,
text: item.text
||
null
,
scheduled_for: item.scheduled_for
||
null
,
published_at: item.published_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const post_ideas = await db.post_ideas.bulkCreate(post_ideasData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < post_ideas.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'media',
belongsToId: post_ideas[i].id,
},
data[i].media,
options,
);
}
for (let i = 0; i < post_ideas.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'attachments',
belongsToId: post_ideas[i].id,
},
data[i].attachments,
options,
);
}
return post_ideas;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const post_ideas = await db.post_ideas.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.format !== undefined) updatePayload.format = data.format;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.text !== undefined) updatePayload.text = data.text;
if (data.scheduled_for !== undefined) updatePayload.scheduled_for = data.scheduled_for;
if (data.published_at !== undefined) updatePayload.published_at = data.published_at;
updatePayload.updatedById = currentUser.id;
await post_ideas.update(updatePayload, {transaction});
if (data.persona_profile !== undefined) {
await post_ideas.setPersona_profile(
data.persona_profile,
{ transaction }
);
}
if (data.generation !== undefined) {
await post_ideas.setGeneration(
data.generation,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'media',
belongsToId: post_ideas.id,
},
data.media,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.post_ideas.getTableName(),
belongsToColumn: 'attachments',
belongsToId: post_ideas.id,
},
data.attachments,
options,
);
return post_ideas;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const post_ideas = await db.post_ideas.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of post_ideas) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of post_ideas) {
await record.destroy({transaction});
}
});
return post_ideas;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const post_ideas = await db.post_ideas.findByPk(id, options);
await post_ideas.update({
deletedBy: currentUser.id
}, {
transaction,
});
await post_ideas.destroy({
transaction
});
return post_ideas;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const post_ideas = await db.post_ideas.findOne(
{ where },
{ transaction },
);
if (!post_ideas) {
return post_ideas;
}
const output = post_ideas.get({plain: true});
output.persona_profile = await post_ideas.getPersona_profile({
transaction
});
output.generation = await post_ideas.getGeneration({
transaction
});
output.media = await post_ideas.getMedia({
transaction
});
output.attachments = await post_ideas.getAttachments({
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.persona_profiles,
as: 'persona_profile',
where: filter.persona_profile ? {
[Op.or]: [
{ id: { [Op.in]: filter.persona_profile.split('|').map(term => Utils.uuid(term)) } },
{
username: {
[Op.or]: filter.persona_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.generations,
as: 'generation',
where: filter.generation ? {
[Op.or]: [
{ id: { [Op.in]: filter.generation.split('|').map(term => Utils.uuid(term)) } },
{
niche_input: {
[Op.or]: filter.generation.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'media',
},
{
model: db.file,
as: 'attachments',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.text) {
where = {
...where,
[Op.and]: Utils.ilike(
'post_ideas',
'text',
filter.text,
),
};
}
if (filter.scheduled_forRange) {
const [start, end] = filter.scheduled_forRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
scheduled_for: {
...where.scheduled_for,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
scheduled_for: {
...where.scheduled_for,
[Op.lte]: end,
},
};
}
}
if (filter.published_atRange) {
const [start, end] = filter.published_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
published_at: {
...where.published_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
published_at: {
...where.published_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.format) {
where = {
...where,
format: filter.format,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.post_ideas.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(
'post_ideas',
'text',
query,
),
],
};
}
const records = await db.post_ideas.findAll({
attributes: [ 'id', 'text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.text,
}));
}
};