|
1 | 1 | import { Claim } from '@po.et/poet-js' |
2 | 2 | import { inject, injectable } from 'inversify' |
3 | | -import { Collection, Db } from 'mongodb' |
4 | 3 | import * as Pino from 'pino' |
| 4 | +import { pipeP } from 'ramda' |
5 | 5 |
|
6 | 6 | import { childWithFileName } from 'Helpers/Logging' |
7 | | -import { Messaging } from 'Messaging/Messaging' |
8 | 7 |
|
9 | | -import { ExchangeConfiguration } from './ExchangeConfiguration' |
| 8 | +import { DAOClaims } from './DAOClaims' |
10 | 9 | import { IPFS } from './IPFS' |
11 | 10 |
|
| 11 | +enum LogTypes { |
| 12 | + info = 'info', |
| 13 | + trace = 'trace', |
| 14 | + error = 'error', |
| 15 | +} |
| 16 | + |
| 17 | +interface StoreNextClaimData { |
| 18 | + readonly claim: Claim |
| 19 | + readonly ipfsFileHash?: string |
| 20 | +} |
| 21 | + |
12 | 22 | @injectable() |
13 | 23 | export class ClaimController { |
14 | 24 | private readonly logger: Pino.Logger |
15 | | - private readonly db: Db |
16 | | - private readonly collection: Collection |
17 | | - private readonly messaging: Messaging |
| 25 | + private readonly daoClaims: DAOClaims |
18 | 26 | private readonly ipfs: IPFS |
19 | | - private readonly exchange: ExchangeConfiguration |
20 | 27 |
|
21 | 28 | constructor( |
22 | 29 | @inject('Logger') logger: Pino.Logger, |
23 | | - @inject('DB') db: Db, |
24 | | - @inject('Messaging') messaging: Messaging, |
25 | | - @inject('IPFS') ipfs: IPFS, |
26 | | - @inject('ExchangeConfiguration') exchange: ExchangeConfiguration |
| 30 | + @inject('DAOClaims') daoClaims: DAOClaims, |
| 31 | + @inject('IPFS') ipfs: IPFS |
27 | 32 | ) { |
28 | 33 | this.logger = childWithFileName(logger, __filename) |
29 | | - this.db = db |
30 | | - this.collection = this.db.collection('storageWriter') |
31 | | - this.messaging = messaging |
| 34 | + this.daoClaims = daoClaims |
32 | 35 | this.ipfs = ipfs |
33 | | - this.exchange = exchange |
34 | 36 | } |
35 | 37 |
|
36 | | - async create(claim: Claim): Promise<void> { |
| 38 | + private readonly log = (level: LogTypes) => (message: string) => async (value: any) => { |
| 39 | + const logger = this.logger |
| 40 | + logger[level]({ value }, message) |
| 41 | + return value |
| 42 | + } |
| 43 | + |
| 44 | + public readonly create = async (claim: Claim): Promise<void> => { |
37 | 45 | const logger = this.logger.child({ method: 'create' }) |
38 | 46 |
|
39 | | - logger.trace({ claim }, 'Storing Claim') |
| 47 | + logger.trace({ claim }, 'Adding Claim') |
| 48 | + |
| 49 | + await this.daoClaims.addClaim(claim) |
40 | 50 |
|
41 | | - const ipfsFileHash = await this.ipfs.addText(JSON.stringify(claim)) |
| 51 | + logger.trace({ claim }, 'Added Claim') |
| 52 | + } |
| 53 | + |
| 54 | + private readonly findNextClaim = async (): Promise<StoreNextClaimData> => { |
| 55 | + const claim = await this.daoClaims.findNextClaim() |
| 56 | + return { claim } |
| 57 | + } |
42 | 58 |
|
43 | | - logger.info({ claim, ipfsFileHash }, 'Claim Stored') |
| 59 | + private readonly uploadClaim = (claim: Claim) => this.ipfs.addText(JSON.stringify(claim)) |
44 | 60 |
|
45 | | - await this.collection.insertOne({ |
46 | | - claimId: claim.id, |
47 | | - ipfsFileHash, |
48 | | - }) |
49 | | - await this.messaging.publish(this.exchange.claimIpfsHash, { |
50 | | - claimId: claim.id, |
| 61 | + private readonly storeClaim = async (data: StoreNextClaimData): Promise<StoreNextClaimData> => { |
| 62 | + const { claim } = data |
| 63 | + const ipfsFileHash = await this.uploadClaim(claim) |
| 64 | + return { |
| 65 | + ...data, |
51 | 66 | ipfsFileHash, |
52 | | - }) |
| 67 | + } |
53 | 68 | } |
| 69 | + |
| 70 | + private readonly addIPFSHashToClaim = async (data: StoreNextClaimData): Promise<StoreNextClaimData> => { |
| 71 | + const { claim, ipfsFileHash } = data |
| 72 | + await this.daoClaims.addClaimHash(claim.id, ipfsFileHash) |
| 73 | + return data |
| 74 | + } |
| 75 | + |
| 76 | + public storeNextClaim = pipeP( |
| 77 | + this.log(LogTypes.trace)('Finding Claim'), |
| 78 | + this.findNextClaim, |
| 79 | + this.log(LogTypes.trace)('Storing Claim'), |
| 80 | + this.storeClaim, |
| 81 | + this.log(LogTypes.trace)('Adding IPFS hash to Claim Entry'), |
| 82 | + this.addIPFSHashToClaim, |
| 83 | + this.log(LogTypes.trace)('Finished Storing Claim') |
| 84 | + ) |
54 | 85 | } |
0 commit comments