---
title: e-commerce 問題與建議
tags: v1
---
## Cart
### Function `checkoutAllCart` 成立訂單
```javascript=
// create order
const orderItems = await this.getOrderItems(this.userId);
const args = {
order_date: new Date(),
taxRate: orderItems[0].tax_rate,
currency: orderItems[0].currency,
paymentMethod: orderItems[0].paymentMethod,
user_id: this.userId,
};
const order = new Order(this.userId);
const orderObj = await order.create(orderItems, recurringPayment, args);
let paymentCount = orderObj.recurring_payment.count;
if (paymentCount === RECURRING_PAYMENT.SUBSCRIPTION.count) {
paymentCount = 2;
}
// create payments
let paymentResult = [];
let firstPayment;
let secondPayment;
for (let i = 0; i < paymentCount; i++) {
const payment = new Payment(this.userId, orderObj._id);
if (i === 0) {
firstPayment = await payment.createDownPayment(orderObj);
if (!firstPayment) {
throw new Error('Create payment failed, please try again.');
}
} else {
const paymentNumber = firstPayment.payment_number;
secondPayment = await payment.createNextPayment(paymentNumber);
}
}
paymentResult.push(firstPayment, secondPayment);
let result = { order: orderObj, paymentList: paymentResult };
return result;
```
### 問題
1. Cart 每一個都有 paymentMethod, 但是成立訂單卻直接拿第一個當作 order 的 paymentMethod(tax_rate, currency 也是), 這樣子是否在要建立訂單的時候再決定即可, 不用在每一項 cart 加入時都必須帶入 paymentMethod
> paymentMethod 已拿掉, tax_rate 跟 currency 還是有直接取用第一個的問題
>> <font color="red"> tax_rate 與 currency 會寫在產品上,譬如不同的幣值就會是不同的 product item,無法在建立訂單時才確認。</font>
2. [x] `recurringPayment` 這個參數只有一個物件, 是不是代表我不能訂閱制的產品跟單次付款的產品一起結帳, 如果有這種狀況時該怎麼處理
- <font color="red">感謝提醒,得再看看該如何設計</font>
- <font color="red">經 5/11 早會討論,維持原設計,但因為payment 的 amount 是直接取用 order 的 amount,需修改這個 bug </font>
3. 上方程式碼第 20 行開始的 for 迴圈, 如果 paymentCount 超過 2, 假設是 3, 那麼 paymentResult 也只會回傳兩張 payment. 不確定如果未來有預付 3 個月, 3 年, 或是買 3 個月送 3 個月的時候, 這裡該怎麼操作
- <font color="red">recurringPayment 的 count 是觸發次數,此設定已表示時間為何,所以不會有時間上的問題。接下來就是檢查 count 數量,與訂單的張數關係,兩者不等於就是 call function payment.createNextPayment() 產生下期 payment。P.S. 若仍不懂 `recurringPayment` 的設計,可參考[文件](https://hackmd.io/5uFLaBFGQOO286LsiZaCrw) </font>
### 建議
- [x] Function `getOrderItems` 是去找 product info 然後組成 orderItems, 這個名字感覺不是這麼直觀, 改成 `convertToOrderItems` 會不會好一點呢
- <font color="red">agree 要改名,改為 `combineInfoIntoOrderItems`</font>
- 每次新增商品或減少商品, 都不會給出目前總金額, 這邊前端應該需要, 或許該考慮在 CartSchema 新增金額的欄位, 這樣就不用每次要計算總價的時候都要去找 product 的 table 在去做計算了
- <font color="red">需再考慮</font>
- checkout 以後,購物車應該就要被清空, 可以將 clear 這個 function 直接擺在 checkoutAllCart 的最後面
- <font color="red">可討論 clear function 要單獨出來,還是直接放在 checkoutAllCart 裡。我是傾向於不想要綁太死,但使用時要記得 call clear()</font>
## Order
### 問題
1. 如果我要查看我的訂單, 我只知道 user_id, 還需要一個 function 找屬於這個人的訂單(透過 user_id 找), 然後才可以透過找到的 order_id 或是 order_number 去做操作
2. constructor 要求帶入 userId, 但是使用 getAllOrders 卻跟這個人沒有關係, 邏輯有衝突,
- [x] 綜合以上兩點, 我認為沒有必要在 constructor 要求帶入 user_id, 因為可能會找尋特定範圍的訂單, 不一定跟一個特定的 user 有關係
- <font color="red">agree</font>
## Payment
### 問題
1. [x] 如果我想要查關於這個人的所有 payment, 我不知道 order_id, 我只有 user_id, 但是 constructor 卻要求要有 order_id, 我就沒辦法查詢了
- <font color="red">agree</font>
2. checkoutAllCart 後成立訂單, due_date 就馬上就寫入了, 如果 user 隔天才付款, 甚至隔個幾天, 那麼 due_date 日期就不會正確了
- <font color="red">目前的設計是,當天成立的訂單必須當天付款,若超過時間就是過期了,所以本來就是要讓 due_date 作為付款最後期限。(這規則是暫定的,要等之後產品功能確認了,才會再做調整。)</font>
## TapPayRepository
### 問題
- [x] constructor 規定要帶入帶入 userId, orderId, paymentId 但是裡面的 function 都沒有用到
- <font color="red">agree, payByPrice() 和 payBytoken() 會用到,沒寫在函式裡面,但會需要這些參數找到需要的資料,我來修修 </font>