proposal.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const Proposal = db.define('proposal', {
  4. id: {
  5. type: DataTypes.INTEGER,
  6. primaryKey: true,
  7. },
  8. createdAt: DataTypes.INTEGER,
  9. finalizedAt: DataTypes.INTEGER,
  10. title: DataTypes.STRING,
  11. type: DataTypes.STRING,
  12. stage: DataTypes.STRING,
  13. result: DataTypes.STRING,
  14. executed: DataTypes.STRING,
  15. parameters: DataTypes.STRING,
  16. description: DataTypes.TEXT,
  17. })
  18. Proposal.findAllWithIncludes = function () {
  19. return this.findAll({
  20. include: [
  21. { association: 'author', attributes: ['handle'] },
  22. {
  23. association: 'posts',
  24. include: [{ association: 'author', attributes: ['handle'] }],
  25. },
  26. {
  27. association: 'votes',
  28. attributes: ['id', 'vote'],
  29. include: [{ model: db.models.member, attributes: ['id', 'handle'] }],
  30. },
  31. ],
  32. })
  33. }
  34. Proposal.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  35. return this.findByPk(id, {
  36. ...args,
  37. include: [
  38. { association: 'author', attributes: ['handle'] },
  39. {
  40. association: 'posts',
  41. include: [{ association: 'author', attributes: ['handle'] }],
  42. },
  43. {
  44. association: 'votes',
  45. attributes: ['id', 'vote'],
  46. include: [{ model: db.models.member, attributes: ['id', 'handle'] }],
  47. },
  48. ],
  49. })
  50. }
  51. Proposal.findWithIncludes = function (args: { where: any }) {
  52. return this.findAll({
  53. ...args,
  54. include: [
  55. { association: 'author', attributes: ['handle'] },
  56. {
  57. association: 'posts',
  58. include: [{ association: 'author', attributes: ['handle'] }],
  59. },
  60. {
  61. association: 'votes',
  62. attributes: ['id', 'vote'],
  63. include: [{ association: 'author', attributes: ['id', 'handle'] }],
  64. },
  65. ],
  66. })
  67. }
  68. export default Proposal