-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.js
310 lines (291 loc) Β· 9.33 KB
/
User.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const { getSSMParameter } = require("ssm.js");
const { ExpectedError } = require("./ExpectedError.js");
class User {
constructor({
id = null,
name,
birthday,
nick = " ",
phone,
is_deleted = false,
gender,
image = "https://d2da4yi19up8sp.cloudfront.net/profile/profile.png",
zonecode = null,
address = null,
detail_address = null,
created_at = null,
is_tikkling = false,
device_token = null,
tikkling_ticket = 2,
account = null,
bank_code = null,
last_present_amount = null,
funnel = "meta_ad",
source_tikkling_id = null,
db,
}) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.nick = nick;
this.phone = phone;
this.is_deleted = is_deleted;
this.gender = gender;
this.image = image;
this.zonecode = zonecode;
this.address = address;
this.detail_address = detail_address;
this.created_at = created_at;
this.is_tikkling = is_tikkling;
this.device_token = device_token;
this.tikkling_ticket = tikkling_ticket;
this.account = account;
this.bank_code = bank_code;
this.funnel = source_tikkling_id ? "share_link" : funnel;
this.source_tikkling_id = source_tikkling_id;
this.db = db;
this.last_present_amount = last_present_amount;
}
/**
* Asynchronously creates a new user with the given ID.
* @param {number} id - User ID
* @returns {Promise<User>} - A promise that resolves with a new User instance.
* @throws {ExpectedError} Throws an ExpectedError with status 500 if the database query fails.
* @memberof User
* @static
* @async
* @example
* const user = await User.createById(1);
* // => User { id: 1, name: 'νκΈΈλ', ... }
*/
static createById = async ({ id, db }) => {
try {
const query = `SELECT * FROM users WHERE id = ?`;
const [user] = await db.executeQuery(query, [id]);
return new User({ ...user, db });
} catch (error) {
console.error(`π¨ error -> createById : π${error}`);
throw error;
}
};
async updateLastPresentAmount(last_present_amount) {
try {
const result = await this.db.executeQuery(`UPDATE users SET last_present_amount = ? WHERE id = ?`, [last_present_amount, this.id]);
if (result.affectedRows !== 1) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "last_present_amount μμ μ€ν¨",
});
}
this.last_present_amount = last_present_amount;
return;
}
catch (error) {
console.error(`π¨ error -> updateLastPresentAmount : π${error}`);
throw error;
}
}
async registerUser() {
try {
const result = await this.db.executeQuery(`INSERT INTO users
(name, birthday, nick, phone, gender, image, address, detail_address, is_tikkling, tikkling_ticket, funnel)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
this.name,
this.birthday,
this.nick,
this.phone,
this.gender,
this.image,
this.address,
this.detail_address,
this.is_tikkling,
this.tikkling_ticket,
this.funnel,
])
if (result.affectedRows !== 1) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "μ μ λ±λ‘ μ€ν¨",
});
}
this.id = result.insertId;
}
catch (error) {
console.error(`π¨ error -> β‘οΈ registerUser : π${error}`);
throw error;
}
}
async logIfUserFromTikkling() {
try {
if (this.source_tikkling_id) {
const result = await this.db.executeQuery(`INSERT INTO shared_tikkling_signup_log (tikkling_id, user_id) VALUES (?, ?)`, [this.source_tikkling_id, this.id]);
if (result.affectedRows !== 1) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "μ μ λ±λ‘ μ€ν¨",
});
}
}
}
catch (error) {
console.error(`π¨ error -> logIfUserFromTikkling : π${error}`);
throw error;
}
}
async validateUserForRegister() {
try {
//μ μ μ΄λ¦ μ ν¨μ± κ²μ¦
if (!this.name || typeof this.name !== "string" || this.name.length > 30) {
//return invalid
throw new ExpectedError({
status: "400",
message: `λΉμ μμ μμ², μ΄λ¦ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
detail_code: "01",
});
}
//μ μ μμΌ μ ν¨μ± κ²μ¦
const parsedDate = new Date(this.birthday);
if (isNaN(parsedDate) || Object.prototype.toString.call(parsedDate) !== "[object Date]") {
throw new ExpectedError({
status: "400",
message: `λΉμ μμ μμ², μμΌ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
detail_code: "02",
});
}
// Check if the string matches the numeric pattern and its length is between 9 and 12
const numericPattern = /^\d+$/;
if (!this.phone || typeof this.phone !== "string" || this.phone.length < 9 || this.phone.length > 11 || !numericPattern.test(this.phone)) {
throw new ExpectedError({
status: "400",
message: `λΉμ μμ μμ², μ νλ²νΈ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
detail_code: "05",
});
}
// μ±λ³ λ°μ΄ν° 체ν¬
if (!this.gender || typeof this.gender !== "string" || !(this.gender === "male" || this.gender === "female" || this.gender === "others")) {
throw new ExpectedError({
status: "400",
message: `λΉμ μμ μμ², μ±λ³ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
detail_code: "06",
});
}
//λλ€μ μ ν¨μ± κ²μ¦
// if (!this.nick || typeof this.nick !== "string" || this.nick.length > 30) {
// throw new ExpectedError({
// status: "400",
// message: `λΉμ μμ μμ², λλ€μ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
// detail_code: "04",
// });
// }
}
catch (error) {
console.error(`π¨ error -> validateUserForRegister : π${error}`);
throw error;
}
}
async restrictUserUnder14() {
try {
const dob = new Date(this.birthday);
const today = new Date();
let age = today.getFullYear() - dob.getFullYear();
// Check if birthday hasn't occurred yet this year
if (today.getMonth() < dob.getMonth() || (today.getMonth() === dob.getMonth() && today.getDate() < dob.getDate())) {
age--;
}
if (age < 14) {
throw new ExpectedError({
status: "400",
message: `νμκ°μ
μ€ν¨, λ§ 14μΈ λ―Έλ§μ κ°μ
ν μ μμ΅λλ€.`,
detail_code: "03",
});
}
} catch (error) {
console.error(`π¨ error -> restrictUserForRegister : π${error}`);
throw error;
}
}
async validatteUserForStartTikkling() {
try {
// νμ¬ ν°ν΄λ§μ΄ μ§νμ€μΈμ§ νμΈ
if (this.is_tikkling) {
throw new ExpectedError({
status: "403",
message: `λΉμ μμ μμ², μ΄λ―Έ ν°ν΄λ§μ€μΈ μ μ μ
λλ€.`,
detail_code: "01",
});
}
// ν°ν΄λ§ ν°μΌμ΄ λ¨μμλμ§ νμΈ
if (this.tikkling_ticket == 0) {
throw new ExpectedError({
status: "403",
message: `λΉμ μμ μμ², ν°ν΄λ§ ν°μΌμ΄ μμ΅λλ€.`,
detail_code: "02",
});
}
} catch (error) {
console.error(`π¨ error -> β‘οΈ validatteUser : π ${error}`);
throw error;
}
}
async decreaseTikkleTicket() {
try {
if (this.tikkling_ticket) {
this.tikkling_ticket -= 1;
}
const query = `UPDATE users SET tikkling_ticket = tikkling_ticket - 1 WHERE id = ?`;
await this.db.executeQuery(query, [this.id]);
} catch (error) {
console.error(`π¨ error -> decreaseTikkleTicket : π${error}`);
throw error;
}
}
async increaseTikkleTicket() {
try {
if (this.tikkling_ticket) {
this.tikkling_ticket += 1;
}
const query = `UPDATE users SET tikkling_ticket = tikkling_ticket + 1 WHERE id = ?`;
await this.db.executeQuery(query, [this.id]);
} catch (error) {
console.error(`π¨ error -> decreaseTikkleTicket : π${error}`);
throw error;
}
}
async deleteWishlist(product_id) {
try {
const query = `DELETE FROM user_wish_list WHERE user_id = ? AND product_id = ?`;
await this.db.executeQuery(query, [this.id, product_id]);
} catch (error) {
console.error(`π¨ error -> deleteWishlist : π${error}`);
throw error;
}
}
validateAddress() {
try {
if (
!this.zonecode ||
!this.address ||
!this.detail_address ||
typeof this.zonecode !== "string" ||
typeof this.address !== "string" ||
typeof this.detail_address !== "string" ||
this.zonecode.length !== 5 ||
this.address.length > 250 ||
this.detail_address.length > 250
) {
throw new ExpectedError({
status: "400",
message: `λΉμ μμ μμ², μ£Όμ λ°μ΄ν°κ° μ¬λ°λ₯΄μ§ μμ΅λλ€.`,
detail_code: "05",
});
}
} catch (error) {
console.error(`π¨ error -> validateAddress : π${error}`);
throw error;
}
}
}
module.exports = { User };