38667-vm/backend/src/db/api/tracked_competitors.js
2026-02-21 13:56:02 +00:00

459 lines
11 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 Tracked_competitorsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tracked_competitors = await db.tracked_competitors.create(
{
id: data.id || undefined,
shop_key: data.shop_key
||
null
,
shop_name: data.shop_name
||
null
,
shop_url: data.shop_url
||
null
,
added_at: data.added_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await tracked_competitors.setUser( data.user || null, {
transaction,
});
return tracked_competitors;
}
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 tracked_competitorsData = data.map((item, index) => ({
id: item.id || undefined,
shop_key: item.shop_key
||
null
,
shop_name: item.shop_name
||
null
,
shop_url: item.shop_url
||
null
,
added_at: item.added_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tracked_competitors = await db.tracked_competitors.bulkCreate(tracked_competitorsData, { transaction });
// For each item created, replace relation files
return tracked_competitors;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tracked_competitors = await db.tracked_competitors.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.shop_key !== undefined) updatePayload.shop_key = data.shop_key;
if (data.shop_name !== undefined) updatePayload.shop_name = data.shop_name;
if (data.shop_url !== undefined) updatePayload.shop_url = data.shop_url;
if (data.added_at !== undefined) updatePayload.added_at = data.added_at;
updatePayload.updatedById = currentUser.id;
await tracked_competitors.update(updatePayload, {transaction});
if (data.user !== undefined) {
await tracked_competitors.setUser(
data.user,
{ transaction }
);
}
return tracked_competitors;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tracked_competitors = await db.tracked_competitors.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tracked_competitors) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tracked_competitors) {
await record.destroy({transaction});
}
});
return tracked_competitors;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tracked_competitors = await db.tracked_competitors.findByPk(id, options);
await tracked_competitors.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tracked_competitors.destroy({
transaction
});
return tracked_competitors;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tracked_competitors = await db.tracked_competitors.findOne(
{ where },
{ transaction },
);
if (!tracked_competitors) {
return tracked_competitors;
}
const output = tracked_competitors.get({plain: true});
output.user = await tracked_competitors.getUser({
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: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.shop_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracked_competitors',
'shop_key',
filter.shop_key,
),
};
}
if (filter.shop_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracked_competitors',
'shop_name',
filter.shop_name,
),
};
}
if (filter.shop_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'tracked_competitors',
'shop_url',
filter.shop_url,
),
};
}
if (filter.added_atRange) {
const [start, end] = filter.added_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
added_at: {
...where.added_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
added_at: {
...where.added_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
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.tracked_competitors.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(
'tracked_competitors',
'shop_name',
query,
),
],
};
}
const records = await db.tracked_competitors.findAll({
attributes: [ 'id', 'shop_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['shop_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.shop_name,
}));
}
};