|
@@ -18,12 +18,9 @@ import {
|
|
|
StorageDataObject,
|
|
|
StorageSystemParameters,
|
|
|
GeoCoordinates,
|
|
|
- StorageBagDistributionAssignment,
|
|
|
- StorageBagStorageAssignment,
|
|
|
} from 'query-node/dist/model'
|
|
|
import BN from 'bn.js'
|
|
|
-import { getById } from '../common'
|
|
|
-import { In } from 'typeorm'
|
|
|
+import { getById, inconsistentState } from '../common'
|
|
|
import {
|
|
|
processDistributionBucketFamilyMetadata,
|
|
|
processDistributionOperatorMetadata,
|
|
@@ -141,25 +138,15 @@ export async function storage_StorageBucketsUpdatedForBag({
|
|
|
event,
|
|
|
store,
|
|
|
}: EventContext & StoreContext): Promise<void> {
|
|
|
- const [bagId, addedBucketsIds, removedBucketsIds] = new Storage.StorageBucketsUpdatedForBagEvent(event).params
|
|
|
+ const [bagId, addedBucketsSet, removedBucketsSet] = new Storage.StorageBucketsUpdatedForBagEvent(event).params
|
|
|
// Get or create bag
|
|
|
- const storageBag = await getBag(store, bagId)
|
|
|
- const assignmentsToRemove = await store.getMany(StorageBagStorageAssignment, {
|
|
|
- where: {
|
|
|
- storageBag,
|
|
|
- storageBucket: { id: In(Array.from(removedBucketsIds).map((bucketId) => bucketId.toString())) },
|
|
|
- },
|
|
|
- })
|
|
|
- const assignmentsToAdd = Array.from(addedBucketsIds).map(
|
|
|
- (bucketId) =>
|
|
|
- new StorageBagStorageAssignment({
|
|
|
- id: `${storageBag.id}-${bucketId.toString()}`,
|
|
|
- storageBag,
|
|
|
- storageBucket: new StorageBucket({ id: bucketId.toString() }),
|
|
|
- })
|
|
|
- )
|
|
|
- await Promise.all(assignmentsToRemove.map((a) => store.remove<StorageBagStorageAssignment>(a)))
|
|
|
- await Promise.all(assignmentsToAdd.map((a) => store.save<StorageBagStorageAssignment>(a)))
|
|
|
+ const storageBag = await getBag(store, bagId, ['storageBuckets'])
|
|
|
+ const removedBucketsIds = Array.from(removedBucketsSet).map((id) => id.toString())
|
|
|
+ const addedBucketsIds = Array.from(addedBucketsSet).map((id) => id.toString())
|
|
|
+ storageBag.storageBuckets = (storageBag.storageBuckets || [])
|
|
|
+ .filter((bucket) => !removedBucketsIds.includes(bucket.id))
|
|
|
+ .concat(addedBucketsIds.map((id) => new StorageBucket({ id })))
|
|
|
+ await store.save<StorageBag>(storageBag)
|
|
|
}
|
|
|
|
|
|
export async function storage_VoucherChanged({ event, store }: EventContext & StoreContext): Promise<void> {
|
|
@@ -189,11 +176,21 @@ export async function storage_StorageBucketVoucherLimitsSet({
|
|
|
export async function storage_StorageBucketDeleted({ event, store }: EventContext & StoreContext): Promise<void> {
|
|
|
const [bucketId] = new Storage.StorageBucketDeletedEvent(event).params
|
|
|
// TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
|
|
|
- const assignments = await store.getMany(StorageBagStorageAssignment, {
|
|
|
- where: { storageBucket: { id: bucketId.toString() } },
|
|
|
+ const storageBucket = await store.get(StorageBucket, {
|
|
|
+ where: { id: bucketId.toString() },
|
|
|
+ relations: ['bags', 'bags.storageBuckets'],
|
|
|
})
|
|
|
- await Promise.all(assignments.map((a) => store.remove<StorageBagStorageAssignment>(a)))
|
|
|
- await store.remove<StorageBucket>(new StorageBucket({ id: bucketId.toString() }))
|
|
|
+ if (!storageBucket) {
|
|
|
+ inconsistentState(`Storage bucket by id ${bucketId.toString()} not found!`)
|
|
|
+ }
|
|
|
+ // Remove relations
|
|
|
+ await Promise.all(
|
|
|
+ (storageBucket.bags || []).map((bag) => {
|
|
|
+ bag.storageBuckets = (bag.storageBuckets || []).filter((bucket) => bucket.id !== bucketId.toString())
|
|
|
+ return store.save<StorageBag>(bag)
|
|
|
+ })
|
|
|
+ )
|
|
|
+ await store.remove<StorageBucket>(storageBucket)
|
|
|
}
|
|
|
|
|
|
// DYNAMIC BAGS
|
|
@@ -202,36 +199,17 @@ export async function storage_DynamicBagCreated({ event, store }: EventContext &
|
|
|
const storageBag = new StorageBag({
|
|
|
id: getDynamicBagId(bagId),
|
|
|
owner: getDynamicBagOwner(bagId),
|
|
|
+ storageBuckets: Array.from(storageBucketIdsSet).map((id) => new StorageBucket({ id: id.toString() })),
|
|
|
+ distributionBuckets: Array.from(distributionBucketIdsSet).map(
|
|
|
+ (id) => new DistributionBucket({ id: id.toString() })
|
|
|
+ ),
|
|
|
})
|
|
|
- const storageAssignments = Array.from(storageBucketIdsSet).map(
|
|
|
- (bucketId) =>
|
|
|
- new StorageBagStorageAssignment({
|
|
|
- id: `${storageBag.id}-${bucketId.toString()}`,
|
|
|
- storageBag,
|
|
|
- storageBucket: new StorageBucket({ id: bucketId.toString() }),
|
|
|
- })
|
|
|
- )
|
|
|
- const distributionAssignments = Array.from(distributionBucketIdsSet).map(
|
|
|
- (bucketId) =>
|
|
|
- new StorageBagDistributionAssignment({
|
|
|
- id: `${storageBag.id}-${bucketId.toString()}`,
|
|
|
- storageBag,
|
|
|
- distributionBucket: new DistributionBucket({ id: bucketId.toString() }),
|
|
|
- })
|
|
|
- )
|
|
|
await store.save<StorageBag>(storageBag)
|
|
|
- await Promise.all(storageAssignments.map((a) => store.save<StorageBagStorageAssignment>(a)))
|
|
|
- await Promise.all(distributionAssignments.map((a) => store.save<StorageBagDistributionAssignment>(a)))
|
|
|
}
|
|
|
|
|
|
export async function storage_DynamicBagDeleted({ event, store }: EventContext & StoreContext): Promise<void> {
|
|
|
const [, bagId] = new Storage.DynamicBagDeletedEvent(event).params
|
|
|
const storageBag = await getDynamicBag(store, bagId, ['objects'])
|
|
|
- // TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
|
|
|
- const storageAssignments = await store.getMany(StorageBagStorageAssignment, { where: { storageBag } })
|
|
|
- const distributionAssignments = await store.getMany(StorageBagDistributionAssignment, { where: { storageBag } })
|
|
|
- await Promise.all(storageAssignments.map((a) => store.remove<StorageBagStorageAssignment>(a)))
|
|
|
- await Promise.all(distributionAssignments.map((a) => store.remove<StorageBagDistributionAssignment>(a)))
|
|
|
await store.remove<StorageBag>(storageBag)
|
|
|
}
|
|
|
|
|
@@ -341,36 +319,36 @@ export async function storage_DistributionBucketStatusUpdated({
|
|
|
export async function storage_DistributionBucketDeleted({ event, store }: EventContext & StoreContext): Promise<void> {
|
|
|
const [, bucketId] = new Storage.DistributionBucketDeletedEvent(event).params
|
|
|
// TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
|
|
|
- const assignments = await store.getMany(StorageBagDistributionAssignment, {
|
|
|
- where: { distributionBucket: { id: bucketId.toString() } },
|
|
|
+ const distributionBucket = await store.get(DistributionBucket, {
|
|
|
+ where: { id: bucketId.toString() },
|
|
|
+ relations: ['bags', 'bags.distributionBuckets'],
|
|
|
})
|
|
|
- await Promise.all(assignments.map((a) => store.remove<StorageBagDistributionAssignment>(a)))
|
|
|
- await store.remove<DistributionBucket>(new DistributionBucket({ id: bucketId.toString() }))
|
|
|
+ if (!distributionBucket) {
|
|
|
+ inconsistentState(`Distribution bucket by id ${bucketId.toString()} not found!`)
|
|
|
+ }
|
|
|
+ // Remove relations
|
|
|
+ await Promise.all(
|
|
|
+ (distributionBucket.bags || []).map((bag) => {
|
|
|
+ bag.distributionBuckets = (bag.distributionBuckets || []).filter((bucket) => bucket.id !== bucketId.toString())
|
|
|
+ return store.save<StorageBag>(bag)
|
|
|
+ })
|
|
|
+ )
|
|
|
+ await store.remove<DistributionBucket>(distributionBucket)
|
|
|
}
|
|
|
|
|
|
export async function storage_DistributionBucketsUpdatedForBag({
|
|
|
event,
|
|
|
store,
|
|
|
}: EventContext & StoreContext): Promise<void> {
|
|
|
- const [bagId, , addedBucketsIds, removedBucketsIds] = new Storage.DistributionBucketsUpdatedForBagEvent(event).params
|
|
|
+ const [bagId, , addedBucketsSet, removedBucketsSet] = new Storage.DistributionBucketsUpdatedForBagEvent(event).params
|
|
|
// Get or create bag
|
|
|
- const storageBag = await getBag(store, bagId)
|
|
|
- const assignmentsToRemove = await store.getMany(StorageBagDistributionAssignment, {
|
|
|
- where: {
|
|
|
- storageBag,
|
|
|
- distributionBucket: { id: In(Array.from(removedBucketsIds).map((bucketId) => bucketId.toString())) },
|
|
|
- },
|
|
|
- })
|
|
|
- const assignmentsToAdd = Array.from(addedBucketsIds).map(
|
|
|
- (bucketId) =>
|
|
|
- new StorageBagDistributionAssignment({
|
|
|
- id: `${storageBag.id}-${bucketId.toString()}`,
|
|
|
- storageBag,
|
|
|
- distributionBucket: new DistributionBucket({ id: bucketId.toString() }),
|
|
|
- })
|
|
|
- )
|
|
|
- await Promise.all(assignmentsToRemove.map((a) => store.remove<StorageBagDistributionAssignment>(a)))
|
|
|
- await Promise.all(assignmentsToAdd.map((a) => store.save<StorageBagDistributionAssignment>(a)))
|
|
|
+ const storageBag = await getBag(store, bagId, ['distributionBuckets'])
|
|
|
+ const removedBucketsIds = Array.from(removedBucketsSet).map((id) => id.toString())
|
|
|
+ const addedBucketsIds = Array.from(addedBucketsSet).map((id) => id.toString())
|
|
|
+ storageBag.distributionBuckets = (storageBag.distributionBuckets || [])
|
|
|
+ .filter((bucket) => !removedBucketsIds.includes(bucket.id))
|
|
|
+ .concat(addedBucketsIds.map((id) => new DistributionBucket({ id })))
|
|
|
+ await store.save<StorageBag>(storageBag)
|
|
|
}
|
|
|
|
|
|
export async function storage_DistributionBucketModeUpdated({
|