council.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const Council = db.define('council', {
  4. round: {
  5. type: DataTypes.INTEGER,
  6. primaryKey: true,
  7. },
  8. start: DataTypes.INTEGER,
  9. startDate: DataTypes.DATE,
  10. end: DataTypes.INTEGER,
  11. endDate: DataTypes.DATE,
  12. })
  13. Council.findAllWithIncludes = function () {
  14. return this.findAll({
  15. include: [
  16. {
  17. model: db.models.consul,
  18. include: [
  19. { model: db.models.member, attributes: ['handle'] },
  20. {
  21. association: 'votes',
  22. include: [{ model: db.models.proposal, attributes: ['title'] }],
  23. },
  24. {
  25. association: 'voters',
  26. include: [{ model: db.models.member, attributes: ['handle'] }],
  27. },
  28. ],
  29. },
  30. ],
  31. })
  32. }
  33. Council.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  34. return this.findByPk(id, {
  35. ...args,
  36. include: [
  37. {
  38. model: db.models.consul,
  39. include: [
  40. { model: db.models.member, attributes: ['handle'] },
  41. {
  42. association: 'votes',
  43. include: [{ model: db.models.proposal, attributes: ['title'] }],
  44. },
  45. {
  46. association: 'voters',
  47. include: [{ model: db.models.member, attributes: ['handle'] }],
  48. },
  49. ],
  50. },
  51. ],
  52. })
  53. }
  54. Council.findWithIncludes = function (args?: { where: any }) {
  55. return this.findAll({
  56. ...args,
  57. include: [
  58. {
  59. model: db.models.consul,
  60. include: [
  61. { model: db.models.member, attributes: ['handle'] },
  62. {
  63. association: 'votes',
  64. include: [{ model: db.models.proposal, attributes: ['title'] }],
  65. },
  66. {
  67. association: 'voters',
  68. include: [{ model: db.models.member, attributes: ['handle'] }],
  69. },
  70. ],
  71. },
  72. ],
  73. })
  74. }
  75. export default Council