user.db.ts 2.91 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";
Kim, MinGyu's avatar
Kim, MinGyu committed
4
import fs from "fs";
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 newAvatar = await Avatar.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    if (originalfilename === null) {
      await Avatar.findByIdAndUpdate(profile.avatar._id, {
        nickname: nickname,
      });
    } else if (nickname === "") {
      await Avatar.findByIdAndUpdate(profile.avatar._id, {
        originalfilename: originalfilename,
        newfilename: newfilename,
        picturepath: picturepath,
      });
    } else {
      await Avatar.findByIdAndUpdate(profile.avatar._id, {
        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
105
106
107
108
109
110
111
112
113
114
115

export const deleteUser = async (userId: ObjectId) => {
  const user = await User.findById(userId);
  if (!(user?.avatar === undefined)) {
    const ref = await Avatar.findById(user.avatar._id);
    fs.unlink("../travel/uploads/" + ref?.newfilename, (err) => {
      console.log(err);
    });
    await Avatar.deleteOne({ _id: user.avatar._id });
    await User.deleteOne({ _id: userId });
  }
};