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 Collateral_itemsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const collateral_items = await db.collateral_items.create( { id: data.id || undefined, item_name: data.item_name || null , item_description: data.item_description || null , estimated_value: data.estimated_value || null , condition: data.condition || null , status: data.status || null , received_at: data.received_at || null , returned_at: data.returned_at || null , storage_location: data.storage_location || null , security_note: data.security_note || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await collateral_items.setLoan_application( data.loan_application || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.collateral_items.getTableName(), belongsToColumn: 'photos', belongsToId: collateral_items.id, }, data.photos, options, ); return collateral_items; } 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 collateral_itemsData = data.map((item, index) => ({ id: item.id || undefined, item_name: item.item_name || null , item_description: item.item_description || null , estimated_value: item.estimated_value || null , condition: item.condition || null , status: item.status || null , received_at: item.received_at || null , returned_at: item.returned_at || null , storage_location: item.storage_location || null , security_note: item.security_note || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const collateral_items = await db.collateral_items.bulkCreate(collateral_itemsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < collateral_items.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.collateral_items.getTableName(), belongsToColumn: 'photos', belongsToId: collateral_items[i].id, }, data[i].photos, options, ); } return collateral_items; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const collateral_items = await db.collateral_items.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.item_name !== undefined) updatePayload.item_name = data.item_name; if (data.item_description !== undefined) updatePayload.item_description = data.item_description; if (data.estimated_value !== undefined) updatePayload.estimated_value = data.estimated_value; if (data.condition !== undefined) updatePayload.condition = data.condition; if (data.status !== undefined) updatePayload.status = data.status; if (data.received_at !== undefined) updatePayload.received_at = data.received_at; if (data.returned_at !== undefined) updatePayload.returned_at = data.returned_at; if (data.storage_location !== undefined) updatePayload.storage_location = data.storage_location; if (data.security_note !== undefined) updatePayload.security_note = data.security_note; updatePayload.updatedById = currentUser.id; await collateral_items.update(updatePayload, {transaction}); if (data.loan_application !== undefined) { await collateral_items.setLoan_application( data.loan_application, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.collateral_items.getTableName(), belongsToColumn: 'photos', belongsToId: collateral_items.id, }, data.photos, options, ); return collateral_items; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const collateral_items = await db.collateral_items.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of collateral_items) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of collateral_items) { await record.destroy({transaction}); } }); return collateral_items; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const collateral_items = await db.collateral_items.findByPk(id, options); await collateral_items.update({ deletedBy: currentUser.id }, { transaction, }); await collateral_items.destroy({ transaction }); return collateral_items; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const collateral_items = await db.collateral_items.findOne( { where }, { transaction }, ); if (!collateral_items) { return collateral_items; } const output = collateral_items.get({plain: true}); output.loan_application = await collateral_items.getLoan_application({ transaction }); output.photos = await collateral_items.getPhotos({ 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.loan_applications, as: 'loan_application', where: filter.loan_application ? { [Op.or]: [ { id: { [Op.in]: filter.loan_application.split('|').map(term => Utils.uuid(term)) } }, { application_number: { [Op.or]: filter.loan_application.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.file, as: 'photos', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.item_name) { where = { ...where, [Op.and]: Utils.ilike( 'collateral_items', 'item_name', filter.item_name, ), }; } if (filter.item_description) { where = { ...where, [Op.and]: Utils.ilike( 'collateral_items', 'item_description', filter.item_description, ), }; } if (filter.storage_location) { where = { ...where, [Op.and]: Utils.ilike( 'collateral_items', 'storage_location', filter.storage_location, ), }; } if (filter.security_note) { where = { ...where, [Op.and]: Utils.ilike( 'collateral_items', 'security_note', filter.security_note, ), }; } if (filter.estimated_valueRange) { const [start, end] = filter.estimated_valueRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, estimated_value: { ...where.estimated_value, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, estimated_value: { ...where.estimated_value, [Op.lte]: end, }, }; } } if (filter.received_atRange) { const [start, end] = filter.received_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, received_at: { ...where.received_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, received_at: { ...where.received_at, [Op.lte]: end, }, }; } } if (filter.returned_atRange) { const [start, end] = filter.returned_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, returned_at: { ...where.returned_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, returned_at: { ...where.returned_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.condition) { where = { ...where, condition: filter.condition, }; } 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.collateral_items.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( 'collateral_items', 'item_name', query, ), ], }; } const records = await db.collateral_items.findAll({ attributes: [ 'id', 'item_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['item_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.item_name, })); } };