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 Store_dataDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const store_data = await db.store_data.create( { id: data.id || undefined, daily_expenses: data.daily_expenses || null, online_payment_received: data.online_payment_received || null, cash_received: data.cash_received || null, total_daily_sales: data.total_daily_sales || null, monthly_sales: data.monthly_sales || null, yearly_sales: data.yearly_sales || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await store_data.setStore(data.store || null, { transaction, }); return store_data; } 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 store_dataData = data.map((item, index) => ({ id: item.id || undefined, daily_expenses: item.daily_expenses || null, online_payment_received: item.online_payment_received || null, cash_received: item.cash_received || null, total_daily_sales: item.total_daily_sales || null, monthly_sales: item.monthly_sales || null, yearly_sales: item.yearly_sales || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const store_data = await db.store_data.bulkCreate(store_dataData, { transaction, }); // For each item created, replace relation files return store_data; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const store_data = await db.store_data.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.daily_expenses !== undefined) updatePayload.daily_expenses = data.daily_expenses; if (data.online_payment_received !== undefined) updatePayload.online_payment_received = data.online_payment_received; if (data.cash_received !== undefined) updatePayload.cash_received = data.cash_received; if (data.total_daily_sales !== undefined) updatePayload.total_daily_sales = data.total_daily_sales; if (data.monthly_sales !== undefined) updatePayload.monthly_sales = data.monthly_sales; if (data.yearly_sales !== undefined) updatePayload.yearly_sales = data.yearly_sales; updatePayload.updatedById = currentUser.id; await store_data.update(updatePayload, { transaction }); if (data.store !== undefined) { await store_data.setStore( data.store, { transaction }, ); } return store_data; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const store_data = await db.store_data.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of store_data) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of store_data) { await record.destroy({ transaction }); } }); return store_data; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const store_data = await db.store_data.findByPk(id, options); await store_data.update( { deletedBy: currentUser.id, }, { transaction, }, ); await store_data.destroy({ transaction, }); return store_data; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const store_data = await db.store_data.findOne({ where }, { transaction }); if (!store_data) { return store_data; } const output = store_data.get({ plain: true }); output.store = await store_data.getStore({ 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.stores, as: 'store', where: filter.store ? { [Op.or]: [ { id: { [Op.in]: filter.store .split('|') .map((term) => Utils.uuid(term)), }, }, { store_name: { [Op.or]: filter.store .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.daily_expensesRange) { const [start, end] = filter.daily_expensesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, daily_expenses: { ...where.daily_expenses, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, daily_expenses: { ...where.daily_expenses, [Op.lte]: end, }, }; } } if (filter.online_payment_receivedRange) { const [start, end] = filter.online_payment_receivedRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, online_payment_received: { ...where.online_payment_received, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, online_payment_received: { ...where.online_payment_received, [Op.lte]: end, }, }; } } if (filter.cash_receivedRange) { const [start, end] = filter.cash_receivedRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, cash_received: { ...where.cash_received, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, cash_received: { ...where.cash_received, [Op.lte]: end, }, }; } } if (filter.total_daily_salesRange) { const [start, end] = filter.total_daily_salesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, total_daily_sales: { ...where.total_daily_sales, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, total_daily_sales: { ...where.total_daily_sales, [Op.lte]: end, }, }; } } if (filter.monthly_salesRange) { const [start, end] = filter.monthly_salesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, monthly_sales: { ...where.monthly_sales, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, monthly_sales: { ...where.monthly_sales, [Op.lte]: end, }, }; } } if (filter.yearly_salesRange) { const [start, end] = filter.yearly_salesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, yearly_sales: { ...where.yearly_sales, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, yearly_sales: { ...where.yearly_sales, [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.store_data.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('store_data', 'store', query), ], }; } const records = await db.store_data.findAll({ attributes: ['id', 'store'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['store', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.store, })); } };