Skip to content

Commit

Permalink
Merge pull request #1048 from multiversx/SERVICES-1887-add-remove-whi…
Browse files Browse the repository at this point in the history
…telist-collection

Add remove whitelist collection
  • Loading branch information
danielailie authored Oct 25, 2023
2 parents 606bf50 + fa4e938 commit eef4e03
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 15 deletions.
6 changes: 6 additions & 0 deletions schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -1341,6 +1342,11 @@ input RemoveLikeArgs {
identifier: String!
}

input RemoveWhitelistCollectionArgs {
collection: String!
marketplaceKey: String!
}

input ReportCollectionInput {
collectionIdentifier: String!
}
Expand Down
18 changes: 13 additions & 5 deletions src/common/persistence/persistence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ export class PersistenceService {
this.marketplaceCollectionsRepository.getMarketplaceByKeyAndCollection(collection, key),
);
}

async getCollectionByKeyAndCollection(collection: string, key: string): Promise<MarketplaceCollectionEntity> {
return await this.execute(
this.getCollectionByKeyAndCollection.name,
this.marketplaceCollectionsRepository.getCollectionByKeyAndCollection(collection, key),
);
}
async getAllMarketplaceCollections(): Promise<MarketplaceCollectionEntity[]> {
return await this.execute(this.getAllMarketplaceCollections.name, this.marketplaceCollectionsRepository.getAllCollections());
}
Expand All @@ -277,11 +284,12 @@ export class PersistenceService {
);
}

async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<MarketplaceCollectionEntity> {
return await this.execute(
this.getCollectionsByMarketplace.name,
this.marketplaceCollectionsRepository.saveMarketplaceCollection(entity),
);
async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<boolean> {
return await this.execute(this.saveMarketplaceCollection.name, this.marketplaceCollectionsRepository.saveMarketplaceCollection(entity));
}

async deleteMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<MarketplaceCollectionEntity> {
return this.execute(this.deleteMarketplaceCollection.name, this.marketplaceCollectionsRepository.deleteMarketplaceCollection(entity));
}

async saveMarketplace(entity: MarketplaceEntity): Promise<MarketplaceEntity> {
Expand Down
26 changes: 23 additions & 3 deletions src/db/marketplaces/marketplace-collections.repository.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -87,7 +87,27 @@ export class MarketplaceCollectionsRepository {
.execute();
}

async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<MarketplaceCollectionEntity> {
return await this.marketplaceCollectionRepository.save(entity);
async getCollectionByIdentifier(collectionIdentifier: string): Promise<MarketplaceCollectionEntity> {
return this.marketplaceCollectionRepository.findOne({
where: [{ collectionIdentifier: collectionIdentifier }],
});
}

async getCollectionByKeyAndCollection(collection: string, key: string): Promise<MarketplaceCollectionEntity> {
return this.marketplaceCollectionRepository
.createQueryBuilder('mc')
.select('mc.*')
.leftJoin('mc.marketplaces', 'm')
.where(`collectionIdentifier = '${collection}' and m.key= '${key}'`)
.execute();
}

async saveMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<boolean> {
const result = await this.marketplaceCollectionRepository.save(entity);
return result ? true : false;
}

async deleteMarketplaceCollection(entity: MarketplaceCollectionEntity): Promise<MarketplaceCollectionEntity> {
return await this.marketplaceCollectionRepository.remove(entity);
}
}
6 changes: 5 additions & 1 deletion src/modules/analytics/collections-analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnalyticsAggregateValue[]> {
public async getFloorPriceVolumeForTimePeriod(
time: string,
series: string,
metric: string = 'floorPriceUSD',
): Promise<AnalyticsAggregateValue[]> {
return await this.analyticsGetter.getFloorPriceForTimePeriod(time, series, metric);
}

Expand Down
10 changes: 8 additions & 2 deletions src/modules/marketplaces/marketplaces-mutations.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Boolean> {
return this.marketplaceService.removeWhitelistCollection(RemoveWhitelistCollectionRequest.fromArgs(input));
}

@Mutation(() => Boolean)
@UseGuards(JwtOrNativeAuthGuard, GqlAdminAuthGuard)
async updateMarketplace(@Args('input') input: UpdateMarketplaceArgs): Promise<Boolean> {
Expand Down
29 changes: 27 additions & 2 deletions src/modules/marketplaces/marketplaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -192,6 +192,31 @@ export class MarketplacesService {
}
}

async removeWhitelistCollection(request: RemoveWhitelistCollectionRequest): Promise<Boolean> {
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) {
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<Boolean> {
const marketplace = await this.persistenceService.getMarketplaceByKey(request.marketplaceKey);
if (marketplace) {
Expand Down
13 changes: 13 additions & 0 deletions src/modules/marketplaces/models/WhitelistCollectionArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WhitelistCollectionArgs } from '../WhitelistCollectionArgs';
import { RemoveWhitelistCollectionArgs, WhitelistCollectionArgs } from '../WhitelistCollectionArgs';

export class WhitelistCollectionRequest {
collection: string;
Expand All @@ -14,3 +14,18 @@ export class WhitelistCollectionRequest {
});
}
}

export class RemoveWhitelistCollectionRequest {
collection: string;
marketplaceKey: string;
constructor(init?: Partial<RemoveWhitelistCollectionRequest>) {
Object.assign(this, init);
}

static fromArgs(args: RemoveWhitelistCollectionArgs) {
return new RemoveWhitelistCollectionRequest({
collection: args.collection,
marketplaceKey: args.marketplaceKey,
});
}
}
60 changes: 59 additions & 1 deletion src/modules/marketplaces/tests/marketplaces.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MarketplaceCollectionEntity, MarketplaceEntity } from 'src/db/marketpla
import { MarketplaceTypeEnum } from '../models/MarketplaceType.enum';
import { MarketplaceFilters } from '../models/Marketplace.Filter';
import { Marketplace } from '../models';
import { WhitelistCollectionRequest } from '../models/requests/WhitelistCollectionOnMarketplaceRequest';
import { RemoveWhitelistCollectionRequest, WhitelistCollectionRequest } from '../models/requests/WhitelistCollectionOnMarketplaceRequest';
import { BadRequestError } from 'src/common/models/errors/bad-request-error';
import { Logger } from '@nestjs/common';
import { WhitelistMarketplaceRequest } from '../models/requests/WhitelistMarketplaceRequest';
Expand Down Expand Up @@ -738,6 +738,64 @@ describe('Marketplaces Service', () => {
});
});

