서예진

checkAuction.js

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 +};