Skip to content

Commit

Permalink
feat: expose function to get type of ofx transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabiopf02 committed Dec 2, 2024
1 parent 0e87378 commit c065ed5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ yarn add ofx-data-extractor
### Methods
The `Ofx` class provides the following methods:

- `getType(): Types`: Used to get type of transactions from ofx (BANK or CREDIT_CARD)
- `fromBuffer(data: Buffer)`: Used to read files on the node. Returns the methods
- `fromBlob(data: Blob)`: Used to read files in the browser. Returns the methods below.
- `config(options: OfxConfig)`: Used for formatting the generated json.`
Expand Down
5 changes: 5 additions & 0 deletions src/@types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ export type TransactionsSummary = {
dateStart: string
dateEnd: string
}

export enum Types {
'BANK' = 'BANK',
'CREDIT_CARD' = 'CREDIT_CARD',
}
9 changes: 9 additions & 0 deletions src/common/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Types } from '../@types/common'
import { BANK_SERVICE_START } from './constants'

export const extractType = (content: string) => {
if (content.includes(BANK_SERVICE_START)) {
return Types.BANK
}
return Types.CREDIT_CARD
}
5 changes: 5 additions & 0 deletions src/implementations/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Config } from '../common/config'
import { Reader } from './reader'
import { OfxExtractor } from './ofx-extractor'
import { OfxResponse, OfxStructure, STRTTRN } from '../@types/ofx'
import { extractType } from '../common/helpers'

export class Extractor<T = any> implements IExtractor<T> {
private customExtractorInstance: CustomExtractor
Expand All @@ -30,6 +31,10 @@ export class Extractor<T = any> implements IExtractor<T> {
return this
}

getType() {
return extractType(this.dataReaderInstance.getData())
}

getHeaders(): MetaData {
const [metaDataString] = this.dataReaderInstance.getData().split('<OFX>')
return convertMetaDataToObject(
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/extractor.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import {
ExtractorConfig,
MetaData,
TransactionsSummary,
Types,
} from '../@types/common'

export interface IExtractor<T> {
getType(): Types

config(config: ExtractorConfig): this

getHeaders(): MetaData
Expand Down
4 changes: 4 additions & 0 deletions src/older-implementation/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Ofx {
this.extractor.config(config || {})
}

getType() {
return this.extractor.getType()
}

static fromBuffer(data: Buffer) {
return new Ofx(Reader.fromBuffer(data).getData())
}
Expand Down
5 changes: 0 additions & 5 deletions tests/file-reader-unavailable.web.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import path from 'path'
import { Ofx } from '../src/index'
import fs from 'fs'

const file = fs.readFileSync(path.resolve(__dirname, 'example.ofx'))

describe('Tests for Web/Browser environment with feature not available ', () => {
test.concurrent('Should throw error for FileReader undefined', () => {
const blob = new Blob([file.toString()], { type: 'text/plain' })
jest.mock('../src/common/reader', () => ({
window: null,
}))
Expand Down
15 changes: 15 additions & 0 deletions tests/new-ofx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs, { readFileSync } from 'fs'
import { Extractor } from '../src/implementations/extractor'
import { OfxExtractor } from '../src/implementations/ofx-extractor'
import { Reader } from '../src/implementations/reader'
import { Types } from '../src/@types/common'

const ofxExtractor = new OfxExtractor()
const reader = new Reader()
Expand Down Expand Up @@ -104,4 +105,18 @@ describe('Tests in the Node.js environment', () => {
const extractorInstance = extractor.data(Reader.fromBuffer(file))
expect(extractorInstance.getBankTransferList()).toHaveLength(0)
})

test.concurrent('Should correctly return ofx type (bank)', () => {
const data = Reader.fromString(file.toString())
expect(extractor.data(data).getType()).toBe(Types.BANK)
})

test.concurrent(
'Should correctly return ofx type (credit card)',
async () => {
const file = readFileSync(path.resolve(__dirname, 'example3.ofx'))
const extractorInstance = extractor.data(Reader.fromBuffer(file))
expect(extractorInstance.getType()).toBe(Types.CREDIT_CARD)
},
)
})
5 changes: 5 additions & 0 deletions tests/ofx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Ofx } from '../src/index'
import path from 'path'
import fs from 'fs'
import { Types } from '../src/@types/common'

describe('Tests in the Node.js environment', () => {
const file = fs.readFileSync(path.resolve(__dirname, 'example.ofx'))
Expand Down Expand Up @@ -75,4 +76,8 @@ describe('Tests in the Node.js environment', () => {
expect(summary.dateStart).toBe('2018-01-30')
expect(summary.dateEnd).toBe('2018-04-29')
})

test.concurrent('Should correctly return ofx type (bank)', () => {
expect(ofx.getType()).toBe(Types.BANK)
})
})

0 comments on commit c065ed5

Please sign in to comment.