30410/backend/src/db/api/coupons.js
2025-04-02 09:12:04 +00:00

351 lines
8.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 CouponsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const coupons = await db.coupons.create(
{
id: data.id || undefined,
code: data.code || null,
discount: data.discount || null,
valid_from: data.valid_from || null,
valid_until: data.valid_until || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return coupons;
}
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 couponsData = data.map((item, index) => ({
id: item.id || undefined,
code: item.code || null,
discount: item.discount || null,
valid_from: item.valid_from || null,
valid_until: item.valid_until || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const coupons = await db.coupons.bulkCreate(couponsData, { transaction });
// For each item created, replace relation files
return coupons;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const coupons = await db.coupons.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.code !== undefined) updatePayload.code = data.code;
if (data.discount !== undefined) updatePayload.discount = data.discount;
if (data.valid_from !== undefined)
updatePayload.valid_from = data.valid_from;
if (data.valid_until !== undefined)
updatePayload.valid_until = data.valid_until;
updatePayload.updatedById = currentUser.id;
await coupons.update(updatePayload, { transaction });
return coupons;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const coupons = await db.coupons.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of coupons) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of coupons) {
await record.destroy({ transaction });
}
});
return coupons;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const coupons = await db.coupons.findByPk(id, options);
await coupons.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await coupons.destroy({
transaction,
});
return coupons;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const coupons = await db.coupons.findOne({ where }, { transaction });
if (!coupons) {
return coupons;
}
const output = coupons.get({ plain: true });
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 = [];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.code) {
where = {
...where,
[Op.and]: Utils.ilike('coupons', 'code', filter.code),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
valid_from: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
valid_until: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.discountRange) {
const [start, end] = filter.discountRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
discount: {
...where.discount,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
discount: {
...where.discount,
[Op.lte]: end,
},
};
}
}
if (filter.valid_fromRange) {
const [start, end] = filter.valid_fromRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
valid_from: {
...where.valid_from,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
valid_from: {
...where.valid_from,
[Op.lte]: end,
},
};
}
}
if (filter.valid_untilRange) {
const [start, end] = filter.valid_untilRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
valid_until: {
...where.valid_until,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
valid_until: {
...where.valid_until,
[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.coupons.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('coupons', 'code', query),
],
};
}
const records = await db.coupons.findAll({
attributes: ['id', 'code'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['code', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.code,
}));
}
};