const Schmervice = require('@hapipal/schmervice') module.exports = class MatchQueueService extends Schmervice.Service { constructor(...args) { super(...args) } /** * Gets a list of queue entries for profileId * @param {number} profileId * @returns {array} MatchQueue */ async getQueue(profileId) { const { MatchQueue } = this.server.models() return await MatchQueue.query() .where('profile_id', profileId) .where('is_deleted', 0) } /** * Returns queues by profile id by user type * @returns {object} */ async getAllQueues() { const { MatchQueue } = this.server.models() const queueEntries = await MatchQueue.query() .andWhere('is_deleted', false) .withGraphFetched('user') const queueByProfileId = { poster: {}, seeker: {}, } queueEntries.forEach(entry => { const type = entry.user.is_poster == 1 ? 'poster' : 'seeker' if (!queueByProfileId[type][entry.profile_id]) { queueByProfileId[type][entry.profile_id] = [] } queueByProfileId[type][entry.profile_id].push(entry.target_id) }) return queueByProfileId } /** * Saves Scored Profile Ids to MatchQue IN ORDER * @param {number} profileId * @param {array} targetIds */ async saveMatchQueue(profileId, targetIds) { const { MatchQueue } = this.server.models() // returns an array of all matches for the profileId where the target_id already exists in the targetIds array await MatchQueue.query() .patch({ is_deleted: true, }) .where('profile_id', profileId) for (let id of targetIds) { await MatchQueue.query().insert({ profile_id: profileId, target_id: id, is_deleted: false, }) } return await this.getQueue(profileId) } /** * Set the rows deleted as true, does NOT DELETE from database * @param {number} profileId * @param {number} targetId * @param {boolean} reinsert * @returns */ async markAsDeleted(profileId, targetId, reinsert) { const { MatchQueue } = this.server.models() await MatchQueue.query() .patch({ is_deleted: true, }) .where('profile_id', profileId) .andWhere('target_id', targetId) .first() if (reinsert) { await MatchQueue.query().insert({ profile_id: profileId, target_id: targetId, is_deleted: false, }) } return await this.getQueue(profileId) } }