Browse Source

Merge pull request #3458 from mnaamani/fix-qn-dist-bucket-removed-handler

patch bucket removed handler to drop invited operators
Mokhtar Naamani 3 years ago
parent
commit
9fd2d3011b
1 changed files with 14 additions and 2 deletions
  1. 14 2
      query-node/mappings/storage/index.ts

+ 14 - 2
query-node/mappings/storage/index.ts

@@ -1,7 +1,7 @@
 /*
 eslint-disable @typescript-eslint/naming-convention
 */
-import { EventContext, StoreContext } from '@joystream/hydra-common'
+import { DatabaseManager, EventContext, StoreContext } from '@joystream/hydra-common'
 import { Storage } from '../generated/types/storage'
 import {
   DistributionBucket,
@@ -411,6 +411,15 @@ export async function storage_DistributionBucketDeleted({ event, store }: EventC
       return store.save<StorageBag>(bag)
     })
   )
+
+  // Remove invited bucket operators
+  const invitedOperators = await store.getMany(DistributionBucketOperator, {
+    where: {
+      status: DistributionBucketOperatorStatus.INVITED,
+      distributionBucket,
+    },
+  })
+  await Promise.all(invitedOperators.map((operator) => removeDistributionBucketOperator(store, operator)))
   await store.remove<DistributionBucket>(distributionBucket)
 }
 
@@ -509,8 +518,11 @@ export async function storage_DistributionBucketOperatorRemoved({
   const [bucketId, workerId] = new Storage.DistributionBucketOperatorRemovedEvent(event).params
 
   // TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
-
   const operator = await getDistributionBucketOperatorWithMetadata(store, distributionOperatorId(bucketId, workerId))
+  await removeDistributionBucketOperator(store, operator)
+}
+
+async function removeDistributionBucketOperator(store: DatabaseManager, operator: DistributionBucketOperator) {
   await store.remove<DistributionBucketOperator>(operator)
   if (operator.metadata) {
     await store.remove<DistributionBucketOperatorMetadata>(operator.metadata)