-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GET AddressBook and POST AddressBookItem
- Loading branch information
1 parent
d72edee
commit cc8e0bc
Showing
13 changed files
with
293 additions
and
14 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/datasources/accounts/address-books/__tests__/test.address-books.datasource.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IAddressBooksDataSource } from '@/domain/interfaces/address-books.datasource.interface'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
const addressBooksDatasource = { | ||
createAddressBookItem: jest.fn(), | ||
getAddressBook: jest.fn(), | ||
updateAddressBookItem: jest.fn(), | ||
deleteAddressBook: jest.fn(), | ||
deleteAddressBookItem: jest.fn(), | ||
} as jest.MockedObjectDeep<IAddressBooksDataSource>; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: IAddressBooksDataSource, | ||
useFactory: (): jest.MockedObjectDeep<IAddressBooksDataSource> => { | ||
return jest.mocked(addressBooksDatasource); | ||
}, | ||
}, | ||
], | ||
exports: [IAddressBooksDataSource], | ||
}) | ||
export class TestAddressBooksDataSourceModule {} |
10 changes: 5 additions & 5 deletions
10
src/datasources/accounts/address-books/address-books.datasource.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { AddressBooksDatasource } from '@/datasources/accounts/address-books/address-books.datasource'; | ||
import { AddressBookDbMapper } from '@/datasources/accounts/address-books/entities/address-book.db.mapper'; | ||
import { EncryptionApiManager } from '@/datasources/accounts/encryption/encryption-api.manager'; | ||
import { PostgresDatabaseModule } from '@/datasources/db/v1/postgres-database.module'; | ||
import { IAddressBooksDataSource } from '@/domain/interfaces/address-books.datasource.interface'; | ||
import { IEncryptionApiManager } from '@/domain/interfaces/encryption-api.manager.interface'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
@Module({ | ||
imports: [PostgresDatabaseModule], | ||
providers: [ | ||
AddressBookDbMapper, | ||
{ | ||
provide: IAddressBooksDataSource, | ||
useClass: AddressBooksDatasource, | ||
}, | ||
{ provide: IAddressBooksDataSource, useClass: AddressBooksDatasource }, | ||
{ provide: IEncryptionApiManager, useClass: EncryptionApiManager }, | ||
], | ||
exports: [IAddressBooksDataSource], | ||
exports: [IAddressBooksDataSource, IEncryptionApiManager], | ||
}) | ||
export class AddressBooksDatasourceModule {} |
39 changes: 39 additions & 0 deletions
39
src/domain/accounts/address-books/address-books.repository.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { AddressBooksDatasourceModule } from '@/datasources/accounts/address-books/address-books.datasource.module'; | ||
import { AccountsRepositoryModule } from '@/domain/accounts/accounts.repository.interface'; | ||
import { AddressBooksRepository } from '@/domain/accounts/address-books/address-books.repository'; | ||
import type { | ||
AddressBook, | ||
AddressBookItem, | ||
} from '@/domain/accounts/address-books/entities/address-book.entity'; | ||
import { CreateAddressBookItemDto } from '@/domain/accounts/address-books/entities/create-address-book-item.dto.entity'; | ||
import { AuthPayload } from '@/domain/auth/entities/auth-payload.entity'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
export const IAddressBooksRepository = Symbol.for('IAddressBooksRepository'); | ||
|
||
export interface IAddressBooksRepository { | ||
getAddressBook(args: { | ||
authPayload: AuthPayload; | ||
address: `0x${string}`; | ||
chainId: string; | ||
}): Promise<AddressBook>; | ||
|
||
createAddressBookItem(args: { | ||
authPayload: AuthPayload; | ||
address: `0x${string}`; | ||
chainId: string; | ||
createAddressBookItemDto: CreateAddressBookItemDto; | ||
}): Promise<AddressBookItem>; | ||
} | ||
|
||
@Module({ | ||
imports: [AccountsRepositoryModule, AddressBooksDatasourceModule], | ||
providers: [ | ||
{ | ||
provide: IAddressBooksRepository, | ||
useClass: AddressBooksRepository, | ||
}, | ||
], | ||
exports: [IAddressBooksRepository], | ||
}) | ||
export class AddressBooksRepositoryModule {} |
124 changes: 124 additions & 0 deletions
124
src/domain/accounts/address-books/address-books.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { IAccountsRepository } from '@/domain/accounts/accounts.repository.interface'; | ||
import type { IAddressBooksRepository } from '@/domain/accounts/address-books/address-books.repository.interface'; | ||
import type { | ||
AddressBook, | ||
AddressBookItem, | ||
} from '@/domain/accounts/address-books/entities/address-book.entity'; | ||
import { CreateAddressBookItemDto } from '@/domain/accounts/address-books/entities/create-address-book-item.dto.entity'; | ||
import { | ||
AccountDataType, | ||
AccountDataTypeNames, | ||
} from '@/domain/accounts/entities/account-data-type.entity'; | ||
import { AuthPayload } from '@/domain/auth/entities/auth-payload.entity'; | ||
import { IAddressBooksDataSource } from '@/domain/interfaces/address-books.datasource.interface'; | ||
import { ILoggingService, LoggingService } from '@/logging/logging.interface'; | ||
import { | ||
GoneException, | ||
Inject, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AddressBooksRepository implements IAddressBooksRepository { | ||
constructor( | ||
@Inject(IAddressBooksDataSource) | ||
private readonly dataSource: IAddressBooksDataSource, | ||
@Inject(IAccountsRepository) | ||
private readonly accountsRepository: IAccountsRepository, | ||
@Inject(LoggingService) private readonly loggingService: ILoggingService, | ||
) {} | ||
|
||
/** | ||
* Gets an AddressBook. | ||
* Checks that the account has the AddressBooks Data Setting enabled. | ||
*/ | ||
async getAddressBook(args: { | ||
authPayload: AuthPayload; | ||
address: `0x${string}`; | ||
chainId: string; | ||
}): Promise<AddressBook> { | ||
if (!args.authPayload.isForSigner(args.address)) { | ||
throw new UnauthorizedException(); | ||
} | ||
await this.checkAddressBooksIsEnabled({ | ||
authPayload: args.authPayload, | ||
address: args.address, | ||
}); | ||
const account = await this.accountsRepository.getAccount({ | ||
authPayload: args.authPayload, | ||
address: args.address, | ||
}); | ||
return await this.dataSource.getAddressBook({ | ||
account, | ||
chainId: args.chainId, | ||
}); | ||
} | ||
|
||
/** | ||
* Creates an AddressBookItem. | ||
* Checks that the account has the AddressBooks Data Setting enabled. | ||
* | ||
* If an AddressBook for the Account and chainId does not exist, it's created. | ||
*/ | ||
async createAddressBookItem(args: { | ||
authPayload: AuthPayload; | ||
address: `0x${string}`; | ||
chainId: string; | ||
createAddressBookItemDto: CreateAddressBookItemDto; | ||
}): Promise<AddressBookItem> { | ||
if (!args.authPayload.isForSigner(args.address)) { | ||
throw new UnauthorizedException(); | ||
} | ||
await this.checkAddressBooksIsEnabled({ | ||
authPayload: args.authPayload, | ||
address: args.address, | ||
}); | ||
const account = await this.accountsRepository.getAccount({ | ||
authPayload: args.authPayload, | ||
address: args.address, | ||
}); | ||
return this.dataSource.createAddressBookItem({ | ||
account, | ||
chainId: args.chainId, | ||
createAddressBookItemDto: args.createAddressBookItemDto, | ||
}); | ||
} | ||
|
||
// TODO: Extract this functionality in AccountsRepository['checkIsEnabled(DataType, Account)'] | ||
private async checkAddressBooksIsEnabled(args: { | ||
authPayload: AuthPayload; | ||
address: `0x${string}`; | ||
}): Promise<void> { | ||
const addressBookDataType = await this.checkAddressBookDataTypeIsActive(); | ||
const accountDataSettings = | ||
await this.accountsRepository.getAccountDataSettings({ | ||
authPayload: args.authPayload, | ||
address: args.address, | ||
}); | ||
const addressBookSetting = accountDataSettings.find( | ||
(setting) => setting.account_data_type_id === addressBookDataType.id, | ||
); | ||
if (!addressBookSetting?.enabled) { | ||
this.loggingService.warn({ | ||
message: `Account ${args.address} does not have AddressBooks enabled`, | ||
}); | ||
throw new GoneException(); | ||
} | ||
} | ||
|
||
// TODO: Extract this functionality in AccountsRepository['checkIsActive(DataType)'] | ||
private async checkAddressBookDataTypeIsActive(): Promise<AccountDataType> { | ||
const dataTypes = await this.accountsRepository.getDataTypes(); | ||
const addressBookDataType = dataTypes.find( | ||
(dataType) => dataType.name === AccountDataTypeNames.AddressBook, | ||
); | ||
if (!addressBookDataType?.is_active) { | ||
this.loggingService.warn({ | ||
message: `${AccountDataTypeNames.AddressBook} data type is not active`, | ||
}); | ||
throw new GoneException(); | ||
} | ||
return addressBookDataType; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
src/domain/accounts/address-books/entities/address-book.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.