UNPKG

@overture-stack/lyric

Version:
33 lines (32 loc) 1.49 kB
import SQONBuilder from '@overture-stack/sqon-builder'; import submittedRepository from '../repository/submittedRepository.js'; import { convertSqonToQuery } from '../utils/convertSqonToQuery.js'; const validationService = (dependencies) => { const { logger } = dependencies; const submittedDataRepo = submittedRepository(dependencies); const LOG_MODULE = 'VALIDATION_SERVICE'; return { /** * Checks whether a specific record exists in the database based on the given criteria. * @param param0 * @returns A promise that resolves to `true` if the record exists, or `false` if not. */ existsRecord: async ({ categoryId, entityName, field, organization, value, }) => { const { getTotalRecordsByCategoryIdAndOrganization } = submittedDataRepo; try { const sqonFilter = SQONBuilder.default.in(field, value); const sqlFilter = convertSqonToQuery(sqonFilter); const totalRecords = await getTotalRecordsByCategoryIdAndOrganization(categoryId, organization, { sql: sqlFilter, entityNames: [entityName], }); return totalRecords > 0; } catch (error) { logger.error(LOG_MODULE, 'Error validating record', { error }); throw new Error('Error validating the record.'); } }, }; }; export default validationService;