From 5fd0f39d8b654aa92960155fedfbe2cf9b463fcb Mon Sep 17 00:00:00 2001 From: CameronEYDS <53542131+cameron-eyds@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:14:46 -0700 Subject: [PATCH] Update or Create Qs Records (#1542) * Conditionally update or create Qs Records * clean Up --- .../composables/userAccess/useUserAccess.ts | 13 +++++- .../MhrQsPayloadIF.ts | 4 +- ppr-ui/src/utils/mhr-api-helper.ts | 44 ++++++++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/ppr-ui/src/composables/userAccess/useUserAccess.ts b/ppr-ui/src/composables/userAccess/useUserAccess.ts index 0c9ed7279..70b12869d 100644 --- a/ppr-ui/src/composables/userAccess/useUserAccess.ts +++ b/ppr-ui/src/composables/userAccess/useUserAccess.ts @@ -8,8 +8,10 @@ import { getFeatureFlag, getKeyByValue, getQsServiceAgreements, + getQualifiedSupplier, hasTruthyValue, requestProductAccess, + updateQualifiedSupplier, updateUserSettings } from '@/utils' import { MhrSubTypes, ProductCode, ProductStatus, RouteNames, SettingOptions } from '@/enums' @@ -251,13 +253,20 @@ export const useUserAccess = () => { */ const submitQsApplication = async (): Promise => { const payload: MhrQsPayloadIF = { - ...cleanEmpty(getMhrQsInformation.value), + ...cleanEmpty(getMhrQsInformation.value) as MhrQsPayloadIF, authorizationName: getMhrQsAuthorization.value.authorizationName, phoneNumber: fromDisplayPhone(getMhrQsInformation.value.phoneNumber) } try { - const qsData: MhrQsPayloadIF = await createQualifiedSupplier(payload) + // Check for current QS Record + const hasQsRecord = await getQualifiedSupplier() + + // Create or Update based on previous record + const qsData: MhrQsPayloadIF = hasQsRecord + ? await updateQualifiedSupplier(payload) + : await createQualifiedSupplier(payload) + const authProductCode = ProductCode[getKeyByValue(MhrSubTypes, getMhrSubProduct.value)] const authData = await requestProductAccess(authProductCode) diff --git a/ppr-ui/src/interfaces/mhr-user-access-interfaces/MhrQsPayloadIF.ts b/ppr-ui/src/interfaces/mhr-user-access-interfaces/MhrQsPayloadIF.ts index 7046ea70d..e37818e15 100644 --- a/ppr-ui/src/interfaces/mhr-user-access-interfaces/MhrQsPayloadIF.ts +++ b/ppr-ui/src/interfaces/mhr-user-access-interfaces/MhrQsPayloadIF.ts @@ -3,7 +3,7 @@ import { AddressIF } from '@/interfaces' export interface MhrQsPayloadIF { authorizationName: string businessName: string - dbaName: string + dbaName?: string address: AddressIF - phoneNumber: number + phoneNumber: string } diff --git a/ppr-ui/src/utils/mhr-api-helper.ts b/ppr-ui/src/utils/mhr-api-helper.ts index b68f33625..e5bff1e23 100644 --- a/ppr-ui/src/utils/mhr-api-helper.ts +++ b/ppr-ui/src/utils/mhr-api-helper.ts @@ -12,12 +12,14 @@ import { MhrDraftApiIF, RegistrationSortIF, MhrDraftIF, - MhrManufacturerInfoIF, PartyIF, MhrQsPayloadIF + MhrManufacturerInfoIF, + MhrQsPayloadIF } from '@/interfaces' import { APIMhrTypes, ErrorCategories, ErrorCodes } from '@/enums' import { useSearch } from '@/composables/useSearch' import { SessionStorageKeys } from 'sbc-common-components/src/util/constants' -import { addTimestampToDate, parsePayDetail } from '@/utils' +import { addTimestampToDate } from '@/utils' +import { AxiosError } from 'axios' const { mapMhrSearchType } = useSearch() // Create default request base URL and headers. @@ -732,6 +734,28 @@ export async function getMhrManufacturerInfo (): Promise }) } +/** Request Qualified Supplier record in MHR */ +export async function getQualifiedSupplier (): Promise { + try { + const response = await axios.get('qualified-suppliers', getDefaultConfig()) + const data: MhrQsPayloadIF = response?.data + if (!data) { + throw new Error('Invalid API response') + } + return data + } catch (error: AxiosError | any) { + if (error.response && error.response.status === 404) { + console.error('Resource not found:', error.message) + // Handle 404 gracefully, returning null + return null + } else { + // Handle other errors differently if needed + console.error('API Error:', error.message) + throw error + } + } +} + /** * Request creation of a Qualified Supplier in MHR * @param payload The request payload containing the qualified supplier application information @@ -748,6 +772,22 @@ export async function createQualifiedSupplier (payload: MhrQsPayloadIF): Promise }) } +/** + * Request update or creation of a Qualified Supplier in MHR + * @param payload The request payload containing the qualified supplier application information + */ +export async function updateQualifiedSupplier (payload: MhrQsPayloadIF): Promise { + return axios + .put('qualified-suppliers', payload, getDefaultConfig()) + .then(response => { + const data: MhrQsPayloadIF = response?.data + if (!data) { + throw new Error('Invalid API response') + } + return data + }) +} + // Get pdf for a Qualified Supplier Service Agreement export async function getQsServiceAgreements (): Promise { const url = sessionStorage.getItem('MHR_API_URL')