Skip to content

Commit

Permalink
feat: Add Last Updated to Guardian Directory (#1194)
Browse files Browse the repository at this point in the history
* Add getLastModifiedAt

* Add last updated to guardian directory

* Add tests
  • Loading branch information
jcbcapps authored Jan 16, 2024
1 parent 141a348 commit 540edb5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/__tests__/pages/api/dataSources/personnel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const mockData = {
lastModifiedAt: '2021-10-01',
}

const mockLastModifiedAt = '2021-10-01'

describe('Personnel API', () => {
describe('getUserData', () => {
test('call getUserData with an id', async () => {
Expand All @@ -31,7 +33,7 @@ describe('Personnel API', () => {
personnelAPI.baseURL = 'https://example.com'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post');
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post')
mockPost.mockResolvedValue({ data: mockData })

const result = await personnelAPI.getUserData('1234567890')
Expand Down Expand Up @@ -59,7 +61,7 @@ describe('Personnel API', () => {
personnelAPI.baseURL = 'https://example.com'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post');
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post')
mockPost.mockResolvedValue({ data: mockData })

const result = await personnelAPI.getGuardianDirectory()
Expand All @@ -78,4 +80,60 @@ describe('Personnel API', () => {
)
})
})

describe('getLastModifiedAt', () => {
test('call getLastModifiedAt with an id', async () => {
const mockCache = {} as KeyValueCache
const personnelAPI = new PersonnelAPI({ cache: mockCache })

personnelAPI.baseURL = 'https://example.com'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post')
mockPost.mockResolvedValue({ data: mockLastModifiedAt })

const result = await personnelAPI.getLastModifiedAt()

expect(result).toEqual({ data: mockLastModifiedAt })
})

test('throw an error when there is no baseURL', async () => {
const mockCache = {} as KeyValueCache
const personnelAPI = new PersonnelAPI({ cache: mockCache })

personnelAPI.baseURL = ''

await expect(personnelAPI.getLastModifiedAt()).rejects.toThrow(
'No Personnel API URL found'
)
})
})

describe('searchGuardianDirectory', () => {
test('call searchGuardianDirectory with an id', async () => {
const mockCache = {} as KeyValueCache
const personnelAPI = new PersonnelAPI({ cache: mockCache })

personnelAPI.baseURL = 'https://example.com'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockPost = jest.spyOn(PersonnelAPI.prototype as any, 'post')
mockPost.mockResolvedValue({ data: mockData })

const result = await personnelAPI.searchGuardianDirectory('1234567890')

expect(result).toEqual({ data: mockData })
})

test('throw an error when there is no baseURL', async () => {
const mockCache = {} as KeyValueCache
const personnelAPI = new PersonnelAPI({ cache: mockCache })

personnelAPI.baseURL = ''

await expect(
personnelAPI.searchGuardianDirectory('1234567890')
).rejects.toThrow('No Personnel API URL found')
})
})
})
3 changes: 3 additions & 0 deletions src/operations/portal/queries/getLastModifiedAt.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query getLastModifiedAt {
getLastModifiedAt
}
16 changes: 16 additions & 0 deletions src/pages/api/dataSources/personnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class PersonnelAPI extends RESTDataSource {
})
}

async getLastModifiedAt() {
if (!this.baseURL) throw new Error('No Personnel API URL found')

return this.post(this.baseURL + `/api/graphql`, {
body: {
query: getLastModifiedAtQuery,
},
})
}

async searchGuardianDirectory(query: string) {
if (!this.baseURL) throw new Error('No Personnel API URL found')

Expand Down Expand Up @@ -92,6 +102,12 @@ query GetGuardianDirectory {
}
`

const getLastModifiedAtQuery = `
query GetLastModifiedAt {
getLastModifiedAt
}
`

const searchGuardianDirectoryQuery = `
query SearchGuardianDirectory($search: String!) {
searchGuardianDirectory(search: $search) {
Expand Down
11 changes: 11 additions & 0 deletions src/pages/guardian-directory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import styles from 'styles/pages/guardianDirectory.module.scss'
import { GuardianDirectoryTable } from 'components/GuardianDirectoryTable/GuardianDirectoryTable'
import { useGetGuardianDirectoryQuery } from 'operations/portal/queries/getGuardianDirectory.g'
import { useSearchGuardianDirectoryQuery } from 'operations/portal/queries/searchGuardianDirectory.g'
import { useGetLastModifiedAtQuery } from 'operations/portal/queries/getLastModifiedAt.g'
import { GuardianDirectory as GuardianDirectoryType } from 'types'

const GuardianDirectory = () => {
Expand All @@ -17,6 +18,7 @@ const GuardianDirectory = () => {
const { loading } = useUser()
const [directory, setDirectory] = useState(Array<GuardianDirectoryType>)
const { data } = useGetGuardianDirectoryQuery()
const { data: lastModifiedAt } = useGetLastModifiedAtQuery()

const [searchQuery, setSearchQuery] = useState('')
const { data: searchData, loading: loadingSearch } =
Expand Down Expand Up @@ -108,6 +110,15 @@ const GuardianDirectory = () => {
rows={directory}
/>
)}

<p>
Last updated:{' '}
{lastModifiedAt?.getLastModifiedAt &&
new Date(lastModifiedAt.getLastModifiedAt).toLocaleDateString(
'en-US',
{ year: 'numeric', month: 'short', day: 'numeric' }
)}
</p>
</div>
</>
)
Expand Down
20 changes: 20 additions & 0 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ type GetGuardianDirectoryPromise = {
}
}

type GetLastModifiedAtPromise = {
data: {
getLastModifiedAt: {
lastModifiedAt: string
}
}
}

type SearchGuardianDirectoryPromise = {
data: {
searchGuardianDirectory: [
Expand Down Expand Up @@ -168,6 +176,7 @@ type PortalUserContext = {
personnelAPI: {
getUserData: (dodId: string) => Promise<GetPersonnelDataPromise>
getGuardianDirectory: () => Promise<GetGuardianDirectoryPromise>
getLastModifiedAt: () => Promise<GetLastModifiedAtPromise>
searchGuardianDirectory: (
query: string
) => Promise<SearchGuardianDirectoryPromise>
Expand Down Expand Up @@ -255,6 +264,17 @@ const resolvers = {

// Root resolvers
Query: {
getLastModifiedAt: async (
_: undefined,
args: undefined,
{ dataSources }: PortalUserContext
) => {
const {
data: { getLastModifiedAt },
} = await dataSources.personnelAPI.getLastModifiedAt()

return getLastModifiedAt
},
searchGuardianDirectory: async (
_: undefined,
{ search }: { search: string },
Expand Down
1 change: 1 addition & 0 deletions src/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const typeDefs = gql`
theme: String!
personnelData: PersonnelData
guardianDirectory: [GuardianDirectory]
getLastModifiedAt: String
searchGuardianDirectory(search: String!): [GuardianDirectory]
}
Expand Down

0 comments on commit 540edb5

Please sign in to comment.