選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

membership.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. const Schmervice = require('@hapipal/schmervice');
  3. module.exports = class MembershipService extends Schmervice.Service {
  4. constructor(...args) { super(...args) }
  5. /**
  6. * Internal method to get list of grouping_ids for this user
  7. * @param {number} userId
  8. * @returns {Array} List of all grouping_ids for user
  9. */
  10. async _getGroupIdsForUserId(userId) {
  11. const { Membership } = this.server.models()
  12. /** Grab every Membership associated with this id */
  13. const allMemberships = await Membership.query()
  14. .where('user_id', userId)
  15. /** Copy a list of the just the Groupings */
  16. const groupingIdsToGrab = allMemberships.map(membership => membership.grouping_id)
  17. /** Uncomment to dedupe the list just in case */
  18. return [...new Set(groupingIdsToGrab)]
  19. }
  20. /**
  21. * Internal method to create a new grouping
  22. * @param {*} groupingToTry
  23. * @param {*} txn
  24. * @returns
  25. */
  26. async _createGrouping(groupingToTry, txn) {
  27. const { Grouping } = this.server.models()
  28. const groupingInfo = {
  29. grouping_name: groupingToTry.grouping_name,
  30. grouping_type: groupingToTry.grouping_type,
  31. }
  32. return await Grouping.query(txn)
  33. .insert(groupingInfo)
  34. }
  35. async findOrCreateGrouping(groupingToTry) {
  36. let idToReturn = groupingToTry.grouping_id
  37. if(!idToReturn) {
  38. /** ?: For some reason this returns the key id */
  39. const grouping = await this._createGrouping(groupingToTry)
  40. idToReturn = grouping.id
  41. }
  42. return idToReturn
  43. }
  44. /**
  45. * Get a list of groupings for user
  46. * @param {number} userId
  47. * @returns {Array}
  48. */
  49. async findGroupingsById(userId) {
  50. const { Grouping } = this.server.models()
  51. const dedupedGroupings = await this._getGroupIdsForUserId(userId)
  52. /** Grab just the Groupings this id has a Membership for */
  53. return await Grouping.query()
  54. .throwIfNotFound()
  55. .whereIn('grouping_id', dedupedGroupings)
  56. }
  57. /**
  58. * Check for grouping membership then add membership record
  59. * @param {number} userId
  60. * @param {number} groupingId
  61. * @param {string} role
  62. * @returns
  63. */
  64. async joinGrouping(userId, groupingId, role, txn) {
  65. const { Membership } = this.server.models()
  66. const dedupedGroupings = await this._getGroupIdsForUserId(userId)
  67. /** Do NOTHING if already in Grouping */
  68. if(dedupedGroupings.includes(groupingId)) return
  69. console.log(dedupedGroupings)
  70. const membershipInfo = {
  71. user_id: userId,
  72. grouping_id: groupingId,
  73. membership_type: role,
  74. can_edit: false
  75. }
  76. console.log(membershipInfo)
  77. return await Membership.query(txn)
  78. .insert(membershipInfo)
  79. }
  80. /**
  81. * Remove membership record based on grouping_id
  82. * @param {number} userId
  83. * @param {number} groupingId
  84. * @returns
  85. */
  86. async leaveGrouping(userId, groupingId) {
  87. const { Membership } = this.server.models()
  88. const dedupedGroupings = await this._getGroupIdsForUserId(userId)
  89. /** Do NOTHING if NOT in Grouping */
  90. if(!dedupedGroupings.includes(groupingId)) return
  91. return await Membership.query()
  92. .delete()
  93. .where('grouping_id', groupingId)
  94. }
  95. }