| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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, limit, offset = 0) {
- const { MatchQueue } = this.server.models()
- if (typeof limit === 'undefined') {
- return await MatchQueue.query()
- .where('profile_id', profileId)
- .where('is_deleted', 0)
- }
-
- return await MatchQueue.query()
- .where('profile_id', profileId)
- .where('is_deleted', 0)
- .limit(limit)
- .offset(offset)
- }
- /**
- * 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()
- /** Always set row to deleted */
- await MatchQueue.query()
- .where('profile_id', profileId)
- .andWhere('target_id', targetId)
- .patch({
- is_deleted: true,
- })
- .first()
-
- if (reinsert) {
- await MatchQueue.query().insert({
- profile_id: profileId,
- target_id: targetId,
- is_deleted: false,
- })
- }
- return await this.getQueue(profileId)
- }
- }
|