392 lines
9.5 KiB
JavaScript
392 lines
9.5 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 BotDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const bot = await db.bot.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name || null,
|
|
description: data.description || null,
|
|
order_count: data.order_count || null,
|
|
initial_order_value: data.initial_order_value || null,
|
|
order_size_increment: data.order_size_increment || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await bot.setUser(data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
return bot;
|
|
}
|
|
|
|
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 botData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name || null,
|
|
description: item.description || null,
|
|
order_count: item.order_count || null,
|
|
initial_order_value: item.initial_order_value || null,
|
|
order_size_increment: item.order_size_increment || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const bot = await db.bot.bulkCreate(botData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return bot;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const bot = await db.bot.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
if (data.description !== undefined)
|
|
updatePayload.description = data.description;
|
|
|
|
if (data.order_count !== undefined)
|
|
updatePayload.order_count = data.order_count;
|
|
|
|
if (data.initial_order_value !== undefined)
|
|
updatePayload.initial_order_value = data.initial_order_value;
|
|
|
|
if (data.order_size_increment !== undefined)
|
|
updatePayload.order_size_increment = data.order_size_increment;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await bot.update(updatePayload, { transaction });
|
|
|
|
if (data.user !== undefined) {
|
|
await bot.setUser(
|
|
data.user,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return bot;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const bot = await db.bot.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of bot) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of bot) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return bot;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const bot = await db.bot.findByPk(id, options);
|
|
|
|
await bot.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await bot.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return bot;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const bot = await db.bot.findOne({ where }, { transaction });
|
|
|
|
if (!bot) {
|
|
return bot;
|
|
}
|
|
|
|
const output = bot.get({ plain: true });
|
|
|
|
output.orders_strategy = await bot.getOrders_strategy({
|
|
transaction,
|
|
});
|
|
|
|
output.user = await bot.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.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('bot', 'name', filter.name),
|
|
};
|
|
}
|
|
|
|
if (filter.description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('bot', 'description', filter.description),
|
|
};
|
|
}
|
|
|
|
if (filter.order_countRange) {
|
|
const [start, end] = filter.order_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
order_count: {
|
|
...where.order_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
order_count: {
|
|
...where.order_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.initial_order_valueRange) {
|
|
const [start, end] = filter.initial_order_valueRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
initial_order_value: {
|
|
...where.initial_order_value,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
initial_order_value: {
|
|
...where.initial_order_value,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.order_size_incrementRange) {
|
|
const [start, end] = filter.order_size_incrementRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
order_size_increment: {
|
|
...where.order_size_increment,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
order_size_increment: {
|
|
...where.order_size_increment,
|
|
[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.bot.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('bot', 'name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.bot.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,
|
|
}));
|
|
}
|
|
};
|