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, source_language: data.source_language || null, target_language: data.target_language || null, translation_time: data.translation_time || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await translations.setDevice(data.device || 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, source_language: item.source_language || null, target_language: item.target_language || null, translation_time: item.translation_time || 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 translations = await db.translations.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.source_language !== undefined) updatePayload.source_language = data.source_language; if (data.target_language !== undefined) updatePayload.target_language = data.target_language; if (data.translation_time !== undefined) updatePayload.translation_time = data.translation_time; updatePayload.updatedById = currentUser.id; await translations.update(updatePayload, { transaction }); if (data.device !== undefined) { await translations.setDevice( data.device, { 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.device = await translations.getDevice({ 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.devices, as: 'device', where: filter.device ? { [Op.or]: [ { id: { [Op.in]: filter.device .split('|') .map((term) => Utils.uuid(term)), }, }, { device_name: { [Op.or]: filter.device .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.source_language) { where = { ...where, [Op.and]: Utils.ilike( 'translations', 'source_language', filter.source_language, ), }; } if (filter.target_language) { where = { ...where, [Op.and]: Utils.ilike( 'translations', 'target_language', filter.target_language, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { translation_time: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { translation_time: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.translation_timeRange) { const [start, end] = filter.translation_timeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, translation_time: { ...where.translation_time, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, translation_time: { ...where.translation_time, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } 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.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) { let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('translations', 'source_language', query), ], }; } const records = await db.translations.findAll({ attributes: ['id', 'source_language'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['source_language', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.source_language, })); } };