/
composition.go
611 lines (488 loc) · 20.7 KB
/
composition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package composition
import (
"context"
"encoding/json"
"fmt"
"time"
"golang.org/x/crypto/sha3"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/vmihailenco/msgpack/v5"
"github.com/bsn-si/IPEHR-gateway/src/pkg/access"
"github.com/bsn-si/IPEHR-gateway/src/pkg/common"
"github.com/bsn-si/IPEHR-gateway/src/pkg/crypto/chachaPoly"
"github.com/bsn-si/IPEHR-gateway/src/pkg/crypto/keybox"
"github.com/bsn-si/IPEHR-gateway/src/pkg/docs/model"
"github.com/bsn-si/IPEHR-gateway/src/pkg/docs/model/base"
proc "github.com/bsn-si/IPEHR-gateway/src/pkg/docs/service/processing"
"github.com/bsn-si/IPEHR-gateway/src/pkg/docs/status"
"github.com/bsn-si/IPEHR-gateway/src/pkg/docs/types"
"github.com/bsn-si/IPEHR-gateway/src/pkg/errors"
"github.com/bsn-si/IPEHR-gateway/src/pkg/helper"
"github.com/bsn-si/IPEHR-gateway/src/pkg/indexer"
"github.com/bsn-si/IPEHR-gateway/src/pkg/indexer/ehrIndexer"
"github.com/bsn-si/IPEHR-gateway/src/pkg/storage/treeindex"
)
type (
GroupAccessService interface {
Default() *uuid.UUID
}
Indexer interface {
MultiCallEhrNew() *indexer.MultiCallTx
GetDocByVersion(ctx context.Context, ehrUUID *uuid.UUID, docType types.DocumentType, docBaseUIDHash, version *[32]byte) (*model.DocumentMeta, error)
AddEhrDoc(docType types.DocumentType, docMeta *model.DocumentMeta, privKey *[32]byte) ([]byte, error)
GetDocLastByBaseID(ctx context.Context, userID, systemID string, docType types.DocumentType, docBaseUIDHash *[32]byte) (*model.DocumentMeta, error)
DeleteDoc(ctx context.Context, ehrUUID *uuid.UUID, docType types.DocumentType, docBaseUIDHash, version, privKey *[32]byte) (string, error)
ListDocByType(ctx context.Context, userID, systemID string, docType types.DocumentType) ([]model.DocumentMeta, error)
DataUpdate(ctx context.Context, groupID, dataID, ehrID *uuid.UUID, data []byte) (string, error)
SetAccessWrapper(subjectIDHash *[32]byte, accessObj *indexer.AccessObject, userPrivKey *[32]byte) ([]byte, error)
}
IpfsService interface {
Add(ctx context.Context, fileContent []byte) (*cid.Cid, error)
}
FileCoinService interface {
StartDeal(ctx context.Context, CID *cid.Cid, dataSizeBytes uint64) (*cid.Cid, string, error)
}
DocumentsSvc interface {
GetDocFromStorageByID(ctx context.Context, userID, systemID string, CID *cid.Cid, authData, docIDEncrypted []byte) ([]byte, error)
GetDocAccessKey(ctx context.Context, userID, systemID string, CID *cid.Cid) (*chachaPoly.Key, error)
DecryptKey(userID string, encryptedKey []byte) (*chachaPoly.Key, error)
}
KeyStore interface {
Get(userID string) (publicKey, privateKey *[32]byte, err error)
}
Compressor interface {
Compress(decompressedData []byte) (compressedData []byte, err error)
}
Service struct {
helper.Finder
indexer Indexer
ipfs IpfsService
fileCoin FileCoinService
keyStore KeyStore
compressor Compressor
docSvc DocumentsSvc
groupAccessService GroupAccessService
}
)
func NewCompositionService(
indexer Indexer,
ipfs IpfsService,
fileCoin FileCoinService,
keyStore KeyStore,
compressor Compressor,
docSvc DocumentsSvc,
groupAccessService GroupAccessService,
) *Service {
return &Service{
docSvc: docSvc,
indexer: indexer,
ipfs: ipfs,
fileCoin: fileCoin,
keyStore: keyStore,
compressor: compressor,
groupAccessService: groupAccessService,
}
}
func (s *Service) Create(ctx context.Context, userID, systemID string, ehrUUID, groupAccessUUID *uuid.UUID, composition *model.Composition, procRequest *proc.Request) (*model.Composition, error) {
objectVersionID, err := base.NewObjectVersionID(composition.UID.Value, systemID)
if err != nil {
return nil, fmt.Errorf("NewObjectVersionID error: %w UID.Value: %s ehrSystemID %s", err, composition.UID.Value, systemID)
}
// Checking the existence of the Composition
docMeta, err := s.indexer.GetDocByVersion(ctx, ehrUUID, types.Composition, objectVersionID.BaseIDHash(), objectVersionID.VersionBytes())
if err != nil && !errors.Is(err, errors.ErrNotFound) {
return nil, fmt.Errorf("Index.GetDocByVersion error: %w", err)
} else if docMeta != nil {
return nil, fmt.Errorf("%w objectVersionID %s", errors.ErrAlreadyExist, objectVersionID.String())
}
multiCallTx := s.indexer.MultiCallEhrNew()
dataIndexUUID := uuid.New()
err = s.save(ctx, multiCallTx, procRequest, userID, systemID, &dataIndexUUID, composition)
if err != nil {
return nil, fmt.Errorf("Composition %s save error: %w", composition.UID.Value, err)
}
txHash, err := multiCallTx.Commit()
if err != nil {
return nil, fmt.Errorf("Create composition commit error: %w", err)
}
for _, txKind := range multiCallTx.GetTxKinds() {
procRequest.AddEthereumTx(proc.TxKind(txKind), txHash)
}
err = s.addDataIndex(ctx, ehrUUID, groupAccessUUID, &dataIndexUUID, composition, procRequest)
if err != nil {
return nil, fmt.Errorf("addDataIndex error: %w", err)
}
return composition, nil
}
func (s *Service) Update(ctx context.Context, procRequest *proc.Request, userID, systemID string, ehrUUID, groupAccessUUID *uuid.UUID, composition *model.Composition) (*model.Composition, error) {
err := s.increaseVersion(composition, systemID)
if err != nil {
return nil, fmt.Errorf("Composition increaseVersion error: %w composition.UID %s", err, composition.UID.Value)
}
objectVersionID, err := base.NewObjectVersionID(composition.UID.Value, systemID)
if err != nil {
return nil, fmt.Errorf("NewObjectVersionID error: %w UID.Value: %s ehrSystemID %s", err, composition.UID.Value, systemID)
}
// Checking the existence of the Composition same version
docMeta, err := s.indexer.GetDocByVersion(ctx, ehrUUID, types.Composition, objectVersionID.BaseIDHash(), objectVersionID.VersionBytes())
if err != nil && !errors.Is(err, errors.ErrNotFound) {
return nil, fmt.Errorf("Index.GetDocByVersion error: %w", err)
} else if docMeta != nil {
return nil, fmt.Errorf("%w objectVersionID %s", errors.ErrAlreadyExist, objectVersionID.String())
}
// Getting last Composition version
docMeta, err = s.indexer.GetDocLastByBaseID(ctx, userID, systemID, types.Composition, objectVersionID.BaseIDHash())
if err != nil {
return nil, fmt.Errorf("indexer.GetDocLastByBaseID error: %w", err)
}
// Getting dataIndexID
dataIndexIDBytes, err := s.extractDataIndexID(ctx, docMeta, userID, systemID)
if err != nil {
return nil, fmt.Errorf("extractDataIndexID error: %w", err)
}
dataIndexUUID, err := uuid.FromBytes(dataIndexIDBytes)
if err != nil {
return nil, fmt.Errorf("dataIndexID UUID parse error: %w", err)
}
multiCallTx := s.indexer.MultiCallEhrNew()
err = s.save(ctx, multiCallTx, procRequest, userID, systemID, &dataIndexUUID, composition)
if err != nil {
return nil, fmt.Errorf("Composition save error: %w userID %s ehrUUID %s composition.UID %s", err, userID, ehrUUID.String(), composition.UID.Value)
}
txHash, err := multiCallTx.Commit()
if err != nil {
return nil, fmt.Errorf("Update composition commit error: %w", err)
}
for _, txKind := range multiCallTx.GetTxKinds() {
procRequest.AddEthereumTx(proc.TxKind(txKind), txHash)
}
// TODO what we should do with prev composition?
// Adding dataStore index
err = s.addDataIndex(ctx, ehrUUID, groupAccessUUID, &dataIndexUUID, composition, procRequest)
if err != nil {
return nil, fmt.Errorf("addDataIndex error: %w", err)
}
return composition, nil
}
func (s *Service) increaseVersion(c *model.Composition, ehrSystemID string) error {
if c == nil || c.UID == nil || c.UID.Value == "" {
return fmt.Errorf("%w Incorrect composition UID", errors.ErrIncorrectFormat)
}
objectVersionID, err := base.NewObjectVersionID(c.UID.Value, ehrSystemID)
if err != nil {
return fmt.Errorf("increaseVersion error: %w versionUID %s ehrSystemID %s", err, objectVersionID.String(), ehrSystemID)
}
if _, err := objectVersionID.IncreaseUIDVersion(); err != nil {
return fmt.Errorf("Composition %s IncreaseUIDVersion error: %w", c.UID.Value, err)
}
c.UID.Value = objectVersionID.String()
return nil
}
func (s *Service) save(ctx context.Context, multiCallTx *indexer.MultiCallTx, procRequest *proc.Request, userID, systemID string, dataIndexUUID *uuid.UUID, doc *model.Composition) error {
userPubKey, userPrivKey, err := s.keyStore.Get(userID)
if err != nil {
return fmt.Errorf("Keystore.Get error: %w userID %s", err, userID)
}
objectVersionID, err := base.NewObjectVersionID(doc.UID.Value, systemID)
if err != nil {
return fmt.Errorf("saving error: %w versionUID %s ehrSystemID %s", err, objectVersionID, systemID)
}
docBytes, err := json.Marshal(doc)
if err != nil {
return fmt.Errorf("Composition marshal error: %w", err)
}
if s.compressor != nil {
docBytes, err = s.compressor.Compress(docBytes)
if err != nil {
return fmt.Errorf("Compress error: %w", err)
}
}
key := chachaPoly.GenerateKey()
docEncrypted, err := key.EncryptWithAuthData(docBytes, []byte(objectVersionID.String()))
if err != nil {
return fmt.Errorf("EncryptWithAuthData error: %w", err)
}
CID, err := s.ipfs.Add(ctx, docEncrypted)
if err != nil {
return fmt.Errorf("IpfsClient.Add error: %w", err)
}
dealCID, minerAddr, err := s.fileCoin.StartDeal(ctx, CID, uint64(len(docEncrypted)))
if err != nil {
return fmt.Errorf("FilecoinClient.StartDeal error: %w", err)
}
procRequest.AddFilecoinTx(proc.TxSaveComposition, CID.String(), dealCID.String(), minerAddr)
err = s.addMetaData(multiCallTx, key, objectVersionID, CID, dealCID, minerAddr, doc.Name.Value, dataIndexUUID, userPubKey, userPrivKey)
if err != nil {
return fmt.Errorf("addMetaData error: %w", err)
}
err = s.setDocAccess(multiCallTx, userID, systemID, CID, key, access.Owner, userPubKey, userPrivKey)
if err != nil {
return fmt.Errorf("setDocAccess error: %w", err)
}
return nil
}
func (s *Service) addMetaData(multiCallTx *indexer.MultiCallTx, key *chachaPoly.Key, objectVersionID *base.ObjectVersionID, CID, dealCID *cid.Cid, minerAddr, docName string, dataIndexUUID *uuid.UUID, userPubKey, userPrivKey *[32]byte) error {
keyEncr, err := keybox.SealAnonymous(key.Bytes(), userPubKey)
if err != nil {
return fmt.Errorf("keybox.SealAnonymous error: %w", err)
}
nameEncr, err := key.Encrypt([]byte(docName))
if err != nil {
return fmt.Errorf("Encrypt name error: %w", err)
}
docIDEncrypted, err := key.Encrypt([]byte(objectVersionID.String()))
if err != nil {
return fmt.Errorf("EncryptWithAuthData error: %w", err)
}
dataIndexIDEncr, err := key.Encrypt(dataIndexUUID[:])
if err != nil {
return fmt.Errorf("Encrypt dataIndexID error: %w", err)
}
CIDEncr, err := key.Encrypt(CID.Bytes())
if err != nil {
return fmt.Errorf("CID encryption error error: %w", err)
}
docMeta := &model.DocumentMeta{
Status: uint8(status.ACTIVE),
Id: CID.Bytes(),
Version: objectVersionID.VersionBytes()[:],
Timestamp: uint32(time.Now().Unix()),
IsLast: true,
Attrs: []ehrIndexer.AttributesAttribute{
{Code: model.AttributeIDEncr, Value: CIDEncr},
{Code: model.AttributeKeyEncr, Value: keyEncr},
{Code: model.AttributeDocUIDHash, Value: objectVersionID.BaseIDHash()[:]},
{Code: model.AttributeDocUIDEncr, Value: docIDEncrypted},
{Code: model.AttributeDealCid, Value: dealCID.Bytes()},
{Code: model.AttributeMinerAddress, Value: []byte(minerAddr)},
{Code: model.AttributeNameEncr, Value: nameEncr},
{Code: model.AttributeDataIndexID, Value: dataIndexIDEncr},
},
}
packed, err := s.indexer.AddEhrDoc(types.Composition, docMeta, userPrivKey)
if err != nil {
return fmt.Errorf("Index.AddEhrDoc error: %w", err)
}
multiCallTx.Add(uint8(proc.TxAddEhrDoc), packed)
return nil
}
func (s *Service) setDocAccess(multiCallTx *indexer.MultiCallTx, userID, systemID string, CID *cid.Cid, key *chachaPoly.Key, accessLevel access.Level, userPubKey, userPrivKey *[32]byte) error {
userIDHash := sha3.Sum256([]byte(userID + systemID))
docIDHash := indexer.Keccak256(CID.Bytes())
CIDEncr, err := key.Encrypt(CID.Bytes())
if err != nil {
return fmt.Errorf("CID encryption error: %w", err)
}
keyEncr, err := keybox.SealAnonymous(key.Bytes(), userPubKey)
if err != nil {
return fmt.Errorf("keybox.SealAnonymous error: %w", err)
}
accessObj := indexer.AccessObject{
Kind: access.Doc,
IdHash: *docIDHash,
IdEncr: CIDEncr,
KeyEncr: keyEncr,
Level: accessLevel,
}
packed, err := s.indexer.SetAccessWrapper(&userIDHash, &accessObj, userPrivKey)
if err != nil {
return fmt.Errorf("Index.SetAccess user to composition error: %w", err)
}
multiCallTx.Add(uint8(proc.TxSetDocAccess), packed)
return nil
}
func (s *Service) GetLastByBaseID(ctx context.Context, userID, systemID string, ehrUUID *uuid.UUID, versionUID string) (*model.Composition, error) {
objectVersionID, err := base.NewObjectVersionID(versionUID, systemID)
if err != nil {
return nil, fmt.Errorf("GetLastByBaseID error: %w versionUID %s ehrSystemID %s", err, objectVersionID.String(), systemID)
}
baseDocumentUIDHash := sha3.Sum256([]byte(objectVersionID.BasedID()))
docMeta, err := s.indexer.GetDocLastByBaseID(ctx, userID, systemID, types.Composition, &baseDocumentUIDHash)
if err != nil {
return nil, fmt.Errorf("GetLastVersionDocIndexByBaseID error: %w userID %s objectVersionID %s", err, userID, objectVersionID)
}
if docMeta.Status == uint8(status.DELETED) {
return nil, fmt.Errorf("GetLastByBaseID error: %w", errors.ErrAlreadyDeleted)
}
CID, err := cid.Parse(docMeta.Id)
if err != nil {
return nil, fmt.Errorf("cid.Parse error: %w", err)
}
docUIDEncrypted := docMeta.GetAttr(model.AttributeDocUIDEncr)
if docUIDEncrypted == nil {
return nil, errors.ErrFieldIsEmpty("DocUIDEncrypted")
}
docDecrypted, err := s.docSvc.GetDocFromStorageByID(ctx, userID, systemID, &CID, ehrUUID[:], docUIDEncrypted)
if err != nil && errors.Is(err, errors.ErrIsInProcessing) {
return nil, err
} else if err != nil {
return nil, fmt.Errorf("GetDocFromStorageByID error: %w userID %s storageID %s", err, userID, &CID)
}
var composition *model.Composition
if err = json.Unmarshal(docDecrypted, &composition); err != nil {
return nil, fmt.Errorf("Composition unmarshal error: %w", err)
}
return composition, nil
}
func (s *Service) GetByID(ctx context.Context, userID, systemID string, ehrUUID *uuid.UUID, versionUID string) (*model.Composition, error) {
objectVersionID, err := base.NewObjectVersionID(versionUID, systemID)
if err != nil {
return nil, fmt.Errorf("NewObjectVersionID error: %w versionUID %s ehrSystemID %s", err, versionUID, systemID)
}
baseDocumentUIDHash := sha3.Sum256([]byte(objectVersionID.BasedID()))
docMeta, err := s.indexer.GetDocByVersion(ctx, ehrUUID, types.Composition, &baseDocumentUIDHash, objectVersionID.VersionBytes())
if err != nil && errors.Is(err, errors.ErrNotFound) {
return nil, errors.ErrNotFound
} else if err != nil {
return nil, fmt.Errorf("Index.GetDocByVersion error: %w ehrUUID %s objectVersionID %s", err, ehrUUID.String(), objectVersionID.String())
}
if docMeta.Status == uint8(status.DELETED) {
return nil, fmt.Errorf("GetCompositionByID error: %w", errors.ErrAlreadyDeleted)
}
CID, err := cid.Parse(docMeta.Id)
if err != nil {
return nil, fmt.Errorf("cid.Parse error: %w", err)
}
docUIDEncrypted := docMeta.GetAttr(model.AttributeDocUIDEncr)
if docUIDEncrypted == nil {
return nil, errors.ErrFieldIsEmpty("DocUIDEncrypted")
}
docDecrypted, err := s.docSvc.GetDocFromStorageByID(ctx, userID, systemID, &CID, ehrUUID[:], docUIDEncrypted)
if err != nil && errors.Is(err, errors.ErrIsInProcessing) {
return nil, err
} else if err != nil {
return nil, fmt.Errorf("GetDocFromStorageByID error: %w userID %s CID %x", err, userID, CID.String())
}
var composition model.Composition
if err = json.Unmarshal(docDecrypted, &composition); err != nil {
return nil, fmt.Errorf("Composition unmarshal error: %w", err)
}
return &composition, nil
}
func (s *Service) DeleteByID(ctx context.Context, procRequest *proc.Request, ehrUUID *uuid.UUID, versionUID, userID, systemID string) (string, error) {
objectVersionID, err := base.NewObjectVersionID(versionUID, systemID)
if err != nil {
return "", fmt.Errorf("NewObjectVersionID error: %w versionUID %s ehrSystemID %s", err, versionUID, systemID)
}
_, userPrivKey, err := s.keyStore.Get(userID)
if err != nil {
return "", fmt.Errorf("Keystore.Get error: %w userID %s", err, userID)
}
baseDocumentUID := []byte(objectVersionID.BasedID())
baseDocumentUIDHash := sha3.Sum256(baseDocumentUID)
txHash, err := s.indexer.DeleteDoc(ctx, ehrUUID, types.Composition, &baseDocumentUIDHash, objectVersionID.VersionBytes(), userPrivKey)
if err != nil {
if errors.Is(err, errors.ErrNotFound) {
return "", err
}
return "", fmt.Errorf("Index.DeleteDoc error: %w", err)
}
procRequest.AddEthereumTx(proc.TxDeleteDoc, txHash)
if _, err = objectVersionID.IncreaseUIDVersion(); err != nil {
return "", fmt.Errorf("IncreaseUIDVersion error: %w objectVersionID %s", err, objectVersionID.String())
}
return objectVersionID.String(), nil
}
func (s *Service) DefaultGroupAccess() *uuid.UUID {
return s.groupAccessService.Default()
}
func (s *Service) GetList(ctx context.Context, userID, systemID string) ([]*model.EhrDocumentItem, error) {
compositions, err := s.indexer.ListDocByType(ctx, userID, systemID, types.Composition)
if err != nil {
if errors.Is(err, errors.ErrNotFound) {
return nil, err
}
return nil, fmt.Errorf("ListDocByType error: %w", err)
}
var list []*model.EhrDocumentItem
for _, c := range compositions {
keyEncr := model.AttributesEhr(c.Attrs).GetByCode(model.AttributeKeyEncr)
if keyEncr == nil {
return nil, fmt.Errorf("%w: Composition %x meta field KeyEncr is empty", errors.ErrCustom, c.Id)
}
nameEncr := model.AttributesEhr(c.Attrs).GetByCode(model.AttributeNameEncr)
if nameEncr == nil {
return nil, fmt.Errorf("%w: Composition %x meta field NameEncr is empty", errors.ErrCustom, c.Id)
}
uidEncr := model.AttributesEhr(c.Attrs).GetByCode(model.AttributeDocUIDEncr)
if uidEncr == nil {
return nil, fmt.Errorf("%w: Composition %x meta field DocUIDEncr is empty", errors.ErrCustom, c.Id)
}
docKey, err := s.docSvc.DecryptKey(userID, keyEncr)
if err != nil {
return nil, fmt.Errorf("DecryptKey error: %w", err)
}
name, err := docKey.Decrypt(nameEncr)
if err != nil {
return nil, fmt.Errorf("Composition %x Name decryption error: %w", c.Id, err)
}
uid, err := docKey.Decrypt(uidEncr)
if err != nil {
return nil, fmt.Errorf("Composition %x UID decryption error: %w", c.Id, err)
}
item := model.EhrDocumentItem{
Name: string(name),
UID: string(uid),
TimeCreated: time.Unix(int64(c.Timestamp), 0).Format(common.OpenEhrTimeFormat),
}
list = append(list, &item)
}
return list, nil
}
func (s *Service) IsExist(ctx context.Context, args ...string) (bool, error) {
if len(args) != 4 {
return false, fmt.Errorf("%w: Expected args: userID, systemID, ehrID, versionUID", errors.ErrCustom)
}
userID, systemID, ehrID, versionUID := args[0], args[1], args[2], args[3]
ehrUUID, err := uuid.Parse(ehrID)
if err != nil {
return false, fmt.Errorf("uuid.Parse error: %w", err)
}
ok, err := s.GetLastByBaseID(ctx, userID, systemID, &ehrUUID, versionUID)
if err != nil {
return false, fmt.Errorf("GetLastByBaseID error: %w", err)
}
return (ok != nil), nil
}
func (s *Service) extractDataIndexID(ctx context.Context, docMeta *model.DocumentMeta, userID, systemID string) ([]byte, error) {
CID, err := cid.Parse(docMeta.Id)
if err != nil {
return nil, fmt.Errorf("cid.Parse error: %w", err)
}
docKey, err := s.docSvc.GetDocAccessKey(ctx, userID, systemID, &CID)
if err != nil {
return nil, fmt.Errorf("GetDocAccessKey error: %w", err)
}
dataIndexIDEncr := docMeta.GetAttr(model.AttributeDataIndexID)
if dataIndexIDEncr == nil {
return nil, errors.ErrFieldIsEmpty("dataIndexID")
}
dataIndexID, err := docKey.Decrypt(dataIndexIDEncr)
if err != nil {
return nil, fmt.Errorf("dataIndexID decryption error: %w", err)
}
return dataIndexID, nil
}
func (s *Service) addDataIndex(ctx context.Context, ehrUUID, groupAccessUUID, dataIndexUUID *uuid.UUID, cmp *model.Composition, procRequest *proc.Request) error {
node, err := treeindex.ProcessComposition(cmp)
if err != nil {
return fmt.Errorf(" treeindex.ProcessComposition error: %w", err)
}
data, err := msgpack.Marshal(node)
if err != nil {
return fmt.Errorf("msgpack.Marshal(ehrNode) error: %w", err)
}
compressed, err := s.compressor.Compress(data)
if err != nil {
return fmt.Errorf("data compressinon error: %w", err)
}
//DEBUG
fmt.Println("Composition compressed data length: ", len(compressed))
txHash, err := s.indexer.DataUpdate(ctx, groupAccessUUID, dataIndexUUID, ehrUUID, compressed)
if err != nil {
return fmt.Errorf("Index.DataUpdate error: %w", err)
}
procRequest.AddEthereumTx(proc.TxIndexDataUpdate, txHash)
return nil
}