plugin-voting/repositories/ResponseRepository.js
2025-11-03 14:01:07 +02:00

63 lines
1.7 KiB
JavaScript

const BaseRepository = require('../../../src/database/repository');
const logger = require('../../../src/utils/logger');
class ResponseRepository extends BaseRepository {
constructor() {
super('pg_vt_responses');
}
/**
* Find responses with filters
* @param {Object} filters - Filter criteria
* @param {Object} options - Query options
*/
async findWithFilters(filters = {}, options = {}) {
const criteria = {};
if (filters.campaign_id) criteria.campaign_id = filters.campaign_id;
if (filters.person_id) criteria.person_id = filters.person_id;
if (filters.answered !== undefined) criteria.answered = filters.answered;
const orderBy = options.orderBy || 'created_at';
const orderDirection = options.orderDirection || 'desc';
const limit = options.limit;
return await this.findAll(criteria, { orderBy, orderDirection, limit });
}
/**
* Get or create response
* @param {string} campaignId - Campaign ID
* @param {string} personId - Person ID
*/
async getOrCreate(campaignId, personId) {
const existing = await this.findOne({
campaign_id: campaignId,
person_id: personId
});
if (existing) return existing;
return await this.create({
campaign_id: campaignId,
person_id: personId,
answered: false
});
}
/**
* Mark response as answered
* @param {string} responseId - Response ID
*/
async markAnswered(responseId) {
return await this.updateById(responseId, {
answered: true,
answered_date: new Date().toISOString(),
last_modified_date: new Date().toISOString(),
updated_at: new Date().toISOString()
});
}
}
module.exports = ResponseRepository;