Skip to content

Commit

Permalink
Merge pull request #33 from Bostads-AB-Mimer/feat/1542-generalize-api…
Browse files Browse the repository at this point in the history
…-responses

feat 1542: generalises api responses and adds metadata
  • Loading branch information
johanneskarlsson authored Aug 27, 2024
2 parents ee2b0b5 + 31ac0eb commit ba2086f
Show file tree
Hide file tree
Showing 10 changed files with 1,640 additions and 168 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,738 changes: 1,598 additions & 140 deletions packages/backend/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@types/jest": "29.5.1",
"@types/jsonwebtoken": "^9.0.5",
"@types/koa": "2.13.6",
"@types/koa": "2.15.0",
"@types/koa__cors": "^5.0.0",
"@types/koa__router": "12.0.0",
"@types/koa-bodyparser": "4.3.10",
Expand Down Expand Up @@ -49,6 +49,8 @@
"koa-body": "^6.0.1",
"koa-jwt": "^4.0.4",
"koa-pino-logger": "^4.0.0",
"onecore-types": "^1.25.1",
"onecore-utilities": "^1.1.0",
"qs": "^6.11.2"
}
}
30 changes: 21 additions & 9 deletions packages/backend/src/services/lease-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { getRentsForLease } from './adapters/rent-adapter'
import fs from 'fs/promises'
import { MaterialChoice } from './types'
import { generateRouteMetadata } from 'onecore-utilities'

const getAccommodation = async (nationalRegistrationNumber: string) => {
const lease = await getLease(nationalRegistrationNumber)
Expand All @@ -36,60 +37,70 @@ export const routes = (router: KoaRouter) => {
* sub objects such as rental property, other tenants and rent.
*/
router.get('(.*)/my-lease', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const nationalRegistrationNumber = ctx.state.user.username

const lease = await getAccommodation(nationalRegistrationNumber)

ctx.body = {
data: lease,
content: lease,
...metadata,
}
})

router.get('(.*)/my-details', async (ctx: any) => {
const metadata = generateRouteMetadata(ctx)
const nationalRegistrationNumber = ctx.state.user.username
const contact = await getContact(nationalRegistrationNumber)

ctx.body = { data: contact }
ctx.body = { content: contact, ...metadata }
})

router.get('(.*)/material-options', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const materialOptions = await getMaterialOptions(
ctx.state.user.rentalPropertyId
)

ctx.body = {
data: materialOptions,
content: materialOptions,
...metadata,
}
})

router.get('(.*)/material-option-details', async (ctx) => {
const metadata = generateRouteMetadata(ctx, ['materialOptionId'])
if (ctx.request.query.materialOptionId) {
const option = await getMaterialOption(
ctx.state.user.rentalPropertyId,
ctx.request.query.materialOptionId.toString()
)

ctx.body = {
data: option,
content: option,
...metadata,
}
}
})

router.get('(.*)/material-options/assets/:id', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const filename = ctx.params.id
const path = process.cwd() + '/assets/' + filename
const data = await fs.readFile(path)

ctx.body = data
ctx.body = { content: data, ...metadata }
})

router.get('(.*)/material-choices', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const roomTypes = await getMaterialChoices(ctx.state.user.rentalPropertyId)

ctx.body = { data: roomTypes }
ctx.body = { content: roomTypes, ...metadata }
})

