user.db.ts 3.2 KB
Newer Older
1
import bcrypt from "bcryptjs";
2
import { ObjectId } from "mongoose";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
import { IUser, Role, Post, User, FileInfo, IRole } from "../models";
Kim, MinGyu's avatar
Kim, MinGyu committed
4
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);
Yoon, Daeki's avatar
Yoon, Daeki committed
9
  // const newFileInfo = 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,
Yoon, Daeki's avatar
Yoon, Daeki committed
22
    // fileInfo: newFileInfo._id,
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
};

export const findUserByEmail = async (
  email: string,
  includePassword: boolean = false
) => {
  let user;
  if (includePassword) {
Yoon, Daeki's avatar
Yoon, Daeki committed
34
35
36
    user = await User.findOne({ email })
      .select("+password")
      .populate<{ role: IRole }>("role");
Yoon, Daeki's avatar
Yoon, Daeki committed
37
  } else {
Yoon, Daeki's avatar
Yoon, Daeki committed
38
    user = await User.findOne({ email }).populate<{ role: IRole }>("role");
Yoon, Daeki's avatar
Yoon, Daeki committed
39
40
41
42
  }
  return user;
};

Lee Soobeom's avatar
Lee Soobeom committed
43
44
45
46
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
47

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

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

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

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

Kim, MinGyu's avatar
Kim, MinGyu committed
85
  if (!(profile?.fileInfo === undefined)) {
Kim, MinGyu's avatar
Kim, MinGyu committed
86
    if (originalfilename === null) {
Kim, MinGyu's avatar
Kim, MinGyu committed
87
      await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
88
89
90
        nickname: nickname,
      });
    } else if (nickname === "") {
Kim, MinGyu's avatar
Kim, MinGyu committed
91
92
93
      const ref = FileInfo.findById(profile.fileInfo._id);
      console.log(ref);

Kim, MinGyu's avatar
Kim, MinGyu committed
94
      await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
95
96
97
98
99
        originalfilename: originalfilename,
        newfilename: newfilename,
        picturepath: picturepath,
      });
    } else {
Kim, MinGyu's avatar
Kim, MinGyu committed
100
      const ref = await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
101
102
103
104
105
106
        originalfilename: originalfilename,
        newfilename: newfilename,
        picturepath: picturepath,
        nickname: nickname,
      });
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
107
  }
108
};
Kim, MinGyu's avatar
Kim, MinGyu committed
109

Kim, MinGyu's avatar
Kim, MinGyu committed
110
export const deleteUser = async (userId: string) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
111
  const user = await User.findById(userId);
Kim, MinGyu's avatar
Kim, MinGyu committed
112
113
114
115
116
117
118
119
  if (!(user?.fileInfo === undefined)) {
    const ref = await FileInfo.findById(user.fileInfo._id);
    if (!(ref?.newfilename === undefined)) {
      await fs.unlink("../travel/uploads/" + ref?.newfilename);
    }
    await FileInfo.deleteOne({ _id: user.fileInfo._id });
    const finish = await User.deleteOne({ _id: userId });
    return finish;
Kim, MinGyu's avatar
Kim, MinGyu committed
120
121
  }
};