From 79e361ee6a691c5c59d9db8e43c1c1f663623d79 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 14 Nov 2023 07:51:43 -0800 Subject: [PATCH] update fetchNameRequest --- package-lock.json | 4 ++-- package.json | 2 +- src/services/legal-services.ts | 33 ++++++++++++++++++++++++------- tests/unit/App.spec.ts | 16 +++++++-------- tests/unit/legal-services.spec.ts | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3e30f86c2..6da0a57cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "business-filings-ui", - "version": "7.0.8", + "version": "7.0.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "business-filings-ui", - "version": "7.0.8", + "version": "7.0.9", "dependencies": { "@babel/compat-data": "^7.21.5", "@bcrs-shared-components/base-address": "2.0.9", diff --git a/package.json b/package.json index c3f772e3c..5446ec413 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "business-filings-ui", - "version": "7.0.8", + "version": "7.0.9", "private": true, "appName": "Filings UI", "sbcName": "SBC Common Components", diff --git a/src/services/legal-services.ts b/src/services/legal-services.ts index 78f0b6414..55fa3634a 100644 --- a/src/services/legal-services.ts +++ b/src/services/legal-services.ts @@ -4,6 +4,7 @@ import { AxiosResponse } from 'axios' import { ApiBusinessIF, ApiFilingIF, CommentIF, DocumentIF, FetchDocumentsIF, NameRequestIF, PresignedUrlIF } from '@/interfaces' import { DigitalCredentialTypes, FilingStatus, Roles } from '@/enums' +import { StatusCodes } from 'http-status-codes' /** * Class that provides integration with the Legal API. @@ -95,15 +96,33 @@ export default class LegalServices { } /** - * Fetches a Name Request. - * @param filingId the NR number - * @returns the name request object + * Fetches name request data. + * @param nrNumber the name request number (eg, NR 1234567) to fetch + * @param phone the name request phone (eg, 12321232) + * @param email the name request email (eg, nr@example.com) + * @returns a promise to return the NR data, or null if not found */ - static async fetchNameRequest (nrNumber: string): Promise { - const url = `nameRequests/${nrNumber}` + static async fetchNameRequest (nrNumber: string, phone = '', email = ''): Promise { + const url = `nameRequests/${nrNumber}/validate?phone=${phone}&email=${email}` + return axios.get(url) - // workaround because data is at "response.data.data" - .then(response => response?.data) + .then(response => { + if (response?.data) { + return response.data + } + // eslint-disable-next-line no-console + console.log('fetchNameRequest() error - invalid response =', response) + throw new Error('Invalid API response') + }).catch(error => { + if (error?.response?.status === StatusCodes.NOT_FOUND) { + return null // NR not found (not an error) + } else if (error?.response?.status === StatusCodes.BAD_REQUEST) { + throw new Error('Sent invalid email or phone number.') // Sent invalid email or phone + } else if (error?.response?.status === StatusCodes.FORBIDDEN) { + throw new Error('Not sent email or phone number.') // Not sent the email or phone + } + throw error + }) } /** diff --git a/tests/unit/App.spec.ts b/tests/unit/App.spec.ts index 3a1e3ba3d..ae39585b8 100644 --- a/tests/unit/App.spec.ts +++ b/tests/unit/App.spec.ts @@ -910,7 +910,7 @@ describe('App as a Draft IA with approved NR', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1032,7 +1032,7 @@ describe('App as a Draft IA with conditional-not required NR', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1135,7 +1135,7 @@ describe('App as a Draft IA with conditional-received NR', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1238,7 +1238,7 @@ describe('App as a Draft IA with conditional-waived NR', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1341,7 +1341,7 @@ describe('App as a PAID (pending) Incorporation Application', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1488,7 +1488,7 @@ describe('App as a COMPLETED Incorporation Application', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1769,7 +1769,7 @@ describe('App as a Draft Registration with approved NR', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, @@ -1892,7 +1892,7 @@ describe('App as a COMPLETED Registration Application', () => { }))) // GET NR data - get.withArgs('nameRequests/NR 1234567') + get.withArgs('nameRequests/NR 1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: { applicants: {}, diff --git a/tests/unit/legal-services.spec.ts b/tests/unit/legal-services.spec.ts index e128dc1bb..34e66d010 100644 --- a/tests/unit/legal-services.spec.ts +++ b/tests/unit/legal-services.spec.ts @@ -137,7 +137,7 @@ describe('Legal Services', () => { } // mock endpoint - get.withArgs('nameRequests/NR1234567') + get.withArgs('nameRequests/NR1234567/validate?phone=&email=') .returns(new Promise(resolve => resolve({ data: NR }))) // call method