/* eslint-disable @typescript-eslint/no-var-requires */ import { Test, TestingModule } from '@nestjs/testing' import { addDays, format } from 'date-fns' import { datatype, date, internet, random } from 'faker' import { factory } from '../../../test/factories' import { MockedModule } from '../../../test/types' import { CardType, Intervals, Patient, PaymentProviders, Plan, SubscriptionInstallment } from '../../models' import { Omie, Secret } from '../../typings/global' import { HttpService } from '../http' import { Person } from '../smartfit' import { BillingPeriod } from './enums' import { ApiOmieGenericError } from './errors' import { buildContractNumber, buildGenerateContractData, buildGenerateServiceOrderData } from './helpers' import { OmieService } from './omie.service' import { AddressParams } from './params/address.params' import { OmieConfigService } from './service' jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox()) globalThis.secret = { omie: { enable: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), }, } as Secret describe('OmieService', () => { const myFetchMock = require('node-fetch') let subject: OmieService let omieConfigService: MockedModule beforeAll(async () => { omieConfigService = { getConfig: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ providers: [ { provide: OmieConfigService, useValue: omieConfigService, }, HttpService, OmieService, ], }).compile() subject = app.get(OmieService) myFetchMock.config.overwriteRoutes = true }) afterEach(() => { jest.resetAllMocks() myFetchMock.mockClear() }) afterAll(() => { jest.restoreAllMocks() myFetchMock.mockRestore() }) describe('#generateCustomer', () => { let omieConfig: Omie let patient: Patient let person: Person let address: AddressParams let getCityCodeSpy: jest.SpyInstance beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } patient = await factory.build('patient:subscriber', { omie_code: datatype.number({ min: 1 }) }) person = await factory.build('smartfit:person') address = { ...person.address, zipCode: person.address.zip, neighborhood: person.address.region } myFetchMock.post(`${omieConfig.endpoint}/geral/clientes/`, {}) }) beforeEach(() => { omieConfigService.getConfig.mockResolvedValue(omieConfig) getCityCodeSpy = jest.spyOn(subject, 'getCityCode').mockResolvedValue(datatype.uuid()) }) it('sends api key and secret', async () => { await subject.generateCustomer({ patient, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/geral/clientes/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.generateCustomer({ patient, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/geral/clientes/`, expect.objectContaining({ body: expect.stringContaining(`"call":"IncluirCliente"`), }), ) }) it('fetchs correct city code from omie', async () => { await subject.generateCustomer({ patient, address }) expect(getCityCodeSpy).toHaveBeenCalled() }) describe('when api returns an error', () => { beforeAll(() => { myFetchMock.post(`${omieConfig.endpoint}/geral/clientes/`, 500) }) it('throws an error', async () => { await expect(subject.generateCustomer({ patient, address })).rejects.toThrowError(ApiOmieGenericError) }) describe('when api returns Client-102 error', () => { const code = datatype.number({ min: 1 }) beforeEach(() => { myFetchMock.post(`${omieConfig.endpoint}/geral/clientes/`, { body: { faultcode: 'SOAP-ENV:Client-102', faultstring: `ERROR: Cliente já cadastrado para o Código de Integração [xxxx] com o nCod [${code}] !`, }, status: 500, }) }) it('returns customer code', async () => { await expect(subject.generateCustomer({ patient, address })).resolves.toEqual(code) }) }) }) }) describe('#generateContract', () => { let omieConfig: Omie let patient: Patient let plan: Plan let amount: number let person: Person let address: AddressParams beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } patient = await factory.build('patient:subscriber', { omie_code: datatype.number({ min: 1 }) }) patient.subscription.originalTransaction = datatype.number({ min: 1, max: 10 }).toString() plan = await factory.build('plan:n2b') amount = plan.getPaymentProvider(PaymentProviders.N2B).amount person = await factory.build('smartfit:person') address = { ...person.address, zipCode: person.address.zip, neighborhood: person.address.region } myFetchMock.post(`${omieConfig.endpoint}/servicos/contrato/`, {}) }) beforeEach(async () => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"call":"IncluirContrato"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect( subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }), ).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/contrato/` }, 500) }) it('throws an error', async () => { await expect( subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }), ).rejects.toThrowError() }) }) describe('when api returns success', () => { beforeAll(() => { myFetchMock.post( `${omieConfig.endpoint}/servicos/contrato/`, buildGenerateContractData({ subscription: patient.subscription, plan, amount, customerCode: Number(patient.omie_code), checkingAccountCode: omieConfig.checking_account_code, }), ) }) it('sends correct contract number', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(buildContractNumber(patient.subscription)) }), ) }) it('sends effective date', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"dVigInicial":"${format(patient.subscription.subscribedAt, 'dd/MM/yyyy')}"`), }), ) }) it('sends final date', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"dVigFinal":"${format(patient.subscription.expiresAt, 'dd/MM/yyyy')}"`), }), ) }) it('sends billing day using trial expires date', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"nDiaFat":${Number(format(patient.subscription.trialExpiresAt, 'dd'))}`), }), ) }) it('sends amount given by param', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"valorUnit":${amount}`), }), ) }) it('sends subscription id as integration code item', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"codIntItem":"${patient.subscription.originalTransaction}"`), }), ) }) describe('when trial expires date is null', () => { beforeAll(() => { patient.subscription.trialExpiresAt = null }) it('sends billing day using subscribe date', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"nDiaFat":${Number(format(patient.subscription.subscribedAt, 'dd'))}`), }), ) }) }) describe.each` planInterval | billingPeriod ${Intervals.MONTHLY} | ${BillingPeriod.MONTHLY} ${Intervals.SEMESTERLY} | ${BillingPeriod.SEMESTERLY} ${Intervals.YEARLY} | ${BillingPeriod.YEARLY} `('when plan intervals is "$planInterval"', ({ billingPeriod, planInterval }) => { beforeAll(() => { plan.interval = planInterval }) it(`sends "${billingPeriod}" billing period according plan`, async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contrato/`, expect.objectContaining({ body: expect.stringContaining(`"cTipoFat":"${billingPeriod}"`) }), ) }) }) }) describe('when patient have not an omie code', () => { let generateOmieSpy: jest.SpyInstance beforeAll(() => { patient.omie_code = null generateOmieSpy = jest.spyOn(subject, 'generateCustomer').mockResolvedValue(datatype.number({ min: 1 })) }) afterAll(() => { generateOmieSpy.mockReset() }) it('tries create a omie customer', async () => { await subject.generateContract({ patient, subscription: patient.subscription, plan, amount, address }) expect(generateOmieSpy).toHaveBeenCalled() }) }) }) describe('#generateServiceOrder', () => { let omieConfig: Omie let patient: Patient let plan: Plan let amount: number let person: Person let address: AddressParams let installment: SubscriptionInstallment let totalInstallments: number let cardType: CardType beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } patient = await factory.build('patient:subscriber', { omie_code: datatype.number({ min: 1 }) }) patient.subscription.originalTransaction = datatype.number({ min: 1, max: 10 }).toString() plan = await factory.build('plan:n2b') amount = plan.getPaymentProvider(PaymentProviders.N2B).amount person = await factory.build('smartfit:person') address = { ...person.address, zipCode: person.address.zip, neighborhood: person.address.region } myFetchMock.post(`${omieConfig.endpoint}/servicos/os/`, {}) cardType = 'credit' installment = SubscriptionInstallment.toInstance({ id: datatype.uuid(), installment: datatype.number({ min: 1, max: 12 }), amount: datatype.number({ min: 10, max: 100 }), created_at: date.recent(0), paid_at: date.recent(0), }) installment.due_date = date.soon(30 * (installment.installment - 1), installment.created_at) console.log(installment.due_date) totalInstallments = 12 }) beforeEach(() => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"call":"IncluirOS"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect( subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }), ).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/os/` }, 500) }) it('throws an error', async () => { await expect( subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }), ).rejects.toThrowError() }) }) describe('when api returns success', () => { beforeAll(() => { myFetchMock.post( `${omieConfig.endpoint}/servicos/os/`, buildGenerateServiceOrderData({ subscription: patient.subscription, plan, amount, customerCode: Number(patient.omie_code), installment, totalInstallments, cardType, config: omieConfig, }), ) }) it('sends correct so code integration', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(patient.subscription.id) }), ) }) it('sends etapa data', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"cEtapa":"50"`), }), ) }) it('sends prevision date for credit', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"dDtPrevisao":"${format(installment.due_date, 'dd/MM/yyyy')}"`), }), ) }) it('sends prevision date with subscription +10 days for debit', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType: 'debit', }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"dDtPrevisao":"${format(addDays(installment.due_date, 10), 'dd/MM/yyyy')}"`), }), ) }) it('sends amount given by param', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"nValUnit":${amount}`), }), ) }) it('sends correct parcel code for debit', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType: 'debit', }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"cCodParc":"999"`), }), ) }) it('sends correct additional data for one installment', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments: 1, cardType: 'debit', }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"cDadosAdicNF":"Assinatura ${plan.name} do app Smartfit Nutri"`), }), ) }) it('sends correct additional data for more than one installment', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType: 'debit', }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining( `"cDadosAdicNF":"Assinatura ${plan.name} do app Smartfit Nutri\\n Parcela ${installment.installment}/${totalInstallments}"`, ), }), ) }) it('sends special forecast parcel for debit', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType: 'debit', }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"parcela_adiantamento":"S"`), }), ) }) describe.each` environment | ambient ${'dev'} | ${'H'} ${'stag'} | ${'H'} ${'prod'} | ${'P'} `('when environment is "$environment"', ({ environment, ambient }) => { beforeAll(() => { process.env.STAGE = environment }) it(`sends "${ambient}" to cAmbiente param`, async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/os/`, expect.objectContaining({ body: expect.stringContaining(`"cAmbiente":"${ambient}"`) }), ) }) }) }) describe('when patient have not an omie code', () => { let generateOmieSpy: jest.SpyInstance beforeAll(() => { patient.omie_code = null generateOmieSpy = jest.spyOn(subject, 'generateCustomer').mockResolvedValue(datatype.number({ min: 1 })) }) afterAll(() => { generateOmieSpy.mockReset() }) it('tries create a omie customer', async () => { await subject.generateServiceOrder({ patient, subscription: patient.subscription, plan, amount, address, installment, totalInstallments, cardType, }) expect(generateOmieSpy).toHaveBeenCalled() }) }) }) describe('#invoiceContract', () => { let omieConfig: Omie const code = datatype.number({ min: 1 }) beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } myFetchMock.post(`${omieConfig.endpoint}/servicos/contratofat/`, {}) }) beforeEach(async () => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.invoiceContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.invoiceContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(`"call":"FaturarContrato"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect(subject.invoiceContract(code)).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/contratofat/` }, 500) }) it('throws an error', async () => { await expect(subject.invoiceContract(code)).rejects.toThrowError() }) }) describe('when api returns success', () => { const response = { nCodOS: datatype.number({ min: 1 }), cCodStatus: datatype.number({ min: 1, max: 9999 }).toString(), cDescStatus: random.words(), } beforeAll(() => { myFetchMock.post(`${omieConfig.endpoint}/servicos/contratofat/`, response) }) it('sends correct contract code', async () => { await subject.invoiceContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(`"nCodCtr":${code}`) }), ) }) it('returns contract status data', async () => { await expect(subject.invoiceContract(code)).resolves.toEqual({ soCode: response.nCodOS, status: response.cCodStatus, message: response.cDescStatus, }) }) }) }) describe('#invoiceServiceOrder', () => { let omieConfig: Omie const code = datatype.number({ min: 1 }) beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } myFetchMock.post(`${omieConfig.endpoint}/servicos/osp/`, {}) }) beforeEach(async () => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.invoiceServiceOrder(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.invoiceServiceOrder(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(`"call":"FaturarOS"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect(subject.invoiceServiceOrder(code)).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/osp/` }, 500) }) it('throws an error', async () => { await expect(subject.invoiceServiceOrder(code)).rejects.toThrowError() }) }) describe('when api returns success', () => { const response = { nCodOS: datatype.number({ min: 1 }), cCodStatus: datatype.number({ min: 1, max: 9999 }).toString(), cDescStatus: random.words(), } beforeAll(() => { myFetchMock.post(`${omieConfig.endpoint}/servicos/osp/`, response) }) it('sends correct service order code', async () => { await subject.invoiceServiceOrder(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(`"nCodOS":${code}`) }), ) }) it('returns service order status data', async () => { await expect(subject.invoiceServiceOrder(code)).resolves.toEqual({ soCode: response.nCodOS, status: response.cCodStatus, message: response.cDescStatus, }) }) }) }) describe('#cancelContract', () => { let omieConfig: Omie const code = datatype.number({ min: 1 }) beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } myFetchMock.post(`${omieConfig.endpoint}/servicos/contratofat/`, {}) }) beforeEach(async () => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.cancelContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.cancelContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(`"call":"CancelarContrato"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect(subject.cancelContract(code)).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/contratofat/` }, 500) }) it('throws an error', async () => { await expect(subject.cancelContract(code)).rejects.toThrowError() }) }) describe('when api returns success', () => { const response = { nCodOS: datatype.number({ min: 1 }), cCodStatus: datatype.number({ min: 1, max: 9999 }).toString(), cDescStatus: random.words(), } beforeAll(() => { myFetchMock.post(`${omieConfig.endpoint}/servicos/contratofat/`, response) }) it('sends correct contract code', async () => { await subject.cancelContract(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/contratofat/`, expect.objectContaining({ body: expect.stringContaining(`"nCodCtr":${code}`) }), ) }) it('returns contract status data', async () => { await expect(subject.cancelContract(code)).resolves.toEqual({ status: response.cCodStatus, message: response.cDescStatus, }) }) }) }) describe('#cancelInvoice', () => { let omieConfig: Omie const code = datatype.number({ min: 1 }) beforeAll(async () => { omieConfig = { enable: true, enable_cancel: true, endpoint: internet.url(), app_key: datatype.uuid(), app_secret: datatype.uuid(), checking_account_code: datatype.number({ min: 1 }), category_code: datatype.uuid(), parc_checking_account_code: datatype.number({ min: 1 }), parc_category_code: datatype.uuid(), } myFetchMock.post(`${omieConfig.endpoint}/servicos/osp/`, {}) }) beforeEach(async () => { omieConfigService.getConfig.mockResolvedValue(omieConfig) }) it('sends api key and secret', async () => { await subject.cancelInvoice(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(omieConfig.app_key), }), ) }) it('sends correct call name', async () => { await subject.cancelInvoice(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(`"call":"CancelarOS"`), }), ) }) describe('when resource is disabled', () => { beforeAll(() => { omieConfig.enable = false }) afterAll(() => { omieConfig.enable = true }) it('throws an error', async () => { await expect(subject.cancelInvoice(code)).rejects.toThrowError() }) }) describe('when api returns error', () => { beforeAll(() => { myFetchMock.post({ url: `${omieConfig.endpoint}/servicos/osp/` }, 500) }) it('throws an error', async () => { await expect(subject.cancelInvoice(code)).rejects.toThrowError() }) }) describe('when api returns success', () => { const response = { nCodOS: datatype.number({ min: 1 }), cCodStatus: datatype.number({ min: 1, max: 9999 }).toString(), cDescStatus: random.words(), } beforeAll(() => { myFetchMock.post(`${omieConfig.endpoint}/servicos/osp/`, response) }) it('sends correct contract code', async () => { await subject.cancelInvoice(code) expect(myFetchMock).toHaveBeenCalledWith( `${omieConfig.endpoint}/servicos/osp/`, expect.objectContaining({ body: expect.stringContaining(`"nCodOS":${code}`) }), ) }) it('returns contract status data', async () => { await expect(subject.cancelInvoice(code)).resolves.toEqual({ status: response.cCodStatus, message: response.cDescStatus, }) }) }) }) })