This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const fs = require('fs'); | |
const path = require('path'); | |
const Sequelize = require('sequelize'); | |
const basename = path.basename(__filename); | |
const config = require('<path-to-sequelize-config>'); | |
const db = {}; | |
const sequelize = new Sequelize(config.url, config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cars = await db.sequelize.query('SELECT "id" FROM Cars WHERE "Cars"."ownerId" = ?', { | |
replacements: ['active'], type: sequelize.QueryTypes.SELECT | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// passing the model type to return the result as the instance of the model | |
currentMonthEarnings = await db.sequelize.query('SELECT SUM("priceEstimate") FROM "Orders" WHERE "assignedCarId" IN(:ids) AND "createdAt" < (:createdDate)', { | |
replacements: {ids: carIds, createdDate: currentDateMonth}, | |
model: db.Order, | |
mapToModel: true | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import db from '../../models'; | |
import moment from 'moment'; | |
const getAllUserEarnings = async (req, res) => { | |
// get the beginning of the current month | |
const currentDateMonth = moment().startOf('month').format(); | |
let cars, carIds, totalEarnings, currentMonthEarnings; | |
// Use raw SQL queries to select all cars which belongs to the user | |
cars = await db.sequelize.query('SELECT "id" FROM Cars" WHERE "Cars"."ownerId" = (:id)', { |
NewerOlder