import { v4 } from 'uuid'; import { IonProfile } from './auth_ion'; import { UserModel } from './models'; /** * Records users by id */ export const users: Record = { myfatemi04: { uuid: '3baeaed6-05cb-4c03-9b43-1d74beafdbb7', email: '2022mfatemi@tjhsst.edu', username: 'myfatemi04', first_name: 'Michael', last_name: 'Fatemi', }, }; /** * Records groups by id */ export const groups: Record = {}; /** * Records pools by id */ export const pools: Record = {}; /** * * @param userID The userID to find * @returns The group IDs with that userID as a member */ export function getGroupsWithUser(userID: string) { let groupIDs = []; for (let group of Object.values(groups)) { if (group.member_ids.includes(userID)) { groupIDs.push(group.id); } } return groupIDs; } /** * * @param userID The userID to find * @returns The post IDs with that userID as the author */ export function getPoolsWithUser(userID: string) { let poolIDs: string[] = []; for (let post of Object.values(pools)) { if (post.author_id == userID) { poolIDs.push(post.id); } } return poolIDs; } export async function getUserByID(userID: string) { return await UserModel.findById(userID).exec(); } export function getPoolByID(poolID: string): Carpool.Pool | undefined { return pools[poolID]; } export function getGroupByID(groupID: string): Carpool.Group | undefined { return groups[groupID]; } export async function getUserByEmail( email: string ): Promise { return (( await UserModel.findOne({ email, }).exec() ).toJSON() as unknown) as Carpool.User; } export async function registerUserFromIonProfile( profile: IonProfile ): Promise { const user = new UserModel({ uuid: v4(), username: profile.ion_username, email: profile.tj_email, first_name: profile.first_name, last_name: profile.last_name, }); user.save(function (err) { if (err) return console.error(err); }); console.log('Registered user', user); return user.id; }