Skip to content

Commit

Permalink
Update or Create Qs Records (#1542)
Browse files Browse the repository at this point in the history
* Conditionally update or create Qs Records

* clean Up
  • Loading branch information
cameron-eyds authored Sep 21, 2023
1 parent 2567069 commit 5fd0f39
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
13 changes: 11 additions & 2 deletions ppr-ui/src/composables/userAccess/useUserAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
getFeatureFlag,
getKeyByValue,
getQsServiceAgreements,
getQualifiedSupplier,
hasTruthyValue,
requestProductAccess,
updateQualifiedSupplier,
updateUserSettings
} from '@/utils'
import { MhrSubTypes, ProductCode, ProductStatus, RouteNames, SettingOptions } from '@/enums'
Expand Down Expand Up @@ -251,13 +253,20 @@ export const useUserAccess = () => {
*/
const submitQsApplication = async (): Promise<void> => {
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 42 additions & 2 deletions ppr-ui/src/utils/mhr-api-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -732,6 +734,28 @@ export async function getMhrManufacturerInfo (): Promise<MhrManufacturerInfoIF>
})
}

/** Request Qualified Supplier record in MHR */
export async function getQualifiedSupplier (): Promise<MhrQsPayloadIF> {
try {
const response = await axios.get<MhrQsPayloadIF>('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
Expand All @@ -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<MhrQsPayloadIF> {
return axios
.put<MhrQsPayloadIF>('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<any> {
const url = sessionStorage.getItem('MHR_API_URL')
Expand Down

0 comments on commit 5fd0f39

Please sign in to comment.