diff --git a/src/generated-types/graphql.ts b/src/generated-types/graphql.ts index 7a23318..dcbf668 100644 --- a/src/generated-types/graphql.ts +++ b/src/generated-types/graphql.ts @@ -37,8 +37,8 @@ export type Grade = { export type Query = { __typename?: 'Query'; Enrollment?: Maybe; + User?: Maybe; grades?: Maybe>>; - users?: Maybe>>; }; @@ -46,11 +46,29 @@ export type QueryEnrollmentArgs = { classId: Scalars['String']; }; + +export type QueryUserArgs = { + email: Scalars['String']; +}; + export type User = { __typename?: 'User'; - email?: Maybe; - google_id?: Maybe; + date_joined: Scalars['String']; + email: Scalars['String']; + email_berkeleytime_update?: Maybe; + email_class_update?: Maybe; + email_enrollment_opening?: Maybe; + email_grade_update?: Maybe; + first_name: Scalars['String']; id: Scalars['String']; + is_active: Scalars['Boolean']; + is_staff: Scalars['Boolean']; + is_superuser: Scalars['Boolean']; + last_login?: Maybe; + last_name: Scalars['String']; + major?: Maybe>>; + password: Scalars['String']; + username: Scalars['String']; }; @@ -166,14 +184,27 @@ export type GradeResolvers = { Enrollment?: Resolver, ParentType, ContextType, RequireFields>; + User?: Resolver, ParentType, ContextType, RequireFields>; grades?: Resolver>>, ParentType, ContextType>; - users?: Resolver>>, ParentType, ContextType>; }; export type UserResolvers = { - email?: Resolver, ParentType, ContextType>; - google_id?: Resolver, ParentType, ContextType>; + date_joined?: Resolver; + email?: Resolver; + email_berkeleytime_update?: Resolver, ParentType, ContextType>; + email_class_update?: Resolver, ParentType, ContextType>; + email_enrollment_opening?: Resolver, ParentType, ContextType>; + email_grade_update?: Resolver, ParentType, ContextType>; + first_name?: Resolver; id?: Resolver; + is_active?: Resolver; + is_staff?: Resolver; + is_superuser?: Resolver; + last_login?: Resolver, ParentType, ContextType>; + last_name?: Resolver; + major?: Resolver>>, ParentType, ContextType>; + password?: Resolver; + username?: Resolver; __isTypeOf?: IsTypeOfResolverFn; }; diff --git a/src/modules/user/controller.ts b/src/modules/user/controller.ts index 86dd63e..017733c 100644 --- a/src/modules/user/controller.ts +++ b/src/modules/user/controller.ts @@ -1,8 +1,13 @@ +import { User } from "../../generated-types/graphql"; import { formatUser } from "./formatter"; -import { UserModel } from "./model"; +import { UserModel, UserType } from "./model"; -export async function users() { - const users = await UserModel.find(); +export async function getByEmail(email: string): Promise { + const user = await UserModel.findOne({ email }); - return users.map(formatUser); + if (!user) { + throw new Error("User not found"); + } + + return formatUser(user as UserType); } diff --git a/src/modules/user/fixture.ts b/src/modules/user/fixture.ts deleted file mode 100644 index cd3b85c..0000000 --- a/src/modules/user/fixture.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Can be used for testing - -export const User = { - _id: "test-id", - _created: "test-created", - _updated: "test-updated", - _version: 1, - access_token: "test-access-token", - bio: "test-bio", - email: "test-email", - google_id: "test-google-id", - notify_update_classes: true, - notify_update_grades: true, - notify_update_berkeleytime: true, - classes_saved: ["test-classes-saved"], - classes_watching: ["test-classes-watching"], - friends: ["test-friends"], -}; - -export const UserGql = { - id: "test-id", - name: "test-name", - google_id: "test-google-id", - email: "test-email", -}; diff --git a/src/modules/user/formatter.ts b/src/modules/user/formatter.ts index 831d5bc..c1a833e 100644 --- a/src/modules/user/formatter.ts +++ b/src/modules/user/formatter.ts @@ -2,9 +2,23 @@ import { UserModule } from "./generated-types/module-types"; import { UserType } from "./model"; export function formatUser(user: UserType): UserModule.User { - return { - id: user._id.toString(), - google_id: user.google_id, - email: user.email, - }; + return Object.assign({ + id: user._id as string, + password: user.password as string, + is_superuser: user.is_superuser as boolean, + username: user.username as string, + first_name: user.first_name as string, + last_name: user.last_name as string, + email: user.email as string, + is_staff: user.is_staff as boolean, + is_active: user.is_active as boolean, + date_joined: user.date_joined as string, + }, + user.last_login === null ? null : { last_login: user.last_login }, + user.major === null ? [null] : { major: user.major }, + user.email_class_update === null ? null : { last_login: user.email_class_update }, + user.email_grade_update === null ? null : { last_login: user.email_grade_update }, + user.email_enrollment_opening === null ? null : { last_login: user.email_enrollment_opening }, + user.email_berkeleytime_update === null ? null : { last_login: user.email_berkeleytime_update } + ); } diff --git a/src/modules/user/generated-types/module-types.ts b/src/modules/user/generated-types/module-types.ts index d4d4083..df84c42 100644 --- a/src/modules/user/generated-types/module-types.ts +++ b/src/modules/user/generated-types/module-types.ts @@ -2,8 +2,8 @@ import * as Types from "../../../generated-types/graphql"; import * as gm from "graphql-modules"; export namespace UserModule { interface DefinedFields { - User: 'id' | 'google_id' | 'email'; - Query: 'users'; + User: 'id' | 'password' | 'last_login' | 'is_superuser' | 'username' | 'first_name' | 'last_name' | 'email' | 'is_staff' | 'is_active' | 'date_joined' | 'major' | 'email_class_update' | 'email_grade_update' | 'email_enrollment_opening' | 'email_berkeleytime_update'; + Query: 'User'; }; export type User = Pick; @@ -24,12 +24,25 @@ export namespace UserModule { User?: { '*'?: gm.Middleware[]; id?: gm.Middleware[]; - google_id?: gm.Middleware[]; + password?: gm.Middleware[]; + last_login?: gm.Middleware[]; + is_superuser?: gm.Middleware[]; + username?: gm.Middleware[]; + first_name?: gm.Middleware[]; + last_name?: gm.Middleware[]; email?: gm.Middleware[]; + is_staff?: gm.Middleware[]; + is_active?: gm.Middleware[]; + date_joined?: gm.Middleware[]; + major?: gm.Middleware[]; + email_class_update?: gm.Middleware[]; + email_grade_update?: gm.Middleware[]; + email_enrollment_opening?: gm.Middleware[]; + email_berkeleytime_update?: gm.Middleware[]; }; Query?: { '*'?: gm.Middleware[]; - users?: gm.Middleware[]; + User?: gm.Middleware[]; }; }; } \ No newline at end of file diff --git a/src/modules/user/model.ts b/src/modules/user/model.ts index 3c27be7..2f0f639 100644 --- a/src/modules/user/model.ts +++ b/src/modules/user/model.ts @@ -1,20 +1,22 @@ import mongoose, { Schema, InferSchemaType, Types } from "mongoose"; export const UserSchema = new Schema({ - _id: { type: Types.ObjectId, required: true }, - _created: String, - _updated: String, - _version: Number, - access_token: String, - bio: String, - email: String, - google_id: String, - notify_update_classes: Boolean, - notify_update_grades: Boolean, - notify_update_berkeleytime: Boolean, - classes_saved: [String], - classes_watching: [String], - friends: [String], + _id: { type: Types.ObjectId, required: true }, + password: { type: String, required: true }, + last_login: { type: String, required: true }, + is_superuser: { type: Boolean, required: false }, + username: { type: String, required: true }, + first_name: { type: String, required: true }, + last_name: { type: String, required: true }, + email: { type: String, required: true }, + is_staff: { type: Boolean, required: true }, + is_active: { type: Boolean, required: true }, + date_joined: { type: String, required: true }, + major: { type: [String], required: false }, + email_class_update: { type: Boolean, required: false }, + email_grade_update: { type: Boolean, required: false }, + email_enrollment_opening: { type: Boolean, required: false }, + email_berkeleytime_update: { type: Boolean, required: false } }); export const UserModel = mongoose.model("user", UserSchema, "user"); diff --git a/src/modules/user/resolver.ts b/src/modules/user/resolver.ts index 56a83c9..320aea2 100644 --- a/src/modules/user/resolver.ts +++ b/src/modules/user/resolver.ts @@ -1,10 +1,12 @@ -import { users } from "./controller"; +import { getByEmail } from "./controller"; import { UserModule } from "./generated-types/module-types"; const resolvers: UserModule.Resolvers = { - Query: { - users, - }, + Query: { + User(_parent, args: { email: string }) { + return getByEmail(args.email); + }, + }, }; export default resolvers; diff --git a/src/modules/user/typedefs/user.ts b/src/modules/user/typedefs/user.ts index 7ee4464..6466395 100644 --- a/src/modules/user/typedefs/user.ts +++ b/src/modules/user/typedefs/user.ts @@ -3,12 +3,25 @@ import { gql } from "graphql-tag"; const typedef = gql` type User { id: String! - google_id: String - email: String + password: String! + last_login: String + is_superuser: Boolean! + username: String! + first_name: String! + last_name: String! + email: String! + is_staff: Boolean! + is_active: Boolean! + date_joined: String! + major: [String], + email_class_update: Boolean, + email_grade_update: Boolean, + email_enrollment_opening: Boolean, + email_berkeleytime_update: Boolean } type Query { - users: [User] + User(email: String!): User } `;