user.controller.ts 2.47 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
Kim, MinGyu's avatar
Kim, MinGyu committed
3
import { Request } from "express";
백승민's avatar
백승민 committed
4
import formidable from "formidable";
5
import fs from "fs/promises";
Kim, MinGyu's avatar
Kim, MinGyu committed
6
import { TypedRequest } from "../types";
7
import { FileInfo, IUser, User } from "../models";
Kim, MinGyu's avatar
Kim, MinGyu committed
8
9
10
11

export interface TypedRequestAuth<T> extends Request {
  auth: T;
}
Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
15
16
17
18
19
20
21

export const getUsers = asyncWrap(async (req, res) => {
  const users = await userDb.getUsers();
  return res.json(users);
});

export const createUser = asyncWrap(async (req, res) => {
  const user = req.body;
  console.log("user body", user);
  const newUser = await userDb.createUser(user);
Yoon, Daeki's avatar
Yoon, Daeki committed
22
  return res.json(newUser);
Yoon, Daeki's avatar
Yoon, Daeki committed
23
});
Kim, MinGyu's avatar
Kim, MinGyu committed
24
25

export const getProfile = asyncWrap(async (reqExp, res) => {
26
  const req = reqExp as TypedRequestAuth<{ userId: string }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
Kim, MinGyu's avatar
Kim, MinGyu committed
27

28
  const { userId } = req.auth;
Kim, MinGyu's avatar
Kim, MinGyu committed
29
  const profile = await userDb.getProfile(userId);
30
31
32
  res.json(profile);
});

33
export const updateProfile = asyncWrap(async (reqExp, res) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
34
  const req = reqExp as TypedRequest;
35
  const { userId } = req.auth;
36
37
  const { name } = req.body;
  const { avatar }: { avatar: formidable.File } = req.files;
38

39
40
41
42
43
44
45
46
47
48
49
50
51
  const duplicate = await FileInfo.findOne({
    originalfilename: avatar.originalFilename,
  }); // 같은 사진이 DB에 있는지 확인

  console.log(duplicate);
  if (!duplicate) {
    //만약 같은 사진이 DB안에 존재하지 않을 경우
    const user = await userDb.updateProfile(userId, name, avatar);

    res.json(user);
  } else {
    // 같은 사진이 DB안에 존재할 경우
    await fs.unlink(avatar.filepath);
Kim, MinGyu's avatar
수정    
Kim, MinGyu committed
52
53
    // 덮기 전의 FileInfo 삭제
    // await FileInfo.deleteOne({ _id: user?.avatar });
54
55
56
57
58
59
    const user = await User.findById(userId);
    if (user !== null) {
      user.avatar = duplicate._id;
      await user.save();
    }
  }
60
});
Kim, MinGyu's avatar
Kim, MinGyu committed
61
62

export const deleteUser = asyncWrap(async (reqExp, res) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
63
  const req = reqExp as TypedRequestAuth<{ userId: string }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
Kim, MinGyu's avatar
Kim, MinGyu committed
64
65

  const { userId } = req.auth;
Kim, MinGyu's avatar
Kim, MinGyu committed
66
67
68
  const finish = await userDb.deleteUser(userId);
  if (finish?.deletedCount === 1) {
    res.json(true);
Kim, MinGyu's avatar
Kim, MinGyu committed
69
70
  } else {
    res.status(422).send("삭제에 실패하였습니다.");
Kim, MinGyu's avatar
Kim, MinGyu committed
71
  }
Kim, MinGyu's avatar
Kim, MinGyu committed
72
});