Skip to content

Commit

Permalink
[Refactor]: unify all notation to camelCase (#114)
Browse files Browse the repository at this point in the history
* [refactor]: fix snake_case to camelCase

* [refactor]: change test directory structure
  • Loading branch information
Istiopaxx authored Jan 6, 2024
1 parent 65da2dd commit 8345897
Show file tree
Hide file tree
Showing 118 changed files with 897 additions and 803 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import {
Req,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthService } from '../services/auth.service';
import { GoogleLoginDto, KakaoLoginDto } from '../dto/oauth.dto';
import { AuthService } from './auth.service';
import { GoogleLoginDto, KakaoLoginDto } from './dto/oauth.dto';
import {
ApiGet,
ApiPostOk,
} from '@app/common/decorators/http-method.decorator';
import { User } from '@app/user/entities/mongo.user.entity';
import { User } from '@app/user/domain/mongo.user.entity';
import { ReqUser } from '@app/common/decorators/req-user.decorator';
import { CreateUserApiDto, CreateUserDto } from '@app/user/dto/modify-user.dto';
import { Auth, RegisterAuth } from '@app/common/decorators/auth.decorator';
import {
LogoutResponseDto,
OAuthLoginResponseDto,
RegisterResponseDto,
} from '../dto/auth-response.dto';
} from './dto/auth-response.dto';
import { FindOneUserResponseDto } from '@app/user/dto/user-response.dto';
import { UserInfo } from '../types/user-info.type';
import { UserInfo } from './types/user-info.type';

@ApiTags('Auth')
@Controller('auth')
Expand Down
6 changes: 3 additions & 3 deletions api/libs/auth/src/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Global, Module } from '@nestjs/common';
import { AuthController } from './controllers/auth.controller';
import { AuthService } from './services/auth.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { MongooseModule } from '@nestjs/mongoose';
import { AuthRepository } from './repositories/auth.repository';
import { JwtModule } from '@nestjs/jwt';
Expand All @@ -11,7 +11,7 @@ import {
Session,
SessionSchema,
SessionSchemaFactory,
} from './entities/mongo.session.entity';
} from './domain/mongo.session.entity';

