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 Secure_gmail_tokensDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const secure_gmail_tokens = await db.secure_gmail_tokens.create( { id: data.id || undefined, access_token: data.access_token || null, refresh_token: data.refresh_token || null, expires_at: data.expires_at || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await secure_gmail_tokens.setAccount(data.account || null, { transaction, }); await secure_gmail_tokens.setAccounts(data.accounts || null, { transaction, }); return secure_gmail_tokens; } 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 secure_gmail_tokensData = data.map((item, index) => ({ id: item.id || undefined, access_token: item.access_token || null, refresh_token: item.refresh_token || null, expires_at: item.expires_at || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const secure_gmail_tokens = await db.secure_gmail_tokens.bulkCreate( secure_gmail_tokensData, { transaction }, ); // For each item created, replace relation files return secure_gmail_tokens; } 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 secure_gmail_tokens = await db.secure_gmail_tokens.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.access_token !== undefined) updatePayload.access_token = data.access_token; if (data.refresh_token !== undefined) updatePayload.refresh_token = data.refresh_token; if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at; updatePayload.updatedById = currentUser.id; await secure_gmail_tokens.update(updatePayload, { transaction }); if (data.account !== undefined) { await secure_gmail_tokens.setAccount( data.account, { transaction }, ); } if (data.accounts !== undefined) { await secure_gmail_tokens.setAccounts( data.accounts, { transaction }, ); } return secure_gmail_tokens; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const secure_gmail_tokens = await db.secure_gmail_tokens.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of secure_gmail_tokens) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of secure_gmail_tokens) { await record.destroy({ transaction }); } }); return secure_gmail_tokens; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const secure_gmail_tokens = await db.secure_gmail_tokens.findByPk( id, options, ); await secure_gmail_tokens.update( { deletedBy: currentUser.id, }, { transaction, }, ); await secure_gmail_tokens.destroy({ transaction, }); return secure_gmail_tokens; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const secure_gmail_tokens = await db.secure_gmail_tokens.findOne( { where }, { transaction }, ); if (!secure_gmail_tokens) { return secure_gmail_tokens; } const output = secure_gmail_tokens.get({ plain: true }); output.account = await secure_gmail_tokens.getAccount({ transaction, }); output.accounts = await secure_gmail_tokens.getAccounts({ 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 userAccounts = (user && user.accounts?.id) || null; if (userAccounts) { if (options?.currentUser?.accountsId) { where.accountsId = options.currentUser.accountsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.accounts, as: 'account', }, { model: db.accounts, as: 'accounts', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.access_token) { where = { ...where, [Op.and]: Utils.ilike( 'secure_gmail_tokens', 'access_token', filter.access_token, ), }; } if (filter.refresh_token) { where = { ...where, [Op.and]: Utils.ilike( 'secure_gmail_tokens', 'refresh_token', filter.refresh_token, ), }; } if (filter.expires_atRange) { const [start, end] = filter.expires_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, expires_at: { ...where.expires_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, expires_at: { ...where.expires_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.account) { const listItems = filter.account.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, accountId: { [Op.or]: listItems }, }; } if (filter.accounts) { const listItems = filter.accounts.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, accountsId: { [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.accountsId; } 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.secure_gmail_tokens.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('secure_gmail_tokens', 'access_token', query), ], }; } const records = await db.secure_gmail_tokens.findAll({ attributes: ['id', 'access_token'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['access_token', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.access_token, })); } };