const Schmervice = require('@hapipal/schmervice') module.exports = class MatchQueueService extends Schmervice.Service { constructor(...args) { super(...args) } async getPotentials(profileId) { const { MatchQueue } = this.server.models() const allPotentials = await MatchQueue.query() .where('profile_id', profileId) .andWhere('is_deleted', false) return allPotentials } /** * Saves Scored Profile Ids to MatchQue IN ORDER * @param {number} profileId * @param {array} potentialProfileIds */ async insertScoredProfilesIntoMatchQueue(profileId, potentialProfileIds) { const { MatchQueue } = this.server.models() // returns an array of all matches for the profileId where the profile_id_2 already exists in the potentialProfileIds array await MatchQueue.query() .patch({ is_deleted: true, }) .where('profile_id', profileId) for (let potentialProfileId of potentialProfileIds) { await MatchQueue.query().insert({ profile_id: profileId, target_id: potentialProfileId, is_deleted: false, }) } return await this.getPotentials(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({ deleted: true, }) .where('profile_id', profileId) .andWhere('target_id', targetId) .first() if (reinsert) { await MatchQueue.query().insert({ profile_id: profileId, target_id: targetId, deleted: false, }) } return await this.getPotentials(profileId) } }