import { HttpStatusCodeEnum , IResponseErrorForbiddenNotAuthorized , IResponseErrorInternal , IResponseErrorNotFound , IResponseErrorValidation , IResponseSuccessJson , ResponseErrorGeneric , ResponseErrorInternal , ResponseErrorValidation , ResponseSuccessJson } from "@pagopa/ts-commons/lib/responses" ; import { readableProblem } from "../utils/errorsFormatter" ; import { EUCovidCertAPIClient } from "../clients/eucovidcert.client" ; import { Certificate } from "../../generated/eucovidcert-api/Certificate" ; import { PreferredLanguages } from "../../generated/eucovidcert-api/PreferredLanguages" ; import { ResponseErrorStatusNotDefinedInSpec , ResponseErrorUnexpectedAuthProblem , withCatchAsInternalError , withValidatedOrInternalError } from "../utils/responses" ; import { User } from "../types/user" ; param detail export function ResponseGatewayTimeout ( detail ) { return { ... ResponseErrorGeneric ( HttpStatusCodeEnum . HTTP_STATUS_504 , "Gateway Timeout" , detail ) , kind : "IResponseErrorInternal" } ; } param detail export function ResponseErrorNotFound403 ( detail ) { return { ... ResponseErrorGeneric ( HttpStatusCodeEnum . HTTP_STATUS_403 , "Not Found" , detail ) , kind : "IResponseErrorNotFound" } ; } export default class EUCovidCertService { constructor ( private readonly eucovidCertApiClient ) { } /**
     * Get the EU Covid Certificte Status related to the user and auth_code
     */ public readonly getEUCovidCertificate = ( user , auth_code , preferred_languages ? ) : Promise < IResponseErrorInternal | IResponseErrorValidation | IResponseErrorNotFound | IResponseErrorForbiddenNotAuthorized | IResponseSuccessJson < Certificate > > => withCatchAsInternalError ( async ( ) => { const validated = await this . eucovidCertApiClient . getCertificate ( { accessData : { auth_code , fiscal_code : user . fiscal_code , preferred_languages } } ) ; return withValidatedOrInternalError ( validated , response => { switch ( response . status ) { case 200 : return ResponseSuccessJson ( response . value ) ; case 400 : return ResponseErrorValidation ( "Bad Request" , "Payload has bad format" ) ; case 401 : return ResponseErrorUnexpectedAuthProblem ( ) ; case 403 : return ResponseErrorNotFound403 ( "Access data provided are invalid or no Certificate has been emitted for the given Citizen" ) ; case 500 : return ResponseErrorInternal ( readableProblem ( response . value ) ) ; case 504 : return ResponseGatewayTimeout ( "DGC took to long to respond" ) ; default : return ResponseErrorStatusNotDefinedInSpec ( response ) ; } } ) ; } ) ; }