-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelivery.js
248 lines (234 loc) · 8.03 KB
/
Delivery.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
const { ExpectedError } = require("./ExpectedError.js");
const { getSSMParameter } = require("ssm.js");
//=========================================Model=======================================================
class Delivery {
constructor({
id,
invoice_number,
courier_company_code,
tikkling_id,
state_id,
zonecode,
address,
detail_address,
created_at,
start_delivery_date,
expected_delivery_date,
actual_delivery_date,
courier_company_name,
db,
}) {
this.id = id || null;
this.invoice_number = invoice_number || null;
this.courier_company_code = courier_company_code || null;
this.tikkling_id = tikkling_id || null;
this.state_id = state_id || null;
this.zonecode = zonecode || null;
this.address = address || null;
this.detail_address = detail_address || null;
this.created_at = created_at || null;
this.start_delivery_date = start_delivery_date || null;
this.expected_delivery_date = expected_delivery_date || null;
this.actual_delivery_date = actual_delivery_date || null;
this.courier_company_name = courier_company_name || null;
this.db = db || null;
}
/**
* @description 객체를 json으로 변환하는 함수
*/
toJSON() {
return {
id: this.id,
invoice_number: this.invoice_number,
courier_company_code: this.courier_company_code,
tikkling_id: this.tikkling_id,
state_id: this.state_id,
zonecode: this.zonecode,
address: this.address,
detail_address: this.detail_address,
created_at: this.created_at,
start_delivery_date: this.start_delivery_date,
expected_delivery_date: this.expected_delivery_date,
actual_delivery_date: this.actual_delivery_date,
courier_company_name: this.courier_company_name,
};
}
/**
* delivery_info정보를 통해 스마트 택배 api에서 제공하는 배송정보 확인 링크를 생성
* @returns {string} 배송조회 링크
*/
async createDeliveryCheckLink() {
const t_key = await getSSMParameter("t_key");
return `http://info.sweettracker.co.kr/tracking/5?t_key=${t_key}&t_code=${this.courier_company_code}&t_invoice=${this.invoice_number}`;
}
updateDelivery(row_of_delivery) {
try {
this.id = row_of_delivery.id;
this.invoice_number = row_of_delivery.invoice_number;
this.courier_company_code = row_of_delivery.courier_company_code;
this.tikkling_id = row_of_delivery.tikkling_id;
this.state_id = row_of_delivery.state_id;
this.zonecode = row_of_delivery.zonecode;
this.address = row_of_delivery.address;
this.detail_address = row_of_delivery.detail_address;
this.created_at = row_of_delivery.created_at;
this.start_delivery_date = row_of_delivery.start_delivery_date;
this.expected_delivery_date = row_of_delivery.expected_delivery_date;
this.actual_delivery_date = row_of_delivery.actual_delivery_date;
this.courier_company_name = row_of_delivery.courier_company_name;
} catch (error) {
console.error(`🚨 error -> ⚡️ updateDelivery : 🐞객체의 모든 값이 전달되지 않았습니다.`);
throw ExpectedError({
status: 500,
detail_code: "00",
message: "서버에러",
});
}
}
async loadDeliveryInfoByTikklingId() {
try {
if (this.tikkling_id == null) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "tikkling_id가 없습니다.",
});
}
const rows = await this.db.executeQuery(
`
SELECT delivery_info.*, courier_company.name as courier_company_name
FROM delivery_info as delivery_info
INNER JOIN courier_company AS courier_company ON delivery_info.courier_company_code = courier_company.code
WHERE tikkling_id = ?`,
[this.tikkling_id]
);
if (rows.length === 0) {
throw new ExpectedError({
status: 404,
detail_code: "01",
message: "해당 티클링의 시작된 배송 기록이 없습니다.",
});
}
this.updateDelivery(rows[0]);
} catch (error) {
console.error(`🚨 error -> ⚡️ loadDeliveryInfoByTikklingId : 🐞${error}`);
throw error;
}
}
/**
* 유저아이디를 받고 해당 유저의 가장 최근 티클링에 대한 배송정보를 가져옴
* @param {number} user_id
* @returns {void}
* @async
*
*/
async getRecentDeliveryInfoOfUser(user_id) {
try {
const rows = await this.db.executeQuery(
`
SELECT delivery_info.*, courier_company.name as courier_company_name
FROM delivery_info as delivery_info
INNER JOIN (SELECT * FROM tikkling WHERE user_id = ?) AS user_tikkling ON delivery_info.tikkling_id = user_tikkling.id
INNER JOIN (SELECT * FROM courier_company) AS courier_company ON delivery_info.courier_company_code = courier_company.code
ORDER BY delivery_info.created_at DESC LIMIT 1`,
[user_id]
);
if (rows.length === 0) {
throw new ExpectedError({
status: 404,
detail_code: "01",
message: "해당 유저의 배송 기록이 없습니다.",
});
}
this.updateDelivery(rows[0]);
return;
} catch (error) {
console.error(`🚨 error -> ⚡️ getRecentDeliveryInfoOfUser : 🐞${error}`);
throw error;
}
}
async updateDeliveryToConfirmed() {
try {
if(this.tikkling_id == null){
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "tikkling_id가 없습니다.",
});
}
if (this.state_id == 4) {
throw new ExpectedError({
status: 400,
detail_code: "00",
message: "이미 수령처리된 배송정보입니다.",
});
}
const result = await this.db.executeQuery(`UPDATE delivery_info SET state_id = 4, actual_delivery_date = NOW() WHERE tikkling_id = ?`, [this.tikkling_id]);
if (result.affectedRows === 0) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "배송 정보 저장 실패",
});
}
this.state_id = 4;
return;
} catch (error) {
console.error(`🚨 error -> ⚡️ updateDeliveryToConrimed : 🐞${error}`);
throw error;
}
}
/**
* @description 주어진 티클링 아이디에 해당하는 배송정보를 가져오는 함수
* @param {number} tikkling_id
* @returns
*/
async getDeliveryInfoByTikklingId(tikkling_id) {
try {
const rows = await this.db.executeQuery(
`
SELECT delivery_info.*, courier_company.name as courier_company_name
FROM delivery_info
INNER JOIN courier_company on delivery_info.courier_company_code = courier_company.code
WHERE tikkling_id = ?`,
[tikkling_id]
);
if (rows.length === 0) {
throw new ExpectedError({
status: 404,
etail_code: "02",
message: "해당 티클링의 배송 기록이 없습니다.",
});
}
this.updateDelivery(rows[0]);
return;
} catch (error) {
console.error(`🚨 error -> ⚡️ getDeliveryInfoByTikklingId : 🐞${error}`);
throw error;
}
}
async saveDeliveryData() {
try {
const result = await this.db.executeQuery(`INSERT INTO delivery_info (tikkling_id, state_id, zonecode, address, detail_address) VALUES (?, ?, ?, ?, ?)`, [
this.tikkling_id,
this.state_id,
this.zonecode,
this.address,
this.detail_address,
]);
if (result.affectedRows === 0) {
throw new ExpectedError({
status: 500,
detail_code: "00",
message: "배송 정보 저장 실패",
});
}
this.id = result.insertId;
} catch (error) {
console.error(`🚨 error -> ⚡️ saveDeleveryData : 🐞${error}`);
throw error;
}
}
}
//=========================================Repository=======================================================
module.exports = { Delivery };