Skip to content

Commit

Permalink
Merge pull request #87 from SigwoTechnologies/feat/change-account-inf…
Browse files Browse the repository at this point in the history
…ormation

Feat/change account information
  • Loading branch information
AlefrankM authored Jan 15, 2023
2 parents a0a15b4 + 70a6f65 commit 39e12cd
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 116 deletions.
10 changes: 5 additions & 5 deletions src/account/__tests__/account.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const collectionServiceMock = () => ({
});

const accountServiceMock = () => ({
findByAddress: jest.fn(),
findCreatedItemsByAddress: jest.fn(),
changeInformation: jest.fn(),
});

describe('AccountController', () => {
let controller: AccountsController;
let itemService;
let accountService;

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand All @@ -39,7 +39,7 @@ describe('AccountController', () => {
}).compile();

controller = await module.get(AccountsController);
itemService = await module.get(ItemService);
accountService = await module.get(AccountService);
});

describe('When calling findItems function', () => {
Expand Down Expand Up @@ -75,9 +75,9 @@ describe('AccountController', () => {
];

const mockData = expected.map((prop) => ({ ...prop }));
itemService.findByAddress.mockResolvedValue(mockData);
accountService.findCreatedItemsByAddress.mockResolvedValue(mockData);

const actual = await controller.findItems('123', { limit: 15, page: 1 });
const actual = await controller.findCreatedItems('123', { limit: 15, page: 1 });

expect(actual[0].itemId).toEqual(expected[0].itemId);
});
Expand Down
23 changes: 16 additions & 7 deletions src/account/controllers/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ export class AccountsController {
) {}

@IsAddressValid()
@Get('/:address/items')
findItems(
@Param('address') address: string,
@Query() paginationDto: PaginationDto
): Promise<Item[]> {
return this.itemService.findByAddress(address, paginationDto);
@Get('/:address/items/created')
findCreatedItems(@Param('address') address: string, @Query() paginationDto: PaginationDto) {
return this.accountService.findCreatedItemsByAddress(address, paginationDto);
}

@IsAddressValid()
@Get('/:address/items/owned')
ownedItems(@Param('address') address: string, @Query() paginationDto: PaginationDto) {
return this.accountService.findOwnedItemsByAddress(address, paginationDto);
}

@IsAddressValid()
@Get('/:address/items/on-sale')
onSaleItems(@Param('address') address: string, @Query() paginationDto: PaginationDto) {
return this.accountService.findOnSaleItemsByAddress(address, paginationDto);
}

@IsAddressValid()
Expand All @@ -42,7 +51,7 @@ export class AccountsController {
@Param('address') address: string,
@Query() paginationDto: PaginationDto
): Promise<Item[]> {
return this.itemService.findLikedByAddress(address, paginationDto);
return this.accountService.findLikedByAddress(address, paginationDto);
}

@IsAddressValid()
Expand Down
14 changes: 9 additions & 5 deletions src/account/repositories/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export class AccountRepository extends Repository<Account> {
}

async findByAddress(address: string): Promise<Account | undefined> {
const account = await this.createQueryBuilder('account')
.where('account.address = :address', { address: address.toLocaleLowerCase() })
.getOne();
const account = await this.findOne({
select: { accountId: true, address: true, username: true },
relations: { picture: true, background: true },
where: {
address: address.toLocaleLowerCase(),
},
});

if (!account) return;

Expand All @@ -23,14 +27,14 @@ export class AccountRepository extends Repository<Account> {

async changeInformation(account: Account, editAccountDto: EditAccountDto): Promise<Account> {
account.background =
account.background &&
editAccountDto.background &&
({
url: formatImageUrl(editAccountDto.background.url),
cid: editAccountDto.background.cid,
} as Image);

account.picture =
account.background &&
editAccountDto.picture &&
({
url: formatImageUrl(editAccountDto.picture.url),
cid: editAccountDto.picture.cid,
Expand Down
37 changes: 36 additions & 1 deletion src/account/services/account.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Injectable } from '@nestjs/common';
import { BusinessErrors } from '../../common/constants';
import { PaginationDto } from '../../common/dto/pagination.dto';
import { BusinessException } from '../../common/exceptions/exception-types';
import { Item } from '../../items/entities/item.entity';
import { CollectionRepository } from '../../collections/repositories/collection.repository';
import { ItemRepository } from '../../items/repositories/item.repository';
import { EditAccountDto } from '../dto/edit-account.dto';
Expand All @@ -13,10 +17,41 @@ export class AccountService {
private itemsRepository: ItemRepository
) {}

async findByAddress(address: string) {
async findAccountByAddress(address: string) {
return this.accountRepository.findByAddress(address);
}

async findCreatedItemsByAddress(address: string, paginationDto: PaginationDto) {
const account = await this.accountRepository.findByAddress(address);

if (!account) throw new BusinessException(BusinessErrors.address_not_associated);

return this.itemsRepository.findCreatedByAccount(account.accountId, paginationDto);
}
async findOnSaleItemsByAddress(address: string, paginationDto: PaginationDto) {
const account = await this.accountRepository.findByAddress(address);

if (!account) throw new BusinessException(BusinessErrors.address_not_associated);

return this.itemsRepository.findOnSaleByAccount(account.accountId, paginationDto);
}

async findOwnedItemsByAddress(address: string, paginationDto: PaginationDto) {
const account = await this.accountRepository.findByAddress(address);

if (!account) throw new BusinessException(BusinessErrors.address_not_associated);

return this.itemsRepository.findOwnedByAccount(account.accountId, paginationDto);
}

async findLikedByAddress(address: string, paginationDto: PaginationDto): Promise<Item[]> {
const account = await this.accountRepository.findByAddress(address);

if (!account) throw new BusinessException(BusinessErrors.address_not_associated);

return this.itemsRepository.findLikedByAccount(account.accountId, paginationDto);
}

async changeInformation(address: string, editAccountDto: EditAccountDto): Promise<Account> {
const account = await this.accountRepository.findByAddress(address);

Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './strategies/jwt.strategy';
import { CacheModule, Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { AccountService } from 'src/account/services/account.service';
import { CollectionRepository } from 'src/collections/repositories/collection.repository';
import { ItemRepository } from 'src/items/repositories/item.repository';
import { AccountService } from '../account/services/account.service';
import { CollectionRepository } from '../collections/repositories/collection.repository';
import { ItemRepository } from '../items/repositories/item.repository';

@Module({
imports: [
Expand Down
6 changes: 3 additions & 3 deletions src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { CredentialsResponseDto } from '../dto/credentials-response.dto';
import { Public } from '../../auth/decorators/public.decorator';
import { SigninRequestDto } from '../dto/signin-request.dto';
import { IsAddressValid } from '../decorators/address.decorator';
import { AccountService } from 'src/account/services/account.service';
import { Account } from 'src/config/entities.config';
import { AccountService } from '../../account/services/account.service';
import { Account } from '../../account/entities/account.entity';

@Controller('auth')
export class AuthController {
Expand All @@ -27,6 +27,6 @@ export class AuthController {
@IsAddressValid()
@Post('/authenticate')
authenticate(@Body() authCredentialsDto: CredentialsRequestDto): Promise<Account> {
return this.accountService.findByAddress(authCredentialsDto.address);
return this.accountService.findAccountByAddress(authCredentialsDto.address);
}
}
6 changes: 3 additions & 3 deletions src/collections/controllers/collection.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Body, Controller, Get, Param, Patch, Query } from '@nestjs/common';
import { Collection } from '../../collections/entities/collection.entity';
import { NewestItemsRequestDto } from 'src/items/dto/newest-items-request.dto';
import { ItemPaginationDto } from 'src/items/dto/pagination-request.dto';
import { PriceRangeDto } from 'src/items/dto/price-range.dto';
import { NewestItemsRequestDto } from '../../items/dto/newest-items-request.dto';
import { ItemPaginationDto } from '../../items/dto/pagination-request.dto';
import { PriceRangeDto } from '../../items/dto/price-range.dto';
import { Public } from '../../auth/decorators/public.decorator';
import { CollectionPaginationDto } from '../dto/collection-pagination-request.dto';
import { EditCollectionDto } from '../dto/edit-collection.dto';
Expand Down
2 changes: 1 addition & 1 deletion src/collections/dto/create-collection.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsArray, IsNotEmpty, IsNumber, Max, MaxLength, Min, MinLength } from 'class-validator';
import { ImageRequestDto } from 'src/items/dto/image-request.dto';
import { ImageRequestDto } from '../../items/dto/image-request.dto';
import { Collection } from '../entities/collection.entity';

export class CreateCollectionDto {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/services/collection.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ItemPaginationDto } from 'src/items/dto/pagination-request.dto';
import { PriceRangeDto } from 'src/items/dto/price-range.dto';
import { ItemPaginationDto } from '../../items/dto/pagination-request.dto';
import { PriceRangeDto } from '../../items/dto/price-range.dto';
import { AccountRepository } from '../../account/repositories/account.repository';
import { BusinessErrors } from '../../common/constants';
import { BusinessException } from '../../common/exceptions/exception-types';
Expand Down
52 changes: 0 additions & 52 deletions src/items/__tests__/item.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,41 +183,6 @@ describe('ItemService', () => {
});
});

describe('When findByAddress function is called', () => {
describe('and the address exist', () => {
it('should return the expected item', async () => {
const account = { accountId: '456' } as Account;
const expected = items;

accountRepository.findByAddress.mockResolvedValue({ ...account });

const mockedData = expected.map((prop) => ({ ...prop }));
itemRepository.findByAccount.mockResolvedValue(mockedData);

const actual = await service.findByAddress('123', { limit: 15, page: 1 });

expect(actual).toEqual(expected);
});
});

describe('and the address does not exist', () => {
it('should throw a BusinessException with expected message', async () => {
const unexistingAddress = '123';

accountRepository.findByAddress.mockResolvedValue(null);

const exception = () => service.findByAddress(unexistingAddress, { limit: 15, page: 1 });

await expect(exception).rejects.toThrow(BusinessException);
await expect(exception).rejects.toEqual(
new BusinessException(BusinessErrors.address_not_associated)
);

expect(itemRepository.findByAccount).not.toHaveBeenCalled();
});
});
});

describe('When findPagination function is called', () => {
it('should return a pagination object ', async () => {
const expected = {
Expand Down Expand Up @@ -281,23 +246,6 @@ describe('ItemService', () => {
expect(actual).toEqual(expected);
});
});

describe('and the address does not exist', () => {
it('should throw a BusinessException with expected message', async () => {
const unexistingAddress = '123';

accountRepository.findByAddress.mockResolvedValue(null);

const exception = () => service.findByAddress(unexistingAddress, { limit: 15, page: 1 });

await expect(exception).rejects.toThrow(BusinessException);
await expect(exception).rejects.toEqual(
new BusinessException(BusinessErrors.file_extension_not_supported)
);

expect(itemRepository.createItem).not.toHaveBeenCalled();
});
});
});

describe('When listAnItem function is called', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/items/controllers/image.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { PinataService } from '../services/pinata.service';
import { IpfsAttribute } from 'src/common/types/ipfs-attribute';
import { IpfsAttribute } from '../../common/types/ipfs-attribute';

@Controller('images')
export class ImagesController {
Expand Down
2 changes: 1 addition & 1 deletion src/items/repositories/history.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { PaginationDto } from 'src/common/dto/pagination.dto';
import { PaginationDto } from '../../common/dto/pagination.dto';
import { DataSource, Repository } from 'typeorm';
import { Account } from '../../config/entities.config';
import { History } from '../entities/history.entity';
Expand Down
Loading

0 comments on commit 39e12cd

Please sign in to comment.