40193-vm/backend/src/db/api/landing_pages.js
2026-06-04 13:56:56 +00:00

771 lines
20 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 Landing_pagesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const landing_pages = await db.landing_pages.create(
{
id: data.id || undefined,
title: data.title
||
null
,
slug: data.slug
||
null
,
status: data.status
||
null
,
hero_heading: data.hero_heading
||
null
,
hero_subheading: data.hero_subheading
||
null
,
primary_cta_label: data.primary_cta_label
||
null
,
primary_cta_url: data.primary_cta_url
||
null
,
secondary_cta_label: data.secondary_cta_label
||
null
,
secondary_cta_url: data.secondary_cta_url
||
null
,
seo_title: data.seo_title
||
null
,
seo_description: data.seo_description
||
null
,
published_from: data.published_from
||
null
,
published_until: data.published_until
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await landing_pages.setSite( data.site || null, {
transaction,
});
await landing_pages.setCreated_by_user( data.created_by_user || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.landing_pages.getTableName(),
belongsToColumn: 'hero_images',
belongsToId: landing_pages.id,
},
data.hero_images,
options,
);
return landing_pages;
}
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 landing_pagesData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
slug: item.slug
||
null
,
status: item.status
||
null
,
hero_heading: item.hero_heading
||
null
,
hero_subheading: item.hero_subheading
||
null
,
primary_cta_label: item.primary_cta_label
||
null
,
primary_cta_url: item.primary_cta_url
||
null
,
secondary_cta_label: item.secondary_cta_label
||
null
,
secondary_cta_url: item.secondary_cta_url
||
null
,
seo_title: item.seo_title
||
null
,
seo_description: item.seo_description
||
null
,
published_from: item.published_from
||
null
,
published_until: item.published_until
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const landing_pages = await db.landing_pages.bulkCreate(landing_pagesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < landing_pages.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.landing_pages.getTableName(),
belongsToColumn: 'hero_images',
belongsToId: landing_pages[i].id,
},
data[i].hero_images,
options,
);
}
return landing_pages;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const landing_pages = await db.landing_pages.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.slug !== undefined) updatePayload.slug = data.slug;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.hero_heading !== undefined) updatePayload.hero_heading = data.hero_heading;
if (data.hero_subheading !== undefined) updatePayload.hero_subheading = data.hero_subheading;
if (data.primary_cta_label !== undefined) updatePayload.primary_cta_label = data.primary_cta_label;
if (data.primary_cta_url !== undefined) updatePayload.primary_cta_url = data.primary_cta_url;
if (data.secondary_cta_label !== undefined) updatePayload.secondary_cta_label = data.secondary_cta_label;
if (data.secondary_cta_url !== undefined) updatePayload.secondary_cta_url = data.secondary_cta_url;
if (data.seo_title !== undefined) updatePayload.seo_title = data.seo_title;
if (data.seo_description !== undefined) updatePayload.seo_description = data.seo_description;
if (data.published_from !== undefined) updatePayload.published_from = data.published_from;
if (data.published_until !== undefined) updatePayload.published_until = data.published_until;
updatePayload.updatedById = currentUser.id;
await landing_pages.update(updatePayload, {transaction});
if (data.site !== undefined) {
await landing_pages.setSite(
data.site,
{ transaction }
);
}
if (data.created_by_user !== undefined) {
await landing_pages.setCreated_by_user(
data.created_by_user,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.landing_pages.getTableName(),
belongsToColumn: 'hero_images',
belongsToId: landing_pages.id,
},
data.hero_images,
options,
);
return landing_pages;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const landing_pages = await db.landing_pages.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of landing_pages) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of landing_pages) {
await record.destroy({transaction});
}
});
return landing_pages;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const landing_pages = await db.landing_pages.findByPk(id, options);
await landing_pages.update({
deletedBy: currentUser.id
}, {
transaction,
});
await landing_pages.destroy({
transaction
});
return landing_pages;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const landing_pages = await db.landing_pages.findOne(
{ where },
{ transaction },
);
if (!landing_pages) {
return landing_pages;
}
const output = landing_pages.get({plain: true});
output.page_sections_landing_page = await landing_pages.getPage_sections_landing_page({
transaction
});
output.contact_submissions_landing_page = await landing_pages.getContact_submissions_landing_page({
transaction
});
output.site = await landing_pages.getSite({
transaction
});
output.hero_images = await landing_pages.getHero_images({
transaction
});
output.created_by_user = await landing_pages.getCreated_by_user({
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.sites,
as: 'site',
where: filter.site ? {
[Op.or]: [
{ id: { [Op.in]: filter.site.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.site.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'created_by_user',
where: filter.created_by_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.created_by_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.created_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'hero_images',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'title',
filter.title,
),
};
}
if (filter.slug) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'slug',
filter.slug,
),
};
}
if (filter.hero_heading) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'hero_heading',
filter.hero_heading,
),
};
}
if (filter.hero_subheading) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'hero_subheading',
filter.hero_subheading,
),
};
}
if (filter.primary_cta_label) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'primary_cta_label',
filter.primary_cta_label,
),
};
}
if (filter.primary_cta_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'primary_cta_url',
filter.primary_cta_url,
),
};
}
if (filter.secondary_cta_label) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'secondary_cta_label',
filter.secondary_cta_label,
),
};
}
if (filter.secondary_cta_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'secondary_cta_url',
filter.secondary_cta_url,
),
};
}
if (filter.seo_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'seo_title',
filter.seo_title,
),
};
}
if (filter.seo_description) {
where = {
...where,
[Op.and]: Utils.ilike(
'landing_pages',
'seo_description',
filter.seo_description,
),
};
}
if (filter.published_fromRange) {
const [start, end] = filter.published_fromRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
published_from: {
...where.published_from,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
published_from: {
...where.published_from,
[Op.lte]: end,
},
};
}
}
if (filter.published_untilRange) {
const [start, end] = filter.published_untilRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
published_until: {
...where.published_until,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
published_until: {
...where.published_until,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
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.landing_pages.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(
'landing_pages',
'title',
query,
),
],
};
}
const records = await db.landing_pages.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};