38286-vm/backend/src/db/api/branding_kits.js
2026-02-08 10:30:33 +00:00

659 lines
16 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 Branding_kitsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const branding_kits = await db.branding_kits.create(
{
id: data.id || undefined,
name: data.name
||
null
,
primary_color: data.primary_color
||
null
,
secondary_color: data.secondary_color
||
null
,
accent_color: data.accent_color
||
null
,
font_family: data.font_family
||
null
,
watermark_position: data.watermark_position
||
null
,
watermark_opacity: data.watermark_opacity
||
null
,
is_default: data.is_default
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await branding_kits.setWorkspace( data.workspace || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'logo_images',
belongsToId: branding_kits.id,
},
data.logo_images,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'watermark_images',
belongsToId: branding_kits.id,
},
data.watermark_images,
options,
);
return branding_kits;
}
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 branding_kitsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
primary_color: item.primary_color
||
null
,
secondary_color: item.secondary_color
||
null
,
accent_color: item.accent_color
||
null
,
font_family: item.font_family
||
null
,
watermark_position: item.watermark_position
||
null
,
watermark_opacity: item.watermark_opacity
||
null
,
is_default: item.is_default
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const branding_kits = await db.branding_kits.bulkCreate(branding_kitsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < branding_kits.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'logo_images',
belongsToId: branding_kits[i].id,
},
data[i].logo_images,
options,
);
}
for (let i = 0; i < branding_kits.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'watermark_images',
belongsToId: branding_kits[i].id,
},
data[i].watermark_images,
options,
);
}
return branding_kits;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const globalAccess = currentUser.app_role?.globalAccess;
const branding_kits = await db.branding_kits.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.primary_color !== undefined) updatePayload.primary_color = data.primary_color;
if (data.secondary_color !== undefined) updatePayload.secondary_color = data.secondary_color;
if (data.accent_color !== undefined) updatePayload.accent_color = data.accent_color;
if (data.font_family !== undefined) updatePayload.font_family = data.font_family;
if (data.watermark_position !== undefined) updatePayload.watermark_position = data.watermark_position;
if (data.watermark_opacity !== undefined) updatePayload.watermark_opacity = data.watermark_opacity;
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
updatePayload.updatedById = currentUser.id;
await branding_kits.update(updatePayload, {transaction});
if (data.workspace !== undefined) {
await branding_kits.setWorkspace(
data.workspace,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'logo_images',
belongsToId: branding_kits.id,
},
data.logo_images,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.branding_kits.getTableName(),
belongsToColumn: 'watermark_images',
belongsToId: branding_kits.id,
},
data.watermark_images,
options,
);
return branding_kits;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const branding_kits = await db.branding_kits.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of branding_kits) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of branding_kits) {
await record.destroy({transaction});
}
});
return branding_kits;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const branding_kits = await db.branding_kits.findByPk(id, options);
await branding_kits.update({
deletedBy: currentUser.id
}, {
transaction,
});
await branding_kits.destroy({
transaction
});
return branding_kits;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const branding_kits = await db.branding_kits.findOne(
{ where },
{ transaction },
);
if (!branding_kits) {
return branding_kits;
}
const output = branding_kits.get({plain: true});
output.clips_branding_kit = await branding_kits.getClips_branding_kit({
transaction
});
output.workspace = await branding_kits.getWorkspace({
transaction
});
output.logo_images = await branding_kits.getLogo_images({
transaction
});
output.watermark_images = await branding_kits.getWatermark_images({
transaction
});
return output;
}
static async findAll(
filter,
globalAccess, options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
const user = (options && options.currentUser) || null;
const userWorkspaces = (user && user.workspaces?.id) || null;
if (userWorkspaces) {
if (options?.currentUser?.workspacesId) {
where.workspacesId = options.currentUser.workspacesId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.workspaces,
as: 'workspace',
},
{
model: db.file,
as: 'logo_images',
},
{
model: db.file,
as: 'watermark_images',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'branding_kits',
'name',
filter.name,
),
};
}
if (filter.primary_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'branding_kits',
'primary_color',
filter.primary_color,
),
};
}
if (filter.secondary_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'branding_kits',
'secondary_color',
filter.secondary_color,
),
};
}
if (filter.accent_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'branding_kits',
'accent_color',
filter.accent_color,
),
};
}
if (filter.font_family) {
where = {
...where,
[Op.and]: Utils.ilike(
'branding_kits',
'font_family',
filter.font_family,
),
};
}
if (filter.watermark_opacityRange) {
const [start, end] = filter.watermark_opacityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
watermark_opacity: {
...where.watermark_opacity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
watermark_opacity: {
...where.watermark_opacity,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.watermark_position) {
where = {
...where,
watermark_position: filter.watermark_position,
};
}
if (filter.is_default) {
where = {
...where,
is_default: filter.is_default,
};
}
if (filter.workspace) {
const listItems = filter.workspace.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
workspaceId: {[Op.or]: listItems}
};
}
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,
},
};
}
}
}
if (globalAccess) {
delete where.workspacesId;
}
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.branding_kits.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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'branding_kits',
'name',
query,
),
],
};
}
const records = await db.branding_kits.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,
}));
}
};