You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const Schmervice = require('@hapipal/schmervice')
  2. module.exports = class ProfileService extends Schmervice.Service {
  3. constructor(...args) {
  4. super(...args)
  5. }
  6. /**
  7. * Internal method to get list of profile_ids for this user
  8. * @param {number} userId
  9. * @returns {Array} List of all profile_ids for user
  10. */
  11. async _getProfileIdsForUserId(userId) {
  12. const { Profile } = this.server.models()
  13. /** Grab every Profile associated with this id */
  14. const allProfiles = await Profile.query().where('user_id', userId)
  15. /** Copy a list of the just the Profiles */
  16. const profileIdsToGrab = allProfiles.map(profile => profile.profile_id)
  17. /** Uncomment to dedupe the list just in case */
  18. return [...new Set(profileIdsToGrab)]
  19. }
  20. async getCompleteProfilesFor(userId) {
  21. const { Profile, Response } = this.server.models()
  22. const dedupedProfiles = await this._getProfileIdsForUserId(userId)
  23. const responses = await Response.query().whereIn(
  24. 'profile_id',
  25. dedupedProfiles,
  26. )
  27. const profiles = await Profile.query().whereIn(
  28. 'profile_id',
  29. dedupedProfiles,
  30. )
  31. //** Get responses asociated with each profile_id */
  32. return profiles.map(profile => {
  33. if (!profile.responses) profile.responses = []
  34. profile.response_keys = []
  35. responses.forEach(response => {
  36. if (response.profile_id !== profile.profile_id) return
  37. profile.response_keys.push(response.response_key_id)
  38. profile.responses.push(response)
  39. })
  40. return profile
  41. })
  42. }
  43. /**
  44. * Save responses in a profile
  45. * @param {number} userId
  46. * @param {Array} responses
  47. * @returns {object}
  48. */
  49. async saveResponsesCreateProfileFor(userId, responses, txn) {
  50. const profile = await Profile.query(txn).insert({
  51. user_id: userId,
  52. })
  53. for (const responseToSave of responses) {
  54. const responseInfo = {
  55. profile_id: profile.id,
  56. response_key_id: responseToSave.response_key_id,
  57. val: responseToSave.val,
  58. }
  59. await Response.query(txn).insert(responseInfo)
  60. }
  61. //** Work around for HAPI returning profile_id as id */
  62. return { user_id: profile.user_id, profile_id: profile.id }
  63. }
  64. /** Update responses in place
  65. * @param {number} profileId
  66. * @param {Array} responses
  67. * @returns {Array} updated responses
  68. */
  69. async updateResponsesInProfile(profileId, responses, txn) {
  70. const { Profile, Response } = this.server.models()
  71. for (const responseToSave of responses) {
  72. await Response.query(txn)
  73. .update({
  74. response_id: responseToSave.response_id,
  75. profile_id: responseToSave.profile_id,
  76. response_key_id: responseToSave.response_key_id,
  77. val: responseToSave.val,
  78. })
  79. .where({
  80. profile_id: profileId,
  81. })
  82. .where({
  83. response_id: responseToSave.response_id,
  84. })
  85. }
  86. return await Response.query(txn).where({
  87. profile_id: profileId,
  88. })
  89. }
  90. /**
  91. * Delete a profile
  92. * @param {number} userId
  93. * @param {number} profileId
  94. * @returns
  95. */
  96. async deleteProfile(userId, profileId) {
  97. const { Profile } = this.server.models()
  98. const dedupedGroupings = await this._getProfileIdsForUserId(userId)
  99. /** Do NOTHING if NOT in Grouping */
  100. if (!dedupedGroupings.includes(profileId)) return
  101. return await Profile.query().delete().where('profile_id', profileId)
  102. }
  103. }