grid-sdk 0.3.5

Hyperledger Grid is a platform for building supply chain solutions that include distributed ledger components. It provides a growing set of tools that accelerate development for supply chain smart contractsand client interfaces.
Documentation
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// Copyright 2018-2021 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Database representations used to implement a diesel backend for the `PikeStore`.
//! These structs differ slightly from their associated native representation to conform to
//! the requirements for storing data with a diesel backend.

use chrono::NaiveDateTime;

use super::{Agent, AlternateId, Organization, OrganizationMetadata, Role};
use crate::commits::MAX_COMMIT_NUM;
use crate::pike::addressing::{
    compute_agent_address, compute_organization_address, compute_role_address,
};
use crate::pike::store::diesel::schema::*;

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_agent"]
pub struct NewAgentModel {
    pub state_address: String,
    pub public_key: String,
    pub org_id: String,
    pub active: bool,
    pub metadata: Vec<u8>,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `Agent`
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_agent"]
pub struct AgentModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub state_address: String,
    pub public_key: String,
    pub org_id: String,
    pub active: bool,
    pub metadata: Vec<u8>,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
    pub last_updated: Option<NaiveDateTime>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_role"]
pub struct NewRoleModel {
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub description: String,
    pub active: bool,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `Role`
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_role"]
pub struct RoleModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub description: String,
    pub active: bool,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
    pub last_updated: Option<NaiveDateTime>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_agent_role_assoc"]
pub struct NewRoleAssociationModel {
    pub agent_public_key: String,
    pub org_id: String,
    pub role_name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `Role` associated with an `Organization` and `Agent`
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_agent_role_assoc"]
pub struct RoleAssociationModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub agent_public_key: String,
    pub org_id: String,
    pub role_name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_role_state_address_assoc"]
pub struct NewRoleStateAddressAssociationModel {
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `Role` associated with a state address
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_role_state_address_assoc"]
pub struct RoleStateAddressAssociationModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_permissions"]
pub struct NewPermissionModel {
    pub role_name: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `Permission`
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_permissions"]
pub struct PermissionModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub role_name: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_inherit_from"]
pub struct NewInheritFromModel {
    pub role_name: String,
    pub org_id: String,
    pub inherit_from_role_name: String,
    pub inherit_from_org_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of the Pike `roles` that a `role` inherits attributes from
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_inherit_from"]
pub struct InheritFromModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub role_name: String,
    pub org_id: String,
    pub inherit_from_role_name: String,
    pub inherit_from_org_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_allowed_orgs"]
pub struct NewAllowedOrgModel {
    pub role_name: String,
    pub org_id: String,
    pub allowed_org_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of Pike `organizations` allowed to use the specified `Role`,
/// besides the defining `organization`
#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_allowed_orgs"]
pub struct AllowedOrgModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub role_name: String,
    pub org_id: String,
    pub allowed_org_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_organization"]
pub struct NewOrganizationModel {
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `organization`
#[derive(Queryable, PartialEq, Identifiable, Debug)]
#[table_name = "pike_organization"]
pub struct OrganizationModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub state_address: String,
    pub org_id: String,
    pub name: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
    pub last_updated: Option<NaiveDateTime>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_organization_metadata"]
pub struct NewOrganizationMetadataModel {
    pub org_id: String,
    pub key: String,
    pub value: Vec<u8>,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `organization`'s `metadata`
#[derive(Queryable, PartialEq, Identifiable, Debug)]
#[table_name = "pike_organization_metadata"]
pub struct OrganizationMetadataModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub org_id: String,
    pub key: String,
    pub value: Vec<u8>,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_organization_alternate_id"]
pub struct NewAlternateIdModel {
    pub org_id: String,
    pub alternate_id_type: String,
    pub alternate_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `alternate_id`
#[derive(Queryable, PartialEq, Identifiable, Debug)]
#[table_name = "pike_organization_alternate_id"]
pub struct AlternateIdModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub org_id: String,
    pub alternate_id_type: String,
    pub alternate_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

pub fn make_alternate_id_models(org: &Organization) -> Vec<NewAlternateIdModel> {
    let mut models = Vec::new();
    for entry in &org.alternate_ids {
        let model = NewAlternateIdModel {
            org_id: org.org_id.to_string(),
            alternate_id_type: entry.alternate_id_type.to_string(),
            alternate_id: entry.alternate_id.to_string(),
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id.clone(),
        };

        models.push(model);
    }

    models
}

#[derive(Insertable, PartialEq, Queryable, Debug)]
#[table_name = "pike_organization_location_assoc"]
pub struct NewLocationAssociationModel {
    pub org_id: String,
    pub location_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

/// Database model representation of a Pike `organization`'s associated `location`
#[derive(Queryable, PartialEq, Identifiable, Debug)]
#[table_name = "pike_organization_location_assoc"]
pub struct LocationAssociationModel {
    ///  This is the record id for the slowly-changing-dimensions table.
    pub id: i64,
    pub org_id: String,
    pub location_id: String,
    pub start_commit_num: i64,
    pub end_commit_num: i64,
    pub service_id: Option<String>,
}

impl From<&NewRoleModel> for NewRoleStateAddressAssociationModel {
    fn from(role: &NewRoleModel) -> Self {
        Self {
            state_address: compute_role_address(&role.name, &role.org_id),
            org_id: role.org_id.to_string(),
            name: role.name.to_string(),
            start_commit_num: role.start_commit_num,
            end_commit_num: MAX_COMMIT_NUM,
            service_id: role.service_id.clone(),
        }
    }
}

impl
    From<(
        RoleModel,
        Vec<InheritFromModel>,
        Vec<PermissionModel>,
        Vec<AllowedOrgModel>,
    )> for Role
{
    fn from(
        (role, inherit_from, permissions, allowed_orgs): (
            RoleModel,
            Vec<InheritFromModel>,
            Vec<PermissionModel>,
            Vec<AllowedOrgModel>,
        ),
    ) -> Self {
        let role_org_id = role.org_id;
        Self {
            org_id: role_org_id.to_string(),
            name: role.name,
            description: role.description,
            active: role.active,
            permissions: permissions.iter().map(|p| p.name.to_string()).collect(),
            allowed_organizations: allowed_orgs
                .iter()
                .map(|o| o.allowed_org_id.to_string())
                .collect(),
            inherit_from: inherit_from
                .iter()
                .map(|i| {
                    if i.inherit_from_org_id == role_org_id {
                        i.inherit_from_role_name.to_string()
                    } else {
                        format!("{}.{}", i.inherit_from_org_id, i.inherit_from_role_name)
                    }
                })
                .collect(),
            start_commit_num: role.start_commit_num,
            end_commit_num: role.end_commit_num,
            service_id: role.service_id,
            last_updated: role.last_updated.map(|d| d.timestamp()),
        }
    }
}

impl From<Role> for NewRoleModel {
    fn from(role: Role) -> Self {
        Self {
            state_address: compute_role_address(&role.name, &role.org_id),
            org_id: role.org_id,
            name: role.name,
            description: role.description,
            active: role.active,
            start_commit_num: role.start_commit_num,
            end_commit_num: MAX_COMMIT_NUM,
            service_id: role.service_id,
        }
    }
}

pub fn make_inherit_from_models(role: &Role) -> Vec<NewInheritFromModel> {
    let mut models = Vec::new();

    for i in &role.inherit_from {
        let mut ifoid = role.org_id.to_string();
        let mut ifname = i.to_string();
        if i.contains('.') {
            let inherit_from: Vec<&str> = i.split('.').collect();
            ifoid = inherit_from[0].to_string();
            ifname = inherit_from[1].to_string();
        }

        let model = NewInheritFromModel {
            role_name: role.name.to_string(),
            org_id: role.org_id.to_string(),
            inherit_from_role_name: ifname,
            inherit_from_org_id: ifoid,
            start_commit_num: role.start_commit_num,
            end_commit_num: role.end_commit_num,
            service_id: role.service_id.clone(),
        };

        models.push(model);
    }

    models
}

pub fn make_location_association_models(org: &Organization) -> Vec<NewLocationAssociationModel> {
    let mut models = Vec::new();

    for l in &org.locations {
        let model = NewLocationAssociationModel {
            org_id: org.org_id.to_string(),
            location_id: l.to_string(),
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id.clone(),
        };

        models.push(model);
    }

    models
}

pub fn make_permissions_models(role: &Role) -> Vec<NewPermissionModel> {
    let mut models = Vec::new();

    for p in &role.permissions {
        let model = NewPermissionModel {
            role_name: role.name.to_string(),
            org_id: role.org_id.to_string(),
            name: p.to_string(),
            start_commit_num: role.start_commit_num,
            end_commit_num: role.end_commit_num,
            service_id: role.service_id.clone(),
        };

        models.push(model);
    }

    models
}

pub fn make_allowed_orgs_models(role: &Role) -> Vec<NewAllowedOrgModel> {
    let mut models = Vec::new();

    for a in &role.allowed_organizations {
        let model = NewAllowedOrgModel {
            role_name: role.name.to_string(),
            org_id: role.org_id.to_string(),
            allowed_org_id: a.to_string(),
            start_commit_num: role.start_commit_num,
            end_commit_num: role.end_commit_num,
            service_id: role.service_id.clone(),
        };

        models.push(model)
    }

    models
}

impl From<(AgentModel, Vec<RoleAssociationModel>)> for Agent {
    fn from((agent_model, role_models): (AgentModel, Vec<RoleAssociationModel>)) -> Self {
        let agent_model_org_id = agent_model.org_id;
        Self {
            public_key: agent_model.public_key,
            org_id: agent_model_org_id.to_string(),
            active: agent_model.active,
            metadata: agent_model.metadata,
            roles: role_models
                .iter()
                .map(|role| {
                    if role.org_id == agent_model_org_id {
                        role.role_name.to_string()
                    } else {
                        format!("{}.{}", role.org_id, role.role_name)
                    }
                })
                .collect(),
            start_commit_num: agent_model.start_commit_num,
            end_commit_num: agent_model.end_commit_num,
            service_id: agent_model.service_id,
            last_updated: agent_model.last_updated.map(|d| d.timestamp()),
        }
    }
}

impl From<Agent> for NewAgentModel {
    fn from(agent: Agent) -> Self {
        Self {
            state_address: compute_agent_address(&agent.public_key),
            public_key: agent.public_key,
            org_id: agent.org_id,
            active: agent.active,
            metadata: agent.metadata,
            start_commit_num: agent.start_commit_num,
            end_commit_num: MAX_COMMIT_NUM,
            service_id: agent.service_id,
        }
    }
}

pub fn make_role_association_models(agent: &Agent) -> Vec<NewRoleAssociationModel> {
    let mut role_assocs = Vec::new();

    for role in &agent.roles {
        role_assocs.push(NewRoleAssociationModel {
            agent_public_key: agent.public_key.to_string(),
            org_id: agent.org_id.to_string(),
            role_name: role.to_string(),
            start_commit_num: agent.start_commit_num,
            end_commit_num: agent.end_commit_num,
            service_id: agent.service_id.clone(),
        })
    }

    role_assocs
}

impl
    From<(
        OrganizationModel,
        Vec<OrganizationMetadataModel>,
        Vec<AlternateIdModel>,
    )> for Organization
{
    fn from(
        (org, metadata, alternate_ids): (
            OrganizationModel,
            Vec<OrganizationMetadataModel>,
            Vec<AlternateIdModel>,
        ),
    ) -> Self {
        Self {
            org_id: org.org_id,
            name: org.name,
            locations: Vec::new(),
            alternate_ids: alternate_ids.iter().map(AlternateId::from).collect(),
            metadata: metadata.iter().map(OrganizationMetadata::from).collect(),
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id,
            last_updated: org.last_updated.map(|d| d.timestamp()),
        }
    }
}

impl
    From<(
        NewOrganizationModel,
        Vec<OrganizationMetadataModel>,
        Vec<AlternateIdModel>,
    )> for Organization
{
    fn from(
        (org, metadata, alternate_ids): (
            NewOrganizationModel,
            Vec<OrganizationMetadataModel>,
            Vec<AlternateIdModel>,
        ),
    ) -> Self {
        Self {
            org_id: org.org_id,
            name: org.name,
            locations: Vec::new(),
            alternate_ids: alternate_ids.iter().map(AlternateId::from).collect(),
            metadata: metadata.iter().map(OrganizationMetadata::from).collect(),
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id,
            last_updated: None,
        }
    }
}

impl
    From<(
        OrganizationModel,
        Vec<OrganizationMetadataModel>,
        Vec<AlternateIdModel>,
        Vec<LocationAssociationModel>,
    )> for Organization
{
    fn from(
        (org, metadata, alternate_ids, locations): (
            OrganizationModel,
            Vec<OrganizationMetadataModel>,
            Vec<AlternateIdModel>,
            Vec<LocationAssociationModel>,
        ),
    ) -> Self {
        Self {
            org_id: org.org_id,
            name: org.name,
            locations: locations
                .iter()
                .map(|l| l.location_id.to_string())
                .collect(),
            alternate_ids: alternate_ids.iter().map(AlternateId::from).collect(),
            metadata: metadata.iter().map(OrganizationMetadata::from).collect(),
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id,
            last_updated: org.last_updated.map(|d| d.timestamp()),
        }
    }
}

impl From<Organization> for NewOrganizationModel {
    fn from(org: Organization) -> Self {
        Self {
            state_address: compute_organization_address(&org.org_id),
            org_id: org.org_id,
            name: org.name,
            start_commit_num: org.start_commit_num,
            end_commit_num: org.end_commit_num,
            service_id: org.service_id,
        }
    }
}

impl From<&AlternateIdModel> for AlternateId {
    fn from(id: &AlternateIdModel) -> Self {
        Self {
            org_id: id.org_id.to_string(),
            alternate_id_type: id.alternate_id_type.to_string(),
            alternate_id: id.alternate_id.to_string(),
            start_commit_num: id.start_commit_num,
            end_commit_num: id.end_commit_num,
            service_id: id.service_id.clone(),
        }
    }
}

impl From<&AlternateId> for NewAlternateIdModel {
    fn from(alt_id: &AlternateId) -> Self {
        Self {
            org_id: alt_id.org_id.to_string(),
            alternate_id_type: alt_id.alternate_id_type.to_string(),
            alternate_id: alt_id.alternate_id.to_string(),
            start_commit_num: alt_id.start_commit_num,
            end_commit_num: alt_id.end_commit_num,
            service_id: alt_id.service_id.clone(),
        }
    }
}

impl From<NewOrganizationMetadataModel> for OrganizationMetadata {
    fn from(metadata: NewOrganizationMetadataModel) -> Self {
        Self {
            key: metadata.key,
            value: String::from_utf8(metadata.value).unwrap(),
            start_commit_num: metadata.start_commit_num,
            end_commit_num: metadata.end_commit_num,
            service_id: metadata.service_id,
        }
    }
}

impl From<&OrganizationMetadataModel> for OrganizationMetadata {
    fn from(metadata: &OrganizationMetadataModel) -> Self {
        Self {
            key: metadata.key.to_string(),
            value: String::from_utf8(metadata.value.clone()).unwrap(),
            start_commit_num: metadata.start_commit_num,
            end_commit_num: metadata.end_commit_num,
            service_id: metadata.service_id.clone(),
        }
    }
}

pub fn make_org_metadata_models(org: &Organization) -> Vec<NewOrganizationMetadataModel> {
    let mut metadata = Vec::new();

    for data in &org.metadata {
        metadata.push(NewOrganizationMetadataModel {
            org_id: org.org_id.to_string(),
            key: data.key.to_string(),
            value: data.value.as_bytes().to_vec(),
            start_commit_num: data.start_commit_num,
            end_commit_num: data.end_commit_num,
            service_id: data.service_id.clone(),
        })
    }

    metadata
}