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 Stock_countsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const stock_counts = await db.stock_counts.create( { id: data.id || undefined, count_number: data.count_number || null , status: data.status || null , scheduled_at: data.scheduled_at || null , started_at: data.started_at || null , completed_at: data.completed_at || null , notes: data.notes || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await stock_counts.setWarehouse( data.warehouse || null, { transaction, }); await stock_counts.setStorage_location( data.storage_location || null, { transaction, }); await stock_counts.setCreated_by_user( data.created_by_user || null, { transaction, }); await stock_counts.setVerified_by_user( data.verified_by_user || null, { transaction, }); return stock_counts; } 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 stock_countsData = data.map((item, index) => ({ id: item.id || undefined, count_number: item.count_number || null , status: item.status || null , scheduled_at: item.scheduled_at || null , started_at: item.started_at || null , completed_at: item.completed_at || 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 stock_counts = await db.stock_counts.bulkCreate(stock_countsData, { transaction }); // For each item created, replace relation files return stock_counts; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const stock_counts = await db.stock_counts.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.count_number !== undefined) updatePayload.count_number = data.count_number; if (data.status !== undefined) updatePayload.status = data.status; if (data.scheduled_at !== undefined) updatePayload.scheduled_at = data.scheduled_at; if (data.started_at !== undefined) updatePayload.started_at = data.started_at; if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at; if (data.notes !== undefined) updatePayload.notes = data.notes; updatePayload.updatedById = currentUser.id; await stock_counts.update(updatePayload, {transaction}); if (data.warehouse !== undefined) { await stock_counts.setWarehouse( data.warehouse, { transaction } ); } if (data.storage_location !== undefined) { await stock_counts.setStorage_location( data.storage_location, { transaction } ); } if (data.created_by_user !== undefined) { await stock_counts.setCreated_by_user( data.created_by_user, { transaction } ); } if (data.verified_by_user !== undefined) { await stock_counts.setVerified_by_user( data.verified_by_user, { transaction } ); } return stock_counts; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const stock_counts = await db.stock_counts.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of stock_counts) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of stock_counts) { await record.destroy({transaction}); } }); return stock_counts; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const stock_counts = await db.stock_counts.findByPk(id, options); await stock_counts.update({ deletedBy: currentUser.id }, { transaction, }); await stock_counts.destroy({ transaction }); return stock_counts; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const stock_counts = await db.stock_counts.findOne( { where }, { transaction }, ); if (!stock_counts) { return stock_counts; } const output = stock_counts.get({plain: true}); output.stock_count_lines_stock_count = await stock_counts.getStock_count_lines_stock_count({ transaction }); output.warehouse = await stock_counts.getWarehouse({ transaction }); output.storage_location = await stock_counts.getStorage_location({ transaction }); output.created_by_user = await stock_counts.getCreated_by_user({ transaction }); output.verified_by_user = await stock_counts.getVerified_by_user({ 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.warehouses, as: 'warehouse', where: filter.warehouse ? { [Op.or]: [ { id: { [Op.in]: filter.warehouse.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.warehouse.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.storage_locations, as: 'storage_location', where: filter.storage_location ? { [Op.or]: [ { id: { [Op.in]: filter.storage_location.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.storage_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'created_by_user', where: filter.created_by_user ? { [Op.or]: [ { id: { [Op.in]: filter.created_by_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.created_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'verified_by_user', where: filter.verified_by_user ? { [Op.or]: [ { id: { [Op.in]: filter.verified_by_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.verified_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.count_number) { where = { ...where, [Op.and]: Utils.ilike( 'stock_counts', 'count_number', filter.count_number, ), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'stock_counts', 'notes', filter.notes, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { scheduled_at: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { completed_at: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.scheduled_atRange) { const [start, end] = filter.scheduled_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, scheduled_at: { ...where.scheduled_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, scheduled_at: { ...where.scheduled_at, [Op.lte]: end, }, }; } } if (filter.started_atRange) { const [start, end] = filter.started_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, started_at: { ...where.started_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, started_at: { ...where.started_at, [Op.lte]: end, }, }; } } if (filter.completed_atRange) { const [start, end] = filter.completed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, completed_at: { ...where.completed_at, [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.stock_counts.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( 'stock_counts', 'count_number', query, ), ], }; } const records = await db.stock_counts.findAll({ attributes: [ 'id', 'count_number' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['count_number', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.count_number, })); } };