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 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 saveProfile(userId, responses, txn) {
  50. console.warn(userId, responses, txn)
  51. }
  52. /**
  53. * Delete a profile
  54. * @param {number} userId
  55. * @param {number} profileId
  56. * @returns
  57. */
  58. async deleteProfile(userId, profileId) {
  59. const { Profile } = this.server.models()
  60. const dedupedGroupings = await this._getProfileIdsForUserId(userId)
  61. /** Do NOTHING if NOT in Grouping */
  62. if (!dedupedGroupings.includes(profileId)) return
  63. return await Profile.query().delete().where('profile_id', profileId)
  64. }
  65. }