| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 'use strict';
-
- const Schmervice = require('@hapipal/schmervice');
-
-
- module.exports = class MembershipService extends Schmervice.Service {
- constructor(...args) { super(...args) }
-
- /**
- * Internal method to get list of grouping_ids for this user
- * @param {number} userId
- * @returns {Array} List of all grouping_ids for user
- */
- async _getGroupIdsForUserId(userId) {
- const { Membership } = this.server.models()
-
- /** Grab every Membership associated with this id */
- const allMemberships = await Membership.query()
- .where('user_id', userId)
-
- /** Copy a list of the just the Groupings */
- const groupingIdsToGrab = allMemberships.map(membership => membership.grouping_id)
-
- /** Uncomment to dedupe the list just in case */
- return [...new Set(groupingIdsToGrab)]
- }
-
- /**
- * Internal method to create a new grouping
- * @param {*} groupingToTry
- * @param {*} txn
- * @returns
- */
- async _createGrouping(groupingToTry, txn) {
- const { Grouping } = this.server.models()
- const groupingInfo = {
- grouping_name: groupingToTry.grouping_name,
- grouping_type: groupingToTry.grouping_type,
- }
- return await Grouping.query(txn)
- .insert(groupingInfo)
- }
-
- async findOrCreateGrouping(groupingToTry) {
- let idToReturn = groupingToTry.grouping_id
- if(!idToReturn) {
- /** ?: For some reason this returns the key id */
- const grouping = await this._createGrouping(groupingToTry)
- idToReturn = grouping.id
- }
- return idToReturn
- }
-
- /**
- * Get a list of groupings for user
- * @param {number} userId
- * @returns {Array}
- */
- async findGroupingsById(userId) {
- const { Grouping } = this.server.models()
-
- const dedupedGroupings = await this._getGroupIdsForUserId(userId)
-
- /** Grab just the Groupings this id has a Membership for */
- return await Grouping.query()
- .throwIfNotFound()
- .whereIn('grouping_id', dedupedGroupings)
- }
-
- /**
- * Check for grouping membership then add membership record
- * @param {number} userId
- * @param {number} groupingId
- * @param {string} role
- * @returns
- */
- async joinGrouping(userId, groupingId, role, txn) {
- const { Membership } = this.server.models()
-
- const dedupedGroupings = await this._getGroupIdsForUserId(userId)
-
- /** Do NOTHING if already in Grouping */
- if(dedupedGroupings.includes(groupingId)) return
-
- console.log(dedupedGroupings)
- const membershipInfo = {
- user_id: userId,
- grouping_id: groupingId,
- membership_type: role,
- can_edit: false
- }
- console.log(membershipInfo)
- return await Membership.query(txn)
- .insert(membershipInfo)
- }
-
- /**
- * Remove membership record based on grouping_id
- * @param {number} userId
- * @param {number} groupingId
- * @returns
- */
- async leaveGrouping(userId, groupingId) {
- const { Membership } = this.server.models()
-
- const dedupedGroupings = await this._getGroupIdsForUserId(userId)
-
- /** Do NOTHING if NOT in Grouping */
- if(!dedupedGroupings.includes(groupingId)) return
-
- return await Membership.query()
- .delete()
- .where('grouping_id', groupingId)
- }
- }
|