user.db.ts 2.89 KB
Newer Older
1
import bcrypt from "bcryptjs";
2
import { ObjectId } from "mongoose";
Lee Soobeom's avatar
Lee Soobeom committed
3
4
import { IUser, Role, Post, User, FileInfo } from "../models";
import fs from "fs/promises";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
6

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

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
41
42
43
44
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
45

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

Yoon, Daeki's avatar
Yoon, Daeki committed
51
52
53
54
55
56
57
58
59
60
61
62
63
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
64

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

Kim, MinGyu's avatar
Kim, MinGyu committed
74
export const postPicture = async (
75
  userId: ObjectId,
Kim, MinGyu's avatar
Kim, MinGyu committed
76
77
78
79
  nickname: string,
  originalfilename?: string | null,
  newfilename?: string,
  picturepath?: string
80
) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
81
  const profile = await User.findById(userId);
Kim, MinGyu's avatar
Kim, MinGyu committed
82

Kim, MinGyu's avatar
Kim, MinGyu committed
83
  if (!(profile?.avatar === undefined)) {
Kim, MinGyu's avatar
Kim, MinGyu committed
84
    if (originalfilename === null) {
Lee Soobeom's avatar
Lee Soobeom committed
85
      await FileInfo.findByIdAndUpdate(profile.avatar._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
86
87
88
        nickname: nickname,
      });
    } else if (nickname === "") {
Lee Soobeom's avatar
Lee Soobeom committed
89
      await FileInfo.findByIdAndUpdate(profile.avatar._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
90
91
92
93
94
        originalfilename: originalfilename,
        newfilename: newfilename,
        picturepath: picturepath,
      });
    } else {
Lee Soobeom's avatar
Lee Soobeom committed
95
      await FileInfo.findByIdAndUpdate(profile.avatar._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
96
97
98
99
100
101
        originalfilename: originalfilename,
        newfilename: newfilename,
        picturepath: picturepath,
        nickname: nickname,
      });
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
102
  }
103
};
Kim, MinGyu's avatar
Kim, MinGyu committed
104

Lee Soobeom's avatar
Lee Soobeom committed
105
export const deleteUser = async (userId: ObjectId) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
106
  const user = await User.findById(userId);
Lee Soobeom's avatar
Lee Soobeom committed
107
108
109
110
111
  if (user && user.avatar) {
    const file = await FileInfo.findById(user.avatar._id);
    await fs.unlink("../travel/uploads/" + file?.newfilename);
    await FileInfo.deleteOne({ _id: user.avatar._id });
    return await user.deleteOne();
Kim, MinGyu's avatar
Kim, MinGyu committed
112
113
  }
};