feat: update time tooltip.
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
This commit is contained in:
@@ -7,7 +7,7 @@ router.get('/', (req, res) => {
|
||||
const userId = req.user.userId;
|
||||
|
||||
db.all(
|
||||
'SELECT date, status FROM calendar_marks WHERE user_id = ?',
|
||||
'SELECT date, status, updated_at FROM calendar_marks WHERE user_id = ?',
|
||||
[userId],
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
@@ -16,7 +16,10 @@ router.get('/', (req, res) => {
|
||||
|
||||
const markedDates = {};
|
||||
rows.forEach((row) => {
|
||||
markedDates[row.date] = row.status;
|
||||
markedDates[row.date] = {
|
||||
status: row.status,
|
||||
updatedAt: row.updated_at
|
||||
};
|
||||
});
|
||||
|
||||
res.json({ success: true, markedDates });
|
||||
@@ -32,18 +35,54 @@ router.post('/', (req, res) => {
|
||||
return res.status(400).json({ success: false, error: '无效的数据格式' });
|
||||
}
|
||||
|
||||
// Helper function to get current timestamp in ISO format (UTC)
|
||||
const getLocalTimestamp = () => {
|
||||
return new Date().toISOString();
|
||||
};
|
||||
|
||||
const dbOperations = Object.entries(markedDates).map(([date, status]) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
updated_at = CURRENT_TIMESTAMP`,
|
||||
[userId, date, status],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
// First check if this date exists and if status has changed
|
||||
db.get(
|
||||
'SELECT status FROM calendar_marks WHERE user_id = ? AND date = ?',
|
||||
[userId, date],
|
||||
(err, row) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
// Only update timestamp if status changed or it's a new entry
|
||||
const statusChanged = !row || row.status !== status;
|
||||
|
||||
if (statusChanged) {
|
||||
// Status changed - update with new timestamp
|
||||
const currentTimestamp = getLocalTimestamp();
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
updated_at = excluded.updated_at`,
|
||||
[userId, date, status, currentTimestamp],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Status unchanged - keep existing timestamp
|
||||
db.run(
|
||||
`INSERT INTO calendar_marks (user_id, date, status, updated_at)
|
||||
VALUES (?, ?, ?, (SELECT updated_at FROM calendar_marks WHERE user_id = ? AND date = ?))
|
||||
ON CONFLICT(user_id, date) DO UPDATE SET
|
||||
status = excluded.status`,
|
||||
[userId, date, status, userId, date],
|
||||
(err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user