1
0

members.ts 3.1 KB

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