@Global()
@Module({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { User } from '@app/user/entities/user.entity';
import { UserService } from '@app/user/services/user.service';
import { User } from '@app/user/domain/user.entity';
import { UserService } from '@app/user/user.service';
import { Injectable, NotFoundException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthRepository } from '../repositories/auth.repository';
import { AuthRepository } from './repositories/auth.repository';
import { CreateUserDto } from '@app/user/dto/modify-user.dto';
import { HttpService } from '@nestjs/axios';
import { GoogleLoginDto, OAuthLoginSessionDto } from '../dto/oauth.dto';
import {
GoogleLoginDto,
KakaoLoginDto,
OAuthLoginSessionDto,
} from './dto/oauth.dto';
import {
GoogleApiResponseType,
KakaoApiResponseType,
} from '../types/oauth.type';
import { UserInfo } from '../types/user-info.type';
import { LoginSessionDto } from '../dto/token.dto';
} from './types/oauth.type';
import { UserInfo } from './types/user-info.type';
import { LoginSessionDto } from './dto/token.dto';
import { v4 as uuidv4 } from 'uuid';
import { Logable } from '@app/common/log/log.decorator';

Expand Down Expand Up @@ -47,14 +51,14 @@ export class AuthService {

@Logable()
async login(user: User): Promise<LoginSessionDto> {
const session_token = uuidv4();
const sessionToken = uuidv4();
await this.authRepository.create({
user_id: user.id,
session_token,
userId: user.id,
sessionToken,
});
return {
user,
session_token,
sessionToken,
};
}

Expand All @@ -63,9 +67,9 @@ export class AuthService {
const { email, username } = userInfo;
const user = await this.userService.findByEmail(email);
return {
is_exist: !!user,
isExist: !!user,
...(!!user ? await this.login(user) : {}),
register_token: !!user
registerToken: !!user
? undefined
: this.jwtService.sign({ sub: email, username }),
};
Expand All @@ -75,13 +79,13 @@ export class AuthService {
async googleLogin(
googleLoginDto: GoogleLoginDto,
): Promise<OAuthLoginSessionDto> {
const { access_token } = googleLoginDto;
const { accessToken } = googleLoginDto;
const { data }: { data: GoogleApiResponseType } =
await this.httpService.axiosRef.get(
'https://www.googleapis.com/oauth2/v2/userinfo',
{
headers: {
Authorization: `Bearer ${access_token}`,
Authorization: `Bearer ${accessToken}`,
},
},
);
Expand All @@ -91,13 +95,13 @@ export class AuthService {

@Logable()
async kakaoLogin(
kakaoLoginDto: GoogleLoginDto,
kakaoLoginDto: KakaoLoginDto,
): Promise<OAuthLoginSessionDto> {
const { access_token } = kakaoLoginDto;
const { accessToken } = kakaoLoginDto;
const { data }: { data: KakaoApiResponseType } =
await this.httpService.axiosRef.get('https://kapi.kakao.com/v2/user/me', {
headers: {
Authorization: `Bearer ${access_token}`,
Authorization: `Bearer ${accessToken}`,
},
});
const { email } = data.kakao_account;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { schemaOptions } from '@app/common/utils/schema-option';
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose';
import { ConfigService } from '@nestjs/config';
import { User } from '@app/user/entities/mongo.user.entity';
import { User } from '@app/user/domain/mongo.user.entity';

export type SessionDocument = HydratedDocument<Session>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Session as SessionType } from '@prisma/client';

export class Session implements SessionType {
id: number;
session_token: string;
user_id: number;
created_at: Date;
updated_at: Date;
sessionToken: string;
userId: number;
createdAt: Date;
updatedAt: Date;
}
2 changes: 1 addition & 1 deletion api/libs/auth/src/dto/auth-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OkResponse } from '@app/common/dto/success-response.dto';
import { User } from '@app/user/entities/user.entity';
import { User } from '@app/user/domain/user.entity';
import { LoginSessionDto } from './token.dto';
import { OAuthLoginSessionDto } from './oauth.dto';

Expand Down
Empty file.
12 changes: 6 additions & 6 deletions api/libs/auth/src/dto/oauth.dto.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { User } from '@app/user/entities/user.entity';
import { User } from '@app/user/domain/user.entity';
import { IsNotEmpty, IsString } from 'class-validator';

export class GoogleLoginDto {
@IsString()
@IsNotEmpty()
access_token: string;
accessToken: string;
}

export class KakaoLoginDto {
@IsString()
@IsNotEmpty()
access_token: string;
accessToken: string;
}

export class OAuthLoginSessionDto {
is_exist: boolean;
session_token?: string;
isExist: boolean;
sessionToken?: string;
user?: User;
register_token?: string;
registerToken?: string;
}
8 changes: 4 additions & 4 deletions api/libs/auth/src/dto/token.dto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { User } from '@app/user/entities/user.entity';
import { User } from '@app/user/domain/user.entity';
import { IsInt, IsUUID } from 'class-validator';

export class LoginSessionDto {
session_token: string;
sessionToken: string;

user: User;
}

export class CreateSessionDto {
@IsUUID()
session_token: string;
sessionToken: string;

@IsInt()
user_id: number;
userId: number;
}
4 changes: 2 additions & 2 deletions api/libs/auth/src/guards/register-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { Request } from 'express';
import { AuthService } from '../services/auth.service';
import { UserService } from '@app/user/services/user.service';
import { AuthService } from '../auth.service';
import { UserService } from '@app/user/user.service';

@Injectable()
export class RegisterAuthGuard implements CanActivate {
Expand Down
4 changes: 2 additions & 2 deletions api/libs/auth/src/guards/session-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Request } from 'express';
import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_KEY } from '@app/common/decorators/public.decorator';
import { AuthService } from '../services/auth.service';
import { AuthService } from '../auth.service';

@Injectable()
export class SessionAuthGuard implements CanActivate {
Expand All @@ -32,7 +32,7 @@ export class SessionAuthGuard implements CanActivate {
try {
const session = await this.authService.findBySessionToken(sessionToken);
request['user'] = session.user;
request['sessionToken'] = session.session_token;
request['sessionToken'] = session.sessionToken;
} catch {
throw new UnauthorizedException();
}
Expand Down
3 changes: 2 additions & 1 deletion api/libs/auth/src/repositories/auth.repository.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Session as PrismaSession } from '@prisma/client';
import { CreateSessionDto } from '../dto/token.dto';
import { Session as MongoSession } from '../entities/mongo.session.entity';
import { Session as MongoSession } from '../domain/mongo.session.entity';
import { ICrudRepository } from '@app/common/repository/crud.repository';

type Session = PrismaSession | MongoSession;

export interface IAuthRepository
extends ICrudRepository<Session, CreateSessionDto, any, any> {
findBySessionToken(session: string): Promise<Session>;

deleteBySessionToken(session: string): Promise<Session>;
}
8 changes: 4 additions & 4 deletions api/libs/auth/src/repositories/auth.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export class AuthRepository
ttl: 24 * 60 * 60 * 1000,
keyGenerator: (session: string) => `session:${session}`,
})
async findBySessionToken(session: string): Promise<any> {
async findBySessionToken(session: string) {
return await this.prisma.session.findUnique({
where: { session_token: session },
where: { sessionToken: session },
include: {
user: {
include: {
device_tokens: true,
deviceTokens: true,
},
},
},
Expand All @@ -50,7 +50,7 @@ export class AuthRepository
})
async deleteBySessionToken(session: string): Promise<any> {
return await this.prisma.session.delete({
where: { session_token: session },
where: { sessionToken: session },
});
}
}
2 changes: 1 addition & 1 deletion api/libs/auth/src/repositories/mongo.auth.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Session, SessionDocument } from '../entities/mongo.session.entity';
import { Session, SessionDocument } from '../domain/mongo.session.entity';
import { CreateSessionDto } from '../dto/token.dto';
import { Logable } from '@app/common/log/log.decorator';
import { Cacheable } from '@app/common/cache/cache.service';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '../services/auth.service';
import { AuthController } from './auth.controller';
import { AuthRepository } from '../repositories/mongo.auth.repository';
import { UserService } from '@app/user/services/user.service';
import { AuthService } from '@app/auth/auth.service';
import { AuthController } from '@app/auth/auth.controller';
import { AuthRepository } from '@app/auth/repositories/mongo.auth.repository';
import { UserService } from '@app/user/user.service';

describe('AuthController', () => {
let controller: AuthController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthRepository } from './mongo.auth.repository';
import { AuthRepository } from '@app/auth/repositories/mongo.auth.repository';

describe('AuthRepository', () => {
let authRepository: AuthRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TestBed } from '@automock/jest';
import { AuthService } from './auth.service';
import { AuthRepository } from '../repositories/auth.repository';
import { UserService } from '@app/user/services/user.service';
import { AuthService } from '@app/auth/auth.service';
import { AuthRepository } from '@app/auth/repositories/auth.repository';
import { UserService } from '@app/user/user.service';
import { JwtService } from '@nestjs/jwt';
import { User } from '@app/user/entities/user.entity';
import { User } from '@app/user/domain/user.entity';
import { CreateUserDto } from '@app/user/dto/modify-user.dto';
import { LoginSessionDto } from '../dto/token.dto';
import { Session } from '../entities/session.entity';
import { OAuthLoginSessionDto } from '../dto/oauth.dto';
import { LoginSessionDto } from '@app/auth/dto/token.dto';
import { Session } from '@app/auth/domain/session.entity';
import { OAuthLoginSessionDto } from '@app/auth/dto/oauth.dto';

jest.mock('uuid', () => ({
v4: () => 'mock-uuid',
Expand Down Expand Up @@ -52,16 +52,16 @@ describe('AuthService', () => {
};
const sessToken = 'mock-uuid';
const loginSessionDto: LoginSessionDto = {
session_token: sessToken,
sessionToken: sessToken,
user,
};
authRepository.create.mockResolvedValueOnce(new Session());

const result = await service.login(user);

expect(authRepository.create).toHaveBeenCalledWith({
user_id: user.id,
session_token: sessToken,
userId: user.id,
sessionToken: sessToken,
});
expect(result).toEqual(loginSessionDto);
});
Expand All @@ -76,11 +76,11 @@ describe('AuthService', () => {
user.username = username;

const loginSessionDto: LoginSessionDto = {
session_token: 'sessToken',
sessionToken: 'sessToken',
user,
};
const oAuthLoginSessionDto: OAuthLoginSessionDto = {
is_exist: true,
isExist: true,
...loginSessionDto,
};
userService.findByEmail.mockResolvedValue(user);
Expand All @@ -97,8 +97,8 @@ describe('AuthService', () => {
const email = '[email protected]';
const username = 'test';
const expectedResponse = {
is_exist: false,
register_token: 'register_token',
isExist: false,
registerToken: 'register_token',
};
jwtService.sign.mockReturnValue('register_token');
userService.findByEmail.mockResolvedValue(undefined);
Expand Down
6 changes: 1 addition & 5 deletions api/libs/common/src/dto/pagenation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,5 @@ export class PagenationResponseDto {

count: number;

has_next: boolean;

// has_prev: boolean;

// total_page?: number;
hasNext: boolean;
}
Loading

0 comments on commit 8345897

Please sign in to comment.