/**
 * This service retrieves and updates the user profile from the API system using
 * an API client.
 */ import { IResponseErrorConflict , IResponseErrorInternal , IResponseErrorNotFound , IResponseErrorTooManyRequests , IResponseSuccessAccepted , IResponseSuccessJson , ResponseErrorConflict , ResponseErrorInternal , ResponseErrorNotFound , ResponseErrorTooManyRequests , ResponseSuccessAccepted , ResponseSuccessJson } from "italia-ts-commons/lib/responses" ; import { errorsToReadableMessages } from "italia-ts-commons/lib/reporters" ; import { ExtendedProfile as ExtendedProfileApi } from "../../generated/io-api/ExtendedProfile" ; import { NewProfile } from "../../generated/io-api/NewProfile" ; import { Profile as ProfileApi } from "../../generated/io-api/Profile" ; import { InitializedProfile } from "../../generated/backend/InitializedProfile" ; import { Profile as ProfileBackend } from "../../generated/backend/Profile" ; import { toInitializedProfile } from "../types/profile" ; import { User } from "../types/user" ; import { unhandledResponseStatus , withCatchAsInternalError , withValidatedOrInternalError } from "../utils/responses" ; import { IApiClientFactoryInterface } from "./IApiClientFactory" ; export default class ProfileService { constructor ( private readonly apiClient ) { } /**
     * Retrieves the profile for a specific user.
     */ public readonly getProfile = ( user ) : Promise < IResponseErrorInternal | IResponseErrorTooManyRequests | IResponseErrorNotFound | IResponseSuccessJson < InitializedProfile > > => { const client = this . apiClient . getClient ( ) ; return withCatchAsInternalError ( async ( ) => { const validated = await client . getProfile ( { fiscal_code : user . fiscal_code } ) ; return withValidatedOrInternalError ( validated , response => { if ( response . status === 200 ) { const validatedExtendedProfile = ExtendedProfileApi . decode ( response . value ) ; return withValidatedOrInternalError ( validatedExtendedProfile , p => ResponseSuccessJson ( toInitializedProfile ( p , user ) ) ) ; } if ( response . status === 404 ) { return ResponseErrorNotFound ( "Not Found" , "Profile not found" ) ; } if ( response . status === 429 ) { return ResponseErrorTooManyRequests ( ) ; } return unhandledResponseStatus ( response . status ) ; } ) ; } ) ; } ; public readonly getApiProfile = ( user ) : Promise < IResponseErrorInternal | IResponseErrorTooManyRequests | IResponseErrorNotFound | IResponseSuccessJson < ExtendedProfileApi > > => { const client = this . apiClient . getClient ( ) ; return withCatchAsInternalError ( async ( ) => { const validated = await client . getProfile ( { fiscal_code : user . fiscal_code } ) ; return withValidatedOrInternalError ( validated , response => { if ( response . status === 200 ) { return ExtendedProfileApi . decode ( response . value ) . fold < IResponseSuccessJson < ExtendedProfileApi > | IResponseErrorInternal > ( _ => ResponseErrorInternal ( errorsToReadableMessages ( _ ) . join ( " / " ) ) , _ => ResponseSuccessJson ( _ ) ) ; } if ( response . status === 404 ) { return ResponseErrorNotFound ( "Not found" , "Profile not found." ) ; } if ( response . status === 429 ) { return ResponseErrorTooManyRequests ( ) ; } return unhandledResponseStatus ( response . status ) ; } ) ; } ) ; } ; /**
     * Create the profile of a specific user.
     */ public readonly createProfile = async ( user , newProfile ) : Promise < IResponseErrorInternal | IResponseErrorTooManyRequests | IResponseErrorConflict | IResponseSuccessJson < InitializedProfile > > => { const client = this . apiClient . getClient ( ) ; return withCatchAsInternalError ( async ( ) => { const validated = await client . createProfile ( { body : newProfile , fiscal_code : user . fiscal_code } ) ; return withValidatedOrInternalError ( validated , response => response . status === 200 ? ResponseSuccessJson ( toInitializedProfile ( response . value , user ) ) : response . status === 409 ? ResponseErrorConflict ( response . value || "A user with the provided fiscal code already exists" ) : response . status === 429 ? ResponseErrorTooManyRequests ( ) : unhandledResponseStatus ( response . status ) ) ; } ) ; } ; /**
     * Upsert the profile of a specific user.
     */ public readonly updateProfile = async ( user , profileBackend ) : Promise < IResponseErrorInternal | IResponseErrorNotFound | IResponseErrorConflict | IResponseErrorTooManyRequests | IResponseSuccessJson < InitializedProfile > > => { const client = this . apiClient . getClient ( ) ; return withValidatedOrInternalError ( ProfileApi . decode ( profileBackend ) , async ( extendedProfileApi ) => withCatchAsInternalError ( async ( ) => { const validated = await client . updateProfile ( { body : extendedProfileApi , fiscal_code : user . fiscal_code } ) ; return withValidatedOrInternalError ( validated , response => response . status === 200 ? ResponseSuccessJson ( toInitializedProfile ( response . value , user ) ) : response . status === 404 ? ResponseErrorNotFound ( "Not found" , "User not found" ) : response . status === 409 ? ResponseErrorConflict ( response . value || "Cannot update profile with wrong version" ) : response . status === 429 ? ResponseErrorTooManyRequests ( ) : unhandledResponseStatus ( response . status ) ) ; } ) ) ; } ; /**
     * Resend the email to complete email validation process
     */ public readonly emailValidationProcess = async ( user ) : Promise < IResponseErrorInternal | IResponseErrorTooManyRequests | IResponseErrorNotFound | IResponseSuccessAccepted > => { const client = this . apiClient . getClient ( ) ; return withCatchAsInternalError ( async ( ) => { const validated = await client . startEmailValidationProcess ( { fiscal_code : user . fiscal_code } ) ; return withValidatedOrInternalError ( validated , response => response . status === 202 ? ResponseSuccessAccepted ( ) : response . status === 404 ? ResponseErrorNotFound ( "Not found" , "User not found." ) : response . status === 429 ? ResponseErrorTooManyRequests ( ) : unhandledResponseStatus ( response . status ) ) ; } ) ; } ; }