import { Injectable , NotFoundException } from '@nestjs/common' ; import { AccessTokens , Account , AccountItem , AccountType , ApproveTransaction , Beneficiary , BeneficiaryUpdateParams , CreateAccountParams , Device , DeviceCreateParams , DeviceState , DeviceUpdateParams , IssuanceStatus , Issue , IssueWithStatus , Organisation , OrganisationRole , RedeemTransactionResult , TransactionResult , TransactionType } from '@energyweb/issuer-irec-api-wrapper' ; import { ILoggedInUser } from '@energyweb/origin-backend-core' ; import { ReadStream } from 'fs' ; import { CreateConnectionDTO } from './dto' ; import { ICreateBeneficiary , IIrecService } from './irec.service' ; import { ConnectionDTO } from '../connection' ; export type UserIdentifier = ILoggedInUser | string | number ; @ Injectable ( ) export class IrecMockService implements IIrecService { private devices : Device [ ] = [ { address : '1 Wind Farm Avenue, London' , capacity : '500' , commissioningDate : new Date ( '2001-08-10' ) , countryCode : 'GB' , defaultAccount : 'someTradeAccount' , deviceType : 'TC110' , fuelType : 'ES200' , issuer : 'someIssuerCode' , latitude : '53.405088' , longitude : '-1.744222' , name : 'DeviceXYZ' , notes : 'Lorem ipsum dolor sit amet' , registrantOrganization : 'someRegistrantCode' , registrationDate : new Date ( '2001-09-20' ) , status : DeviceState . Approved , code : 'mockDeviceCode' , active : true } ] ; private accountInfo : Account [ ] = [ { code : 'TEST001' , details : { name : 'Some new revision' , private : false , restricted : false , active : true , notes : 'Some test' } , type : AccountType . Trade } , { code : 'TESTREDEMPTIONACCOUNT' , details : { name : 'Test Account Details 001' , private : false , restricted : false , active : true , notes : 'Some test notes' } , type : AccountType . Redemption } ] ; private issueRequests : IssueWithStatus [ ] = [ { device : 'TESTDEVICE1' , fuelType : 'ES200' , recipient : 'SOMEORG' , start : new Date ( ) , end : new Date ( ) , production : 1000000 , notes : '' , code : '100500' , status : IssuanceStatus . Approved } ] ; private userOrganization : Organisation = { code : 'REGORG001' , name : 'Registrant Organisation 1' , address : '10 Dorchester Road' , primaryContact : 'John Doe' , telephone : '0114 2002002' , email : 'example@test.com' , regNum : '123 456 789' , vatNum : '987 654 321' , regAddress : '10 Dorchester Road' , country : 'GB' , roles : [ OrganisationRole . Registrant , OrganisationRole . Participant ] } ; public async getConnectionInfo ( user ) { return { id : 'someid' , accessToken : 'access-token' , refreshToken : 'refresh-token' , clientId : 'oauth client id' , clientSecret : 'oauth client secret' , expiryDate : new Date ( ) , userName : 'irecUser' , active : true } ; } async login ( { userName , password , clientId , clientSecret } ) { return { expiryDate : new Date ( ) , accessToken : 'someAccessToken' , refreshToken : 'someRefreshToken' } ; } async createBeneficiary ( user , beneficiary ) { return { id : 1 , name : beneficiary . name , countryCode : beneficiary . countryCode , location : beneficiary . location , active : true } ; } async updateBeneficiary ( user , beneficiaryId , params ) { return { id : 1 , name : 'Test Corp' , countryCode : 'GB' , location : `Manchester, Lennon street` , active : params . active } ; } async createDevice ( user , deviceData ) { const device = { ... deviceData , status : DeviceState . InProgress , code : randomString ( ) } ; this . devices . push ( device ) ; return device ; } async updateDevice ( user , code , device ) { const existingDevice = await this . getDevice ( user , code ) ; const updatedDevice = { ... device , status : DeviceState . InProgress , code } as Device ; if ( ! existingDevice ) { this . devices . push ( updatedDevice ) ; return updatedDevice ; } return Object . assign ( existingDevice , updatedDevice ) ; } async getDevice ( user , code ) { return this . devices . find ( ( d ) => d . code === code ) ; } async getDevices ( user ) { return this . devices ; } async getAccountInfo ( user ) { return this . accountInfo ; } async getAccountInfoByTokens ( clientId , clientSecret , tokens ) { return this . accountInfo ; } async getTradeAccountCode ( user ) { const accounts = await this . getAccountInfo ( user ) ; return accounts . find ( ( account ) => account . type === AccountType . Trade ) ?. code || '' ; } async getIssueAccountCode ( user ) { const accounts = await this . getAccountInfo ( user ) ; return accounts . find ( ( account ) => account . type === AccountType . Issue ) ?. code || '' ; } async getRedemptionAccountCode ( user ) { const accounts = await this . getAccountInfo ( user ) ; return ( accounts . find ( ( account ) => account . type === AccountType . Redemption ) ?. code || '' ) ; } async createIssueRequest ( user , issue ) { const issueRequest = { ... issue , status : IssuanceStatus . InProgress , code : randomString ( ) } ; this . issueRequests . push ( issueRequest ) ; return issueRequest ; } async updateIssueRequest ( user , code , issue ) { return { ... issue , status : IssuanceStatus . InProgress , code } as IssueWithStatus ; } async getIssueRequest ( user , code ) { return this . issueRequests . find ( ( ir ) => ir . code === code ) ; } async createAccount ( user , params ) { } async createAccountByTokens ( clientId , clientSecret , tokens , params ) { } async uploadFiles ( user , files ) { return files . map ( ( ) => randomString ( ) ) ; } async verifyIssueRequest ( user , issueRequestCode ) { const issueRequest = await this . getIssueRequest ( user , issueRequestCode ) ; if ( ! issueRequest ) { throw new NotFoundException ( `Issue request (id=${ issueRequestCode }) not found` ) ; } issueRequest . status = IssuanceStatus . Verified ; } async approveIssueRequest ( user , issueRequestCode , issuerAccountCode ) { return { asset : 'TESTACC-0202-0202-0202-02' , code : `TRANSACTION-${ randomString ( ) }` , volume : 1000 , notes : '' , sender : 'some' , recipient : 'another' , time : new Date ( ) , transactionType : TransactionType . Issue } ; } async rejectIssueRequest ( user , issueRequestCode ) { const issueRequest = await this . getIssueRequest ( user , issueRequestCode ) ; if ( ! issueRequest ) { throw new NotFoundException ( `Issue request (id=${ issueRequestCode }) not found` ) ; } issueRequest . status = IssuanceStatus . Rejected ; } async getCertificates ( user ) { return [ { code : 'PARTICIPANTUSER001-2020-0101-2011-00-1' , volume : 1000 , startDate : '2019-01-01' , endDate : '2019-12-31' , fuelType : { code : 'ES200' , description : 'Wind' } , deviceType : { code : 'T020001' , description : 'Wind: Onshore' } , device : { code : 'mockDeviceCode' , name : 'mockDeviceName' } , deviceSupported : true , tagged : false , co2Produced : 0 , country : 'GB' , product : 'IREC(E)' , asset : 'test-asset-id' } ] ; } async transferCertificate ( fromUser , toUser , assetId ) { return { code : 'transactioncode' , volume : 123 , notes : '' , time : new Date ( ) , transactionType : TransactionType . Transfer , sender : 'sender-trade-acc' , recipient : 'recepient-trade-acc' } ; } async redeem ( user , assetId ) { return { code : 'TESTACC-2020-0903-1232-34' , transactionType : TransactionType . Redemption , volume : 1000 , notes : '' , time : new Date ( ) , sender : 'TPOTRADE1' , recipient : 'TRUPOWER' , verificationKey : 'S4ELosCw' , encryptedKey : 'S4ELosCw' } ; } async approveDevice ( user , code ) { const device = await this . getDevice ( user , code ) ; if ( device . status !== DeviceState . InProgress ) { throw new Error ( 'To approve IREC device its state has to be In-Progress' ) ; } device . status = DeviceState . Approved ; return device ; } async rejectDevice ( user , code ) { const device = await this . getDevice ( user , code ) ; if ( device . status !== DeviceState . InProgress ) { throw new Error ( 'To reject IREC device its state have to be In-Progress' ) ; } device . status = DeviceState . Rejected ; return device ; } async getUserOrganization ( user ) { return this . userOrganization ; } async getUserOrganizationByTokens ( clientId , clientSecret , tokens ) { return this . userOrganization ; } } function randomString ( ) { return ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) . toString ( 36 ) ; }