curatorGroup.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. eslint-disable @typescript-eslint/naming-convention
  3. */
  4. import { EventContext, StoreContext } from '@joystream/hydra-common'
  5. import { FindConditions } from 'typeorm'
  6. import { CuratorGroup } from 'query-node/dist/model'
  7. import { Content } from '../generated/types'
  8. import { inconsistentState, logger } from '../common'
  9. export async function content_CuratorGroupCreated({ store, event }: EventContext & StoreContext): Promise<void> {
  10. // read event data
  11. const [curatorGroupId] = new Content.CuratorGroupCreatedEvent(event).params
  12. // create new curator group
  13. const curatorGroup = new CuratorGroup({
  14. // main data
  15. id: curatorGroupId.toString(),
  16. curatorIds: [],
  17. isActive: false, // runtime creates inactive curator groups by default
  18. // fill in auto-generated fields
  19. createdAt: new Date(event.blockTimestamp),
  20. updatedAt: new Date(event.blockTimestamp),
  21. })
  22. // save curator group
  23. await store.save<CuratorGroup>(curatorGroup)
  24. // emit log event
  25. logger.info('Curator group has been created', { id: curatorGroupId })
  26. }
  27. export async function content_CuratorGroupStatusSet({ store, event }: EventContext & StoreContext): Promise<void> {
  28. // read event data
  29. const [curatorGroupId, isActive] = new Content.CuratorGroupStatusSetEvent(event).params
  30. // load curator group
  31. const curatorGroup = await store.get(CuratorGroup, {
  32. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  33. })
  34. // ensure curator group exists
  35. if (!curatorGroup) {
  36. return inconsistentState('Non-existing curator group status set requested', curatorGroupId)
  37. }
  38. // update curator group
  39. curatorGroup.isActive = isActive.isTrue
  40. // set last update time
  41. curatorGroup.updatedAt = new Date(event.blockTimestamp)
  42. // save curator group
  43. await store.save<CuratorGroup>(curatorGroup)
  44. // emit log event
  45. logger.info('Curator group status has been set', { id: curatorGroupId, isActive })
  46. }
  47. export async function content_CuratorAdded({ store, event }: EventContext & StoreContext): Promise<void> {
  48. // read event data
  49. const [curatorGroupId, curatorId] = new Content.CuratorAddedEvent(event).params
  50. // load curator group
  51. const curatorGroup = await store.get(CuratorGroup, {
  52. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  53. })
  54. // ensure curator group exists
  55. if (!curatorGroup) {
  56. return inconsistentState('Curator add to non-existing curator group requested', curatorGroupId)
  57. }
  58. // update curator group
  59. curatorGroup.curatorIds.push(curatorId.toNumber())
  60. // set last update time
  61. curatorGroup.updatedAt = new Date(event.blockTimestamp)
  62. // save curator group
  63. await store.save<CuratorGroup>(curatorGroup)
  64. // emit log event
  65. logger.info('Curator has been added to curator group', { id: curatorGroupId, curatorId })
  66. }
  67. export async function content_CuratorRemoved({ store, event }: EventContext & StoreContext): Promise<void> {
  68. // read event data
  69. const [curatorGroupId, curatorId] = new Content.CuratorAddedEvent(event).params
  70. // load curator group
  71. const curatorGroup = await store.get(CuratorGroup, {
  72. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  73. })
  74. // ensure curator group exists
  75. if (!curatorGroup) {
  76. return inconsistentState('Non-existing curator group removal requested', curatorGroupId)
  77. }
  78. const curatorIndex = curatorGroup.curatorIds.indexOf(curatorId.toNumber())
  79. // ensure curator group exists
  80. if (curatorIndex < 0) {
  81. return inconsistentState('Non-associated curator removal from curator group requested', curatorId)
  82. }
  83. // update curator group
  84. curatorGroup.curatorIds.splice(curatorIndex, 1)
  85. // save curator group
  86. await store.save<CuratorGroup>(curatorGroup)
  87. // emit log event
  88. logger.info('Curator has been removed from curator group', { id: curatorGroupId, curatorId })
  89. }