38510-vm/backend/src/db/api/order_discounts.js
2026-02-17 11:45:45 +00:00

432 lines
10 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 Order_discountsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const order_discounts = await db.order_discounts.create(
{
id: data.id || undefined,
amount: data.amount
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await order_discounts.setOrder( data.order || null, {
transaction,
});
await order_discounts.setDiscount_code( data.discount_code || null, {
transaction,
});
return order_discounts;
}
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 order_discountsData = data.map((item, index) => ({
id: item.id || undefined,
amount: item.amount
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const order_discounts = await db.order_discounts.bulkCreate(order_discountsData, { transaction });
// For each item created, replace relation files
return order_discounts;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const order_discounts = await db.order_discounts.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.amount !== undefined) updatePayload.amount = data.amount;
updatePayload.updatedById = currentUser.id;
await order_discounts.update(updatePayload, {transaction});
if (data.order !== undefined) {
await order_discounts.setOrder(
data.order,
{ transaction }
);
}
if (data.discount_code !== undefined) {
await order_discounts.setDiscount_code(
data.discount_code,
{ transaction }
);
}
return order_discounts;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const order_discounts = await db.order_discounts.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of order_discounts) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of order_discounts) {
await record.destroy({transaction});
}
});
return order_discounts;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const order_discounts = await db.order_discounts.findByPk(id, options);
await order_discounts.update({
deletedBy: currentUser.id
}, {
transaction,
});
await order_discounts.destroy({
transaction
});
return order_discounts;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const order_discounts = await db.order_discounts.findOne(
{ where },
{ transaction },
);
if (!order_discounts) {
return order_discounts;
}
const output = order_discounts.get({plain: true});
output.order = await order_discounts.getOrder({
transaction
});
output.discount_code = await order_discounts.getDiscount_code({
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.orders,
as: 'order',
where: filter.order ? {
[Op.or]: [
{ id: { [Op.in]: filter.order.split('|').map(term => Utils.uuid(term)) } },
{
order_number: {
[Op.or]: filter.order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.discount_codes,
as: 'discount_code',
where: filter.discount_code ? {
[Op.or]: [
{ id: { [Op.in]: filter.discount_code.split('|').map(term => Utils.uuid(term)) } },
{
code: {
[Op.or]: filter.discount_code.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.amountRange) {
const [start, end] = filter.amountRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
amount: {
...where.amount,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
amount: {
...where.amount,
[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.order_discounts.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(
'order_discounts',
'amount',
query,
),
],
};
}
const records = await db.order_discounts.findAll({
attributes: [ 'id', 'amount' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['amount', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.amount,
}));
}
};