39433-vm/backend/src/db/api/lead_submissions.js
2026-04-01 13:56:57 +00:00

666 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 Lead_submissionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lead_submissions = await db.lead_submissions.create(
{
id: data.id || undefined,
name: data.name
||
null
,
email: data.email
||
null
,
phone: data.phone
||
null
,
message: data.message
||
null
,
status: data.status
||
null
,
preferred_contact_method: data.preferred_contact_method
||
null
,
submitted_on: data.submitted_on
||
null
,
source_url: data.source_url
||
null
,
utm_source: data.utm_source
||
null
,
utm_medium: data.utm_medium
||
null
,
utm_campaign: data.utm_campaign
||
null
,
utm_term: data.utm_term
||
null
,
utm_content: data.utm_content
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await lead_submissions.setLanding_page( data.landing_page || null, {
transaction,
});
return lead_submissions;
}
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 lead_submissionsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
email: item.email
||
null
,
phone: item.phone
||
null
,
message: item.message
||
null
,
status: item.status
||
null
,
preferred_contact_method: item.preferred_contact_method
||
null
,
submitted_on: item.submitted_on
||
null
,
source_url: item.source_url
||
null
,
utm_source: item.utm_source
||
null
,
utm_medium: item.utm_medium
||
null
,
utm_campaign: item.utm_campaign
||
null
,
utm_term: item.utm_term
||
null
,
utm_content: item.utm_content
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const lead_submissions = await db.lead_submissions.bulkCreate(lead_submissionsData, { transaction });
// For each item created, replace relation files
return lead_submissions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lead_submissions = await db.lead_submissions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.email !== undefined) updatePayload.email = data.email;
if (data.phone !== undefined) updatePayload.phone = data.phone;
if (data.message !== undefined) updatePayload.message = data.message;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.preferred_contact_method !== undefined) updatePayload.preferred_contact_method = data.preferred_contact_method;
if (data.submitted_on !== undefined) updatePayload.submitted_on = data.submitted_on;
if (data.source_url !== undefined) updatePayload.source_url = data.source_url;
if (data.utm_source !== undefined) updatePayload.utm_source = data.utm_source;
if (data.utm_medium !== undefined) updatePayload.utm_medium = data.utm_medium;
if (data.utm_campaign !== undefined) updatePayload.utm_campaign = data.utm_campaign;
if (data.utm_term !== undefined) updatePayload.utm_term = data.utm_term;
if (data.utm_content !== undefined) updatePayload.utm_content = data.utm_content;
updatePayload.updatedById = currentUser.id;
await lead_submissions.update(updatePayload, {transaction});
if (data.landing_page !== undefined) {
await lead_submissions.setLanding_page(
data.landing_page,
{ transaction }
);
}
return lead_submissions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lead_submissions = await db.lead_submissions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of lead_submissions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of lead_submissions) {
await record.destroy({transaction});
}
});
return lead_submissions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lead_submissions = await db.lead_submissions.findByPk(id, options);
await lead_submissions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await lead_submissions.destroy({
transaction
});
return lead_submissions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const lead_submissions = await db.lead_submissions.findOne(
{ where },
{ transaction },
);
if (!lead_submissions) {
return lead_submissions;
}
const output = lead_submissions.get({plain: true});
output.landing_page = await lead_submissions.getLanding_page({
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.landing_pages,
as: 'landing_page',
where: filter.landing_page ? {
[Op.or]: [
{ id: { [Op.in]: filter.landing_page.split('|').map(term => Utils.uuid(term)) } },
{
slug: {
[Op.or]: filter.landing_page.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'name',
filter.name,
),
};
}
if (filter.email) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'email',
filter.email,
),
};
}
if (filter.phone) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'phone',
filter.phone,
),
};
}
if (filter.message) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'message',
filter.message,
),
};
}
if (filter.source_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'source_url',
filter.source_url,
),
};
}
if (filter.utm_source) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'utm_source',
filter.utm_source,
),
};
}
if (filter.utm_medium) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'utm_medium',
filter.utm_medium,
),
};
}
if (filter.utm_campaign) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'utm_campaign',
filter.utm_campaign,
),
};
}
if (filter.utm_term) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'utm_term',
filter.utm_term,
),
};
}
if (filter.utm_content) {
where = {
...where,
[Op.and]: Utils.ilike(
'lead_submissions',
'utm_content',
filter.utm_content,
),
};
}
if (filter.submitted_onRange) {
const [start, end] = filter.submitted_onRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
submitted_on: {
...where.submitted_on,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
submitted_on: {
...where.submitted_on,
[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.preferred_contact_method) {
where = {
...where,
preferred_contact_method: filter.preferred_contact_method,
};
}
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.lead_submissions.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(
'lead_submissions',
'name',
query,
),
],
};
}
const records = await db.lead_submissions.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,
}));
}
};