import { db } from '../utils/db' import { Grouping } from '../entities' /** * Get Memberships associated with a single Profile from the database and * create a class from the data and * validate the incoming against the schema * @param {number} profileId * @returns {array} instantiated Profile objects (see: /entites/profile) */ const fetchMembershipsByProfileId = async profileId => { const membershipsForProfileId = await db.get(`/membership/${profileId}`) const validGroupingInstances = [] for (let membership of membershipsForProfileId) { const grouping = new Grouping(membership) if (grouping.isValid()) { validGroupingInstances.push(grouping) } } return validGroupingInstances } const postMembershipByProfileId = async ({ profileId, targetId, groupingType = 'match', }) => { const membership = { target_id: targetId, grouping_type: groupingType, grouping_name: `delete_${profileId}_${targetId}`, } const createdMembershipRecord = await db.post( `/membership/${profileId}/join`, membership, ) return createdMembershipRecord } export { fetchMembershipsByProfileId, postMembershipByProfileId }