@@ -21,6 +21,7 @@ import {
21
21
} from "@pagopa/ts-commons/lib/responses" ;
22
22
23
23
import { pipe } from "fp-ts/lib/function" ;
24
+ import { sequenceS } from "fp-ts/lib/Apply" ;
24
25
import { Errors } from "io-ts" ;
25
26
import { FiscalCode , NonEmptyString } from "@pagopa/ts-commons/lib/strings" ;
26
27
import { readableReport } from "@pagopa/ts-commons/lib/reporters" ;
@@ -32,7 +33,7 @@ import { CreateWalletInstanceBody } from "../../generated/io-wallet/CreateWallet
32
33
import { CreateWalletAttestationBody } from "../../generated/io-wallet/CreateWalletAttestationBody" ;
33
34
import { WalletAttestationView } from "../../generated/io-wallet/WalletAttestationView" ;
34
35
import { FF_IO_WALLET_TRIAL_ENABLED } from "../config" ;
35
- import { SetCurrentWalletInstanceStatusBody } from "../../generated/io-wallet/SetCurrentWalletInstanceStatusBody " ;
36
+ import { SetWalletInstanceStatusBody } from "../../generated/io-wallet/SetWalletInstanceStatusBody " ;
36
37
import { WalletInstanceData } from "../../generated/io-wallet/WalletInstanceData" ;
37
38
38
39
const toValidationError = ( errors : Errors ) =>
@@ -125,6 +126,48 @@ export default class IoWalletController {
125
126
) ( )
126
127
) ;
127
128
129
+ /**
130
+ * Update current Wallet Instance status.
131
+ */
132
+ public readonly setWalletInstanceStatus = (
133
+ req : express . Request
134
+ ) : Promise <
135
+ | IResponseErrorInternal
136
+ | IResponseSuccessNoContent
137
+ | IResponseErrorServiceUnavailable
138
+ | IResponseErrorValidation
139
+ | IResponseErrorForbiddenNotAuthorized
140
+ > =>
141
+ withUserFromRequest ( req , async ( user ) =>
142
+ pipe (
143
+ this . ensureFiscalCodeIsAllowed ( user . fiscal_code ) ,
144
+ TE . chainW ( ( ) =>
145
+ pipe (
146
+ sequenceS ( E . Apply ) ( {
147
+ body : pipe (
148
+ req . body ,
149
+ SetWalletInstanceStatusBody . decode ,
150
+ E . mapLeft ( toValidationError )
151
+ ) ,
152
+ id : pipe (
153
+ NonEmptyString . decode ( req . params . walletInstanceId ) ,
154
+ E . mapLeft ( toValidationError )
155
+ ) ,
156
+ } ) ,
157
+ TE . fromEither
158
+ )
159
+ ) ,
160
+ TE . map ( ( { id, body : { status } } ) =>
161
+ this . ioWalletService . setWalletInstanceStatus (
162
+ id ,
163
+ status ,
164
+ user . fiscal_code
165
+ )
166
+ ) ,
167
+ TE . toUnion
168
+ ) ( )
169
+ ) ;
170
+
128
171
/**
129
172
* Update current Wallet Instance status.
130
173
*/
@@ -144,7 +187,7 @@ export default class IoWalletController {
144
187
TE . chainW ( ( ) =>
145
188
pipe (
146
189
req . body ,
147
- SetCurrentWalletInstanceStatusBody . decode ,
190
+ SetWalletInstanceStatusBody . decode ,
148
191
E . mapLeft ( toValidationError ) ,
149
192
TE . fromEither
150
193
)
@@ -162,22 +205,31 @@ export default class IoWalletController {
162
205
/**
163
206
* Get current Wallet Instance status.
164
207
*/
165
- public readonly getCurrentWalletInstanceStatus = (
208
+ public readonly getWalletInstanceStatus = (
166
209
req : express . Request
167
210
) : Promise <
168
211
| IResponseErrorInternal
169
212
| IResponseSuccessJson < WalletInstanceData >
170
213
| IResponseErrorNotFound
171
- | IResponseErrorInternal
172
214
| IResponseErrorServiceUnavailable
173
215
| IResponseErrorValidation
174
216
| IResponseErrorForbiddenNotAuthorized
175
217
> =>
176
218
withUserFromRequest ( req , async ( user ) =>
177
219
pipe (
178
220
this . ensureFiscalCodeIsAllowed ( user . fiscal_code ) ,
179
- TE . map ( ( ) =>
180
- this . ioWalletService . getCurrentWalletInstanceStatus ( user . fiscal_code )
221
+ TE . chainW ( ( ) =>
222
+ pipe (
223
+ NonEmptyString . decode ( req . params . walletInstanceId ) ,
224
+ E . mapLeft ( toValidationError ) ,
225
+ TE . fromEither
226
+ )
227
+ ) ,
228
+ TE . map ( ( walletInstanceId ) =>
229
+ this . ioWalletService . getWalletInstanceStatus (
230
+ walletInstanceId ,
231
+ user . fiscal_code
232
+ )
181
233
) ,
182
234
TE . toUnion
183
235
) ( )
@@ -186,32 +238,32 @@ export default class IoWalletController {
186
238
private readonly ensureUserIsAllowed = (
187
239
userId : NonEmptyString
188
240
) : TE . TaskEither < Error , void > =>
241
+ pipe (
242
+ TE . tryCatch (
243
+ ( ) => this . ioWalletService . getSubscription ( userId ) ,
244
+ E . toError
245
+ ) ,
246
+ // if a successful response with state != "ACTIVE" or an error is returned, return left
247
+ TE . chain ( ( response ) =>
248
+ response . kind === "IResponseSuccessJson" &&
249
+ response . value . state === "ACTIVE"
250
+ ? TE . right ( undefined )
251
+ : TE . left ( new Error ( ) )
252
+ )
253
+ ) ;
254
+
255
+ private readonly ensureFiscalCodeIsAllowed = ( fiscalCode : FiscalCode ) =>
189
256
FF_IO_WALLET_TRIAL_ENABLED
190
257
? pipe (
191
- TE . tryCatch (
192
- ( ) => this . ioWalletService . getSubscription ( userId ) ,
193
- E . toError
194
- ) ,
195
- // if a successful response with state != "ACTIVE" or an error is returned, return left
196
- TE . chain ( ( response ) =>
197
- response . kind === "IResponseSuccessJson" &&
198
- response . value . state === "ACTIVE"
199
- ? TE . right ( undefined )
200
- : TE . left ( new Error ( ) )
258
+ fiscalCode ,
259
+ NonEmptyString . decode ,
260
+ TE . fromEither ,
261
+ TE . chainW ( this . ensureUserIsAllowed ) ,
262
+ TE . mapLeft ( ( ) =>
263
+ getResponseErrorForbiddenNotAuthorized (
264
+ "Not authorized to perform this action"
265
+ )
201
266
)
202
267
)
203
268
: TE . right ( undefined ) ;
204
-
205
- private readonly ensureFiscalCodeIsAllowed = ( fiscalCode : FiscalCode ) =>
206
- pipe (
207
- fiscalCode ,
208
- NonEmptyString . decode ,
209
- TE . fromEither ,
210
- TE . chainW ( this . ensureUserIsAllowed ) ,
211
- TE . mapLeft ( ( ) =>
212
- getResponseErrorForbiddenNotAuthorized (
213
- "Not authorized to perform this action"
214
- )
215
- )
216
- ) ;
217
269
}
0 commit comments