router.post('(.*)/material-choices', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
if (ctx.request.body) {
const choices: Array<MaterialChoice> = ctx.request.body?.choices.map(
(choice: MaterialChoice) => {
Expand All @@ -102,20 +113,21 @@ export const routes = (router: KoaRouter) => {
)
await saveMaterialChoice(ctx.state.user.rentalPropertyId, choices)

ctx.body = { message: 'Save successful' }
ctx.body = { message: 'Save successful', ...metadata }
} else {
ctx.body = { message: 'No choices proviced' }
ctx.body = { message: 'No choices proviced', ...metadata }
}
})

/**
* Streams the floor plan of the logged in user as an image binary
*/
router.get('(.*)/my-lease/floorplan', async (ctx) => {
const metadata = generateRouteMetadata(ctx)
const response = await getFloorPlanStream(ctx.state.user.rentalPropertyId)

ctx.type = response.headers['content-type']?.toString() ?? 'image/jpeg'
ctx.headers['cache-control'] = 'public, max-age=600'
ctx.body = response.data
ctx.body = { content: response.data, ...metadata }
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('lease-service index', () => {

expect(res.status).toBe(200)
expect(materialChoicesSpy).toHaveBeenCalled()
expect(res.body.data.roomTypes).toBeDefined()
expect(res.body.content.roomTypes).toBeDefined()
})
})
})
8 changes: 4 additions & 4 deletions packages/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@mui/material": "^5.15.6",
"axios": "^1.6.5",
"material-ui-popup-state": "^5.0.10",
"onecore-types": "^1.0.9",
"onecore-types": "^1.25.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-query": "^3.39.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/pages/Details/hooks/useContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useContact = () => {
return useQuery<ContactResponse, AxiosError>({
queryKey: ['my-details'],
queryFn: async () => {
const { data } = await axios.get<ContactResponse>(
const { data } = await axios.get<{ content: ContactResponse }>(
`${backendUrl}/my-details`,
{
headers: {
Expand All @@ -22,7 +22,7 @@ export const useContact = () => {
withCredentials: true,
}
)
return data
return data.content
},
retry: (failureCount: number, error: AxiosError) => {
if (error.response?.status === 401) {
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/pages/Lease/hooks/useLease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useLease = () => {
return useQuery<LeaseResponse, AxiosError>({
queryKey: ['lease'],
queryFn: async () => {
const { data } = await axios.get<LeaseResponse>(
const { data } = await axios.get<{ content: LeaseResponse }>(
`${backendUrl}/my-lease`,
{
headers: {
Expand All @@ -23,7 +23,7 @@ export const useLease = () => {
}
)

return data
return data.content
},
retry: (failureCount: number, error: AxiosError) => {
if (error.response?.status === 401) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useMaterialOptions = ({
queryKey: ['apartmentId', apartmentId],
queryFn: async () => {
if (apartmentId) {
const { data } = await axios.get<RoomTypesResponse>(
const { data } = await axios.get<{ content: RoomTypesResponse }>(
`${backendUrl}/material-options`,
{
headers: {
Expand All @@ -36,7 +36,7 @@ export const useMaterialOptions = ({
},
}
)
return data
return data.content
} else {
return {
data: {
Expand All @@ -58,7 +58,7 @@ export const useMaterialChoices = () =>
useQuery<RoomTypesResponse, AxiosError>({
queryKey: ['materialChoices'],
queryFn: async () => {
const { data } = await axios.get<RoomTypesResponse>(
const { data } = await axios.get<{ content: RoomTypesResponse }>(
`${backendUrl}/material-choices`,
{
headers: {
Expand All @@ -67,7 +67,7 @@ export const useMaterialChoices = () =>
},
}
)
return data
return data.content
},
retry: (failureCount: number, error: AxiosError) => {
if (error.response?.status === 401) {
Expand Down Expand Up @@ -114,7 +114,7 @@ export const useMaterialOptionDetails = ({
queryKey: ['materialOptionDetails', materialOptionId],
queryFn: async () => {
if (materialOptionId) {
const { data } = await axios.get<MaterialOptionResponse>(
const { data } = await axios.get<{ content: MaterialOptionResponse }>(
`${backendUrl}/material-option-details`,
{
headers: {
Expand All @@ -126,7 +126,7 @@ export const useMaterialOptionDetails = ({
},
}
)
return data
return data.content
} else {
return {
data: undefined,
Expand Down

0 comments on commit ba2086f

Please sign in to comment.