πŸ“¦ SeolJaeHyeok / My-shopping-mall

πŸ“„ admin-service.js Β· 284 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284import { userModel, orderModel, productModel } from "../db";

import bcrypt from "bcrypt";

class AdminService {
  // λ³Έ 파일의 맨 μ•„λž˜μ—μ„œ, new UserService(userModel) ν•˜λ©΄, 이 ν•¨μˆ˜μ˜ 인자둜 전달됨
  constructor(userModel, orderModel, productModel) {
    this.userModel = userModel;
    this.orderModel = orderModel;
    this.productModel = productModel;
  }

  // νšŒμ› λͺ©λ‘ 쑰회
  async getUsers() {
    // dbμ—μ„œ νšŒμ› 정보 쑰회
    const users = await this.userModel.findAll();
    if (!users) {
      throw new Error("νšŒμ› λͺ©λ‘μ„ μ‘°νšŒν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    return users;
  }

  // μ£Όλ¬Έ λͺ©λ‘ 쑰회
  async getOrders() {
    // dbμ—μ„œ μ£Όλ¬Έ 정보 쑰회
    const orders = await this.orderModel.findAll();
    if (!orders) {
      throw new Error("μ£Όλ¬Έ λͺ©λ‘μ„ μ‘°νšŒν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    return orders;
  }

  // orderId둜 μ£Όλ¬Έ λͺ©λ‘ 쑰회
  async getOrdersByOrderId(orderId) {
    // dbμ—μ„œ μ£Όλ¬Έ 정보 쑰회
    const order = await this.orderModel.findById(orderId);
    if (!order) {
      throw new Error("μ£Όλ¬Έ λͺ©λ‘μ„ μ‘°νšŒν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    return order;
  }

  // μƒν’ˆ μΆ”κ°€
  async addProduct(productInfo) {
    const {
      name,
      price,
      category,
      quantity,
      img,
      brandName,
      keyword,
      shortDescription,
      detailDescription,
      userId,
    } = productInfo;
    if (
      !name ||
      !price ||
      !category ||
      !brandName ||
      !img ||
      !shortDescription ||
      !detailDescription ||
      !userId
    ) {
      throw new Error("μƒν’ˆ 정보λ₯Ό λͺ¨λ‘ μž…λ ₯ν•΄μ£Όμ„Έμš”.");
    }
    // μƒν’ˆλͺ… 쀑볡 확인
    const usedproduct = await this.productModel.findByName(name);
    if (usedproduct) {
      throw new Error(
        "이 μƒν’ˆλͺ…은 ν˜„μž¬ μ‚¬μš©μ€‘μž…λ‹ˆλ‹€. λ‹€λ₯Έ μƒν’ˆλͺ…을 μž…λ ₯ν•΄ μ£Όμ„Έμš”."
      );
    }
    const newProductInfo = {
      name,
      price,
      category,
      quantity,
      img,
      brandName,
      keyword,
      shortDescription,
      detailDescription,
      userId,
    };
    // μƒν’ˆ 등둝
    const newProduct = await this.productModel.create(newProductInfo);
    if (!newProduct) {
      throw new Error("μƒν’ˆμ΄ μ •μƒμ μœΌλ‘œ μΆ”κ°€λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return newProduct;
  }

  // νšŒμ› κΆŒν•œ μˆ˜μ •
  async setUserRole(userInfoRequired, toUpdate) {
    const { email } = userInfoRequired;
    if (!email) {
      throw new Error("νšŒμ› 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const userInfo = await this.userModel.findByEmail(email);
    if (!userInfo) {
      throw new Error("νšŒμ› 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const userId = userInfo._id;
    const updatedUser = await this.userModel.update({
      userId,
      update: toUpdate,
    });
    if (!updatedUser) {
      throw new Error("νšŒμ› 정보가 μˆ˜μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return updatedUser;
  }

  // 배솑 μƒνƒœ μˆ˜μ •
  async setOrderStatus(orderInfoRequired, toUpdate) {
    const { orderId, productId } = orderInfoRequired;
    const { status } = toUpdate;
    if (!orderId || !productId) {
      throw new Error("μ£Όλ¬Έ 정보 ν˜Ήμ€ μƒν’ˆ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    if (!status) {
      throw new Error("배솑 μƒνƒœ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const updateOrderList = await this.orderModel.findById(orderId);
    if (updateOrderList) {
      throw new Error("μ£Όλ¬Έ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const newUpdate = await updateOrderList.orderList.map((e) => {
      if (e.productId === productId) {
        e.status = status;
      }
      return e;
    });

    const update = { $set: { orderList: newUpdate } };
    const updatedOrder = await this.orderModel.update({ orderId, update });
    if (!updatedOrder) {
      throw new Error("배솑 μƒνƒœκ°€ μˆ˜μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return updatedOrder;
  }

  // μ£Όλ¬Έ μ‚­μ œ
  async deleteOrder(orderInfoRequired) {
    const { orderId } = orderInfoRequired;
    if (!orderId) {
      throw new Error("μ£Όλ¬Έ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const deletedOrder = await this.orderModel.deleteById(orderId);
    if (!deletedOrder) {
      throw new Error("주문이 μ •μƒμ μœΌλ‘œ μ‚­μ œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return deletedOrder;
  }

  // μƒν’ˆ μˆ˜μ •
  async setProduct(productId, toUpdate) {
    if (!productId || !toUpdate) {
      throw new Error("μƒν’ˆ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    // μš°μ„  ν•΄λ‹Ή id의 μƒν’ˆμ΄ db에 μžˆλŠ”μ§€ 확인
    const product = await this.productModel.findById(productId);

    //dbμ—μ„œ μ°Ύμ§€ λͺ»ν•œ 경우, μ—λŸ¬ λ©”μ‹œμ§€ λ°˜ν™˜
    if (!product) {
      throw new Error("μƒν’ˆ 내역이 μ—†μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번 확인해 μ£Όμ„Έμš”.");
    }
    //μ—…λ°μ΄νŠΈ μ§„ν–‰
    const updatedProduct = await this.productModel.update({
      productId,
      update: toUpdate,
    });
    if (!updatedProduct) {
      throw new Error("μƒν’ˆ μˆ˜μ •μ΄ λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return updatedProduct;
  }

  // μ£Όλ¬Έ μƒν’ˆ μ‚­μ œ
  async deleteOrderProduct({ orderId, productId }) {
    if (!orderId || !productId) {
      throw new Error("μ£Όλ¬Έ 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const deleteOrderList = await this.orderModel.findById(orderId);
    const newDelete = await deleteOrderList.orderList.filter(
      (e) => e.productId !== productId
    );
    const deleteUpdate = { $set: { orderList: newDelete } };
    const newOrder = await this.orderModel.update({
      orderId,
      update: deleteUpdate,
    });
    if (!newOrder) {
      throw new Error("μ£Όλ¬Έ μƒν’ˆμ΄ μ‚­μ œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    if (newOrder.orderList.length < 1) {
      return await this.orderModel.deleteById(orderId);
    }

    return newOrder;
  }

  // μƒν’ˆ μ‚­μ œ
  async deleteProduct(productIdArray) {
    if (!productIdArray) {
      throw new Error("μ‚­μ œν•  μƒν’ˆμ„ 선택해 μ£Όμ„Έμš”.");
    }
    const deletedProduct = await productIdArray.map((productId) =>
      this.productModel.deleteById({ productId })
    );
    if (!deletedProduct) {
      throw new Error("μƒν’ˆμ΄ μ‚­μ œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return deletedProduct;
  }

  // νšŒμ› 정보 μ‚­μ œ
  async deleteUser(userInfoRequired) {
    const { email } = userInfoRequired;
    if (!email) {
      throw new Error("νšŒμ› 이메일 정보가 μ—†μŠ΅λ‹ˆλ‹€.");
    }
    const deletedUser = await this.userModel.deleteByEmail(email);
    if (!deletedUser) {
      throw new Error("νšŒμ› 정보가 μ‚­μ œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
    }
    return deletedUser;
  }

  // λΉ„λ°€λ²ˆν˜Έ 일치 μ—¬λΆ€ 확인
  async userVerify(userId, currentPassword) {
    // μš°μ„  ν•΄λ‹Ή id의 μœ μ €κ°€ db에 μžˆλŠ”μ§€ 확인
    let user = await this.userModel.findById(userId);
    // dbμ—μ„œ μ°Ύμ§€ λͺ»ν•œ 경우, μ—λŸ¬ λ©”μ‹œμ§€ λ°˜ν™˜
    if (!user) {
      throw new Error("κ°€μž… 내역이 μ—†μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번 확인해 μ£Όμ„Έμš”.");
    }

    // 이제, 정보 μˆ˜μ •μ„ μœ„ν•΄ μ‚¬μš©μžκ°€ μž…λ ₯ν•œ λΉ„λ°€λ²ˆν˜Έκ°€ μ˜¬λ°”λ₯Έ 값인지 확인해야 함

    // λΉ„λ°€λ²ˆν˜Έ 일치 μ—¬λΆ€ 확인
    const correctPasswordHash = user.password;
    const isPasswordCorrect = await bcrypt.compare(
      currentPassword,
      correctPasswordHash
    );

    if (!isPasswordCorrect) {
      throw new Error(
        "ν˜„μž¬ λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번 확인해 μ£Όμ„Έμš”."
      );
    }
  }

  // κ΄€λ¦¬μž 계정 확인
  async adminVerify(userId) {
    if (!userId) {
      throw new Error("둜그인이 ν•„μš”ν•©λ‹ˆλ‹€.");
    }
    const user = await this.userModel.findById(userId);
    if (!user) {
      throw new Error("κ°€μž… 내역이 μ—†μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ ν•œ 번 확인해 μ£Όμ„Έμš”.");
    }

    const userRole = user.role;
    if (userRole !== "admin") {
      throw new Error("κ΄€λ¦¬μžκ°€ μ•„λ‹ˆλ©΄ 접속할 수  μ—†μŠ΅λ‹ˆλ‹€.");
    }
  }

  async exceptPwd(userInfo) {
    return await (({ password, ...o }) => o)(userInfo);
  }
}

export const adminService = new AdminService(
  userModel,
  orderModel,
  productModel
);