wheelshare-old-backend/src/data.ts

100 lines
2.0 KiB
TypeScript

import { v4 } from 'uuid';
import { IonProfile } from './auth_ion';
import { UserModel } from './models';
/**
* Records users by id
*/
export const users: Record<string, Carpool.User> = {
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<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 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<Carpool.User | undefined> {
return ((
await UserModel.findOne({
email,
}).exec()
).toJSON() as unknown) as Carpool.User;
}
export async function registerUserFromIonProfile(
profile: IonProfile
): Promise<string> {
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;
}