3
1

members.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const router = require('express').Router()
  2. import {
  3. Member,
  4. Post,
  5. Proposal,
  6. Council,
  7. Consul,
  8. ConsulStake,
  9. ProposalVote,
  10. } from '../db/models'
  11. const findMember = (handle: number | string) =>
  12. handle > 0 ? Member.findByPk(handle) : Member.findOne({ where: { handle } })
  13. const findMemberWithIncludes = (handle: number | string) =>
  14. handle > 0
  15. ? Member.findByIdWithIncludes(handle)
  16. : Member.findWithIncludes({ where: { handle } })
  17. router.get('/', async (req: any, res: any, next: any) => {
  18. try {
  19. Member.findAll().then((m: any) => res.json(m))
  20. } catch (err) {
  21. next(err)
  22. }
  23. })
  24. router.get('/:id', async (req: any, res: any, next: any) => {
  25. try {
  26. res.json(await findMemberWithIncludes(req.params.id))
  27. } catch (err) {
  28. next(err)
  29. }
  30. })
  31. router.get('/:id/posts', async (req: any, res: any, next: any) => {
  32. try {
  33. const { id } = await findMember(req.params.id)
  34. if (!id) res.json({})
  35. const posts = await Post.findWithIncludes({ where: { authorId: id } })
  36. res.json(posts)
  37. } catch (err) {
  38. next(err)
  39. }
  40. })
  41. router.get('/:id/proposals', async (req: any, res: any, next: any) => {
  42. try {
  43. const { id } = await findMember(req.params.id)
  44. if (!id) res.json({})
  45. const proposals = await Proposal.findWithIncludes({
  46. where: { authorId: id },
  47. })
  48. res.json(proposals)
  49. } catch (err) {
  50. next(err)
  51. }
  52. })
  53. router.get('/:id/terms', async (req: any, res: any, next: any) => {
  54. try {
  55. const { id } = await findMember(req.params.id)
  56. if (!id) res.json({})
  57. const terms = await Consul.findWithIncludes({ where: { memberId: id } })
  58. res.json(terms)
  59. } catch (err) {
  60. next(err)
  61. }
  62. })
  63. router.get('/:id/votes', async (req: any, res: any, next: any) => {
  64. try {
  65. const member = await findMember(req.params.id)
  66. if (!member) res.json({})
  67. const memberId = member.id
  68. const proposals = await Consul.findAll({
  69. where: { memberId },
  70. include: [
  71. {
  72. association: 'votes',
  73. include: [
  74. {
  75. model: Proposal,
  76. attributes: ['title'],
  77. },
  78. ],
  79. },
  80. ],
  81. })
  82. const councils = await Consul.findAll({
  83. include: [
  84. { model: Member, attributes: ['handle'] },
  85. { association: 'voters', required: true, where: { memberId } },
  86. ],
  87. })
  88. return res.json({ councils, proposals })
  89. } catch (err) {
  90. next(err)
  91. }
  92. })
  93. router.post('/', async (req: any, res: any, next: any) => {
  94. try {
  95. //Member.create(req.body).then((member: any) => res.json(member))
  96. } catch (err) {
  97. next(err)
  98. }
  99. })
  100. router.put('/:id', async (req: any, res: any, next: any) => {
  101. try {
  102. // Member.findByPk(req.params.id).then((member: any) =>
  103. // member.update(req.body).then((m: any) => res.json(m))
  104. // )
  105. } catch (err) {
  106. next(err)
  107. }
  108. })
  109. router.post('/:id/delete', async (req: any, res: any, next: any) => {
  110. try {
  111. //Member.findByPk(req.params.id).then((member:any)=>res.json(member.delete())
  112. } catch (err) {
  113. next(err)
  114. }
  115. })
  116. module.exports = router