import {z} from 'zod' export type Gender = 'FEMININO' | 'MASCULINO'; export type Nationality = 'BRASILEIRO' | 'ESTRANGEIRO'; export type State = 'AC' | 'AL' | 'AP' | 'AM' | 'BA' | 'CE' | 'DF' | 'ES' | 'GO' | 'MA' | 'MT' | 'MS' | 'MG' | 'PA' | 'PB' | 'PR' | 'PE' | 'PI' | 'RJ' | 'RN' | 'RS' | 'RO' | 'RR' | 'SC' | 'SP' | 'SE' | 'TO'; export type City = string; export type Education = 'NAO_ALFABETIZADO' | 'ENSINO_FUNDAMENTAL_INCOMPLETO' | 'ENSINO_FUNDAMENTAL_COMPLETO' | 'ENSINO_MEDIO_INCOMPLETO' | 'ENSINO_MEDIO_COMPLETO' | 'ENSINO_SUPERIOR_INCOMPLETO' | 'ENSINO_SUPERIOR_COMPLETO' | 'POS_GRADUACAO'; export type RelationshipStatus = 'CASADO' | 'DIVORCIADO' | 'VIUVO' | 'SEPARADO' | 'SOLTEIRO' | 'COMPANHEIRO' | 'UNIAO_ESTAVEL' | 'SEPARADO_JUDICIALMENTE'; export type IdentityType = 'RG' | 'CNH' | 'PASSAPORTE' | 'CARTEIRA_CRC' | 'CARTEIRA_CREA' | 'CARTEIRA_OAB' | 'CARTEIRA_CRE'; export type IdentityIssuer = 'SSP' | 'DETRAN' | 'CONS_REG_ECONOMIA' | 'MIN_EXERCITO' | 'OAB' | 'CREA' | 'SJS' | 'CTPS' | 'CREME' | 'MINEX' | 'CRM' | 'SJTC' | 'MAER' | 'DPF' | 'RBF' | 'IML' | 'CPROF' | 'CRO' | 'CRC' | 'DPMAF' | 'ME' | 'BM' | 'AERON' | 'SJT' | 'COREN' | 'DETRA' | 'EXERC' | 'SSI' | 'MINISTERIO_DA_MARINHA' | 'CRA_CONSELHO_ADMINISTRATIVO' | 'INSTITUTO_FELIX_PACHECO' | 'INST_NAC_PROPRIEDADE_INDUSTRIA' | 'MINISTERIO_DA_FAZENDA' | 'TRIBUNAL_REGIONAL_DO_TRABALHO' | 'CONSELHO_REGIONAL_PSICOLOGIA' | 'POLICIA_MILITAR'; export type Identity = {type: IdentityType, number: string, issuer: IdentityIssuer, state: State, issuingDate: string}; export type HomeType = 'ALUGADA' | 'PARENTES' | 'FUNCIONAL' | 'PROPRIA_QUITADA' | 'PROPRIA_FINANCIADA' | 'OUTROS'; export type HomeSince = 'MENOR_6_MESES' | 'MENOR_1_ANO' | 'MENOR_2_ANOS' | 'MAIOR_2_ANOS'; export type Address = {zipCode: string, address: string, number: string, complement?: string | undefined, district: string, state: State, city: City, homeType: HomeType, homeSince: HomeSince}; export type Vehicle = {licensePlate: string}; export type ConsumerUnit = {number: string}; export type Occupation = 'APOSENTADO_PENSIONISTA' | 'AUTONOMO' | 'EMPRESARIO' | 'PROFISSIONAL_LIBERAL' | 'ASSALARIADO' | 'FUNCIONARIO_PUBLICO' | 'DESEMPREGADO'; export type OccupyType = 'INSS' | 'SIAPE' | 'GOVERNO_ESTADUAL' | 'PREFEITURA_MUNICIPAL' | 'FORCAS_ARMADAS' | 'TRIBUNAL_DE_JUSTICA' | 'ASSEMBLEIA_LEGISLATIVA'; export type Profession = string; export type EmploymentSince = 'LESS_THAN_SIX_MONTHS' | 'BETWEEN_SIX_MONTHS_AND_ONE_YEAR' | 'BETWEEN_ONE_AND_TWO_YEARS' | 'BETWEEN_TWO_AND_THREE_YEARS' | 'BETWEEN_THREE_AND_FOUR_YEARS' | 'BETWEEN_FOUR_AND_FIVE_YEARS' | 'BETWEEN_FIVE_AND_TEN_YEARS' | 'MORE_THAN_TEN_YEARS'; export type Business = {occupation: Occupation, occupyType?: OccupyType | undefined, profession: Profession, companyName?: string | undefined, phone?: string | undefined, income: string, employmentSince: EmploymentSince, payday: string, benefitNumber?: string | undefined, zipCode?: string | undefined, address?: string | undefined, number?: string | undefined, complement?: string | undefined, district?: string | undefined, state?: State | undefined, city?: City | undefined}; export type Banks = string; export type AccountType = 'CONTA_CORRENTE_INDIVIDUAL' | 'CONTA_CORRENTE_CONJUNTA' | 'CONTA_POUPANCA_CONJUNTA' | 'CONTA_POUPANCA_INDIVIDUAL'; export type Bank = {bank: Banks, type: AccountType, agency: string, account: string, pixKey?: string | undefined}; export type Reference = {name: string, phone: string}; export type ProductType = 'LOAN' | 'CARD' | 'REFINANCING_AUTO' | 'REFINANCING_HOME' | 'FINANCING_AUTO' | 'CAAS' | 'WORKING_CAPITAL' | 'DEBT'; export type ProductLoan = {type?: ProductType | undefined, value?: number | undefined, installments?: number | undefined}; export type Network = 'VISA' | 'MASTERCARD' | 'ELO'; export type ProductCard = {type?: ProductType | undefined, network?: Network | undefined, payDay?: string | undefined}; export type ProductAuto = {type?: ProductType | undefined, value?: number | undefined, vehicleBrand?: string | undefined, vehicleModel?: string | undefined, installments?: number | undefined, vehicleModelYear?: string | undefined, codeFipe?: string | undefined, vehicleFipeValue?: number | undefined}; export type RealEstateType = 'house' | 'apartment' | 'commercial' | 'land' | 'others'; export type ProductHome = {type?: ProductType | undefined, value?: number | undefined, installments?: number | undefined, realEstateType?: RealEstateType | undefined, realEstateValue?: number | undefined, outstandingBalance?: number | undefined}; export type ProductFinancingAuto = {type?: ProductType | undefined, value?: number | undefined, vehicleBrand?: string | undefined, vehicleModel?: string | undefined, installments?: number | undefined, vehicleModelYear?: string | undefined, codeFipe?: string | undefined, vehicleFipeValue?: number | undefined, downPayment?: number | undefined, used?: boolean | undefined}; export type ProductCaas = {type?: ProductType | undefined, release_date?: string | undefined, due_date?: string | undefined, installments?: number | undefined, monthlyTax?: number | undefined, cadValue?: number | undefined, value?: number | undefined}; export type BusinessProfession = 'SOCIO_DONO' | 'ADMINISTRADOR_DIRETOR' | 'PROCURADOR' | 'OUTROS'; export type EmployeesCount = 'NENHUM' | 'DE_1_A_5' | 'DE_6_A_10' | 'DE_11_A_20' | 'DE_21_A_50' | 'DE_51_A_100' | 'MAIS_DE_100'; export type WorkingCapitalLoanObjectives = 'CAPITAL_DE_GIRO' | 'RENEGOCIACAO_DE_DIVIDAS' | 'MARKETING_E_VENDAS' | 'EXPANSAO_DE_NEGOCIOS' | 'EXPANSAO' | 'ESTOQUE' | 'REFORMA' | 'USO_PESSOAL'; export type ProductWorkingCapital = {type?: ProductType | undefined, installments?: number | undefined, value?: number | undefined, cnpj?: string | undefined, businessProfession?: BusinessProfession | undefined, employeesCount?: EmployeesCount | undefined, businessIncomeCnpj?: number | undefined, loanObjectives?: WorkingCapitalLoanObjectives | undefined, bank?: Banks | undefined, accountType?: AccountType | undefined, agency?: string | undefined, account?: string | undefined}; export type ProductDebt = {type?: ProductType | undefined}; export type Products = ProductLoan | ProductCard | ProductAuto | ProductHome | ProductFinancingAuto | ProductCaas | ProductWorkingCapital | ProductDebt; export type Proposal = {mother: string, gender: Gender, nationality?: Nationality | undefined, hometownState?: State | undefined, hometown?: City | undefined, education: Education, relationshipStatus: RelationshipStatus, phoneLandline?: string | undefined, identity: Identity, address: Address, vehicle?: Vehicle | undefined, consumerUnit?: ConsumerUnit | undefined, business: Business, bank: Bank, reference?: Array | undefined, products?: Array | undefined}; export type CreateApiV21ProcessProposalIdCaasRequestArgs = {id_caasRequest: string, body: Proposal}; export type CreateApiV21ProcessProposalIdCaasRequestOptions = {baseUrl?: string | undefined, partnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pipelineStatus = z.enum(["SIGNUP_ANALISIS", "SIGNUP_COMPLETED", "SIGNUP_DENIED", "PROPOSAL_ANALISIS", "PROPOSAL_CREATED", "PROPOSAL_DENIED"]); export const matchLoan = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), minValue: z.number().int().optional(), maxValue: z.number().int().optional(), minInstallment: z.number().int().optional(), maxInstallment: z.number().int().optional(), monthlyTax: z.number().optional()}); export const network = z.enum(["VISA", "MASTERCARD", "ELO"]); export const matchCard = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), annuity: z.number().optional(), network: network.optional()}); export const matchAuto = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), minValue: z.number().int().optional(), maxValue: z.number().int().optional(), minInstallment: z.number().int().optional(), maxInstallment: z.number().int().optional(), monthlyTax: z.number().optional()}); export const matchHome = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), minValue: z.number().int().optional(), maxValue: z.number().int().optional(), minInstallment: z.number().int().optional(), maxInstallment: z.number().int().optional(), monthlyTax: z.number().optional()}); export const matchFinancingAuto = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), minValue: z.number().int().optional(), maxValue: z.number().int().optional(), minInstallment: z.number().int().optional(), maxInstallment: z.number().int().optional(), monthlyTax: z.number().optional()}); export const matchDebt = z.object({product_id: z.number().int().optional(), name: z.string().optional(), logo: z.string().optional(), minValue: z.number().int().optional(), maxValue: z.number().int().optional(), minInstallment: z.number().int().optional(), maxInstallment: z.number().int().optional(), monthlyTax: z.number().optional()}); export const matches = z.union([matchLoan, matchCard, matchAuto, matchHome, matchFinancingAuto, matchDebt]); export const productType = z.enum(["LOAN", "CARD", "REFINANCING_AUTO", "REFINANCING_HOME", "FINANCING_AUTO", "CAAS", "WORKING_CAPITAL", "DEBT"]); export const creditStatus = z.enum(["PRE_PROCESSAMENTO", "PRE_APROVADO", "PENDENTE", "AGUARDANDO_DOCUMENTOS", "EM_ANALISE_MANUAL", "EM_ANALISE_OPERACIONAL", "EM_ANALISE_DOCUMENTAL", "CONTRATADO", "LIBERADO", "REPROVADO", "ESTORNO", "EXPIRADO", "APROVADO"]); export const pendentDocuments = z.enum(["SELF", "IDENTITY_FRONT", "IDENTITY_BACK", "ADDRESS_PROOF", "INCOME_PROOF", "DECLARATION_IR", "CORPORATE_BILLING_RELATIONSHIP", "PROOF_OF_COMPANY_ADDRESS", "SOCIAL_CONTRACT", "PROOF_OF_CORPORATE_INCOME"]); export const loan = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), value: z.number(), installments: z.number().int(), monthlyTax: z.number(), installmentsValue: z.number().optional(), iofValue: z.number().optional(), grossValue: z.number().optional(), firstPaymentDate: z.string().optional(), cet: z.number().optional(), releasedDate: z.string().optional()}); export const card = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), international: z.boolean().optional(), annuity: z.number().optional(), network: network.optional(), prepaid: z.boolean().optional(), digitalAccount: z.boolean().optional()}); export const auto = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), value: z.number(), installments: z.number().int(), monthlyTax: z.number(), installmentsValue: z.number().optional(), iofValue: z.number().optional(), grossValue: z.number().optional(), firstPaymentDate: z.string().optional(), cet: z.number().optional(), releasedDate: z.string().optional()}); export const home = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), value: z.number(), installments: z.number().int(), monthlyTax: z.number(), installmentsValue: z.number().optional(), iofValue: z.number().optional(), grossValue: z.number().optional(), firstPaymentDate: z.string().optional(), cet: z.number().optional(), releasedDate: z.string().optional()}); export const financingAuto = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), value: z.number(), installments: z.number().int(), monthlyTax: z.number(), installmentsValue: z.number().optional(), iofValue: z.number().optional(), grossValue: z.number().optional(), firstPaymentDate: z.string().optional(), cet: z.number().optional(), releasedDate: z.string().optional()}); export const debt = z.object({customerServiceNumber: z.string(), type: productType, product: z.string(), product_id: z.number().int(), hasDocuments: z.boolean(), hasContracts: z.boolean(), logo: z.string(), lastStatus: creditStatus, pendentDocuments: z.array(pendentDocuments).optional(), dateCreated: z.string(), lastUpdated: z.string(), value: z.number(), installments: z.number().int(), monthlyTax: z.number(), installmentsValue: z.number().optional(), iofValue: z.number().optional(), grossValue: z.number().optional(), firstPaymentDate: z.string().optional(), cet: z.number().optional(), releasedDate: z.string().optional()}); export const proposals = z.union([loan, card, auto, home, financingAuto, debt]); export const pipeline = z.object({id: z.string().optional(), status: pipelineStatus.optional(), cpf: z.number().int().optional(), name: z.string().optional(), dateCreated: z.string().optional(), lastUpdated: z.string().optional(), matches: z.array(matches).optional(), proposals: z.array(proposals).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Step 2 - Create caas proposals * * POST /v2.1/process/proposal/{id_caasRequest} */ export const createApiV21ProcessProposalIdCaasRequest = async (args: CreateApiV21ProcessProposalIdCaasRequestArgs, options?: CreateApiV21ProcessProposalIdCaasRequestOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, partnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (partnerAuth !== undefined) headers.set('authorization', `Bearer ${partnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2.1/process/proposal/${encodeURIComponent(String(args.id_caasRequest))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(pipeline, await res.json()) };