Showing
1 changed file
with
28 additions
and
0 deletions
checkAuction.js
0 → 100644
1 | +const { Good, Auction, User, sequelize } = require('./models'); | ||
2 | + | ||
3 | +module.exports = async () => { | ||
4 | + try { | ||
5 | + const yesterday = new Date(); | ||
6 | + yesterday.setDate(yesterday.getDate() - 1); | ||
7 | + const targets = await Good.findAll({ | ||
8 | + where: { | ||
9 | + soldId: null, | ||
10 | + createdAt: { $lte: yesterday }, | ||
11 | + }, | ||
12 | + }); | ||
13 | + targets.forEach(async (target) => { | ||
14 | + const success = await Auction.find({ | ||
15 | + where: { goodId: target.id }, | ||
16 | + order: [['bid', 'DESC']], | ||
17 | + }); | ||
18 | + await Good.update({ soldId: success.userId }, { where: { id: target.id } }); | ||
19 | + await User.update({ | ||
20 | + money: sequelize.literal(`money - ${success.bid}`), | ||
21 | + }, { | ||
22 | + where: { id: success.userId }, | ||
23 | + }); | ||
24 | + }); | ||
25 | + } catch (error) { | ||
26 | + console.error(error); | ||
27 | + } | ||
28 | +}; |
-
Please register or login to post a comment