1- import  {  Work ,  PoetBlockAnchor  }  from  '@po.et/poet-js' 
1+ import  {  Work ,  PoetBlockAnchor ,   SignedVerifiableClaim  }  from  '@po.et/poet-js' 
22import  {  Collection ,  Db  }  from  'mongodb' 
33import  *  as  Pino  from  'pino' 
44
@@ -15,6 +15,7 @@ export interface Arguments {
1515} 
1616
1717export  interface  WorkController  { 
18+   readonly  createDbIndices : ( )  =>  Promise < void > 
1819  readonly  createWork : ( work : Work )  =>  Promise < void > 
1920  readonly  setIPFSHash : ( workId : string ,  ipfsFileHash : string )  =>  Promise < void > 
2021  readonly  setDirectoryHashOnEntries : ( data : { 
@@ -39,16 +40,23 @@ export const WorkController = ({
3940  const  workControllerLogger : Pino . Logger  =  childWithFileName ( logger ,  __filename ) 
4041  const  anchorCollection : Collection  =  db . collection ( 'anchors' ) 
4142  const  workCollection : Collection  =  db . collection ( 'works' ) 
43+   const  graphCollection : Collection  =  db . collection ( 'graph' ) 
4244
43-   const  createWork  =  async  ( work : Work ) : Promise < void >  =>  { 
45+   const  createDbIndices  =  async  ( )  =>  { 
46+     await  graphCollection . createIndex ( {  origin : 1 ,  target : 1  } ,  {  unique : true  } ) 
47+   } 
48+ 
49+   const  createWork  =  async  ( verifiableClaim : SignedVerifiableClaim ) : Promise < void >  =>  { 
4450    const  logger  =  workControllerLogger . child ( {  method : 'createWork'  } ) 
45-     logger . trace ( {  work  } ,  'Creating Work' ) 
51+     logger . trace ( {  verifiableClaim  } ,  'Creating Work' ) 
4652
47-     const  existing  =  await  workCollection . findOne ( {  id : work . id  } ) 
53+     const  existing  =  await  workCollection . findOne ( {  id : verifiableClaim . id  } ) 
4854
4955    if  ( existing )  return 
5056
51-     await  workCollection . insertOne ( work ) 
57+     await  workCollection . insertOne ( verifiableClaim ) 
58+     if  ( Array . isArray ( verifiableClaim . claim . about ) ) 
59+       await  insertGraphEdges ( verifiableClaim . id ,  verifiableClaim . claim . about ) 
5260  } 
5361
5462  const  setIPFSHash  =  async  ( workId : string ,  ipfsFileHash : string ) : Promise < void >  =>  { 
@@ -132,9 +140,19 @@ export const WorkController = ({
132140        workCollection . updateOne ( {  'anchor.ipfsFileHash' : ipfsFileHash  } ,  {  $set : claim  } ,  {  upsert : true  } ) , 
133141      ) , 
134142    ) 
143+     const  verifiableClaims  =  claimIPFSHashPairs . map ( ( {  claim } )  =>  claim ) 
144+     await  Promise . all ( 
145+       verifiableClaims 
146+         . filter ( verifiableClaim  =>  Array . isArray ( verifiableClaim . claim . about ) ) 
147+         . map ( verifiableClaim  =>  insertGraphEdges ( verifiableClaim . id ,  verifiableClaim . claim . about  as  string [ ] ) ) ,  // See 1 
148+     ) 
135149  } 
136150
151+   const  insertGraphEdges  =  async  ( id : string ,  about : ReadonlyArray < string > )  => 
152+     graphCollection . insertMany ( about . map ( target  =>  ( {  origin : `poet:claims/${ id }  ,  target } ) ) ) 
153+ 
137154  return  { 
155+     createDbIndices, 
138156    createWork, 
139157    setIPFSHash, 
140158    setDirectoryHashOnEntries, 
@@ -144,3 +162,12 @@ export const WorkController = ({
144162    upsertClaimIPFSHashPair, 
145163  } 
146164} 
165+ 
166+ // 1. as string[]: TypeScript doesn't [yet] automatically narrow types in Array.filter unless an explicit type 
167+ // guard is provided. Proper solution would be something like 
168+ // ``` 
169+ // const isStandardVerifiableClaim = (x: VerifiableClaim): x is StandardVerifiableClaim => 
170+ //   Array.isArray(x.claim.about) && etc 
171+ // ... 
172+ // verifiableClaims.filter(isStandardVerifiableClaim).map(...) 
173+ // ``` 
0 commit comments