37796-vm/backend/src/db/api/site_settings.js
2026-01-25 08:08:04 +00:00

543 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 Site_settingsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const site_settings = await db.site_settings.create(
{
id: data.id || undefined,
site_title: data.site_title
||
null
,
tagline: data.tagline
||
null
,
about: data.about
||
null
,
twitter: data.twitter
||
null
,
github: data.github
||
null
,
linkedin: data.linkedin
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'logo',
belongsToId: site_settings.id,
},
data.logo,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'favicon',
belongsToId: site_settings.id,
},
data.favicon,
options,
);
return site_settings;
}
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 site_settingsData = data.map((item, index) => ({
id: item.id || undefined,
site_title: item.site_title
||
null
,
tagline: item.tagline
||
null
,
about: item.about
||
null
,
twitter: item.twitter
||
null
,
github: item.github
||
null
,
linkedin: item.linkedin
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const site_settings = await db.site_settings.bulkCreate(site_settingsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < site_settings.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'logo',
belongsToId: site_settings[i].id,
},
data[i].logo,
options,
);
}
for (let i = 0; i < site_settings.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'favicon',
belongsToId: site_settings[i].id,
},
data[i].favicon,
options,
);
}
return site_settings;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const site_settings = await db.site_settings.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.site_title !== undefined) updatePayload.site_title = data.site_title;
if (data.tagline !== undefined) updatePayload.tagline = data.tagline;
if (data.about !== undefined) updatePayload.about = data.about;
if (data.twitter !== undefined) updatePayload.twitter = data.twitter;
if (data.github !== undefined) updatePayload.github = data.github;
if (data.linkedin !== undefined) updatePayload.linkedin = data.linkedin;
updatePayload.updatedById = currentUser.id;
await site_settings.update(updatePayload, {transaction});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'logo',
belongsToId: site_settings.id,
},
data.logo,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.site_settings.getTableName(),
belongsToColumn: 'favicon',
belongsToId: site_settings.id,
},
data.favicon,
options,
);
return site_settings;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const site_settings = await db.site_settings.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of site_settings) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of site_settings) {
await record.destroy({transaction});
}
});
return site_settings;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const site_settings = await db.site_settings.findByPk(id, options);
await site_settings.update({
deletedBy: currentUser.id
}, {
transaction,
});
await site_settings.destroy({
transaction
});
return site_settings;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const site_settings = await db.site_settings.findOne(
{ where },
{ transaction },
);
if (!site_settings) {
return site_settings;
}
const output = site_settings.get({plain: true});
output.logo = await site_settings.getLogo({
transaction
});
output.favicon = await site_settings.getFavicon({
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.file,
as: 'logo',
},
{
model: db.file,
as: 'favicon',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.site_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'site_title',
filter.site_title,
),
};
}
if (filter.tagline) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'tagline',
filter.tagline,
),
};
}
if (filter.about) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'about',
filter.about,
),
};
}
if (filter.twitter) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'twitter',
filter.twitter,
),
};
}
if (filter.github) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'github',
filter.github,
),
};
}
if (filter.linkedin) {
where = {
...where,
[Op.and]: Utils.ilike(
'site_settings',
'linkedin',
filter.linkedin,
),
};
}
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.site_settings.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(
'site_settings',
'site_title',
query,
),
],
};
}
const records = await db.site_settings.findAll({
attributes: [ 'id', 'site_title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['site_title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.site_title,
}));
}
};