Skip to content

Commit f05e61d

Browse files
🥳 inital gql api's
1 parent adce501 commit f05e61d

File tree

7 files changed

+75
-41
lines changed

7 files changed

+75
-41
lines changed

src/app.module.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { DBModule } from '@libs/db/db.module';
2+
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
23
import { Module } from '@nestjs/common';
34
import { ConfigModule } from '@nestjs/config';
5+
import { GraphQLModule } from '@nestjs/graphql';
46
import { validateEnv } from '@src/libs/core/utils/config';
57
import { LoggerModule } from 'nestjs-pino';
68

@@ -18,6 +20,22 @@ import { UserModule } from './modules';
1820
}),
1921
DBModule,
2022
UserModule,
23+
GraphQLModule.forRootAsync<ApolloDriverConfig>({
24+
driver: ApolloDriver,
25+
useFactory: (): ApolloDriverConfig => ({
26+
autoSchemaFile: true,
27+
playground: process.env.NODE_ENV !== 'production',
28+
formatError: err => ({
29+
message: err.message,
30+
status: err.extensions?.code,
31+
statusCode: (err.extensions?.originalError as any)?.statusCode,
32+
}),
33+
context: ({ req, res }: { req: Request; res: Response }) => ({
34+
req,
35+
res,
36+
}),
37+
}),
38+
}),
2139
LoggerModule.forRoot({
2240
pinoHttp: {
2341
autoLogging: false,

src/libs/converters/user.converter.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { GqlUser } from '@src/modules/admin/user/dto/user.output';
1+
import { GqlUser, GqlUserWithToken } from '@src/modules/user/dto/user.outputs';
22

33
import { UserEntity } from '../db/entities';
44

55
export const transformToUserGQLType = (user: UserEntity): GqlUser => ({
66
id: user.id,
77
fullName: user.fullName,
8+
email: user.email,
9+
gender: user.gender,
810
});
911

10-
export const transformToUserWithGQLType = (user: UserEntity): GqlUser => ({
12+
export const transformToUserWithTokenGQLType = (
13+
user: UserEntity,
14+
token: string,
15+
): GqlUserWithToken => ({
1116
id: user.id,
1217
fullName: user.fullName,
18+
email: user.email,
19+
gender: user.gender,
20+
token,
1321
});

src/libs/db/db.module.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
1515
database: configService.get('DATABASE_NAME'),
1616
autoLoadEntities: true,
1717
synchronize: true,
18-
ssl: true,
19-
extra: {
20-
ssl: {
21-
rejectUnauthorized: false,
22-
},
23-
},
18+
ssl: false,
2419
}),
2520

2621
inject: [ConfigService],

src/libs/db/entities/user.entity.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,16 @@ export class UserEntity {
1212
@PrimaryGeneratedColumn()
1313
id: number;
1414

15-
@Column({ unique: true, nullable: true })
15+
@Column({ unique: true, nullable: false })
1616
email: string;
1717

18-
@Column({ nullable: true })
19-
countryCode: string;
20-
21-
@Column({ nullable: true })
22-
mobileNumber: string;
23-
2418
@Column()
2519
fullName: string;
2620

27-
@Column({ nullable: true })
28-
profilePicture: string;
21+
@Column()
22+
gender: string;
2923

30-
@Column({ type: 'enum', enum: UserRoles })
24+
@Column({ type: 'enum', enum: UserRoles, default: UserRoles.USER })
3125
role: string;
3226

3327
@Column({

src/modules/user/dto/user.inputs.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Field, ObjectType } from '@nestjs/graphql';
1+
import { Field, InputType } from '@nestjs/graphql';
22

3-
@ObjectType('User')
3+
@InputType()
44
export class CreateUserInput {
55
@Field(() => String)
66
fullName: string;
@@ -14,3 +14,12 @@ export class CreateUserInput {
1414
@Field(() => String)
1515
gender: string;
1616
}
17+
18+
@InputType()
19+
export class LoginUserInput {
20+
@Field(() => String)
21+
email: string;
22+
23+
@Field(() => String)
24+
password: string;
25+
}

src/modules/user/user.resolver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@src/libs/core/decorators';
66
import { UserEntity } from '@src/libs/db/entities';
77

8-
import { CreateUserInput } from './dto/user.inputs';
8+
import { CreateUserInput, LoginUserInput } from './dto/user.inputs';
99
import { GqlUser, GqlUserWithToken } from './dto/user.outputs';
1010
import { UserService } from './user.service';
1111

@@ -15,14 +15,14 @@ export class UserResolver {
1515

1616
@AllowUnauthorizedRequest()
1717
@Mutation(() => GqlUserWithToken, { name: 'login' })
18-
login(@Args('adminLogInput') adminLogInput: CreateUserInput) {
19-
return this.userService.login(adminLogInput);
18+
login(@Args('loginUserInput') loginUserInput: LoginUserInput) {
19+
return this.userService.login(loginUserInput);
2020
}
2121

2222
@AllowUnauthorizedRequest()
23-
@Mutation(() => GqlUserWithToken, { name: 'login' })
24-
createUser(@Args('adminLogInput') adminLogInput: CreateUserInput) {
25-
return this.userService.login(adminLogInput);
23+
@Mutation(() => GqlUserWithToken)
24+
createUser(@Args('createUserInput') createUserInput: CreateUserInput) {
25+
return this.userService.createUser(createUserInput);
2626
}
2727
@Query(() => GqlUserWithToken, { name: 'profile' })
2828
async profile(@CurrentUser() user: UserEntity): Promise<GqlUserWithToken> {

src/modules/user/user.service.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { BadRequestException, Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3+
import { transformToUserWithTokenGQLType } from '@src/libs/converters';
34
import { AuthService } from '@src/libs/core/auth/auth.service';
4-
import { DefaultStatus } from '@src/libs/core/utils';
5+
import { UserStatus } from '@src/libs/core/utils';
56
import { UserEntity } from '@src/libs/db/entities';
67
import * as bcrypt from 'bcrypt';
78
import { PinoLogger } from 'nestjs-pino';
89
import { Repository } from 'typeorm';
910

10-
import { AdminLoginInput } from './dto/admin-login.input';
11+
import { CreateUserInput, LoginUserInput } from './dto/user.inputs';
1112
import { GqlUserWithToken } from './dto/user.outputs';
1213

1314
@Injectable()
@@ -20,11 +21,11 @@ export class UserService {
2021
private authService: AuthService,
2122
) {}
2223

23-
async login(inputs: AdminLoginInput): Promise<GqlUserWithToken> {
24+
async login(inputs: LoginUserInput): Promise<GqlUserWithToken> {
2425
const user = await this.usersRepository.findOne({
2526
where: {
2627
email: inputs.email,
27-
status: DefaultStatus.ACTIVE,
28+
status: UserStatus.ACTIVE,
2829
},
2930
});
3031

@@ -37,27 +38,36 @@ export class UserService {
3738
throw new BadRequestException('email or password is incorrect');
3839
}
3940

40-
const userWithToken = new GqlUserWithToken();
41-
userWithToken.id = user.id;
42-
userWithToken.token = this.authService.createJwt(user).token;
43-
userWithToken.fullName = user.fullName;
44-
return userWithToken;
41+
const token = this.authService.createJwt(user).token;
42+
return transformToUserWithTokenGQLType(user, token);
4543
}
4644

4745
async profile(user: UserEntity): Promise<GqlUserWithToken> {
4846
const foundUser = await this.usersRepository.findOne({
4947
where: {
5048
id: user.id,
51-
status: DefaultStatus.ACTIVE,
49+
status: UserStatus.ACTIVE,
5250
},
5351
});
5452
if (!foundUser) {
5553
throw new BadRequestException('email or password is incorrect');
5654
}
57-
const userWithToken = new GqlUserWithToken();
58-
userWithToken.id = foundUser.id;
59-
userWithToken.token = this.authService.createJwt(foundUser).token;
60-
userWithToken.fullName = foundUser.fullName;
61-
return userWithToken;
55+
const token = this.authService.createJwt(foundUser).token;
56+
return transformToUserWithTokenGQLType(foundUser, token);
57+
}
58+
59+
async createUser(user: CreateUserInput): Promise<GqlUserWithToken> {
60+
const foundUser = await this.usersRepository.findOne({
61+
where: {
62+
email: user.email,
63+
},
64+
});
65+
if (foundUser) {
66+
throw new BadRequestException('email is already taken');
67+
}
68+
user.password = bcrypt.hashSync(user.password, 10);
69+
const createdUser = await this.usersRepository.save(user);
70+
const token = this.authService.createJwt(createdUser).token;
71+
return transformToUserWithTokenGQLType(createdUser, token);
6272
}
6373
}

0 commit comments

Comments
 (0)