Skip to content

Commit 11f8201

Browse files
authored
Merge pull request #254 from scientist-softserv/138-diplay-pos
138 diplay POs
2 parents 5b5d9fb + a5b1f02 commit 11f8201

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

pages/api/auth/[...nextauth].js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ const refreshAccessToken = async (token) => {
6565
const url = `https://${process.env.NEXT_PUBLIC_PROVIDER_NAME}.scientist.com/oauth/token`
6666
const encodedString = Buffer.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`).toString('base64')
6767
const params = new URLSearchParams({
68+
/* eslint-disable camelcase */
6869
grant_type: 'refresh_token',
6970
refresh_token: token.refreshToken,
71+
/* eslint-enable camelcase */
7072
})
7173

7274
const response = await axios.post(url, params, {

pages/requests/[uuid].js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { useRouter } from 'next/router'
33
import { useSession } from 'next-auth/react'
44
import {
@@ -22,6 +22,7 @@ import {
2222
statusBarBg,
2323
useMessages,
2424
useFiles,
25+
getAllPOs,
2526
useAllSOWs,
2627
useOneRequest,
2728
} from '../../utils'
@@ -38,10 +39,25 @@ const Request = () => {
3839
const { allSOWs, isLoadingSOWs, isSOWError } = useAllSOWs(uuid, request?.identifier, session?.accessToken)
3940
const { messages, isLoadingMessages, isMessagesError, mutateMessages, messagesData } = useMessages(uuid, session?.accessToken)
4041
const { files, isLoadingFiles, isFilesError, mutateFiles, filesData } = useFiles(uuid, session?.accessToken)
41-
const documents = (allSOWs) ? [...allSOWs] : []
4242

43-
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages
44-
const isError = isRequestError || isSOWError || isFilesError|| isMessagesError
43+
const [allPOs, setAllPOs] = useState([])
44+
const [isPOError, setIsPOError] = useState(false)
45+
const [isLoadingPOs, setIsLoadingPOs] = useState(true)
46+
useEffect(() => {
47+
if (request) {
48+
(async () => {
49+
const { allPOs, isLoadingPOs, isPOError } = await getAllPOs(request?.quotedWareID, uuid, request?.identifier, session?.accessToken)
50+
51+
setIsLoadingPOs(isLoadingPOs)
52+
setAllPOs(allPOs)
53+
setIsPOError(isPOError)
54+
})()
55+
}
56+
}, [allPOs, isPOError, request, uuid, session])
57+
58+
const isLoading = isLoadingRequest || isLoadingSOWs || isLoadingFiles || isLoadingMessages || isLoadingPOs
59+
const isError = isRequestError || isSOWError || isFilesError|| isMessagesError || isPOError
60+
const documents = (allSOWs) ? [...allSOWs, ...allPOs] : []
4561

4662
if (isLoading) return <Loading wrapperClass='item-page mt-5' />
4763

@@ -61,7 +77,7 @@ const Request = () => {
6177
if (isError) {
6278
return (
6379
<Notice
64-
alert={configureErrors([isRequestError, isSOWError, isMessagesError, isFilesError])}
80+
alert={configureErrors([isRequestError, isSOWError, isMessagesError, isFilesError, isPOError])}
6581
dismissible={false}
6682
withBackButton={true}
6783
buttonProps={{

utils/api/configurations.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,16 @@ export const configureFiles = (data) => {
185185
return allFiles
186186
}
187187

188-
export const configureDocuments = (documents, requestIdentifier) => {
189-
return documents?.map(document => ({
190-
identifier: document.identifier,
188+
export const configureDocument = (document, requestIdentifier) => {
189+
return {
191190
date: normalizeDate(document.created_at),
192191
documentStatus: document.status,
193192
documentStatusColor: statusColors[configureStatus(document.status)].bg,
194193
documentType: document.type,
195194
documentTypeColor: 'bg-dark',
195+
identifier: document.identifier,
196196
lineItems: configureLineItems(document.line_items),
197197
requestIdentifier,
198-
subtotalPrice: document.retail_subtotal_price_currency,
199-
taxAmount: document.tax_cost_currency,
200-
terms: document.payment_terms,
201-
totalPrice: document.retail_total_price_currency,
202198
shippingPrice: document.shipping_cost_currency,
203199
shipTo: {
204200
organizationName: document.ship_to.organization_name,
@@ -208,9 +204,27 @@ export const configureDocuments = (documents, requestIdentifier) => {
208204
organizationName: document.ship_from.organization_name,
209205
text: document.ship_from.text,
210206
},
207+
subtotalPrice: document.retail_subtotal_price_currency,
208+
taxAmount: document.tax_cost_currency,
209+
terms: document.payment_terms,
210+
totalPrice: document.retail_total_price_currency,
211+
}
212+
}
213+
214+
export const configureSOWs = (sows, requestIdentifier) => {
215+
return sows?.map((sow) => ({
216+
...configureDocument(sow, requestIdentifier),
211217
}))
212218
}
213219

220+
export const configurePO = (po, requestIdentifier) => ({
221+
...configureDocument(po, requestIdentifier),
222+
turnaroundTime: po.turn_around_time.human,
223+
poNumber: po.po_number,
224+
relatedSOWIdentifier: po.proposal_ref?.identifier,
225+
adPO: po.scientist_identifier,
226+
})
227+
214228
const configureLineItems = (lineItems) => (lineItems.map(lineItem => ({
215229
id: lineItem.id,
216230
quantity: lineItem.quantity,

utils/api/requests.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import useSWR from 'swr'
22
import {
33
configureFiles,
4-
configureDocuments,
54
configureDynamicFormSchema,
65
configureDynamicFormUiSchema,
76
configureMessages,
7+
configurePO,
88
configureRequests,
9+
configureSOWs,
910
} from './configurations'
1011
import { fetcher, posting, updating } from './base'
1112

@@ -45,7 +46,7 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
4546
const { data, error } = useSWR(id ? [`/quote_groups/${id}/proposals.json`, accessToken] : null)
4647
let allSOWs
4748
if (data) {
48-
allSOWs = configureDocuments(data, requestIdentifier)
49+
allSOWs = configureSOWs(data, requestIdentifier)
4950
}
5051

5152
return {
@@ -55,6 +56,33 @@ export const useAllSOWs = (id, requestIdentifier, accessToken) => {
5556
}
5657
}
5758

59+
// The name of this function is getAllPOs vs. useAllPOs. Since it is not a hook, it should not start with "use".
60+
export const getAllPOs = async (quotedWareId, uuid, requestIdentifier, accessToken) => {
61+
try {
62+
// TODO(summer-cook): eventually we can use the useSWRList hook here instead of mapping & calling the fetcher.
63+
// This hook is actively being contributed to the swr repo, but the semantics of the work are still being debated.
64+
// See https://github.com/vercel/swr/discussions/1988 for the RFC and https://github.com/vercel/swr/pull/2047 for the PR.
65+
const data = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders.json`, accessToken)
66+
const configuredPOs = data.map(async (po) => {
67+
const purchaseOrder = await fetcher(`quote_groups/${uuid}/quoted_wares/${quotedWareId}/purchase_orders/${po.id}.json`, accessToken)
68+
return configurePO(purchaseOrder, requestIdentifier)
69+
})
70+
const allPOs = await Promise.all(configuredPOs).then(res => res)
71+
72+
return {
73+
allPOs,
74+
isLoadingPOs: !allPOs,
75+
isPOError: false,
76+
}
77+
} catch (error) {
78+
return {
79+
allPOs: [],
80+
isLoadingPOs: false,
81+
isPOError: error,
82+
}
83+
}
84+
}
85+
5886
export const useMessages = (requestUuid, accessToken) => {
5987
const { data, error, mutate } = useSWR(requestUuid ? [`/quote_groups/${requestUuid}/messages.json`, accessToken] : null)
6088
let messages

0 commit comments

Comments
 (0)