Skip to content

Commit

Permalink
Merge branch 'main' into chore/develop-setting
Browse files Browse the repository at this point in the history
  • Loading branch information
leGit-y committed Nov 14, 2023
2 parents 797b485 + 95b2895 commit b31f108
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 149 deletions.
9 changes: 6 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ model quote {

model review {
worry_id Int @id
content String @db.VarChar(50)
content String
created_at DateTime @db.Timestamptz(6)
updated_at DateTime @db.Timestamptz(6)
user_id Int?
user user? @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "review_user_id_fk")
worry worry @relation(fields: [worry_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "review_worry_id_fk")
}

Expand All @@ -40,9 +42,10 @@ model user {
created_at DateTime @db.Timestamptz(6)
updated_at DateTime @db.Timestamptz(6)
kakao_id BigInt? @unique
apple_id Int? @unique
apple_id String? @unique
age_range String? @db.VarChar(5)
gender String? @db.VarChar(6)
review review[]
token token?
worry worry[]
}
Expand All @@ -56,7 +59,7 @@ model worry {
created_at DateTime @db.Timestamptz(6)
updated_at DateTime @db.Timestamptz(6)
deadline DateTime? @db.Date
final_answer String? @db.VarChar(50)
final_answer String? @db.VarChar(100)
review review?
template template @relation(fields: [template_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "worry_template_id_fk")
user user @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "worry_user_id_fk")
Expand Down
14 changes: 9 additions & 5 deletions src/constants/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default {
// 회원가입 및 로그인
SIGNUP_SUCCESS: "회원 가입 성공",
SIGNUP_FAIL: "회원 가입 실패",
UNREGISTER_SUCCESS: "회원 탈퇴 성공",
UNREGISTER_FAIL: "회원 탈퇴 실패",
LOGIN_SUCCESS: "로그인 성공",
LOGIN_FAIL: "로그인 실패",
LOGOUT_SUCCESS: "로그아웃 성공",
Expand Down Expand Up @@ -67,11 +69,13 @@ export default {
GET_WORRY_LIST_BY_TEMPLATE_SUCCESS: "템플릿별 고민목록 조회 성공",
GET_WORRY_LIST_BY_TEMPLATE_FAIL: "템플릿별 고민목록 조회 실패",

//리뷰
CREATE_REVIEW_SUCCESS: "리뷰 생성 성공",
CREATE_REVIEW_FAIL: "리뷰 생성 실패",
UPDATE_REVIEW_SUCCESS: "리뷰 수정 성공",
UPDATE_REVIEW_FAIL: "리뷰 수정 실패",
//나의기록
CREATE_REVIEW_SUCCESS: "나의기록 등록 성공",
CREATE_REVIEW_FAIL: "나의기록 등록 실패",
UPDATE_REVIEW_SUCCESS: "나의기록 수정 성공",
UPDATE_REVIEW_FAIL: "나의기록 수정 실패",
CREATE_UPDATE_REVIEW_SUCCESS: "나의기록 등록/수정 성공",
CREATE_UPDATE_REVIEW_FAIL: "나의기록 등록/수정 실패",

//카카오로그인
KAKAO_LOGIN_SUCCESS: "카카오 로그인 성공",
Expand Down
102 changes: 65 additions & 37 deletions src/controller/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import qs from "qs";
import authService from "../service/authService";



// 서버에서 테스트용 카카오 토큰을 받기 위해 쓰일, 인가코드를 받기 위한 함수
const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: NextFunction) => {
try{
Expand Down Expand Up @@ -109,35 +110,6 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N
}

}

const kakaoLogout =async (req: Request, res:Response, next:NextFunction) => {
try{
const { accessToken } = req.body;

const response = await axios({
method: 'POST',
url: 'https://kapi.kakao.com/v1/user/logout',
headers:{
'Authorization': `Bearer ${accessToken}`,
}
})

return res.status(sc.OK).send(success(statusCode.OK, rm.KAKAO_LOGOUT_SUCCESS, response.data.msg));

}catch(error:any){

//토큰이 유효하지 않은 경우
if(error.response.data.msg == "this access token does not exist"){
return res.status(sc.UNAUTHORIZED).send(fail(sc.UNAUTHORIZED, rm.INVALID_TOKEN));
}
console.log(error)

return res.status(error.response.status).send(fail(error.response.status, error.response.data.msg));

}

}


const appleLogin =async (req: Request, res:Response, next:NextFunction) => {
try {
Expand All @@ -150,24 +122,80 @@ const kakaoLogin_getAuthorizedCode = async (req: Request, res: Response, next: N
const data = await authService.serviceLogin("apple", DTO);


// Respond with the user
// return res.status(sc.OK).send(success(statusCode.OK, rm.LOGIN_SUCCESS ,data))
// - 회원가입한 경우
if(data.isNew){
return res.status(sc.OK).send(success(sc.OK, rm.SIGNUP_SUCCESS, data.result));
}

// - 기존회원이 로그인한 경우
return res.status(sc.OK).send(success(sc.OK, rm.LOGIN_SUCCESS, data.result));

} catch (err) {
console.log("Err", err)
}
}

const serviceLogout =async (req: Request, res:Response, next:NextFunction) => {
try{
const { userId } = req.body;
await authService.serviceLogout(userId);


return res.status(sc.OK).send(success(sc.OK, rm.LOGOUT_SUCCESS));

}catch(error){
next(error);
}
}

const serviceUnregister =async (req: Request, res:Response, next:NextFunction) => {
try{
const { userId } = req.body;
await authService.serviceUnregister(userId);


return res.status(sc.OK).send(success(sc.OK, rm.UNREGISTER_SUCCESS));

}catch(error){
next(error);
}
}


// const kakaoLogout =async (req: Request, res:Response, next:NextFunction) => {
// try{
// const { accessToken } = req.body;

// const response = await axios({
// method: 'POST',
// url: 'https://kapi.kakao.com/v1/user/logout',
// headers:{
// 'Authorization': `Bearer ${accessToken}`,
// }
// })

// return res.status(sc.OK).send(success(statusCode.OK, rm.KAKAO_LOGOUT_SUCCESS, response.data.msg));

} catch (err) {
console.log("Err", err)
}
// }catch(error:any){

}
// //토큰이 유효하지 않은 경우
// if(error.response.data.msg == "this access token does not exist"){
// return res.status(sc.UNAUTHORIZED).send(fail(sc.UNAUTHORIZED, rm.INVALID_TOKEN));
// }
// console.log(error)


// return res.status(error.response.status).send(fail(error.response.status, error.response.data.msg));

// }

// }

export default{
kakaoLogin_getAuthorizedCode,
kakaoLogin_getToken,
kakaoLogin,
kakaoLogout,
appleLogin,
serviceLogout,
serviceUnregister

}
13 changes: 7 additions & 6 deletions src/controller/reviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import statusCode from "../constants/statusCode";
import { reviewDTO } from "../interfaces/DTO/reviewDTO"
import reviewService from "../service/reviewService";

const putReview = async (req: Request, res: Response, next: NextFunction) => {
const patchReview = async (req: Request, res: Response, next: NextFunction) => {
try {
const reviewDTO: reviewDTO = req.body;
const data = await reviewService.putReview(reviewDTO);
let message = null;
if(!data)
const data = await reviewService.patchReview(reviewDTO);

let message;
if(data.isNew)
message = rm.CREATE_REVIEW_SUCCESS
else
message = rm.UPDATE_REVIEW_SUCCESS


return res.status(sc.OK).send(success(statusCode.OK, message, data));
return res.status(sc.OK).send(success(statusCode.OK, message, data.result));

} catch (error) {
next(error);
}
};

export default{
putReview
patchReview
}
3 changes: 2 additions & 1 deletion src/interfaces/DTO/userDTO.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
interface userCreateDTO {
kakaoId: number | null;
appleId: number | null;
appleId: string | null;
name: string;
ageRange: string | null;
gender: string | null;
email: string | null;
refreshToken: string;
}


Expand Down
3 changes: 2 additions & 1 deletion src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// import templateRepository from './templateRepository';
import userRepository from './userRepository';
// import worryRepository from './worryRepository';
import tokenRepository from './tokenRepository';

// import {userRepository} from './userRepository';


export {
// quoteRepository,
// reviewRepository,
// templateRepository,
userRepository,
tokenRepository
// worryRepository,
}
1 change: 1 addition & 0 deletions src/repository/reviewRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const createReview = async (reviewDTO: reviewDTO) => {
return await prisma.review.create({
data: {
worry_id: reviewDTO.worryId,
user_id: reviewDTO.userId,
content: reviewDTO.review,
created_at: new Date(),
updated_at: new Date(),
Expand Down
13 changes: 12 additions & 1 deletion src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ const updateRefreshTokenById = async (userId: number, token:string) => {
});
};

const disableRefreshTokenById = async (userId: number) => {
return await prisma.token.update({
where: {
user_id: userId
},
data:{
refresh_token: ""
}
});
};


export default {
createRefreshToken,
findRefreshTokenById,
findIdByRefreshToken,
updateRefreshTokenById,

disableRefreshTokenById

}
33 changes: 30 additions & 3 deletions src/repository/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const findUserByKakaoId = async (KakaoId: number) => {
});
};

const findUserByAppleId = async (AppleId: number) => {
const findUserByAppleId = async (AppleId: string) => {
return await prisma.user.findUnique({
where: {
apple_id: AppleId
Expand All @@ -35,20 +35,47 @@ const createUser = async(userCreateDTO:userCreateDTO) => {
email: userCreateDTO.email,
age_range: userCreateDTO.ageRange,
gender: userCreateDTO.gender,
// used_template: 0,
created_at: new Date(),
updated_at: new Date(),

token:{
create:{
refresh_token: userCreateDTO.refreshToken
}
}
}
})
}


const deleteUser = async(userId: number) => {
return await prisma.user.delete({

const deletedReview = prisma.review.deleteMany({
where: {
user_id: userId
}
})

const deletedWorry = prisma.worry.deleteMany({
where: {
user_id: userId
}
})

const deletedToken = prisma.token.delete({
where: {
user_id: userId
}
})

const deletedUser = prisma.user.delete({
where:{
id: userId
}
})

return await prisma.$transaction([deletedReview,deletedWorry,deletedToken,deletedUser])

}

// ? class로 사용하는 경우는 언제 ?
Expand Down
Loading

0 comments on commit b31f108

Please sign in to comment.