664 lines
16 KiB
JavaScript
664 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 CampaignsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const campaigns = await db.campaigns.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
status: data.status || null,
|
|
scheduled_completion_date: data.scheduled_completion_date || null,
|
|
campaign_notes: data.campaign_notes || null,
|
|
total_cost: data.total_cost || null,
|
|
campaign_number: data.campaign_number || null,
|
|
departments: data.departments || null,
|
|
campaign_name: data.campaign_name || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await campaigns.setClient(data.client || null, {
|
|
transaction,
|
|
});
|
|
|
|
await campaigns.setBrand(data.brand || null, {
|
|
transaction,
|
|
});
|
|
|
|
await campaigns.setClient_contact(data.client_contact || null, {
|
|
transaction,
|
|
});
|
|
|
|
await campaigns.setAccount_manager(data.account_manager || null, {
|
|
transaction,
|
|
});
|
|
|
|
await campaigns.setSales(data.sales || null, {
|
|
transaction,
|
|
});
|
|
|
|
await campaigns.setCompany(data.company || null, {
|
|
transaction,
|
|
});
|
|
|
|
return campaigns;
|
|
}
|
|
|
|
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 campaignsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
status: item.status || null,
|
|
scheduled_completion_date: item.scheduled_completion_date || null,
|
|
campaign_notes: item.campaign_notes || null,
|
|
total_cost: item.total_cost || null,
|
|
campaign_number: item.campaign_number || null,
|
|
departments: item.departments || null,
|
|
campaign_name: item.campaign_name || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const campaigns = await db.campaigns.bulkCreate(campaignsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return campaigns;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const campaigns = await db.campaigns.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
if (data.scheduled_completion_date !== undefined)
|
|
updatePayload.scheduled_completion_date = data.scheduled_completion_date;
|
|
|
|
if (data.campaign_notes !== undefined)
|
|
updatePayload.campaign_notes = data.campaign_notes;
|
|
|
|
if (data.total_cost !== undefined)
|
|
updatePayload.total_cost = data.total_cost;
|
|
|
|
if (data.campaign_number !== undefined)
|
|
updatePayload.campaign_number = data.campaign_number;
|
|
|
|
if (data.departments !== undefined)
|
|
updatePayload.departments = data.departments;
|
|
|
|
if (data.campaign_name !== undefined)
|
|
updatePayload.campaign_name = data.campaign_name;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await campaigns.update(updatePayload, { transaction });
|
|
|
|
if (data.client !== undefined) {
|
|
await campaigns.setClient(
|
|
data.client,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.brand !== undefined) {
|
|
await campaigns.setBrand(
|
|
data.brand,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.client_contact !== undefined) {
|
|
await campaigns.setClient_contact(
|
|
data.client_contact,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.account_manager !== undefined) {
|
|
await campaigns.setAccount_manager(
|
|
data.account_manager,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.sales !== undefined) {
|
|
await campaigns.setSales(
|
|
data.sales,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.company !== undefined) {
|
|
await campaigns.setCompany(
|
|
data.company,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return campaigns;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const campaigns = await db.campaigns.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of campaigns) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of campaigns) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return campaigns;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const campaigns = await db.campaigns.findByPk(id, options);
|
|
|
|
await campaigns.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await campaigns.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return campaigns;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const campaigns = await db.campaigns.findOne({ where }, { transaction });
|
|
|
|
if (!campaigns) {
|
|
return campaigns;
|
|
}
|
|
|
|
const output = campaigns.get({ plain: true });
|
|
|
|
output.jobs_campaign = await campaigns.getJobs_campaign({
|
|
transaction,
|
|
});
|
|
|
|
output.finances_campaign = await campaigns.getFinances_campaign({
|
|
transaction,
|
|
});
|
|
|
|
output.client = await campaigns.getClient({
|
|
transaction,
|
|
});
|
|
|
|
output.brand = await campaigns.getBrand({
|
|
transaction,
|
|
});
|
|
|
|
output.client_contact = await campaigns.getClient_contact({
|
|
transaction,
|
|
});
|
|
|
|
output.account_manager = await campaigns.getAccount_manager({
|
|
transaction,
|
|
});
|
|
|
|
output.sales = await campaigns.getSales({
|
|
transaction,
|
|
});
|
|
|
|
output.company = await campaigns.getCompany({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userCompany = (user && user.Company?.id) || null;
|
|
|
|
if (userCompany) {
|
|
if (options?.currentUser?.CompanyId) {
|
|
where.CompanyId = options.currentUser.CompanyId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.clients,
|
|
as: 'client',
|
|
|
|
where: filter.client
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.client
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
client_name: {
|
|
[Op.or]: filter.client
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.brands,
|
|
as: 'brand',
|
|
|
|
where: filter.brand
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.brand
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
brand_name: {
|
|
[Op.or]: filter.brand
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.contacts,
|
|
as: 'client_contact',
|
|
|
|
where: filter.client_contact
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.client_contact
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
contact_name: {
|
|
[Op.or]: filter.client_contact
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'account_manager',
|
|
|
|
where: filter.account_manager
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.account_manager
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.account_manager
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'sales',
|
|
|
|
where: filter.sales
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.sales
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.sales
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.company,
|
|
as: 'company',
|
|
|
|
where: filter.company
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.company
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.company
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.campaign_notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'campaigns',
|
|
'campaign_notes',
|
|
filter.campaign_notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.campaign_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'campaigns',
|
|
'campaign_name',
|
|
filter.campaign_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.scheduled_completion_dateRange) {
|
|
const [start, end] = filter.scheduled_completion_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_completion_date: {
|
|
...where.scheduled_completion_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_completion_date: {
|
|
...where.scheduled_completion_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.total_costRange) {
|
|
const [start, end] = filter.total_costRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_cost: {
|
|
...where.total_cost,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_cost: {
|
|
...where.total_cost,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.campaign_numberRange) {
|
|
const [start, end] = filter.campaign_numberRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
campaign_number: {
|
|
...where.campaign_number,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
campaign_number: {
|
|
...where.campaign_number,
|
|
[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.departments) {
|
|
where = {
|
|
...where,
|
|
departments: filter.departments,
|
|
};
|
|
}
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.CompanyId;
|
|
}
|
|
|
|
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.campaigns.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,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('campaigns', 'campaign_name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.campaigns.findAll({
|
|
attributes: ['id', 'campaign_name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['campaign_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.campaign_name,
|
|
}));
|
|
}
|
|
};
|