user.db.ts 2.13 KB
Newer Older
1
import bcrypt from "bcryptjs";
2
import { ObjectId } from "mongoose";
Kim, MinGyu's avatar
Kim, MinGyu committed
3
import { IUser, Role, Post, User, Avatar } from "../models";
Yoon, Daeki's avatar
Yoon, Daeki committed
4
5

export const createUser = async (user: IUser) => {
6
7
  // 비밀번호 암호화
  const hash = await bcrypt.hash(user.password, 10);
Kim, MinGyu's avatar
Kim, MinGyu committed
8
  const newAvatar = await Avatar.create({});
Yoon, Daeki's avatar
Yoon, Daeki committed
9
10
11
12
13
14
15
16
  // 사용자 역할 추가: 기본값은 "user"
  let userRole = null;
  if (user.role) {
    userRole = await Role.findById(user.role);
  } else {
    userRole = await Role.findOne({ name: "user" });
  }
  const newUser = new User({
Lee Soobeom's avatar
Lee Soobeom committed
17
18
    email: user.email,
    password: hash,
Yoon, Daeki's avatar
Yoon, Daeki committed
19
20
    role: userRole,
    isNew: true,
Kim, MinGyu's avatar
Kim, MinGyu committed
21
    avatar: newAvatar,
Lee Soobeom's avatar
Lee Soobeom committed
22
  });
Yoon, Daeki's avatar
Yoon, Daeki committed
23
24
  const retUser = await newUser.save();
  return retUser;
Yoon, Daeki's avatar
Yoon, Daeki committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
};

export const findUserByEmail = async (
  email: string,
  includePassword: boolean = false
) => {
  let user;
  if (includePassword) {
    user = await User.findOne({ email }).select("+password");
  } else {
    user = await User.findOne({ email });
  }
  return user;
};

Lee Soobeom's avatar
Lee Soobeom committed
40
41
42
43
export const findUserByPostId = async (postId: string) => {
  const post = await Post.findOne({ _id: postId }).populate("user");
  return post?.user;
};
Kim, MinGyu's avatar
Kim, MinGyu committed
44

Yoon, Daeki's avatar
Yoon, Daeki committed
45
export const getProfile = async (userId: string) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
46
  const profile = await User.findById(userId).populate("avatar");
Yoon, Daeki's avatar
Yoon, Daeki committed
47
48
  return profile; //이름 수정
};
Lee Soobeom's avatar
Lee Soobeom committed
49

Yoon, Daeki's avatar
Yoon, Daeki committed
50
51
52
53
54
55
56
57
58
59
60
61
62
export const getUsers = async () => {
  const users = await User.find({});
  return users;
};

export const isUser = async (email: string) => {
  const user = await User.findOne({ email });
  if (user) {
    return true;
  } else {
    return false;
  }
};
Kim, MinGyu's avatar
Kim, MinGyu committed
63

Yoon, Daeki's avatar
Yoon, Daeki committed
64
65
66
67
68
69
70
export const isValidUserId = async (userId: string) => {
  const user = await User.findById(userId);
  if (user) {
    return true;
  } else {
    return false;
  }
71
72
};

Kim, MinGyu's avatar
Kim, MinGyu committed
73
export const postPicture = async (
74
75
76
  userId: ObjectId,
  originalfilename: string | null,
  newfilename: string,
Kim, MinGyu's avatar
Kim, MinGyu committed
77
78
  picturepath: string,
  nickname: string
79
) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
80
81
82
  const profile = await User.findById(userId);
  if (!(profile?.avatar === undefined)) {
    await Avatar.findByIdAndUpdate(profile.avatar._id, {
83
84
85
      originalfilename: originalfilename,
      newfilename: newfilename,
      picturepath: picturepath,
Kim, MinGyu's avatar
Kim, MinGyu committed
86
87
88
      nickname: nickname,
    });
  }
89
};