| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import { db } from '../utils/db.js'
- import { Profile } from '../entities/profile/profile.js'
-
- /**
- * Get Profiles associated with a single user from the database and
- * create a class from the data and
- * validate the incoming against the schema
- * @param {number} userId
- * @returns {array} instantiated Profile objects (see: /entites/profile)
- */
- const fetchProfilesByUserId = async (userId, sessionToken) => {
- const profilesForUserId = await db.get(
- `/user/${userId}/profiles`,
- sessionToken,
- )
- const validProfileInstances = []
- for (let profileData of profilesForUserId) {
- const profile = new Profile(profileData)
- if (profile.isValid()) {
- validProfileInstances.push(profile)
- }
- }
- return validProfileInstances
- }
-
- const createProfileForUserId = async (userId, responses) => {
- const profile = await db.post(`/user/${userId}/profile`, responses)
- return profile
- }
-
- const fetchProfileByProfileId = async (profileId, sessionToken) => {
- let profile
- try {
- const profileData = await db.get(`/profile/${profileId}`, sessionToken)
- profile = new Profile(profileData)
- if (!profile.isValid()) {
- throw '[Profile Service error]: Invalid or incomplete profile returned.'
- }
- } catch (err) {
- console.error(err)
- }
- return profile
- }
-
- const revealProfileInfo = async (groupingId, profileId, tagId) => {
- const revealed = await db.post(
- `/membership/${groupingId}/reveal?profile_id=${profileId}&tag_id=${tagId}`,
- )
- return revealed
- }
- export {
- fetchProfilesByUserId,
- fetchProfileByProfileId,
- createProfileForUserId,
- revealProfileInfo,
- }
|