38367-vm/backend/src/db/api/ad_campaigns.js
2026-02-11 23:41:19 +00:00

804 lines
21 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 Ad_campaignsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ad_campaigns = await db.ad_campaigns.create(
{
id: data.id || undefined,
name: data.name
||
null
,
objective: data.objective
||
null
,
status: data.status
||
null
,
start_at: data.start_at
||
null
,
end_at: data.end_at
||
null
,
total_budget: data.total_budget
||
null
,
daily_budget: data.daily_budget
||
null
,
currency: data.currency
||
null
,
frequency_cap_per_day: data.frequency_cap_per_day
||
null
,
max_views_per_user: data.max_views_per_user
||
null
,
target_countries: data.target_countries
||
null
,
target_languages: data.target_languages
||
null
,
target_devices: data.target_devices
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ad_campaigns.setOrganization(currentUser.organization.id || null, {
transaction,
});
return ad_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 ad_campaignsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
objective: item.objective
||
null
,
status: item.status
||
null
,
start_at: item.start_at
||
null
,
end_at: item.end_at
||
null
,
total_budget: item.total_budget
||
null
,
daily_budget: item.daily_budget
||
null
,
currency: item.currency
||
null
,
frequency_cap_per_day: item.frequency_cap_per_day
||
null
,
max_views_per_user: item.max_views_per_user
||
null
,
target_countries: item.target_countries
||
null
,
target_languages: item.target_languages
||
null
,
target_devices: item.target_devices
||
null
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ad_campaigns = await db.ad_campaigns.bulkCreate(ad_campaignsData, { transaction });
// For each item created, replace relation files
return ad_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 ad_campaigns = await db.ad_campaigns.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.objective !== undefined) updatePayload.objective = data.objective;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
if (data.total_budget !== undefined) updatePayload.total_budget = data.total_budget;
if (data.daily_budget !== undefined) updatePayload.daily_budget = data.daily_budget;
if (data.currency !== undefined) updatePayload.currency = data.currency;
if (data.frequency_cap_per_day !== undefined) updatePayload.frequency_cap_per_day = data.frequency_cap_per_day;
if (data.max_views_per_user !== undefined) updatePayload.max_views_per_user = data.max_views_per_user;
if (data.target_countries !== undefined) updatePayload.target_countries = data.target_countries;
if (data.target_languages !== undefined) updatePayload.target_languages = data.target_languages;
if (data.target_devices !== undefined) updatePayload.target_devices = data.target_devices;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await ad_campaigns.update(updatePayload, {transaction});
if (data.organization !== undefined) {
await ad_campaigns.setOrganization(
(globalAccess ? data.organization : currentUser.organization.id),
{ transaction }
);
}
return ad_campaigns;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ad_campaigns = await db.ad_campaigns.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ad_campaigns) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ad_campaigns) {
await record.destroy({transaction});
}
});
return ad_campaigns;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ad_campaigns = await db.ad_campaigns.findByPk(id, options);
await ad_campaigns.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ad_campaigns.destroy({
transaction
});
return ad_campaigns;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ad_campaigns = await db.ad_campaigns.findOne(
{ where },
{ transaction },
);
if (!ad_campaigns) {
return ad_campaigns;
}
const output = ad_campaigns.get({plain: true});
output.ads_campaign = await ad_campaigns.getAds_campaign({
transaction
});
output.organization = await ad_campaigns.getOrganization({
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 userAdvertiser_organizations = (user && user.advertiser_organizations?.id) || null;
if (userAdvertiser_organizations) {
if (options?.currentUser?.advertiser_organizationsId) {
where.advertiser_organizationsId = options.currentUser.advertiser_organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.advertiser_organizations,
as: 'organization',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'name',
filter.name,
),
};
}
if (filter.objective) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'objective',
filter.objective,
),
};
}
if (filter.currency) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'currency',
filter.currency,
),
};
}
if (filter.target_countries) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'target_countries',
filter.target_countries,
),
};
}
if (filter.target_languages) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'target_languages',
filter.target_languages,
),
};
}
if (filter.target_devices) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'target_devices',
filter.target_devices,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'ad_campaigns',
'notes',
filter.notes,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
start_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
end_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.start_atRange) {
const [start, end] = filter.start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.lte]: end,
},
};
}
}
if (filter.end_atRange) {
const [start, end] = filter.end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.lte]: end,
},
};
}
}
if (filter.total_budgetRange) {
const [start, end] = filter.total_budgetRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_budget: {
...where.total_budget,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_budget: {
...where.total_budget,
[Op.lte]: end,
},
};
}
}
if (filter.daily_budgetRange) {
const [start, end] = filter.daily_budgetRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
daily_budget: {
...where.daily_budget,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
daily_budget: {
...where.daily_budget,
[Op.lte]: end,
},
};
}
}
if (filter.frequency_cap_per_dayRange) {
const [start, end] = filter.frequency_cap_per_dayRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
frequency_cap_per_day: {
...where.frequency_cap_per_day,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
frequency_cap_per_day: {
...where.frequency_cap_per_day,
[Op.lte]: end,
},
};
}
}
if (filter.max_views_per_userRange) {
const [start, end] = filter.max_views_per_userRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_views_per_user: {
...where.max_views_per_user,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_views_per_user: {
...where.max_views_per_user,
[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.organization) {
const listItems = filter.organization.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationId: {[Op.or]: listItems}
};
}
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.advertiser_organizationsId;
}
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.ad_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(
'ad_campaigns',
'name',
query,
),
],
};
}
const records = await db.ad_campaigns.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,
}));
}
};