workdays.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { sequelize } = require('../database')
  2. const moment = require('moment-immutable')
  3. const { Op } = require('sequelize')
  4. const list = async (req, res) => {
  5. const date = req.params.date
  6. ? moment(req.params.date)
  7. : moment(moment.now())
  8. const startDate = date.add(-365, 'days')
  9. const endDate = date.add(365, 'days')
  10. const [results, metadata] = await sequelize.query(`
  11. select distinct date,
  12. (select count(*) from labors where workdays.id = workdayId) as hasLabor,
  13. (select count(*) from services where workdays.id = workdayId) as hasService
  14. from workdays
  15. where laborCost > 0
  16. and date >= :startDate
  17. and date <= :endDate
  18. `, {
  19. replacements: {
  20. startDate: startDate.format('YYYY-MM-DD'),
  21. endDate: endDate.format('YYYY-MM-DD')
  22. }
  23. })
  24. const workdays = results.map(x => ({
  25. date: x.date,
  26. hasLabor: !!x.hasLabor,
  27. hasService: !!x.hasService
  28. }))
  29. res.status(200).send({
  30. startDate: startDate.format('YYYY-MM-DD'),
  31. endDate: endDate.format('YYYY-MM-DD'),
  32. workdays
  33. })
  34. }
  35. module.exports = {
  36. list
  37. }