user.db.ts 1.4 KB
Newer Older
1
import bcrypt from "bcryptjs";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
import { IUser, Role, User } from "../models";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
4

export const createUser = async (user: IUser) => {
5
6
  // 비밀번호 암호화
  const hash = await bcrypt.hash(user.password, 10);
Yoon, Daeki's avatar
Yoon, Daeki committed
7
8
9
10
11
12
13
14
  // 사용자 역할 추가: 기본값은 "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
15
16
    email: user.email,
    password: hash,
Yoon, Daeki's avatar
Yoon, Daeki committed
17
18
    role: userRole,
    isNew: true,
Lee Soobeom's avatar
Lee Soobeom committed
19
  });
Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
  const retUser = await newUser.save();
  return retUser;
Yoon, Daeki's avatar
Yoon, Daeki committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
};

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;
};

Yoon, Daeki's avatar
Yoon, Daeki committed
37
38
39
40
41
export const getProfile = async (userId: string) => {
  const profile = await User.findById(userId);
  return profile; //이름 수정
};

Yoon, Daeki's avatar
Yoon, Daeki committed
42
43
44
45
46
47
48
49
50
51
52
53
54
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
55

Yoon, Daeki's avatar
Yoon, Daeki committed
56
57
58
59
60
61
62
63
export const isValidUserId = async (userId: string) => {
  const user = await User.findById(userId);
  if (user) {
    return true;
  } else {
    return false;
  }
};