describe('removeWhitelistCollection', () => {
it('when marketplace not found throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(null);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

await expect(service.removeWhitelistCollection(new RemoveWhitelistCollectionRequest())).rejects.toThrowError(BadRequestError);
});

it('when collection not found throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(null);

await expect(service.removeWhitelistCollection(new RemoveWhitelistCollectionRequest())).rejects.toThrowError(BadRequestError);
});

it('when marketplace exists and delete fails returns false', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

persistenceService.deleteMarketplaceCollection = jest.fn(() => {
throw new Error();
});

const expectedResult = await service.removeWhitelistCollection(
new RemoveWhitelistCollectionRequest({ marketplaceKey: 'xoxno', collection: 'identifier' }),
);

expect(expectedResult).toBeFalsy();
});

it('when marketplace exists and save is succesfull returns true', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);
const eventPublisher = module.get<CacheEventsPublisherService>(CacheEventsPublisherService);

eventPublisher.publish = jest.fn();
persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

persistenceService.deleteMarketplaceCollection = jest.fn().mockReturnValueOnce(
new MarketplaceCollectionEntity({
collectionIdentifier: 'collection',
marketplaces: [inputMarketplace[0]],
}),
);
const expectedResult = await service.removeWhitelistCollection(
new RemoveWhitelistCollectionRequest({ marketplaceKey: 'xoxno', collection: 'identifier' }),
);

expect(expectedResult).toBeTruthy();
});
});

describe('whitelistMarketplace', () => {
it('when marketplace key exists throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);
Expand Down

0 comments on commit eef4e03

Please sign in to comment.