const db = require('../models'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class SellersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const sellers = await db.sellers.create( { id: data.id || undefined, status: data.status || null , bank_account: data.bank_account || null , ifsc: data.ifsc || null , score: data.score || null , notes: data.notes || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return sellers; } 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 sellersData = data.map((item, index) => ({ id: item.id || undefined, status: item.status || null , bank_account: item.bank_account || null , ifsc: item.ifsc || null , score: item.score || null , notes: item.notes || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const sellers = await db.sellers.bulkCreate(sellersData, { transaction }); return sellers; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const sellers = await db.sellers.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.status !== undefined) updatePayload.status = data.status; if (data.bank_account !== undefined) updatePayload.bank_account = data.bank_account; if (data.ifsc !== undefined) updatePayload.ifsc = data.ifsc; if (data.score !== undefined) updatePayload.score = data.score; if (data.notes !== undefined) updatePayload.notes = data.notes; updatePayload.updatedById = currentUser.id; await sellers.update(updatePayload, {transaction}); return sellers; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const sellers = await db.sellers.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of sellers) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of sellers) { await record.destroy({transaction}); } }); return sellers; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const sellers = await db.sellers.findByPk(id, options); await sellers.update({ deletedBy: currentUser.id }, { transaction, }); await sellers.destroy({ transaction }); return sellers; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const sellers = await db.sellers.findOne( { where }, { transaction }, ); if (!sellers) { return sellers; } const output = sellers.get({plain: true}); return output; } static async findAll(filter, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; 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.bank_account) { where = { ...where, [Op.and]: Utils.ilike( 'sellers', 'bank_account', filter.bank_account, ), }; } if (filter.ifsc) { where = { ...where, [Op.and]: Utils.ilike( 'sellers', 'ifsc', filter.ifsc, ), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'sellers', 'notes', filter.notes, ), }; } if (filter.scoreRange) { const [start, end] = filter.scoreRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, score: { ...where.score, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, score: { ...where.score, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.status) { where = { ...where, status: filter.status, }; } 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.sellers.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( 'sellers', 'bank_account', query, ), ], }; } const records = await db.sellers.findAll({ attributes: [ 'id', 'bank_account' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['bank_account', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.bank_account, })); } };