council.ts 2.3 KB

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