Skip to content

Commit

Permalink
Merge pull request #36 from veryfi/feature/PLAT-5459-add-multiple-tags
Browse files Browse the repository at this point in the history
Add function to add multiple tags to a single document
  • Loading branch information
Kaevan89 authored Jan 3, 2024
2 parents 03a3533 + 71d948c commit 293b026
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 5 deletions.
30 changes: 29 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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.4",
"User-Agent": "Node.js Veryfi-Nodejs/1.2.5",
"Accept": "application/json",
"Content-Type": "application/json",
"Client-Id": this.client_id,
Expand Down Expand Up @@ -535,6 +535,34 @@ Client.prototype.delete_tags = async function (document_id) {
return this._request("DELETE", endpoint_name, request_arguments);
}

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

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


// Exports

Expand Down
18 changes: 18 additions & 0 deletions lib/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ export declare class Client {
*/
public delete_tags(document_id: string): Promise<any>;

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

/**
* Replace multiple tags on an existing document
*
* @param {number} document_id ID of the document you'd like to add a Tag
* @param {string[]} tags names to be added
* @return {Promise<Tag>} response about tags added.
*/
public replace_tags(document_id: string, tags: string[]): Promise<Tag>;

}

/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.4",
"version": "1.2.5",
"description": "Node.js module for communicating with the Veryfi OCR API",
"main": "lib/main.js",
"typings": "lib/types/main.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ describe('Managing tags', () => {
throw new Error(error);
}
});

test(`Add multiple tags to a document`, async () => {
try {
let tags = ['TAG_1', 'TAG_2', 'TAG_3']
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
await veryfi_client.delete_tags(doc_id);
let response = await veryfi_client.add_tags(doc_id, tags);
expect(response).toBeDefined()
} catch (error) {
throw new Error(error);
}
});

test(`Replace tags in a document`, async () => {
try {
let tags = ['TAG_1', 'TAG_2', 'TAG_3']
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
await veryfi_client.delete_tags(doc_id);
let response = await veryfi_client.replace_tags(doc_id, tags);
expect(response).toBeDefined()
} catch (error) {
throw new Error(error);
}
});
});

describe('Editing Documents', () => {
Expand Down
28 changes: 27 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Processing documents', () => {
expect(response.total).toBe(329.74);
expect(response.tax).toBe(23.47);
expect(response.subtotal).toBe(329.74);
expect(response.category).toBe('Utilities');
expect(response.category).toBeDefined();
expect(response.document_type).toBe("invoice");
expect(response.line_items[0].total).toBe(116.32);
expect(response.line_items[1].total).toBe(10);
Expand Down Expand Up @@ -146,6 +146,32 @@ describe('Managing tags', () => {
throw new Error(error);
}
});

test(`Add multiple tags to a document`, async () => {
try {
let tags = ['TAG_1', 'TAG_2', 'TAG_3']
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
await veryfi_client.delete_tags(doc_id);
let response = await veryfi_client.add_tags(doc_id, tags);
expect(response).toBeDefined()
} catch (error) {
throw new Error(error);
}
});

test(`Replace multiple tags in a document`, async () => {
try {
let tags = ['TAG_1', 'TAG_2', 'TAG_3']
let docs = await veryfi_client.get_documents();
const doc_id = docs.documents[0].id;
await veryfi_client.delete_tags(doc_id);
let response = await veryfi_client.replace_tags(doc_id, tags);
expect(response).toBeDefined()
} catch (error) {
throw new Error(error);
}
});
});

describe('Editing Documents', () => {
Expand Down

0 comments on commit 293b026

Please sign in to comment.