38236-vm/backend/src/db/api/inquiries.js
2026-02-06 04:52:19 +00:00

596 lines
14 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 InquiriesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const inquiries = await db.inquiries.create(
{
id: data.id || undefined,
full_name: data.full_name
||
null
,
email: data.email
||
null
,
phone: data.phone
||
null
,
preferred_contact_method: data.preferred_contact_method
||
null
,
message: data.message
||
null
,
status: data.status
||
null
,
submitted_at: data.submitted_at
||
null
,
source_page: data.source_page
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await inquiries.setRequested_service( data.requested_service || null, {
transaction,
});
await inquiries.setPractitioner( data.practitioner || null, {
transaction,
});
return inquiries;
}
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 inquiriesData = data.map((item, index) => ({
id: item.id || undefined,
full_name: item.full_name
||
null
,
email: item.email
||
null
,
phone: item.phone
||
null
,
preferred_contact_method: item.preferred_contact_method
||
null
,
message: item.message
||
null
,
status: item.status
||
null
,
submitted_at: item.submitted_at
||
null
,
source_page: item.source_page
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const inquiries = await db.inquiries.bulkCreate(inquiriesData, { transaction });
// For each item created, replace relation files
return inquiries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const inquiries = await db.inquiries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.full_name !== undefined) updatePayload.full_name = data.full_name;
if (data.email !== undefined) updatePayload.email = data.email;
if (data.phone !== undefined) updatePayload.phone = data.phone;
if (data.preferred_contact_method !== undefined) updatePayload.preferred_contact_method = data.preferred_contact_method;
if (data.message !== undefined) updatePayload.message = data.message;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at;
if (data.source_page !== undefined) updatePayload.source_page = data.source_page;
updatePayload.updatedById = currentUser.id;
await inquiries.update(updatePayload, {transaction});
if (data.requested_service !== undefined) {
await inquiries.setRequested_service(
data.requested_service,
{ transaction }
);
}
if (data.practitioner !== undefined) {
await inquiries.setPractitioner(
data.practitioner,
{ transaction }
);
}
return inquiries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const inquiries = await db.inquiries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of inquiries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of inquiries) {
await record.destroy({transaction});
}
});
return inquiries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const inquiries = await db.inquiries.findByPk(id, options);
await inquiries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await inquiries.destroy({
transaction
});
return inquiries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const inquiries = await db.inquiries.findOne(
{ where },
{ transaction },
);
if (!inquiries) {
return inquiries;
}
const output = inquiries.get({plain: true});
output.appointments_inquiry = await inquiries.getAppointments_inquiry({
transaction
});
output.requested_service = await inquiries.getRequested_service({
transaction
});
output.practitioner = await inquiries.getPractitioner({
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.services,
as: 'requested_service',
where: filter.requested_service ? {
[Op.or]: [
{ id: { [Op.in]: filter.requested_service.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.requested_service.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.practitioners,
as: 'practitioner',
where: filter.practitioner ? {
[Op.or]: [
{ id: { [Op.in]: filter.practitioner.split('|').map(term => Utils.uuid(term)) } },
{
display_name: {
[Op.or]: filter.practitioner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.full_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'full_name',
filter.full_name,
),
};
}
if (filter.email) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'email',
filter.email,
),
};
}
if (filter.phone) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'phone',
filter.phone,
),
};
}
if (filter.preferred_contact_method) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'preferred_contact_method',
filter.preferred_contact_method,
),
};
}
if (filter.message) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'message',
filter.message,
),
};
}
if (filter.source_page) {
where = {
...where,
[Op.and]: Utils.ilike(
'inquiries',
'source_page',
filter.source_page,
),
};
}
if (filter.submitted_atRange) {
const [start, end] = filter.submitted_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[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.inquiries.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(
'inquiries',
'full_name',
query,
),
],
};
}
const records = await db.inquiries.findAll({
attributes: [ 'id', 'full_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['full_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.full_name,
}));
}
};