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 TranslationsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const translations = await db.translations.create( { id: data.id || undefined, swahili_text: data.swahili_text || null, translated_text: data.translated_text || null, target_language: data.target_language || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await translations.setUser(data.user || null, { transaction, }); await translations.setTenants(data.tenants || null, { transaction, }); return translations; } 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 translationsData = data.map((item, index) => ({ id: item.id || undefined, swahili_text: item.swahili_text || null, translated_text: item.translated_text || null, target_language: item.target_language || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const translations = await db.translations.bulkCreate(translationsData, { transaction, }); // For each item created, replace relation files return translations; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const globalAccess = currentUser.app_role?.globalAccess; const translations = await db.translations.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.swahili_text !== undefined) updatePayload.swahili_text = data.swahili_text; if (data.translated_text !== undefined) updatePayload.translated_text = data.translated_text; if (data.target_language !== undefined) updatePayload.target_language = data.target_language; updatePayload.updatedById = currentUser.id; await translations.update(updatePayload, { transaction }); if (data.user !== undefined) { await translations.setUser( data.user, { transaction }, ); } if (data.tenants !== undefined) { await translations.setTenants( data.tenants, { transaction }, ); } return translations; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const translations = await db.translations.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of translations) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of translations) { await record.destroy({ transaction }); } }); return translations; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const translations = await db.translations.findByPk(id, options); await translations.update( { deletedBy: currentUser.id, }, { transaction, }, ); await translations.destroy({ transaction, }); return translations; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const translations = await db.translations.findOne( { where }, { transaction }, ); if (!translations) { return translations; } const output = translations.get({ plain: true }); output.user = await translations.getUser({ transaction, }); output.tenants = await translations.getTenants({ transaction, }); return output; } static async findAll(filter, globalAccess, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; const userTenants = (user && user.tenants?.id) || null; if (userTenants) { if (options?.currentUser?.tenantsId) { where.tenantsId = options.currentUser.tenantsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.users, as: 'user', where: filter.user ? { [Op.or]: [ { id: { [Op.in]: filter.user .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.user .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.tenants, as: 'tenants', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.swahili_text) { where = { ...where, [Op.and]: Utils.ilike( 'translations', 'swahili_text', filter.swahili_text, ), }; } if (filter.translated_text) { where = { ...where, [Op.and]: Utils.ilike( 'translations', 'translated_text', filter.translated_text, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.target_language) { where = { ...where, target_language: filter.target_language, }; } if (filter.tenants) { const listItems = filter.tenants.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, tenantsId: { [Op.or]: listItems }, }; } 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, }, }; } } } if (globalAccess) { delete where.tenantsId; } 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.translations.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, globalAccess, organizationId, ) { let where = {}; if (!globalAccess && organizationId) { where.organizationId = organizationId; } if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('translations', 'swahili_text', query), ], }; } const records = await db.translations.findAll({ attributes: ['id', 'swahili_text'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['swahili_text', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.swahili_text, })); } };