---
title: 排程寄信
tags: v1
---
# Email daily report: Dashboard Today 排程寄信
Email daily report: action required report on Dashboard "Today"
[ToC]

## 觸發時間點
- UTC+8 07:00
## UX/UI
## linux crontab
- 問題
- 伺服器重開,造成 API 尚未執行完成就被中斷了
- 解決辦法
- 資料庫加入最後寄出時間藉此判斷是否重複寄出
- 問題
- 沒處理到的,今天就不會被處理了?
- 答
- 重跑一遍 藉由最後寄出時間此欄位判斷 一天之內寄過就不再寄出
# 伺服器時區 台灣
# 筆記
api.post
user 記住最後寄出時間 alpha_admin
寄信成功 寄信失敗 都要記住 log
去戳我們的 api
## 發生漏寄信可能情況
- 寄信服務掛了
- 寄信 api 有更新,我們沒更新
## 規劃
- 透過 crontab 設定 每天 00:00 call 一次我們的 api
```
00*** curl 'http://dev.fio.one/cron/daily-report' # action required on dashboard
```
## api
```
api.post(/cron/daily-report)
```
> 程式請寫在 ./routes/cron.js 並再於 ./app.js 加上對應的 Express.js route 設定 [name=Joe]
找出每個 user 的 action required
寄給多個人
```
const emails = [
{
to: 'recipient1@example.org',
from: 'sender@example.org',
subject: 'Hello recipient 1',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
},
{
to: 'recipient2@example.org',
from: 'other-sender@example.org',
subject: 'Hello recipient 2',
text: 'Hello other plain world!',
html: '<p>Hello other HTML world!</p>',
},
];
sgMail.send(emails);
```
## 資料庫
在 alpha_admin 加入 last_email_send (如果相隔小於一天就不寄出)
```
alpha_admin{
last_email_send: date
}
```
## 實作
`POST /cron/daily-report`
早上7點時抓 `dashboard_notification` 加入 array
利用 SendGridHelper 加 EmailTemplate 寄出 email
寄出時加入 ` last_email_send: date` 在 `alpha_admin`
寄出失敗 跑 3 次 失敗就不再寄
## 測試
加入 console.log()
寄出日期小於今天的時間