feat: fixed typings and get db methods

This commit is contained in:
Michael Fatemi 2021-04-10 19:27:08 -04:00
parent db8da4a55e
commit 9622ede6e0
4 changed files with 69 additions and 147 deletions

View File

@ -5,9 +5,6 @@ import {
getPoolByID, getPoolByID,
getPoolsWithUser, getPoolsWithUser,
getUserByID, getUserByID,
groups,
pools,
users,
} from './data'; } from './data';
export const router = Router(); export const router = Router();
@ -33,21 +30,18 @@ router.get('/user', async (req, res) => {
}); });
router.patch('/user', (req, res) => { router.patch('/user', (req, res) => {
if (!(req.body.userID in users)) { // if (!(req.body.userID in users)) {
res.json({ status: 'error', error: 'user not found' }); // res.json({ status: 'error', error: 'user not found' });
} else { // } else {
let user = users[req.body.userID]; // let user = users[req.body.userID];
user.username = req.body.username; // user.username = req.body.username;
user.first_name = req.body.first_name; // user.first_name = req.body.first_name;
user.last_name = req.body.last_name; // user.last_name = req.body.last_name;
res.json({ status: 'success' }); // res.json({ status: 'success' });
} // }
}); });
router.delete('/user', (req, res) => { router.delete('/user', (req, res) => {});
delete users[req.body.userID];
res.json({ status: 'success' });
});
router.get('/pool', (req, res) => { router.get('/pool', (req, res) => {
if (typeof req.query.poolID != 'string') { if (typeof req.query.poolID != 'string') {
@ -65,49 +59,46 @@ router.get('/pool', (req, res) => {
}); });
router.post('/pool', (req, res) => { router.post('/pool', (req, res) => {
if (req.body.poolID in pools) { // if (req.body.poolID in pools) {
res.json({ status: 'error', error: 'already_exists' }); // res.json({ status: 'error', error: 'already_exists' });
} else { // } else {
pools[req.body.poolID] = { // pools[req.body.poolID] = {
id: req.body.poolID, // id: req.body.poolID,
title: req.body.title, // title: req.body.title,
description: req.body.description, // description: req.body.description,
participant_ids: [], // participant_ids: [],
driver_id: 'no driver', // driver_id: 'no driver',
create_time: 'create time', // create_time: 'create time',
update_time: 'update time', // update_time: 'update time',
comments: [], // comments: [],
group_id: 'no group', // group_id: 'no group',
status: 'pending', // status: 'pending',
capacity: 0, // capacity: 0,
direction: 'pickup', // direction: 'pickup',
author_id: 'no author', // author_id: 'no author',
type: 'request', // type: 'request',
}; // };
res.json({ status: 'success' }); // res.json({ status: 'success' });
} // }
}); });
router.patch('/pool', (req, res) => { router.patch('/pool', (req, res) => {
if (!(req.body.poolID in pools)) { // if (!(req.body.poolID in pools)) {
res.json({ status: 'error', error: 'not_found' }); // res.json({ status: 'error', error: 'not_found' });
} else { // } else {
let pool = pools[req.body.poolID]; // let pool = pools[req.body.poolID];
pool.title = req.body.title; // pool.title = req.body.title;
pool.description = req.body.description; // pool.description = req.body.description;
pool.driver_id = req.body.driver_id; // pool.driver_id = req.body.driver_id;
pool.update_time = req.body.update_time; // pool.update_time = req.body.update_time;
pool.status = req.body.status; // pool.status = req.body.status;
pool.capacity = req.body.capacity; // pool.capacity = req.body.capacity;
pool.direction = req.body.direction; // pool.direction = req.body.direction;
res.json({ status: 'success' }); // res.json({ status: 'success' });
} // }
}); });
router.delete('/pool', (req, res) => { router.delete('/pool', (req, res) => {});
delete pools[req.body.poolID];
res.json({ status: 'success' });
});
router.get('/group', (req, res) => { router.get('/group', (req, res) => {
if (typeof req.query.groupID != 'string') { if (typeof req.query.groupID != 'string') {
@ -125,39 +116,19 @@ router.get('/group', (req, res) => {
}); });
router.post('/group', (req, res) => { router.post('/group', (req, res) => {
if (req.body.groupID in groups) { // if (req.body.groupID in groups) {
res.json({ status: 'error', error: 'already_exists' }); // res.json({ status: 'error', error: 'already_exists' });
} else { // } else {
groups[req.body.groupID] = { // groups[req.body.groupID] = {
id: req.body.groupID, // id: req.body.groupID,
member_ids: [], // member_ids: [],
}; // };
res.json({ status: 'success' }); // res.json({ status: 'success' });
} // }
});
router.patch('/group', (req, res) => {
if (!(req.body.groupID in groups)) {
res.json({ status: 'error', error: 'group not found' });
} else {
let group = users[req.body.groupID];
res.json({ status: 'success' });
}
});
router.delete('/group', (req, res) => {
delete groups[req.body.groupID];
res.json({ status: 'success' });
}); });
router.get('/my_pools', (req, res) => { router.get('/my_pools', (req, res) => {
if (typeof req.query.userID != 'string') { let groupsWithUser = getPoolsWithUser(req.session.accountID);
return;
}
let userID = req.query.userID;
let groupsWithUser = getPoolsWithUser(userID);
if (groupsWithUser) { if (groupsWithUser) {
res.json({ status: 'success', data: groupsWithUser }); res.json({ status: 'success', data: groupsWithUser });
} else { } else {
@ -165,18 +136,6 @@ router.get('/my_pools', (req, res) => {
} }
}); });
router.post('/my_pools', (req, res) => {
if (req.body.groupID in groups) {
res.json({ status: 'error', error: 'already_exists' });
} else {
groups[req.body.groupID] = {
id: req.body.groupID,
member_ids: [],
};
res.json({ status: 'success' });
}
});
router.post('/create_session', (req, res) => { router.post('/create_session', (req, res) => {
const { code, provider } = req.body; const { code, provider } = req.body;
console.log('Creating session: code =', code, 'provider =', provider); console.log('Creating session: code =', code, 'provider =', provider);

View File

@ -1,28 +1,5 @@
import { IonProfile } from './auth_ion'; import { IonProfile } from './auth_ion';
import { PoolModel, UserModel } from './models'; import { GroupModel, PoolModel, UserModel } from './models';
/**
* 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> = {};
/** /**
* *
@ -30,14 +7,7 @@ export const pools: Record<string, Carpool.Pool> = {};
* @returns The group IDs with that userID as a member * @returns The group IDs with that userID as a member
*/ */
export function getGroupsWithUser(userID: string) { export function getGroupsWithUser(userID: string) {
let groupIDs = []; return undefined;
for (let group of Object.values(groups)) {
if (group.member_ids.includes(userID)) {
groupIDs.push(group.id);
}
}
return groupIDs;
} }
/** /**
@ -46,34 +16,29 @@ export function getGroupsWithUser(userID: string) {
* @returns The post IDs with that userID as the author * @returns The post IDs with that userID as the author
*/ */
export function getPoolsWithUser(userID: string) { export function getPoolsWithUser(userID: string) {
let poolIDs: string[] = []; return undefined;
for (let post of Object.values(pools)) {
if (post.author_id == userID) {
poolIDs.push(post.id);
}
}
return poolIDs;
} }
export async function getUserByID(userID: string) { export async function getUserByID(userID: string) {
return (await UserModel.findById(userID).exec()).toJSON() as Carpool.User; if (userID == null) {
return undefined;
}
return (await UserModel.findById(userID).exec()).toJSON();
} }
export async function getPoolByID(poolID: string) { export async function getPoolByID(poolID: string) {
return (await PoolModel.findById(poolID).exec()).toJSON() as Carpool.Pool; let doc = await PoolModel.findById(poolID).exec();
return doc?.toJSON();
} }
export function getGroupByID(groupID: string): Carpool.Group | undefined { export async function getGroupByID(groupID: string) {
return groups[groupID]; let doc = await GroupModel.findById(groupID).exec();
return doc?.toJSON();
} }
export async function getUserByEmail(email: string) { export async function getUserByEmail(email: string) {
return (( const user = await UserModel.findOne({ email }).exec();
await UserModel.findOne({ return user?.toJSON();
email,
}).exec()
).toJSON() as unknown) as Carpool.User;
} }
export async function registerUserFromIonProfile( export async function registerUserFromIonProfile(

View File

@ -59,7 +59,6 @@ export interface Pool extends Document {
} }
const PoolSchema: Schema = new Schema({ const PoolSchema: Schema = new Schema({
id: { type: String, required: true },
title: { type: String, required: true }, title: { type: String, required: true },
description: { type: String, required: true }, description: { type: String, required: true },
participant_ids: { type: [String], required: true }, participant_ids: { type: [String], required: true },

View File

@ -4,7 +4,6 @@
"esModuleInterop": true, "esModuleInterop": true,
"moduleResolution": "Node" "moduleResolution": "Node"
}, },
"files": ["src/types.ts", "src/typings/session.d.ts"],
"include": ["src/", "session.d.ts"], "include": ["src/", "session.d.ts"],
"exclude": ["node_modules/"] "exclude": ["node_modules/"]
} }