diff --git a/schema.gql b/schema.gql index 12beb5aa5..b36c9ab72 100644 --- a/schema.gql +++ b/schema.gql @@ -995,6 +995,7 @@ type Mutation { removeBlacklistedCollection(collection: String!): Boolean! removeFeaturedCollection(input: FeaturedCollectionsArgs!): Boolean! removeLike(input: RemoveLikeArgs!): Boolean! + removeWhitelistCollection(input: RemoveWhitelistCollectionArgs!): Boolean! reportCollection( """This endpoint can be used to report a Collection""" input: ReportCollectionInput! @@ -1341,6 +1342,11 @@ input RemoveLikeArgs { identifier: String! } +input RemoveWhitelistCollectionArgs { + collection: String! + marketplaceKey: String! +} + input ReportCollectionInput { collectionIdentifier: String! } diff --git a/src/common/persistence/persistence.service.ts b/src/common/persistence/persistence.service.ts index adc49b9de..543080534 100644 --- a/src/common/persistence/persistence.service.ts +++ b/src/common/persistence/persistence.service.ts @@ -259,6 +259,13 @@ export class PersistenceService { this.marketplaceCollectionsRepository.getMarketplaceByKeyAndCollection(collection, key), ); } + + async getCollectionByKeyAndCollection(collection: string, key: string): Promise { + return await this.execute( + this.getCollectionByKeyAndCollection.name, + this.marketplaceCollectionsRepository.getCollectionByKeyAndCollection(collection, key), + ); + } async getAllMarketplaceCollections(): Promise { return await this.execute(this.getAllMarketplaceCollections.name, this.marketplaceCollectionsRepository.getAllCollections()); } @@ -277,11 +284,12 @@ export class PersistenceService { ); } - async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { - return await this.execute( - this.getCollectionsByMarketplace.name, - this.marketplaceCollectionsRepository.saveMarketplaceCollection(entity), - ); + async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { + return await this.execute(this.saveMarketplaceCollection.name, this.marketplaceCollectionsRepository.saveMarketplaceCollection(entity)); + } + + async deleteMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { + return this.execute(this.deleteMarketplaceCollection.name, this.marketplaceCollectionsRepository.deleteMarketplaceCollection(entity)); } async saveMarketplace(entity: MarketplaceEntity): Promise { diff --git a/src/db/marketplaces/marketplace-collections.repository.ts b/src/db/marketplaces/marketplace-collections.repository.ts index f35017643..cde57cbd7 100644 --- a/src/db/marketplaces/marketplace-collections.repository.ts +++ b/src/db/marketplaces/marketplace-collections.repository.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { DeleteResult, Repository } from 'typeorm'; import { MarketplaceCollectionEntity } from './marketplace-collection.entity'; import { MarketplaceEntity } from './marketplace.entity'; @@ -87,7 +87,27 @@ export class MarketplaceCollectionsRepository { .execute(); } - async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { - return await this.marketplaceCollectionRepository.save(entity); + async getCollectionByIdentifier(collectionIdentifier: string): Promise { + return this.marketplaceCollectionRepository.findOne({ + where: [{ collectionIdentifier: collectionIdentifier }], + }); + } + + async getCollectionByKeyAndCollection(collection: string, key: string): Promise { + return this.marketplaceCollectionRepository + .createQueryBuilder('mc') + .select('mc.*') + .leftJoin('mc.marketplaces', 'm') + .where(`collectionIdentifier = '${collection}' and m.key= '${key}'`) + .execute(); + } + + async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { + const result = await this.marketplaceCollectionRepository.save(entity); + return result ? true : false; + } + + async deleteMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise { + return await this.marketplaceCollectionRepository.remove(entity); } } diff --git a/src/modules/analytics/collections-analytics.service.ts b/src/modules/analytics/collections-analytics.service.ts index fbdc9e105..4f1a71a8b 100644 --- a/src/modules/analytics/collections-analytics.service.ts +++ b/src/modules/analytics/collections-analytics.service.ts @@ -80,7 +80,11 @@ export class CollectionsAnalyticsService { return await this.analyticsGetter.getVolumeDataForTimePeriod(time, series, metric); } - public async getFloorPriceVolumeForTimePeriod(time: string, series: string, metric: string): Promise { + public async getFloorPriceVolumeForTimePeriod( + time: string, + series: string, + metric: string = 'floorPriceUSD', + ): Promise { return await this.analyticsGetter.getFloorPriceForTimePeriod(time, series, metric); } diff --git a/src/modules/marketplaces/marketplaces-mutations.resolver.ts b/src/modules/marketplaces/marketplaces-mutations.resolver.ts index 287051579..7b9440aac 100644 --- a/src/modules/marketplaces/marketplaces-mutations.resolver.ts +++ b/src/modules/marketplaces/marketplaces-mutations.resolver.ts @@ -2,8 +2,8 @@ import { Resolver, Args, Mutation } from '@nestjs/graphql'; import { BaseResolver } from '../common/base.resolver'; import { Marketplace } from './models'; import { MarketplacesService } from './marketplaces.service'; -import { WhitelistCollectionArgs } from './models/WhitelistCollectionArgs'; -import { WhitelistCollectionRequest } from './models/requests/WhitelistCollectionOnMarketplaceRequest'; +import { RemoveWhitelistCollectionArgs, WhitelistCollectionArgs } from './models/WhitelistCollectionArgs'; +import { RemoveWhitelistCollectionRequest, WhitelistCollectionRequest } from './models/requests/WhitelistCollectionOnMarketplaceRequest'; import { WhitelistMarketplaceArgs } from './models/WhitelistMarketplaceArgs'; import { WhitelistMarketplaceRequest } from './models/requests/WhitelistMarketplaceRequest'; import { UseGuards } from '@nestjs/common'; @@ -24,6 +24,12 @@ export class MarketplacesMutationsResolver extends BaseResolver(Marketplace) { return this.marketplaceService.whitelistCollectionOnMarketplace(WhitelistCollectionRequest.fromArgs(input)); } + @Mutation(() => Boolean) + @UseGuards(JwtOrNativeAuthGuard, GqlAdminAuthGuard) + async removeWhitelistCollection(@Args('input') input: RemoveWhitelistCollectionArgs): Promise { + return this.marketplaceService.removeWhitelistCollection(RemoveWhitelistCollectionRequest.fromArgs(input)); + } + @Mutation(() => Boolean) @UseGuards(JwtOrNativeAuthGuard, GqlAdminAuthGuard) async updateMarketplace(@Args('input') input: UpdateMarketplaceArgs): Promise { diff --git a/src/modules/marketplaces/marketplaces.service.ts b/src/modules/marketplaces/marketplaces.service.ts index 763b5a7cd..eea5e546a 100644 --- a/src/modules/marketplaces/marketplaces.service.ts +++ b/src/modules/marketplaces/marketplaces.service.ts @@ -7,7 +7,7 @@ import { MarketplaceCollectionEntity, MarketplaceEntity } from 'src/db/marketpla import { MarketplaceTypeEnum } from './models/MarketplaceType.enum'; import { MarketplaceFilters } from './models/Marketplace.Filter'; import { PersistenceService } from 'src/common/persistence/persistence.service'; -import { WhitelistCollectionRequest } from './models/requests/WhitelistCollectionOnMarketplaceRequest'; +import { RemoveWhitelistCollectionRequest, WhitelistCollectionRequest } from './models/requests/WhitelistCollectionOnMarketplaceRequest'; import { BadRequestError } from 'src/common/models/errors/bad-request-error'; import { WhitelistMarketplaceRequest } from './models/requests/WhitelistMarketplaceRequest'; import { UpdateMarketplaceRequest } from './models/requests/UpdateMarketplaceRequest'; @@ -180,7 +180,7 @@ export class MarketplacesService { if (savedCollection) { this.triggerCacheInvalidation(request.marketplaceKey, request.collection, marketplace.address); } - return savedCollection ? true : false; + return savedCollection; } catch (error) { this.logger.error('An error has occured while whitelisting collection', { path: this.whitelistCollectionOnMarketplace.name, @@ -192,6 +192,32 @@ export class MarketplacesService { } } + async removeWhitelistCollection(request: RemoveWhitelistCollectionRequest): Promise { + const collection = await this.persistenceService.getCollectionByKeyAndCollection(request.collection, request.marketplaceKey); + const marketplace = await this.persistenceService.getMarketplaceByKey(request.marketplaceKey); + + if (!collection || !marketplace) { + throw new BadRequestError('Marketplace not available for this key, choose another key if this is not your marketplace'); + } + try { + const removedCollection = await this.persistenceService.deleteMarketplaceCollection(collection); + + if (removedCollection) { + this.triggerCacheInvalidation(request.marketplaceKey, request.collection, marketplace.address); + } + return removedCollection ? true : false; + } catch (error) { + console.log({ error }); + this.logger.error('An error has occured while remove whitelist for collection', { + path: this.whitelistCollectionOnMarketplace.name, + collection: request?.collection, + marketplace: request?.marketplaceKey, + exception: error, + }); + return false; + } + } + async whitelistMarketplace(request: WhitelistMarketplaceRequest): Promise { const marketplace = await this.persistenceService.getMarketplaceByKey(request.marketplaceKey); if (marketplace) { diff --git a/src/modules/marketplaces/models/WhitelistCollectionArgs.ts b/src/modules/marketplaces/models/WhitelistCollectionArgs.ts index 33a042071..ff3d13f33 100644 --- a/src/modules/marketplaces/models/WhitelistCollectionArgs.ts +++ b/src/modules/marketplaces/models/WhitelistCollectionArgs.ts @@ -14,3 +14,16 @@ export class WhitelistCollectionArgs { @Field() marketplaceKey: string; } + +@InputType() +export class RemoveWhitelistCollectionArgs { + @Matches(RegExp(COLLECTION_IDENTIFIER_RGX), { + message: COLLECTION_IDENTIFIER_ERROR, + }) + @Field() + collection: string; + + @MaxLength(62) + @Field() + marketplaceKey: string; +} diff --git a/src/modules/marketplaces/models/requests/WhitelistCollectionOnMarketplaceRequest.ts b/src/modules/marketplaces/models/requests/WhitelistCollectionOnMarketplaceRequest.ts index 9f726cb2c..51c2f51cb 100644 --- a/src/modules/marketplaces/models/requests/WhitelistCollectionOnMarketplaceRequest.ts +++ b/src/modules/marketplaces/models/requests/WhitelistCollectionOnMarketplaceRequest.ts @@ -1,4 +1,4 @@ -import { WhitelistCollectionArgs } from '../WhitelistCollectionArgs'; +import { RemoveWhitelistCollectionArgs, WhitelistCollectionArgs } from '../WhitelistCollectionArgs'; export class WhitelistCollectionRequest { collection: string; @@ -14,3 +14,18 @@ export class WhitelistCollectionRequest { }); } } + +export class RemoveWhitelistCollectionRequest { + collection: string; + marketplaceKey: string; + constructor(init?: Partial) { + Object.assign(this, init); + } + + static fromArgs(args: RemoveWhitelistCollectionArgs) { + return new RemoveWhitelistCollectionRequest({ + collection: args.collection, + marketplaceKey: args.marketplaceKey, + }); + } +}