-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTenantRepository.cs
More file actions
151 lines (136 loc) · 6.24 KB
/
TenantRepository.cs
File metadata and controls
151 lines (136 loc) · 6.24 KB
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
/********************************************************************************
* Copyright (c) 2024 BMW Group AG
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
using Dim.DbAccess.Models;
using Dim.Entities;
using Dim.Entities.Entities;
using Dim.Entities.Enums;
using Microsoft.EntityFrameworkCore;
namespace Dim.DbAccess.Repositories;
public class TenantRepository(DimDbContext dbContext)
: ITenantRepository
{
public Tenant CreateTenant(string companyName, string bpn, string didDocumentLocation, bool isIssuer, Guid processId, Guid operatorId) =>
dbContext.Tenants.Add(new Tenant(Guid.NewGuid(), companyName, bpn, didDocumentLocation, isIssuer, processId, operatorId)).Entity;
public Task<(bool Exists, Guid TenantId, string CompanyName, string Bpn)> GetTenantDataForProcessId(Guid processId) =>
dbContext.Tenants
.Where(x => x.ProcessId == processId)
.Select(x => new ValueTuple<bool, Guid, string, string>(true, x.Id, x.CompanyName, x.Bpn))
.SingleOrDefaultAsync();
public void AttachAndModifyTenant(Guid tenantId, Action<Tenant>? initialize, Action<Tenant> modify)
{
var tenant = new Tenant(tenantId, null!, null!, null!, default, Guid.Empty, Guid.Empty);
initialize?.Invoke(tenant);
dbContext.Tenants.Attach(tenant);
modify(tenant);
}
public Task<(bool IsIssuer, string? HostingUrl)> GetHostingUrlAndIsIssuer(Guid tenantId)
=> dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<bool, string?>(x.IsIssuer, x.DidDocumentLocation))
.SingleOrDefaultAsync();
public Task<(bool Exists, Guid TenantId)> GetTenantForBpn(string bpn) =>
dbContext.Tenants.Where(x => x.Bpn == bpn)
.Select(x => new ValueTuple<bool, Guid>(true, x.Id))
.SingleOrDefaultAsync();
public Task<bool> IsTenantExisting(string companyName, string bpn) =>
dbContext.Tenants
.AnyAsync(x => x.CompanyName == companyName && x.Bpn == bpn);
public Task<Guid?> GetOperationId(Guid tenantId) =>
dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => x.OperationId)
.SingleOrDefaultAsync();
public Task<(string? BaseUrl, WalletData WalletData)> GetCompanyRequestData(Guid tenantId) =>
dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<string?, WalletData>(
x.BaseUrl,
new WalletData(
x.TokenAddress,
x.ClientId,
x.ClientSecret,
x.InitializationVector,
x.EncryptionMode
)))
.SingleOrDefaultAsync();
public Task<(bool Exists, Guid? CompanyId, string? BaseUrl, WalletData WalletData)> GetCompanyAndWalletDataForBpn(string bpn) =>
dbContext.Tenants
.Where(x => x.Bpn == bpn)
.Select(x => new ValueTuple<bool, Guid?, string?, WalletData>(
true,
x.CompanyId,
x.BaseUrl,
new WalletData(
x.TokenAddress,
x.ClientId,
x.ClientSecret,
x.InitializationVector,
x.EncryptionMode
)))
.SingleOrDefaultAsync();
public Task<(Guid? CompanyId, string? BaseUrl, WalletData WalletData)> GetStatusListCreationData(Guid tenantId) =>
dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<Guid?, string?, WalletData>(
x.CompanyId,
x.BaseUrl,
new WalletData(
x.TokenAddress,
x.ClientId,
x.ClientSecret,
x.InitializationVector,
x.EncryptionMode
)))
.SingleOrDefaultAsync();
public Task<(string Bpn, Guid? CompanyId, string? BaseUrl, WalletData WalletData, string? Did, string? DownloadUrl)> GetCallbackData(Guid tenantId) =>
dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<string, Guid?, string?, WalletData, string?, string?>(
x.Bpn,
x.CompanyId,
x.BaseUrl,
new WalletData(
x.TokenAddress,
x.ClientId,
x.ClientSecret,
x.InitializationVector,
x.EncryptionMode
),
x.Did,
x.DidDownloadUrl))
.SingleOrDefaultAsync();
public Task<(string? DownloadUrl, bool IsIssuer)> GetDownloadUrlAndIsIssuer(Guid tenantId) =>
dbContext.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<string?, bool>(x.DidDownloadUrl, x.IsIssuer))
.SingleOrDefaultAsync();
public Task<ProcessData?> GetWalletProcessForTenant(string bpn, string companyName) =>
dbContext.Tenants
.Where(x =>
x.Bpn == bpn &&
x.CompanyName == companyName &&
x.Process!.ProcessTypeId == ProcessTypeId.SETUP_DIM)
.Select(x => new ProcessData(
x.ProcessId,
x.Process!.ProcessSteps.Select(ps => new ProcessStepData(
ps.ProcessStepTypeId,
ps.ProcessStepStatusId))))
.SingleOrDefaultAsync();
}