@@ -4,6 +4,7 @@ import { PrismaService } from '../../../../shared/prisma/prisma.service';
4
4
import { NOT_FOUND_ERR_MESSAGE } from '../api/err.messages' ;
5
5
import { BpiSubject } from '../models/bpiSubject' ;
6
6
import { BpiSubjectRole , BpiSubjectRoleName } from '../models/bpiSubjectRole' ;
7
+ import { PublicKey , PublicKeyType } from '../models/publicKey' ;
7
8
8
9
// Repositories are the only places that talk the Prisma language of models.
9
10
// They are always mapped to and from domain objects so that the business layer of the application
@@ -64,8 +65,37 @@ export class BpiSubjectStorageAgent extends PrismaService {
64
65
return this . mapper . map ( bpiSubjectRole , BpiSubjectRole ) ;
65
66
}
66
67
68
+ async storePublicKey (
69
+ id : string ,
70
+ type : PublicKeyType ,
71
+ value : string ,
72
+ bpiSubjectId : string ,
73
+ ) : Promise < void > {
74
+ await this . prisma . publicKey . create ( {
75
+ data : {
76
+ id : id ,
77
+ type : type ,
78
+ value : value ,
79
+ bpiSubjectId : bpiSubjectId ,
80
+ } ,
81
+ } ) ;
82
+ }
83
+
84
+ async updatePublicKey (
85
+ type : PublicKeyType ,
86
+ value : string ,
87
+ bpiSubjectId : string ,
88
+ ) : Promise < PublicKey > {
89
+ const updatedPublicKey = await this . prisma . publicKey . update ( {
90
+ where : { type_bpiSubjectId : { type : type , bpiSubjectId : bpiSubjectId } } ,
91
+ data : {
92
+ value : value ,
93
+ } ,
94
+ } ) ;
95
+
96
+ return this . mapper . map ( updatedPublicKey , PublicKey ) ;
97
+ }
67
98
async storeNewBpiSubject ( bpiSubject : BpiSubject ) : Promise < BpiSubject > {
68
- bpiSubject . publicKey = bpiSubject . publicKey . toLowerCase ( ) ;
69
99
const newBpiSubjectModel = await this . prisma . bpiSubject . create ( {
70
100
data : {
71
101
...bpiSubject ,
@@ -76,6 +106,13 @@ export class BpiSubjectStorageAgent extends PrismaService {
76
106
} ;
77
107
} ) ,
78
108
} ,
109
+ publicKey : {
110
+ connect : bpiSubject . publicKeys . map ( ( pk ) => {
111
+ return {
112
+ id : pk . id ,
113
+ } ;
114
+ } ) ,
115
+ } ,
79
116
} ,
80
117
} ) ;
81
118
@@ -94,6 +131,13 @@ export class BpiSubjectStorageAgent extends PrismaService {
94
131
} ;
95
132
} ) ,
96
133
} ,
134
+ publicKey : {
135
+ connect : bpiSubject . publicKeys . map ( ( pk ) => {
136
+ return {
137
+ id : pk . id ,
138
+ } ;
139
+ } ) ,
140
+ } ,
97
141
} ,
98
142
} ) ;
99
143
return this . mapper . map ( updatedBpiSubjectModel , BpiSubject ) ;
@@ -105,15 +149,20 @@ export class BpiSubjectStorageAgent extends PrismaService {
105
149
} ) ;
106
150
}
107
151
108
- async getBpiSubjectByPublicKey ( publicKey : string ) : Promise < BpiSubject > {
152
+ async getBpiSubjectByPublicKey ( publicKeyValue : string ) : Promise < BpiSubject > {
109
153
const bpiSubjectModel = await this . prisma . bpiSubject . findFirst ( {
110
154
where : {
111
- publicKey : publicKey ,
155
+ publicKey : {
156
+ some : {
157
+ value : publicKeyValue ,
158
+ } ,
159
+ } ,
112
160
} ,
113
161
include : {
114
- roles : true ,
162
+ publicKey : true ,
115
163
} ,
116
164
} ) ;
165
+
117
166
if ( ! bpiSubjectModel ) {
118
167
throw new NotFoundException ( NOT_FOUND_ERR_MESSAGE ) ;
119
168
}
0 commit comments