567 lines
14 KiB
JavaScript
567 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 Contact_inquiriesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const contact_inquiries = await db.contact_inquiries.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
phone: data.phone
|
|
||
|
|
null
|
|
,
|
|
|
|
email: data.email
|
|
||
|
|
null
|
|
,
|
|
|
|
channel: data.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
message: data.message
|
|
||
|
|
null
|
|
,
|
|
|
|
received_at: data.received_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: data.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await contact_inquiries.setAssigned_to( data.assigned_to || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return contact_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 contact_inquiriesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
phone: item.phone
|
|
||
|
|
null
|
|
,
|
|
|
|
email: item.email
|
|
||
|
|
null
|
|
,
|
|
|
|
channel: item.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
message: item.message
|
|
||
|
|
null
|
|
,
|
|
|
|
received_at: item.received_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: item.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const contact_inquiries = await db.contact_inquiries.bulkCreate(contact_inquiriesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return contact_inquiries;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const contact_inquiries = await db.contact_inquiries.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.phone !== undefined) updatePayload.phone = data.phone;
|
|
|
|
|
|
if (data.email !== undefined) updatePayload.email = data.email;
|
|
|
|
|
|
if (data.channel !== undefined) updatePayload.channel = data.channel;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.message !== undefined) updatePayload.message = data.message;
|
|
|
|
|
|
if (data.received_at !== undefined) updatePayload.received_at = data.received_at;
|
|
|
|
|
|
if (data.responded_at !== undefined) updatePayload.responded_at = data.responded_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await contact_inquiries.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.assigned_to !== undefined) {
|
|
await contact_inquiries.setAssigned_to(
|
|
|
|
data.assigned_to,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return contact_inquiries;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const contact_inquiries = await db.contact_inquiries.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of contact_inquiries) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of contact_inquiries) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return contact_inquiries;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const contact_inquiries = await db.contact_inquiries.findByPk(id, options);
|
|
|
|
await contact_inquiries.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await contact_inquiries.destroy({
|
|
transaction
|
|
});
|
|
|
|
return contact_inquiries;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const contact_inquiries = await db.contact_inquiries.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!contact_inquiries) {
|
|
return contact_inquiries;
|
|
}
|
|
|
|
const output = contact_inquiries.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.assigned_to = await contact_inquiries.getAssigned_to({
|
|
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.users,
|
|
as: 'assigned_to',
|
|
|
|
where: filter.assigned_to ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.assigned_to.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.assigned_to.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(
|
|
'contact_inquiries',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.phone) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'contact_inquiries',
|
|
'phone',
|
|
filter.phone,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.email) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'contact_inquiries',
|
|
'email',
|
|
filter.email,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'contact_inquiries',
|
|
'message',
|
|
filter.message,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.received_atRange) {
|
|
const [start, end] = filter.received_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
received_at: {
|
|
...where.received_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
received_at: {
|
|
...where.received_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.responded_atRange) {
|
|
const [start, end] = filter.responded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.channel) {
|
|
where = {
|
|
...where,
|
|
channel: filter.channel,
|
|
};
|
|
}
|
|
|
|
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.contact_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(
|
|
'contact_inquiries',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.contact_inquiries.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|