user.db.ts 3.1 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, FileInfo } 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);
Kim, MinGyu's avatar
Kim, MinGyu 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,
Kim, MinGyu's avatar
Kim, MinGyu 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
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("fileInfo");
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?.fileInfo === undefined)) {
Kim, MinGyu's avatar
Kim, MinGyu committed
84
    if (originalfilename === null) {
Kim, MinGyu's avatar
Kim, MinGyu committed
85
      await FileInfo.findByIdAndUpdate(profile.fileInfo._id, {
Kim, MinGyu's avatar
Kim, MinGyu committed
86
87
88
        nickname: nickname,
      });
    } else if (nickname === "") {
Kim, MinGyu's avatar
Kim, MinGyu committed
89
90
91
      const ref = FileInfo.findById(profile.fileInfo._id);
      console.log(ref);

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

Kim, MinGyu's avatar
Kim, MinGyu committed
108
export const deleteUser = async (userId: string) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
109
  const user = await User.findById(userId);
Kim, MinGyu's avatar
Kim, MinGyu committed
110
111
112
113
114
115
116
117
  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
118
119
  }
};