Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nftopia-backend/src/graphql/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { JwtStrategy } from '../auth/jwt.strategy';
import { GqlAuthGuard } from '../common/guards/gql-auth.guard';
import { CollectionModule } from '../modules/collection/collection.module';
import { NftModule } from '../modules/nft/nft.module';
import { ListingModule } from '../modules/listing/listing.module';
import { AuctionModule } from '../modules/auction/auction.module';
import { OrderModule } from '../modules/order/order.module';
import { UsersModule } from '../users/users.module';
import { SearchModule } from '../search/search.module';
import { GraphqlContextFactory } from './context/context.factory';
import { GraphqlAuthMiddleware } from './middleware/auth.middleware';
Expand Down Expand Up @@ -49,6 +53,10 @@ const jwtAccessExpiresInSeconds = parseInt(
}),
CollectionModule,
NftModule,
ListingModule,
AuctionModule,
OrderModule,
UsersModule,
SearchModule,
],
providers: [
Expand Down
76 changes: 76 additions & 0 deletions nftopia-backend/src/graphql/inputs/auction.inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Field, Float, ID, InputType } from '@nestjs/graphql';
import {
IsEnum,
IsNumber,
IsOptional,
IsString,
IsUUID,
Min,
} from 'class-validator';
import { Type } from 'class-transformer';
import { AuctionStatus } from '../../modules/auction/interfaces/auction.interface';

@InputType()
export class AuctionFilterInput {
@Field(() => ID, { nullable: true })
@IsOptional()
@IsUUID()
sellerId?: string;

@Field(() => String, { nullable: true })
@IsOptional()
@IsString()
nftContractId?: string;

@Field(() => String, { nullable: true })
@IsOptional()
@IsString()
nftTokenId?: string;

@Field(() => AuctionStatus, { nullable: true })
@IsOptional()
@IsEnum(AuctionStatus)
status?: AuctionStatus;
}

@InputType()
export class CreateAuctionInput {
@Field()
@IsString()
nftContractId: string;

@Field()
@IsString()
nftTokenId: string;

@Field(() => Float)
@Type(() => Number)
@IsNumber({ maxDecimalPlaces: 7 })
@Min(0.0000001)
startPrice: number;

@Field(() => Float, { nullable: true })
@IsOptional()
@Type(() => Number)
@IsNumber({ maxDecimalPlaces: 7 })
@Min(0.0000001)
reservePrice?: number;

@Field({ nullable: true })
@IsOptional()
@IsString()
startTime?: string;

@Field()
@IsString()
endTime: string;
}

@InputType()
export class PlaceBidInput {
@Field(() => Float)
@Type(() => Number)
@IsNumber({ maxDecimalPlaces: 7 })
@Min(0.0000001)
amount: number;
}
61 changes: 61 additions & 0 deletions nftopia-backend/src/graphql/inputs/listing.inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Field, Float, ID, InputType } from '@nestjs/graphql';
import {
IsEnum,
IsNumber,
IsOptional,
IsString,
IsUUID,
Min,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ListingStatus } from '../../modules/listing/interfaces/listing.interface';

@InputType()
export class ListingFilterInput {
@Field(() => ID, { nullable: true })
@IsOptional()
@IsUUID()
sellerId?: string;

@Field(() => String, { nullable: true })
@IsOptional()
@IsString()
nftContractId?: string;

@Field(() => String, { nullable: true })
@IsOptional()
@IsString()
nftTokenId?: string;

@Field(() => ListingStatus, { nullable: true })
@IsOptional()
@IsEnum(ListingStatus)
status?: ListingStatus;
}

@InputType()
export class CreateListingInput {
@Field()
@IsString()
nftContractId: string;

@Field()
@IsString()
nftTokenId: string;

@Field(() => Float)
@Type(() => Number)
@IsNumber({ maxDecimalPlaces: 7 })
@Min(0.0000001)
price: number;

@Field({ nullable: true })
@IsOptional()
@IsString()
currency?: string;

@Field({ nullable: true })
@IsOptional()
@IsString()
expiresAt?: string;
}
34 changes: 34 additions & 0 deletions nftopia-backend/src/graphql/inputs/order.inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Field, ID, InputType } from '@nestjs/graphql';
import { IsEnum, IsOptional, IsUUID } from 'class-validator';
import {
OrderStatus,
OrderType,
} from '../../modules/order/dto/create-order.dto';

