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 Verification_sessionsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const verification_sessions = await db.verification_sessions.create( { id: data.id || undefined, session_code: data.session_code || null , started_at: data.started_at || null , completed_at: data.completed_at || null , client_ip: data.client_ip || null , user_agent: data.user_agent || null , latitude: data.latitude || null , longitude: data.longitude || null , consent_status: data.consent_status || null , consent_at: data.consent_at || null , analysis_mode: data.analysis_mode || null , camera_permission_granted: data.camera_permission_granted || false , photo_captured_at: data.photo_captured_at || null , image_stored: data.image_stored || false , image_deleted_at: data.image_deleted_at || null , age_estimate: data.age_estimate || null , age_confidence: data.age_confidence || null , emotion_estimate: data.emotion_estimate || null , emotion_confidence: data.emotion_confidence || null , min_age_threshold: data.min_age_threshold || null , decision: data.decision || null , fallback_clicked: data.fallback_clicked || false , fallback_type: data.fallback_type || null , failure_reason: data.failure_reason || null , review_status: data.review_status || null , notes: data.notes || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await verification_sessions.setQr_code( data.qr_code || null, { transaction, }); await verification_sessions.setVenue( data.venue || null, { transaction, }); await verification_sessions.setReviewed_by( data.reviewed_by || null, { transaction, }); await verification_sessions.setOrganizations( data.organizations || null, { transaction, }); await FileDBApi.replaceRelationFiles( { belongsTo: db.verification_sessions.getTableName(), belongsToColumn: 'selfie_image', belongsToId: verification_sessions.id, }, data.selfie_image, options, ); return verification_sessions; } 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 verification_sessionsData = data.map((item, index) => ({ id: item.id || undefined, session_code: item.session_code || null , started_at: item.started_at || null , completed_at: item.completed_at || null , client_ip: item.client_ip || null , user_agent: item.user_agent || null , latitude: item.latitude || null , longitude: item.longitude || null , consent_status: item.consent_status || null , consent_at: item.consent_at || null , analysis_mode: item.analysis_mode || null , camera_permission_granted: item.camera_permission_granted || false , photo_captured_at: item.photo_captured_at || null , image_stored: item.image_stored || false , image_deleted_at: item.image_deleted_at || null , age_estimate: item.age_estimate || null , age_confidence: item.age_confidence || null , emotion_estimate: item.emotion_estimate || null , emotion_confidence: item.emotion_confidence || null , min_age_threshold: item.min_age_threshold || null , decision: item.decision || null , fallback_clicked: item.fallback_clicked || false , fallback_type: item.fallback_type || null , failure_reason: item.failure_reason || null , review_status: item.review_status || null , notes: item.notes || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const verification_sessions = await db.verification_sessions.bulkCreate(verification_sessionsData, { transaction }); // For each item created, replace relation files for (let i = 0; i < verification_sessions.length; i++) { await FileDBApi.replaceRelationFiles( { belongsTo: db.verification_sessions.getTableName(), belongsToColumn: 'selfie_image', belongsToId: verification_sessions[i].id, }, data[i].selfie_image, options, ); } return verification_sessions; } 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 verification_sessions = await db.verification_sessions.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.session_code !== undefined) updatePayload.session_code = data.session_code; if (data.started_at !== undefined) updatePayload.started_at = data.started_at; if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at; if (data.client_ip !== undefined) updatePayload.client_ip = data.client_ip; if (data.user_agent !== undefined) updatePayload.user_agent = data.user_agent; if (data.latitude !== undefined) updatePayload.latitude = data.latitude; if (data.longitude !== undefined) updatePayload.longitude = data.longitude; if (data.consent_status !== undefined) updatePayload.consent_status = data.consent_status; if (data.consent_at !== undefined) updatePayload.consent_at = data.consent_at; if (data.analysis_mode !== undefined) updatePayload.analysis_mode = data.analysis_mode; if (data.camera_permission_granted !== undefined) updatePayload.camera_permission_granted = data.camera_permission_granted; if (data.photo_captured_at !== undefined) updatePayload.photo_captured_at = data.photo_captured_at; if (data.image_stored !== undefined) updatePayload.image_stored = data.image_stored; if (data.image_deleted_at !== undefined) updatePayload.image_deleted_at = data.image_deleted_at; if (data.age_estimate !== undefined) updatePayload.age_estimate = data.age_estimate; if (data.age_confidence !== undefined) updatePayload.age_confidence = data.age_confidence; if (data.emotion_estimate !== undefined) updatePayload.emotion_estimate = data.emotion_estimate; if (data.emotion_confidence !== undefined) updatePayload.emotion_confidence = data.emotion_confidence; if (data.min_age_threshold !== undefined) updatePayload.min_age_threshold = data.min_age_threshold; if (data.decision !== undefined) updatePayload.decision = data.decision; if (data.fallback_clicked !== undefined) updatePayload.fallback_clicked = data.fallback_clicked; if (data.fallback_type !== undefined) updatePayload.fallback_type = data.fallback_type; if (data.failure_reason !== undefined) updatePayload.failure_reason = data.failure_reason; if (data.review_status !== undefined) updatePayload.review_status = data.review_status; if (data.notes !== undefined) updatePayload.notes = data.notes; updatePayload.updatedById = currentUser.id; await verification_sessions.update(updatePayload, {transaction}); if (data.qr_code !== undefined) { await verification_sessions.setQr_code( data.qr_code, { transaction } ); } if (data.venue !== undefined) { await verification_sessions.setVenue( data.venue, { transaction } ); } if (data.reviewed_by !== undefined) { await verification_sessions.setReviewed_by( data.reviewed_by, { transaction } ); } if (data.organizations !== undefined) { await verification_sessions.setOrganizations( data.organizations, { transaction } ); } await FileDBApi.replaceRelationFiles( { belongsTo: db.verification_sessions.getTableName(), belongsToColumn: 'selfie_image', belongsToId: verification_sessions.id, }, data.selfie_image, options, ); return verification_sessions; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const verification_sessions = await db.verification_sessions.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of verification_sessions) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of verification_sessions) { await record.destroy({transaction}); } }); return verification_sessions; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const verification_sessions = await db.verification_sessions.findByPk(id, options); await verification_sessions.update({ deletedBy: currentUser.id }, { transaction, }); await verification_sessions.destroy({ transaction }); return verification_sessions; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const verification_sessions = await db.verification_sessions.findOne( { where }, { transaction }, ); if (!verification_sessions) { return verification_sessions; } const output = verification_sessions.get({plain: true}); output.qr_code = await verification_sessions.getQr_code({ transaction }); output.venue = await verification_sessions.getVenue({ transaction }); output.reviewed_by = await verification_sessions.getReviewed_by({ transaction }); output.selfie_image = await verification_sessions.getSelfie_image({ transaction }); output.organizations = await verification_sessions.getOrganizations({ 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 userOrganizations = (user && user.organizations?.id) || null; if (userOrganizations) { if (options?.currentUser?.organizationsId) { where.organizationsId = options.currentUser.organizationsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.qr_codes, as: 'qr_code', where: filter.qr_code ? { [Op.or]: [ { id: { [Op.in]: filter.qr_code.split('|').map(term => Utils.uuid(term)) } }, { label: { [Op.or]: filter.qr_code.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.venues, as: 'venue', where: filter.venue ? { [Op.or]: [ { id: { [Op.in]: filter.venue.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.venue.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'reviewed_by', where: filter.reviewed_by ? { [Op.or]: [ { id: { [Op.in]: filter.reviewed_by.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.reviewed_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.organizations, as: 'organizations', }, { model: db.file, as: 'selfie_image', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.session_code) { where = { ...where, [Op.and]: Utils.ilike( 'verification_sessions', 'session_code', filter.session_code, ), }; } if (filter.client_ip) { where = { ...where, [Op.and]: Utils.ilike( 'verification_sessions', 'client_ip', filter.client_ip, ), }; } if (filter.user_agent) { where = { ...where, [Op.and]: Utils.ilike( 'verification_sessions', 'user_agent', filter.user_agent, ), }; } if (filter.failure_reason) { where = { ...where, [Op.and]: Utils.ilike( 'verification_sessions', 'failure_reason', filter.failure_reason, ), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'verification_sessions', 'notes', filter.notes, ), }; } if (filter.started_atRange) { const [start, end] = filter.started_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, started_at: { ...where.started_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, started_at: { ...where.started_at, [Op.lte]: end, }, }; } } if (filter.completed_atRange) { const [start, end] = filter.completed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.lte]: end, }, }; } } if (filter.latitudeRange) { const [start, end] = filter.latitudeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, latitude: { ...where.latitude, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, latitude: { ...where.latitude, [Op.lte]: end, }, }; } } if (filter.longitudeRange) { const [start, end] = filter.longitudeRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, longitude: { ...where.longitude, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, longitude: { ...where.longitude, [Op.lte]: end, }, }; } } if (filter.consent_atRange) { const [start, end] = filter.consent_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, consent_at: { ...where.consent_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, consent_at: { ...where.consent_at, [Op.lte]: end, }, }; } } if (filter.photo_captured_atRange) { const [start, end] = filter.photo_captured_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, photo_captured_at: { ...where.photo_captured_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, photo_captured_at: { ...where.photo_captured_at, [Op.lte]: end, }, }; } } if (filter.image_deleted_atRange) { const [start, end] = filter.image_deleted_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, image_deleted_at: { ...where.image_deleted_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, image_deleted_at: { ...where.image_deleted_at, [Op.lte]: end, }, }; } } if (filter.age_estimateRange) { const [start, end] = filter.age_estimateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, age_estimate: { ...where.age_estimate, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, age_estimate: { ...where.age_estimate, [Op.lte]: end, }, }; } } if (filter.age_confidenceRange) { const [start, end] = filter.age_confidenceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, age_confidence: { ...where.age_confidence, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, age_confidence: { ...where.age_confidence, [Op.lte]: end, }, }; } } if (filter.emotion_confidenceRange) { const [start, end] = filter.emotion_confidenceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, emotion_confidence: { ...where.emotion_confidence, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, emotion_confidence: { ...where.emotion_confidence, [Op.lte]: end, }, }; } } if (filter.min_age_thresholdRange) { const [start, end] = filter.min_age_thresholdRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, min_age_threshold: { ...where.min_age_threshold, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, min_age_threshold: { ...where.min_age_threshold, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.consent_status) { where = { ...where, consent_status: filter.consent_status, }; } if (filter.analysis_mode) { where = { ...where, analysis_mode: filter.analysis_mode, }; } if (filter.camera_permission_granted) { where = { ...where, camera_permission_granted: filter.camera_permission_granted, }; } if (filter.image_stored) { where = { ...where, image_stored: filter.image_stored, }; } if (filter.emotion_estimate) { where = { ...where, emotion_estimate: filter.emotion_estimate, }; } if (filter.decision) { where = { ...where, decision: filter.decision, }; } if (filter.fallback_clicked) { where = { ...where, fallback_clicked: filter.fallback_clicked, }; } if (filter.fallback_type) { where = { ...where, fallback_type: filter.fallback_type, }; } if (filter.review_status) { where = { ...where, review_status: filter.review_status, }; } if (filter.organizations) { const listItems = filter.organizations.split('|').map(item => { return Utils.uuid(item) }); where = { ...where, organizationsId: {[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.organizationsId; } 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.verification_sessions.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( 'verification_sessions', 'session_code', query, ), ], }; } const records = await db.verification_sessions.findAll({ attributes: [ 'id', 'session_code' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['session_code', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.session_code, })); } };