867 lines
22 KiB
JavaScript
867 lines
22 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,
|
|
|
|
company_name: data.company_name
|
|
||
|
|
null
|
|
,
|
|
|
|
tagline: data.tagline
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_phone: data.primary_phone
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_email: data.primary_email
|
|
||
|
|
null
|
|
,
|
|
|
|
street_address: data.street_address
|
|
||
|
|
null
|
|
,
|
|
|
|
postal_code: data.postal_code
|
|
||
|
|
null
|
|
,
|
|
|
|
city: data.city
|
|
||
|
|
null
|
|
,
|
|
|
|
kvk_number: data.kvk_number
|
|
||
|
|
null
|
|
,
|
|
|
|
btw_number: data.btw_number
|
|
||
|
|
null
|
|
,
|
|
|
|
opening_hours: data.opening_hours
|
|
||
|
|
null
|
|
,
|
|
|
|
hero_headline: data.hero_headline
|
|
||
|
|
null
|
|
,
|
|
|
|
hero_subline: data.hero_subline
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_cta_label: data.primary_cta_label
|
|
||
|
|
null
|
|
,
|
|
|
|
secondary_cta_label: data.secondary_cta_label
|
|
||
|
|
null
|
|
,
|
|
|
|
about_title: data.about_title
|
|
||
|
|
null
|
|
,
|
|
|
|
about_text: data.about_text
|
|
||
|
|
null
|
|
,
|
|
|
|
show_trust_bar: data.show_trust_bar
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
show_featured_projects: data.show_featured_projects
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'logo_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.logo_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'hero_background_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.hero_background_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'about_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.about_images,
|
|
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,
|
|
|
|
company_name: item.company_name
|
|
||
|
|
null
|
|
,
|
|
|
|
tagline: item.tagline
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_phone: item.primary_phone
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_email: item.primary_email
|
|
||
|
|
null
|
|
,
|
|
|
|
street_address: item.street_address
|
|
||
|
|
null
|
|
,
|
|
|
|
postal_code: item.postal_code
|
|
||
|
|
null
|
|
,
|
|
|
|
city: item.city
|
|
||
|
|
null
|
|
,
|
|
|
|
kvk_number: item.kvk_number
|
|
||
|
|
null
|
|
,
|
|
|
|
btw_number: item.btw_number
|
|
||
|
|
null
|
|
,
|
|
|
|
opening_hours: item.opening_hours
|
|
||
|
|
null
|
|
,
|
|
|
|
hero_headline: item.hero_headline
|
|
||
|
|
null
|
|
,
|
|
|
|
hero_subline: item.hero_subline
|
|
||
|
|
null
|
|
,
|
|
|
|
primary_cta_label: item.primary_cta_label
|
|
||
|
|
null
|
|
,
|
|
|
|
secondary_cta_label: item.secondary_cta_label
|
|
||
|
|
null
|
|
,
|
|
|
|
about_title: item.about_title
|
|
||
|
|
null
|
|
,
|
|
|
|
about_text: item.about_text
|
|
||
|
|
null
|
|
,
|
|
|
|
show_trust_bar: item.show_trust_bar
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
show_featured_projects: item.show_featured_projects
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
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_images',
|
|
belongsToId: site_settings[i].id,
|
|
},
|
|
data[i].logo_images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < site_settings.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'hero_background_images',
|
|
belongsToId: site_settings[i].id,
|
|
},
|
|
data[i].hero_background_images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < site_settings.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'about_images',
|
|
belongsToId: site_settings[i].id,
|
|
},
|
|
data[i].about_images,
|
|
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.company_name !== undefined) updatePayload.company_name = data.company_name;
|
|
|
|
|
|
if (data.tagline !== undefined) updatePayload.tagline = data.tagline;
|
|
|
|
|
|
if (data.primary_phone !== undefined) updatePayload.primary_phone = data.primary_phone;
|
|
|
|
|
|
if (data.primary_email !== undefined) updatePayload.primary_email = data.primary_email;
|
|
|
|
|
|
if (data.street_address !== undefined) updatePayload.street_address = data.street_address;
|
|
|
|
|
|
if (data.postal_code !== undefined) updatePayload.postal_code = data.postal_code;
|
|
|
|
|
|
if (data.city !== undefined) updatePayload.city = data.city;
|
|
|
|
|
|
if (data.kvk_number !== undefined) updatePayload.kvk_number = data.kvk_number;
|
|
|
|
|
|
if (data.btw_number !== undefined) updatePayload.btw_number = data.btw_number;
|
|
|
|
|
|
if (data.opening_hours !== undefined) updatePayload.opening_hours = data.opening_hours;
|
|
|
|
|
|
if (data.hero_headline !== undefined) updatePayload.hero_headline = data.hero_headline;
|
|
|
|
|
|
if (data.hero_subline !== undefined) updatePayload.hero_subline = data.hero_subline;
|
|
|
|
|
|
if (data.primary_cta_label !== undefined) updatePayload.primary_cta_label = data.primary_cta_label;
|
|
|
|
|
|
if (data.secondary_cta_label !== undefined) updatePayload.secondary_cta_label = data.secondary_cta_label;
|
|
|
|
|
|
if (data.about_title !== undefined) updatePayload.about_title = data.about_title;
|
|
|
|
|
|
if (data.about_text !== undefined) updatePayload.about_text = data.about_text;
|
|
|
|
|
|
if (data.show_trust_bar !== undefined) updatePayload.show_trust_bar = data.show_trust_bar;
|
|
|
|
|
|
if (data.show_featured_projects !== undefined) updatePayload.show_featured_projects = data.show_featured_projects;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await site_settings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'logo_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.logo_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'hero_background_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.hero_background_images,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.site_settings.getTableName(),
|
|
belongsToColumn: 'about_images',
|
|
belongsToId: site_settings.id,
|
|
},
|
|
data.about_images,
|
|
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_images = await site_settings.getLogo_images({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.hero_background_images = await site_settings.getHero_background_images({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.about_images = await site_settings.getAbout_images({
|
|
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_images',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'hero_background_images',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'about_images',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.company_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'company_name',
|
|
filter.company_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.tagline) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'tagline',
|
|
filter.tagline,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.primary_phone) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'primary_phone',
|
|
filter.primary_phone,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.primary_email) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'primary_email',
|
|
filter.primary_email,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.street_address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'street_address',
|
|
filter.street_address,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.postal_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'postal_code',
|
|
filter.postal_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.city) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'city',
|
|
filter.city,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.kvk_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'kvk_number',
|
|
filter.kvk_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.btw_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'btw_number',
|
|
filter.btw_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.opening_hours) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'opening_hours',
|
|
filter.opening_hours,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.hero_headline) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'hero_headline',
|
|
filter.hero_headline,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.hero_subline) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'hero_subline',
|
|
filter.hero_subline,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.primary_cta_label) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'primary_cta_label',
|
|
filter.primary_cta_label,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.secondary_cta_label) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'secondary_cta_label',
|
|
filter.secondary_cta_label,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.about_title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'about_title',
|
|
filter.about_title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.about_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'site_settings',
|
|
'about_text',
|
|
filter.about_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.show_trust_bar) {
|
|
where = {
|
|
...where,
|
|
show_trust_bar: filter.show_trust_bar,
|
|
};
|
|
}
|
|
|
|
if (filter.show_featured_projects) {
|
|
where = {
|
|
...where,
|
|
show_featured_projects: filter.show_featured_projects,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
'company_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.site_settings.findAll({
|
|
attributes: [ 'id', 'company_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['company_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.company_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|