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.service.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { db } from '../utils/db.js'
  2. import { Profile } from '../entities/profile/profile.js'
  3. /**
  4. * Get Profiles associated with a single user from the database and
  5. * create a class from the data and
  6. * validate the incoming against the schema
  7. * @param {number} userId
  8. * @returns {array} instantiated Profile objects (see: /entites/profile)
  9. */
  10. const fetchProfilesByUserId = async (userId, sessionToken) => {
  11. const profilesForUserId = await db.get(
  12. `/user/${userId}/profiles`,
  13. sessionToken,
  14. )
  15. const validProfileInstances = []
  16. for (let profileData of profilesForUserId) {
  17. const profile = new Profile(profileData)
  18. if (profile.isValid()) {
  19. validProfileInstances.push(profile)
  20. }
  21. }
  22. return validProfileInstances
  23. }
  24. const createProfileForUserId = async (userId, responses) => {
  25. const profile = await db.post(`/user/${userId}/profile`, responses)
  26. return profile
  27. }
  28. const fetchProfileByProfileId = async (profileId, sessionToken) => {
  29. let profile
  30. try {
  31. const profileData = await db.get(`/profile/${profileId}`, sessionToken)
  32. profile = new Profile(profileData)
  33. if (!profile.isValid()) {
  34. throw '[Profile Service error]: Invalid or incomplete profile returned.'
  35. }
  36. } catch (err) {
  37. console.error(err)
  38. }
  39. return profile
  40. }
  41. const revealProfileInfo = async (groupingId, profileId, tagId) => {
  42. const revealed = await db.post(
  43. `/membership/${groupingId}/reveal?profile_id=${profileId}&tag_id=${tagId}`,
  44. )
  45. return revealed
  46. }
  47. export {
  48. fetchProfilesByUserId,
  49. fetchProfileByProfileId,
  50. createProfileForUserId,
  51. revealProfileInfo,
  52. }