70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
const BaseRepository = require('../../../src/database/repository');
|
|
const logger = require('../../../src/utils/logger');
|
|
|
|
class GroupMemberRepository extends BaseRepository {
|
|
constructor() {
|
|
super('pg_vt_group_members');
|
|
}
|
|
|
|
/**
|
|
* Find members with filters
|
|
* @param {Object} filters - Filter criteria
|
|
* @param {Object} options - Query options
|
|
*/
|
|
async findWithFilters(filters = {}, options = {}) {
|
|
const criteria = {};
|
|
|
|
if (filters.group_id) criteria.group_id = filters.group_id;
|
|
if (filters.person_id) criteria.person_id = filters.person_id;
|
|
|
|
const orderBy = options.orderBy || 'created_at';
|
|
const orderDirection = options.orderDirection || 'desc';
|
|
const limit = options.limit;
|
|
|
|
return await this.findAll(criteria, { orderBy, orderDirection, limit });
|
|
}
|
|
|
|
/**
|
|
* Add member to group
|
|
* @param {string} groupId - Group ID
|
|
* @param {string} personId - Person ID
|
|
*/
|
|
async addMember(groupId, personId) {
|
|
try {
|
|
return await this.create({
|
|
group_id: groupId,
|
|
person_id: personId,
|
|
created_at: new Date().toISOString()
|
|
});
|
|
} catch (error) {
|
|
// Already exists, return existing
|
|
if (error.message && error.message.includes('duplicate')) {
|
|
return await this.findOne({ group_id: groupId, person_id: personId });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove member from group
|
|
* @param {string} groupId - Group ID
|
|
* @param {string} personId - Person ID
|
|
*/
|
|
async removeMember(groupId, personId) {
|
|
const member = await this.findOne({ group_id: groupId, person_id: personId });
|
|
if (!member) return false;
|
|
|
|
return await this.deleteById(member.id);
|
|
}
|
|
|
|
/**
|
|
* Get all members of a group
|
|
* @param {string} groupId - Group ID
|
|
*/
|
|
async getGroupMembers(groupId) {
|
|
return await this.findAll({ group_id: groupId });
|
|
}
|
|
}
|
|
|
|
module.exports = GroupMemberRepository;
|
|
|