|
| 1 | +/* |
| 2 | + * Copyright 2020 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Observable, of } from 'rxjs'; |
| 18 | +import { map, withLatestFrom } from 'rxjs/operators'; |
| 19 | +import { RepositoryFactory } from '../infrastructure/RepositoryFactory'; |
| 20 | +import { AccountRepository } from '../infrastructure/AccountRepository'; |
| 21 | +import { NamespaceRepository } from '../infrastructure/NamespaceRepository'; |
| 22 | +import { Address } from '../model/account/Address'; |
| 23 | +import { mergeMap } from 'rxjs/operators'; |
| 24 | +import { DtoMapping } from '../core/utils/DtoMapping'; |
| 25 | +import { IAccountService } from './interfaces/IAccountService'; |
| 26 | +import { NamespaceInfoWithName } from '../model/namespace/NamespaceInfoWithName'; |
| 27 | +import { ResolvedMosaic } from '../model/mosaic/ResolvedMosaic'; |
| 28 | +import { Mosaic } from '../model/mosaic/Mosaic'; |
| 29 | +import { MosaicId } from '../model/mosaic/MosaicId'; |
| 30 | +import { NamespaceId } from '../model/namespace/NamespaceId'; |
| 31 | +import { AccountInfoResolvedMosaic } from '../model/account/AccountInfoResolvedMosaic'; |
| 32 | +import { AccountInfo } from '../model/account/AccountInfo'; |
| 33 | +import { NamespaceName } from '../model/namespace/NamespaceName'; |
| 34 | +/** |
| 35 | + * Account Service |
| 36 | + */ |
| 37 | +export class AccountService implements IAccountService { |
| 38 | + private readonly accountRepository: AccountRepository; |
| 39 | + |
| 40 | + private readonly namespaceRepository: NamespaceRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor |
| 44 | + * @param repositoryFactory |
| 45 | + */ |
| 46 | + constructor(public readonly repositoryFactory: RepositoryFactory) { |
| 47 | + this.accountRepository = repositoryFactory.createAccountRepository(); |
| 48 | + this.namespaceRepository = repositoryFactory.createNamespaceRepository(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get account info with resolved mosaic |
| 53 | + * @param addresses Array of addresses |
| 54 | + */ |
| 55 | + public accountInfoWithResolvedMosaic(addresses: Address[]): Observable<AccountInfoResolvedMosaic[]> { |
| 56 | + const accountInfoObservable = this.accountRepository.getAccountsInfo(addresses); |
| 57 | + const distinctNames = accountInfoObservable.pipe( |
| 58 | + mergeMap((info) => { |
| 59 | + const namespaceIds = this.getDistinctNamespaceIdFromAccountInfos(info); |
| 60 | + if (namespaceIds.length) { |
| 61 | + return this.namespaceRepository.getNamespacesName(namespaceIds); |
| 62 | + } |
| 63 | + return of([]); |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + return accountInfoObservable.pipe( |
| 68 | + withLatestFrom(distinctNames), |
| 69 | + map(([infos, names]) => { |
| 70 | + return infos.map((info) => { |
| 71 | + const resolved = this.resolveMosaics(info.mosaics, names); |
| 72 | + return DtoMapping.assign(info, { resolvedMosaics: resolved }); |
| 73 | + }); |
| 74 | + }), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get namespace info for account with namespace name |
| 80 | + * @param addresses Array of addresses |
| 81 | + * @returns {Observable<NamespaceInfoWithName[]>} |
| 82 | + */ |
| 83 | + public accountNamespacesWithName(addresses: Address[]): Observable<NamespaceInfoWithName[]> { |
| 84 | + return this.namespaceRepository.getNamespacesFromAccounts(addresses).pipe( |
| 85 | + mergeMap((infos) => { |
| 86 | + const namespaceIds = infos.map((i) => i.id); |
| 87 | + return this.namespaceRepository.getNamespacesName(namespaceIds).pipe( |
| 88 | + map((resolved) => { |
| 89 | + return infos.map((info) => { |
| 90 | + const name = resolved.find((r) => r.namespaceId.equals(info.id)); |
| 91 | + return DtoMapping.assign(info, { namespaceName: name?.name }); |
| 92 | + }); |
| 93 | + }), |
| 94 | + ); |
| 95 | + }), |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Resolve mosaics provided namespace names |
| 101 | + * @param mosaics unresolved mosaics |
| 102 | + * @return {ResolvedMosaic[]} |
| 103 | + */ |
| 104 | + private resolveMosaics(mosaics: Mosaic[], names: NamespaceName[]): ResolvedMosaic[] { |
| 105 | + return mosaics.map((mosaic) => { |
| 106 | + if (mosaic.id instanceof MosaicId) { |
| 107 | + return mosaic as ResolvedMosaic; |
| 108 | + } else { |
| 109 | + const name = names.find((f) => f.namespaceId.equals(mosaic.id)); |
| 110 | + if (name) { |
| 111 | + return DtoMapping.assign(mosaic, { namespaceName: name }); |
| 112 | + } else { |
| 113 | + return mosaic as ResolvedMosaic; |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get distinct list of namespaces ids from list of account infos |
| 121 | + * @param accountInfos List of account infos |
| 122 | + * @returns {NamespaceId[]} |
| 123 | + */ |
| 124 | + private getDistinctNamespaceIdFromAccountInfos(accountInfos: AccountInfo[]): NamespaceId[] { |
| 125 | + const namespaceIds: NamespaceId[] = []; |
| 126 | + accountInfos.forEach((info) => { |
| 127 | + info.mosaics.forEach((mosaic) => { |
| 128 | + if (mosaic.id instanceof NamespaceId) { |
| 129 | + if (!namespaceIds.find((n) => n.equals(mosaic.id))) { |
| 130 | + namespaceIds.push(mosaic.id); |
| 131 | + } |
| 132 | + } |
| 133 | + }); |
| 134 | + }); |
| 135 | + return namespaceIds; |
| 136 | + } |
| 137 | +} |
0 commit comments