339 lines
8.0 KiB
JavaScript
339 lines
8.0 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 LeadsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const leads = await db.leads.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
source: data.source || null,
|
|
product: data.product || null,
|
|
stage: data.stage || null,
|
|
score: data.score || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await leads.setOwner_agent(data.owner_agent || null, {
|
|
transaction,
|
|
});
|
|
|
|
return leads;
|
|
}
|
|
|
|
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 leadsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
source: item.source || null,
|
|
product: item.product || null,
|
|
stage: item.stage || null,
|
|
score: item.score || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const leads = await db.leads.bulkCreate(leadsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return leads;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const leads = await db.leads.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.source !== undefined) updatePayload.source = data.source;
|
|
|
|
if (data.product !== undefined) updatePayload.product = data.product;
|
|
|
|
if (data.stage !== undefined) updatePayload.stage = data.stage;
|
|
|
|
if (data.score !== undefined) updatePayload.score = data.score;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await leads.update(updatePayload, { transaction });
|
|
|
|
if (data.owner_agent !== undefined) {
|
|
await leads.setOwner_agent(
|
|
data.owner_agent,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return leads;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const leads = await db.leads.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of leads) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of leads) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return leads;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const leads = await db.leads.findByPk(id, options);
|
|
|
|
await leads.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await leads.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return leads;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const leads = await db.leads.findOne({ where }, { transaction });
|
|
|
|
if (!leads) {
|
|
return leads;
|
|
}
|
|
|
|
const output = leads.get({ plain: true });
|
|
|
|
output.owner_agent = await leads.getOwner_agent({
|
|
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.agents,
|
|
as: 'owner_agent',
|
|
|
|
where: filter.owner_agent
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.owner_agent
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.owner_agent
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.source) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('leads', 'source', filter.source),
|
|
};
|
|
}
|
|
|
|
if (filter.product) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('leads', 'product', filter.product),
|
|
};
|
|
}
|
|
|
|
if (filter.scoreRange) {
|
|
const [start, end] = filter.scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.stage) {
|
|
where = {
|
|
...where,
|
|
stage: filter.stage,
|
|
};
|
|
}
|
|
|
|
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.leads.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('leads', 'source', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.leads.findAll({
|
|
attributes: ['id', 'source'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['source', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.source,
|
|
}));
|
|
}
|
|
};
|