1
- import { ContextProps , WalletDocument } from '../../types' ;
2
- import { v4 as uuid } from 'uuid' ;
3
- import { DocumentEntity } from './document.entity' ;
4
- import { getOrCreateDocumentTypes , saveOptions } from './helpers' ;
5
- import { getDocumentById } from './get-document-by-id' ;
6
- import { logger } from '../../logger' ;
1
+ import { ContextProps , WalletDocument } from '../../types' ;
2
+ import { v4 as uuid } from 'uuid' ;
3
+ import { DocumentEntity } from './document.entity' ;
4
+ import { getOrCreateDocumentTypes , saveOptions } from './helpers' ;
5
+ import { getDocumentById } from './get-document-by-id' ;
6
+ import { logger } from '../../logger' ;
7
7
8
8
/**
9
9
* Create document
@@ -18,57 +18,55 @@ export async function createDocument({
18
18
} ) : Promise < WalletDocument > {
19
19
logger . debug ( `Creating document with id ${ json . id } ...` ) ;
20
20
21
- return dataStore . db . transaction ( async transactionalEntityManager => {
22
- if ( json . id ) {
23
- const existingDocument = await getDocumentById ( {
24
- dataStore,
25
- id : json . id ,
26
- } ) ;
27
-
28
- if ( existingDocument ) {
29
- logger . debug ( `Document with id ${ json . id } already exists` ) ;
30
- throw new Error ( `Document with id ${ json . id } already exists` ) ;
31
- }
32
- }
33
-
34
- const _typeRel = await getOrCreateDocumentTypes ( {
21
+ if ( json . id ) {
22
+ const existingDocument = await getDocumentById ( {
35
23
dataStore,
36
- types : json . type ,
24
+ id : json . id ,
37
25
} ) ;
38
26
39
- if ( ! json . id ) {
40
- json . id = uuid ( ) ;
27
+ if ( existingDocument ) {
28
+ logger . debug ( `Document with id ${ json . id } already exists` ) ;
29
+ throw new Error ( `Document with id ${ json . id } already exists` ) ;
41
30
}
31
+ }
42
32
43
- let networkId ;
33
+ const _typeRel = await getOrCreateDocumentTypes ( {
34
+ dataStore,
35
+ types : json . type ,
36
+ } ) ;
44
37
45
- if ( json . _networkId ) {
46
- networkId = json . _networkId ;
47
- delete json . _networkId ;
48
- } else {
49
- const resolution = await dataStore . resolveDocumentNetwork ( {
50
- document : json ,
51
- dataStore,
52
- } ) ;
38
+ if ( ! json . id ) {
39
+ json . id = uuid ( ) ;
40
+ }
53
41
54
- networkId = resolution . networkId ;
55
- }
42
+ let networkId ;
56
43
57
- const entity : DocumentEntity = {
58
- networkId ,
59
- id : json . id ,
60
- type : json . type ,
61
- _typeRel ,
62
- correlation : json . correlation || [ ] ,
63
- data : JSON . stringify ( json ) ,
64
- } ;
44
+ if ( json . _networkId ) {
45
+ networkId = json . _networkId ;
46
+ delete json . _networkId ;
47
+ } else {
48
+ const resolution = await dataStore . resolveDocumentNetwork ( {
49
+ document : json ,
50
+ dataStore ,
51
+ } ) ;
65
52
66
- const repository = transactionalEntityManager . getRepository ( DocumentEntity ) ;
53
+ networkId = resolution . networkId ;
54
+ }
67
55
68
- const result = await repository . save ( entity , saveOptions ) ;
56
+ const entity : DocumentEntity = {
57
+ networkId,
58
+ id : json . id ,
59
+ type : json . type ,
60
+ _typeRel,
61
+ correlation : json . correlation || [ ] ,
62
+ data : JSON . stringify ( json ) ,
63
+ } ;
69
64
70
- logger . debug ( `Document added to the wallet` ) ;
65
+ const repository = dataStore . db . getRepository ( DocumentEntity ) ;
71
66
72
- return result ;
73
- } ) ;
67
+ const result = await repository . save ( entity , saveOptions ) ;
68
+
69
+ logger . debug ( `Document added to the wallet` ) ;
70
+
71
+ return result ;
74
72
}
0 commit comments