Skip to content

Commit

Permalink
Add new functions to manage tags and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alejouribesanchez committed Sep 8, 2023
1 parent 4e124ff commit df35395
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
coverage/
package-lock.json
*.xml
.env
.DS_Store
81 changes: 54 additions & 27 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Client(
/**
* Check api version for w2 documents
* @private
* @throws if api_version is different to v8
* @throws error when api_version is different to v8
*/
Client.prototype._check_w2_version = function () {
if (this.api_version !== "v8") throw Error("w2 is only supported on v8")
Expand All @@ -64,7 +64,7 @@ Client.prototype._check_w2_version = function () {
*/
Client.prototype._get_headers = function (has_files = false) {
let final_headers = {
"User-Agent": "Node.js Veryfi-Nodejs/1.2.0",
"User-Agent": "Node.js Veryfi-Nodejs/1.2.3",
"Accept": "application/json",
"Content-Type": "application/json",
"Client-Id": this.client_id,
Expand Down Expand Up @@ -211,7 +211,7 @@ Client.prototype.process_document = async function (
if (!categories) {
categories = this.CATEGORIES;
}
let file_name = path.basename(file_path)
let file_name = path.basename(file_path);
const image_file = fs.readFileSync(file_path, {encoding: 'base64'});
const base64_encoded_string = Buffer.from(image_file).toString('utf-8');
let request_arguments = {
Expand All @@ -220,8 +220,8 @@ Client.prototype.process_document = async function (
"categories": categories,
"auto_delete": delete_after_processing,
};
request_arguments = Object.assign(request_arguments, kwargs)
let document = await this._request("POST", endpoint_name, request_arguments)
request_arguments = Object.assign(request_arguments, kwargs);
let document = await this._request("POST", endpoint_name, request_arguments);
return document['data'];
}

Expand Down Expand Up @@ -260,8 +260,8 @@ Client.prototype.process_document_buffer = async function (
"categories": categories,
"auto_delete": delete_after_processing,
};
request_arguments = Object.assign(request_arguments, kwargs)
let document = await this._request("POST", endpoint_name, request_arguments)
request_arguments = Object.assign(request_arguments, kwargs);
let document = await this._request("POST", endpoint_name, request_arguments);
return document['data'];
}

Expand Down Expand Up @@ -304,7 +304,7 @@ Client.prototype.process_document_url = async function (
"file_urls": file_urls,
"max_pages_to_process": max_pages_to_process,
};
request_arguments = Object.assign(request_arguments, kwargs)
request_arguments = Object.assign(request_arguments, kwargs);
let response = await this._request("POST", endpoint_name, request_arguments);
return response['data'];
}
Expand Down Expand Up @@ -344,15 +344,15 @@ Client.prototype.update_document = async function (document_id, {...kwargs} = {}
* @return {Array} An array of JSON with all w2 documents.
*/
Client.prototype.get_w2_documents = async function (page = null) {
this._check_w2_version()
let endpoint_name = "/w2s/"
let request_arguments = {}
let params = {}
this._check_w2_version();
let endpoint_name = "/w2s/";
let request_arguments = {};
let params = {};
if (page !== null) {
params = {"page": page}
params = {"page": page};
}
let response = await this._request("GET", endpoint_name, request_arguments, params)
return response['data']['results']
let response = await this._request("GET", endpoint_name, request_arguments, params);
return response['data']['results'];
}

/**
Expand All @@ -364,11 +364,11 @@ Client.prototype.get_w2_documents = async function (page = null) {
Client.prototype.get_w2_document = async function (
document_id,
) {
this._check_w2_version()
let endpoint_name = `/w2s/${document_id}/`
let request_arguments = {"id": document_id}
let response = await this._request("GET", endpoint_name, request_arguments)
return response['data']
this._check_w2_version();
let endpoint_name = `/w2s/${document_id}/`;
let request_arguments = {"id": document_id};
let response = await this._request("GET", endpoint_name, request_arguments);
return response['data'];
}

/**
Expand Down Expand Up @@ -396,9 +396,9 @@ Client.prototype.process_w2_document_from_buffer = async function (
"auto_delete": delete_after_processing,
"max_pages_to_process": max_pages_to_process,
}
request_arguments = Object.assign(request_arguments, kwargs)
let response = await this._request("POST", endpoint_name, request_arguments)
return response['data']
request_arguments = Object.assign(request_arguments, kwargs);
let response = await this._request("POST", endpoint_name, request_arguments);
return response['data'];
}

/**
Expand All @@ -424,7 +424,7 @@ Client.prototype.process_w2_document = async function (
delete_after_processing,
max_pages_to_process,
kwargs
)
);
}

/**
Expand Down Expand Up @@ -456,11 +456,38 @@ Client.prototype.process_w2_document_from_url = async function (
"file_urls": file_urls,
"max_pages_to_process": max_pages_to_process
}
request_arguments = Object.assign(request_arguments, kwargs)
let response = await this._request("POST", endpoint_name, request_arguments)
return response['data']
request_arguments = Object.assign(request_arguments, kwargs);
let response = await this._request("POST", endpoint_name, request_arguments);
return response['data'];
}

/**
* Add a new tag on an existing document
*
* @param {number} document_id ID of the document you'd like to add a Tag
* @param {string} tag name to add
* @return {JSON} response about tag added.
*/
Client.prototype.add_tag = async function (document_id, tag) {
let endpoint_name = `/documents/${document_id}/tags/`;
let request_arguments = {"name": tag};
let response = await this._request("PUT", endpoint_name, request_arguments);
return response['data'];
}

/**
* Delete all the tags on an existing document
*
* @param {number} document_id ID of the document you'd like to delete tags
* @return {JSON} response about deleted tags.
*/
Client.prototype.delete_tags = async function (document_id) {
let endpoint_name = `/documents/${document_id}/tags/`;
let request_arguments = {};
return this._request("DELETE", endpoint_name, request_arguments);
}


// Exports

module.exports = Client;
27 changes: 25 additions & 2 deletions lib/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ declare type Payment = {
type?: null | string;
};

declare type Tag = {
id?: null | number;
name?: null | string;
};

export declare class Client {
/**
* Create instance of a Client
Expand Down Expand Up @@ -234,10 +239,10 @@ export declare class Client {
* @param {string} document_id ID of the document you'd like to delete
* @returns {Promise<VeryfiDocument>} Object of data extracted from the document
*/
public delete_document(document_id: string): Promise<VeryfiDocument>;
public delete_document(document_id: string): Promise<any>;

/**
* Update data for a previously processed document, including almost any field like `vendor`, `date`, `notes` and etc.
* Update data for a previously processed document, including almost any field like `vendor`, `date`, `notes` etc.
* @example
* veryfi_client.update_document(
* id,
Expand All @@ -252,6 +257,24 @@ export declare class Client {
document_id: string,
{ ...kwargs }?: VeryfiExtraArgs
): Promise<VeryfiDocument>;

/**
* Add a new tag on an existing document
*
* @param {number} document_id ID of the document you'd like to add a Tag
* @param {string} tag name to add
* @return {Promise<Tag>} response about tag added.
*/
public add_tag(document_id: string, tag: string): Promise<Tag>;

/**
* Delete all tags on an existing document
*
* @param {number} document_id ID of the document you'd like to delete all Tags
* @return {Promise<any>} response about deleted tags.
*/
public delete_tags(document_id: string): Promise<any>;

}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@veryfi/veryfi-sdk",
"version": "1.2.2",
"version": "1.2.3",
"description": "Node.js module for communicating with the Veryfi OCR API",
"main": "lib/main.js",
"typings": "lib/types/main.d.ts",
Expand Down
48 changes: 37 additions & 11 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const client_secret = process.env.VERYFI_CLIENT_SECRET;
const username = process.env.VERYFI_USERNAME;
const api_key = process.env.VERYFI_API_KEY;
const base_url = process.env.VERYFI_URL;
const api_version = "v7"
const api_version = "v8"
const timeout = 100000;

//Creating the Client
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('Processing documents', () => {
test('Process document from URL', async () => {
try {
let response = await veryfi_client.process_document_url('https://cdn.veryfi.com/receipts/92233902-c94a-491d-a4f9-0d61f9407cd2.pdf');
expect(response['vendor']['name']).toBe('Rumpke Of Ohio');
expect(response['vendor']['name']).toBe('Rumpke of Ohio');
} catch (error) {
throw new Error(error);
}
Expand All @@ -57,7 +57,7 @@ describe('Managing documents', () => {
test('Get documents', async () => {
try {
let docs = await veryfi_client.get_documents();
expect(docs.length).toBeGreaterThan(0);
expect(docs.documents.length).toBeGreaterThan(0);
} catch (error) {
throw new Error(error);
}
Expand All @@ -66,9 +66,35 @@ describe('Managing documents', () => {
test(`Get document with id `, async () => {
try {
let docs = await veryfi_client.get_documents();
const doc_id = docs[0].id;
const doc_id = docs.documents[0].id;
let doc = await veryfi_client.get_document(doc_id);
expect(doc['id']).toBe(doc_id)
expect(doc['id']).toBe(doc_id);
} catch (error) {
throw new Error(error);
}
});
});

describe('Managing tags', () => {
test(`Delete all tags of a document`, async () => {
try {
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
let response = await veryfi_client.delete_tags(doc_id);
expect(response).toBeDefined()
} catch (error) {
throw new Error(error);
}
});

test(`Add tag to a document`, async () => {
try {
let tag_name = 'TEST_TAG'
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
await veryfi_client.delete_tags(doc_id);
let tag = await veryfi_client.add_tag(doc_id, tag_name);
expect(tag.name).toBe(tag_name)
} catch (error) {
throw new Error(error);
}
Expand All @@ -81,7 +107,7 @@ describe('Editing Documents', () => {
let params = {'notes': randomString};
try {
let docs = await veryfi_client.get_documents();
const doc_id = docs[0].id;
const doc_id = docs.documents[0].id;
let response = await veryfi_client.update_document(doc_id, params);
expect(response).toEqual(expect.objectContaining(params));
} catch (error) {
Expand All @@ -92,33 +118,33 @@ describe('Editing Documents', () => {
test('Delete a document by id', async () => {
try {
let docs = await veryfi_client.get_documents();
const doc_id = docs[0].id;
const doc_id = docs.documents[0].id;
let response = await veryfi_client.delete_document(doc_id);
expect(response['status']).toBe(200)
expect(response['status']).toBeDefined();
} catch (error) {
throw new Error(error);
}
});
})

describe('Process w2 documents', () => {
test('Process a document from file_path', async () => {
test('Process a w2 document from file_path', async () => {
try {
await veryfi_client.process_w2_document('resources/w2.png', true);
assert(false)
} catch (error) {
assert(true)
}
})
test('Get a documents and get a document by id', async () => {
test('Get a w2 documents and get a document by id', async () => {
try {
await veryfi_client.get_w2_documents()
assert(false)
} catch (error) {
assert(true)
}
})
test('Process a document from url', async () => {
test('Process a w2 document from url', async () => {
try{
await veryfi_client.process_w2_document_from_url(
'w2.png',
Expand Down
Loading

0 comments on commit df35395

Please sign in to comment.