wheelshare-old-backend/src/data.ts
2021-04-10 15:09:53 -04:00

92 lines
1.9 KiB
TypeScript

import { v4 } from 'uuid';
import { IonProfile } from './auth_ion';
/**
* Records users by id
*/
export const users: Record<string, Carpool.User> = {
myfatemi04: {
id: '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<string, Carpool.Group> = {};
/**
* Records pools by id
*/
export const pools: Record<string, Carpool.Pool> = {};
/**
*
* @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 function getUserByID(userID: string): Carpool.User | undefined {
return users[userID];
}
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<Carpool.User | undefined> {
return undefined;
}
export async function registerUserFromIonProfile(
profile: IonProfile
): Promise<string> {
const id = v4();
const user: Carpool.User = {
id,
username: profile.ion_username,
email: profile.tj_email,
first_name: profile.first_name,
last_name: profile.last_name,
};
users[id] = user;
console.log('Registered user', user);
return id;
}