@InputType()
export class OrderFilterInput {
@Field(() => ID, { nullable: true })
@IsOptional()
@IsUUID()
nftId?: string;

@Field(() => ID, { nullable: true })
@IsOptional()
@IsUUID()
buyerId?: string;

@Field(() => ID, { nullable: true })
@IsOptional()
@IsUUID()
sellerId?: string;

@Field(() => OrderType, { nullable: true })
@IsOptional()
@IsEnum(OrderType)
type?: OrderType;

@Field(() => OrderStatus, { nullable: true })
@IsOptional()
@IsEnum(OrderStatus)
status?: OrderStatus;
}
111 changes: 111 additions & 0 deletions nftopia-backend/src/graphql/resolvers/auction.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AuctionResolver } from './auction.resolver';
import { AuctionService } from '../../modules/auction/auction.service';
import { AuctionStatus } from '../../modules/auction/interfaces/auction.interface';

const mockAuctionService = {
findOne: jest.fn(),
findAll: jest.fn(),
create: jest.fn(),
cancelAuction: jest.fn(),
placeBid: jest.fn(),
};

const baseAuction = {
id: 'auction-1',
nftContractId: 'C'.repeat(56),
nftTokenId: 'token-1',
sellerId: 'seller-1',
startPrice: 5.0,
currentPrice: 5.0,
reservePrice: undefined,
startTime: new Date('2026-03-20T10:00:00.000Z'),
endTime: new Date('2026-03-25T10:00:00.000Z'),
status: AuctionStatus.ACTIVE,
winnerId: undefined,
createdAt: new Date('2026-03-20T10:00:00.000Z'),
updatedAt: new Date('2026-03-20T10:00:00.000Z'),
};

describe('AuctionResolver', () => {
let resolver: AuctionResolver;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AuctionResolver,
{ provide: AuctionService, useValue: mockAuctionService },
],
}).compile();

resolver = module.get<AuctionResolver>(AuctionResolver);
jest.clearAllMocks();
});

it('returns a single auction by id', async () => {
mockAuctionService.findOne.mockResolvedValue(baseAuction);

const result = await resolver.auction('auction-1');

expect(mockAuctionService.findOne).toHaveBeenCalledWith('auction-1');
expect(result.id).toBe('auction-1');
expect(result.status).toBe(AuctionStatus.ACTIVE);
});

it('returns an auction connection from findAll', async () => {
mockAuctionService.findAll.mockResolvedValue([baseAuction]);

const result = await resolver.auctions({ first: 5 }, { status: AuctionStatus.ACTIVE });

expect(result.edges).toHaveLength(1);
expect(result.totalCount).toBe(1);
expect(result.edges[0].cursor).toEqual(expect.any(String));
});

it('creates an auction for authenticated caller', async () => {
mockAuctionService.create.mockResolvedValue(baseAuction);

const result = await resolver.createAuction(
{
nftContractId: 'C'.repeat(56),
nftTokenId: 'token-1',
startPrice: 5.0,
endTime: '2026-03-25T10:00:00.000Z',
},
{ req: {} as never, res: {} as never, user: { userId: 'seller-1' } },
);

expect(mockAuctionService.create).toHaveBeenCalledWith(
expect.objectContaining({ startPrice: 5.0 }),
'seller-1',
);
expect(result.sellerId).toBe('seller-1');
});

it('rejects createAuction when unauthenticated', async () => {
await expect(
resolver.createAuction(
{ nftContractId: 'C'.repeat(56), nftTokenId: 'token-1', startPrice: 5.0, endTime: '2026-03-25T10:00:00.000Z' },
{ req: {} as never, res: {} as never },
),
).rejects.toThrow(UnauthorizedException);
});

it('places a bid and returns updated auction', async () => {
mockAuctionService.placeBid.mockResolvedValue({});
mockAuctionService.findOne.mockResolvedValue({
...baseAuction,
currentPrice: 12.0,
});

const result = await resolver.placeBid(
'auction-1',
{ amount: 12.0 },
{ req: {} as never, res: {} as never, user: { userId: 'bidder-1' } },
);

expect(mockAuctionService.placeBid).toHaveBeenCalledWith('auction-1', 'bidder-1', { amount: 12.0 });
expect(result.currentPrice).toBe(12.0);
});
});
Loading
Loading