members.ts 3.1 KB

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