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 Business_badgesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const business_badges = await db.business_badges.create( { id: data.id || undefined, badge_type: data.badge_type || null , status: data.status || null , granted_at: data.granted_at || null , revoked_at: data.revoked_at || null , notes: data.notes || null , created_at_ts: data.created_at_ts || null , updated_at_ts: data.updated_at_ts || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await business_badges.setBusiness( data.business || null, { transaction, }); return business_badges; } 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 business_badgesData = data.map((item, index) => ({ id: item.id || undefined, badge_type: item.badge_type || null , status: item.status || null , granted_at: item.granted_at || null , revoked_at: item.revoked_at || null , notes: item.notes || null , created_at_ts: item.created_at_ts || null , updated_at_ts: item.updated_at_ts || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const business_badges = await db.business_badges.bulkCreate(business_badgesData, { transaction }); // For each item created, replace relation files return business_badges; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const business_badges = await db.business_badges.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.badge_type !== undefined) updatePayload.badge_type = data.badge_type; if (data.status !== undefined) updatePayload.status = data.status; if (data.granted_at !== undefined) updatePayload.granted_at = data.granted_at; if (data.revoked_at !== undefined) updatePayload.revoked_at = data.revoked_at; if (data.notes !== undefined) updatePayload.notes = data.notes; if (data.created_at_ts !== undefined) updatePayload.created_at_ts = data.created_at_ts; if (data.updated_at_ts !== undefined) updatePayload.updated_at_ts = data.updated_at_ts; updatePayload.updatedById = currentUser.id; await business_badges.update(updatePayload, {transaction}); if (data.business !== undefined) { await business_badges.setBusiness( data.business, { transaction } ); } return business_badges; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const business_badges = await db.business_badges.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of business_badges) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of business_badges) { await record.destroy({transaction}); } }); return business_badges; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const business_badges = await db.business_badges.findByPk(id, options); await business_badges.update({ deletedBy: currentUser.id }, { transaction, }); await business_badges.destroy({ transaction }); return business_badges; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const business_badges = await db.business_badges.findOne( { where }, { transaction }, ); if (!business_badges) { return business_badges; } const output = business_badges.get({plain: true}); output.business = await business_badges.getBusiness({ 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.businesses, as: 'business', where: filter.business ? { [Op.or]: [ { id: { [Op.in]: filter.business.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.business.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'business_badges', 'notes', filter.notes, ), }; } if (filter.granted_atRange) { const [start, end] = filter.granted_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, granted_at: { ...where.granted_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, granted_at: { ...where.granted_at, [Op.lte]: end, }, }; } } if (filter.revoked_atRange) { const [start, end] = filter.revoked_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, revoked_at: { ...where.revoked_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, revoked_at: { ...where.revoked_at, [Op.lte]: end, }, }; } } if (filter.created_at_tsRange) { const [start, end] = filter.created_at_tsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, created_at_ts: { ...where.created_at_ts, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, created_at_ts: { ...where.created_at_ts, [Op.lte]: end, }, }; } } if (filter.updated_at_tsRange) { const [start, end] = filter.updated_at_tsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, updated_at_ts: { ...where.updated_at_ts, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, updated_at_ts: { ...where.updated_at_ts, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.badge_type) { where = { ...where, badge_type: filter.badge_type, }; } 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.business_badges.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( 'business_badges', 'notes', query, ), ], }; } const records = await db.business_badges.findAll({ attributes: [ 'id', 'notes' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['notes', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.notes, })); } };