Skip to content

Commit 478a642

Browse files
committed
refactor: save
1 parent bb381a4 commit 478a642

28 files changed

+155
-35
lines changed

src/infrastructure/clients/AuthenticationClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { Injectable } from '@nestjs/common'
12
import { Account } from '../../modules/authentication/domain/entities/Account'
2-
import { EnumCountry } from '../../modules/authentication/domain/enums/EnumCountry'
3+
import { EnumCountry } from '../../modules/authentication/presentation/dto/enums/EnumCountry'
34
import { IAuthenticationClient } from './IAuthenticationClient'
4-
import { Injectable } from '@nestjs/common'
55

66
export const accountAuthClientMock1 = {
77
name: 'Name One',
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { BadRequestException, Inject, Injectable } from '@nestjs/common'
2+
import { IAuthenticationClient } from '../../../infrastructure/clients/IAuthenticationClient'
3+
import { Account } from '../domain/entities/Account'
4+
import { ItemAlreadyExistsError } from '../domain/errors/itemAlreadyExists.error'
5+
import { CreateAccountDto } from '../presentation/dto/createAccount.dto'
6+
import { IAuthenticationService } from './IAuthenticationService'
7+
8+
@Injectable()
9+
export class AuthenticationService implements IAuthenticationService {
10+
constructor(
11+
@Inject('IAuthenticationClient')
12+
private readonly authenticationClient: IAuthenticationClient
13+
) {}
14+
15+
async createAccount(body: CreateAccountDto): Promise<Account> {
16+
this.validateZipCode(body)
17+
const accountExists = await this.authenticationClient.getAccountByEmail(body.email)
18+
if (accountExists) {
19+
throw new BadRequestException(new ItemAlreadyExistsError('Account', 'Email'))
20+
}
21+
const account = new Account(body)
22+
const newAccount = await this.authenticationClient.createAccount(account)
23+
return newAccount
24+
}
25+
26+
private validateZipCode(body: CreateAccountDto): void {
27+
if (!body.address.zipCode) {
28+
console.log('Logger: no zipCode to validate.')
29+
return
30+
}
31+
const { zipCode } = body.address
32+
if (zipCode === '99999999') {
33+
throw new BadRequestException('Invalid ZipCode')
34+
}
35+
}
36+
37+
async getAllAccounts(): Promise<Array<Account>> {
38+
return await this.authenticationClient.getAllAccounts()
39+
}
40+
41+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Account } from '../domain/entities/Account'
2+
import { CreateAccountDto } from '../presentation/dto/createAccount.dto'
3+
4+
export interface IAuthenticationService {
5+
createAccount(body: CreateAccountDto): Promise<Account>
6+
}

src/modules/authentication/authentication.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { Module } from '@nestjs/common'
12
import { AuthenticationClient } from '../../infrastructure/clients/AuthenticationClient'
2-
import { AuthenticationController } from './authentication.controller'
3-
import { AuthenticationService } from './authentication.service'
43
import { InfrastructureModule } from '../../infrastructure/infrastructure.module'
5-
import { Module } from '@nestjs/common'
4+
import { AuthenticationService } from './application/AuthenticationService'
5+
import { AuthenticationController } from './presentation/authentication.controller'
66

77
@Module({
88
imports: [InfrastructureModule],

src/modules/authentication/domain/entities/Account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address } from '../interfaces/Address'
1+
import { Address } from '../../presentation/dto/types/Address'
22

33
export class Account {
44
private name: string
File renamed without changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Body, Controller, Get, Post } from '@nestjs/common'
2+
import { AuthenticationService } from '../application/AuthenticationService'
3+
import { CreateAccountDto } from './dto/createAccount.dto'
4+
5+
6+
@Controller()
7+
export class AuthenticationController {
8+
constructor(
9+
private readonly authenticationService: AuthenticationService
10+
) {}
11+
12+
@Post('/signup')
13+
async signup(@Body() body: CreateAccountDto) {
14+
return await this.authenticationService.createAccount(body)
15+
}
16+
17+
@Get('/login')
18+
async login() {
19+
return await this.authenticationService.login()
20+
}
21+
22+
}
File renamed without changes.

src/modules/authentication/domain/dto/address.dto.ts renamed to src/modules/authentication/presentation/dtos/address.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IsEnum, IsNotEmpty, IsNumberString, IsOptional, IsString, Length } from 'class-validator'
22

3-
import { EnumCountry } from '../enums/EnumCountry'
3+
import { EnumCountry } from './enums/EnumCountry'
44

55
export const zipCodeLenghtMessage = 'zipCode must have exactly 8 characters.'
66

File renamed without changes.

0 commit comments

Comments
 (0)