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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  48. */
  49. async saveResponsesCreateProfileFor(userId, responses, txn) {
  50. const { Profile, Response } = this.server.models()
  51. const profile = await Profile.query(txn).insert({
  52. user_id: userId,
  53. })
  54. for (const responseToSave of responses) {
  55. const responseInfo = {
  56. profile_id: profile.id,
  57. response_key_id: responseToSave.response_key_id,
  58. val: responseToSave.val,
  59. }
  60. await Response.query(txn).insert(responseInfo)
  61. }
  62. //** Work around for HAPI returning profile_id as id */
  63. return { user_id: profile.user_id, profile_id: profile.id }
  64. }
  65. /**
  66. * Delete a profile
  67. * @param {number} userId
  68. * @param {number} profileId
  69. * @returns
  70. */
  71. async deleteProfile(userId, profileId) {
  72. const { Profile } = this.server.models()
  73. const dedupedGroupings = await this._getProfileIdsForUserId(userId)
  74. /** Do NOTHING if NOT in Grouping */
  75. if (!dedupedGroupings.includes(profileId)) return
  76. return await Profile.query().delete().where('profile_id', profileId)
  77. }
  78. }