Skip to content

Commit

Permalink
alter api routes & update mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Dec 20, 2023
1 parent e67a951 commit 1649045
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/api/router/fieldOpt/query.getSubDistricts.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const getSubDistricts = async ({ input }: TRPCHandlerParams<TGetSubDistri
tsKey: true,
tsNs: true,
abbrev: true,
country: { select: { cca2: true } },
parent: { select: { tsKey: true, tsNs: true } },
govDistType: {
select: { tsKey: true, tsNs: true },
},
Expand Down
2 changes: 2 additions & 0 deletions packages/api/router/fieldOpt/query.govDists.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const govDists = async ({ input }: TRPCHandlerParams<TGovDistsSchema>) =>
tsKey: true,
tsNs: true,
abbrev: true,
country: { select: { cca2: true } },
govDistType: { select: { tsKey: true, tsNs: true } },
},
})
return results
Expand Down
35 changes: 16 additions & 19 deletions packages/ui/mockData/fieldOpt.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { z } from 'zod'

// import fs from 'fs'
// import path from 'path'
import { type ApiInput, type ApiOutput } from '@weareinreach/api'
import { getTRPCMock } from '~ui/lib/getTrpcMock'
import { type ApiOutput } from '@weareinreach/api'
import { getTRPCMock, type MockAPIHandler } from '~ui/lib/getTrpcMock'

import countryGovDistMapData from './json/countryGovDistMap.json'

const queryAttributeCategories = async (query: ApiInput['fieldOpt']['attributeCategories']) => {
const queryAttributeCategories: MockAPIHandler<'fieldOpt', 'attributeCategories'> = async (query) => {
const attributeCategories = (await import('./json/fieldOpt.attributeCategories.json')).default
if (Array.isArray(query)) {
return attributeCategories.filter(({ tag }) => query.includes(tag))
}
return attributeCategories
}

const queryAttributesByCategory = async (query: ApiInput['fieldOpt']['attributesByCategory']) => {
const queryAttributesByCategory: MockAPIHandler<'fieldOpt', 'attributesByCategory'> = async (query) => {
const attributesByCategory = (await import('./json/fieldOpt.attributesByCategory.json')).default
if (typeof query === 'string' || Array.isArray(query)) {
return attributesByCategory.filter(({ categoryName }) =>
Expand All @@ -25,7 +21,7 @@ const queryAttributesByCategory = async (query: ApiInput['fieldOpt']['attributes
return attributesByCategory as ApiOutput['fieldOpt']['attributesByCategory']
}

const queryLanguages = async (query: ApiInput['fieldOpt']['languages']) => {
const queryLanguages: MockAPIHandler<'fieldOpt', 'languages'> = async (query) => {
const languages = (await import('./json/fieldOpt.languages.json')).default
const { localeCode, activelyTranslated } = query ?? {}
if (localeCode || activelyTranslated) {
Expand All @@ -46,7 +42,7 @@ const queryLanguages = async (query: ApiInput['fieldOpt']['languages']) => {
return languages
}

const queryCountries = async (query: ApiInput['fieldOpt']['countries']) => {
const queryCountries: MockAPIHandler<'fieldOpt', 'countries'> = async (query) => {
const countries = (await import('./json/fieldOpt.countries.json')).default
if (query) {
const { activeForOrgs, cca2 } = query
Expand All @@ -59,13 +55,13 @@ const queryCountries = async (query: ApiInput['fieldOpt']['countries']) => {
return countries
}

const queryGovDistsByCountry = async (query: ApiInput['fieldOpt']['govDistsByCountry']) => {
const queryGovDistsByCountry: MockAPIHandler<'fieldOpt', 'govDistsByCountry'> = async (query) => {
const govDistsByCountry = (await import('./json/fieldOpt.govDistsByCountry.json')).default
if (query) return govDistsByCountry.filter(({ cca2 }) => cca2 === query)
return govDistsByCountry
}

const queryGovDistsByCountryNoSub = async (query: ApiInput['fieldOpt']['govDistsByCountryNoSub']) => {
const queryGovDistsByCountryNoSub: MockAPIHandler<'fieldOpt', 'govDistsByCountryNoSub'> = async (query) => {
const govDistByCountryNoSub = (await import('./json/fieldOpt.govDistsByCountryNoSub.json')).default
if (query) return govDistByCountryNoSub.filter(({ cca2 }) => cca2 === query)
return govDistByCountryNoSub
Expand Down Expand Up @@ -129,10 +125,11 @@ const countryGovDistMapSchema = z
])
.array()

const countryGovDistMap = new Map<string, CountryGovDistMapItem>(
countryGovDistMapSchema.parse(countryGovDistMapData)
)
const getSubDistricts = async (id: string) => {
const countryGovDistMap = async () => {
const countryGovDistMapData = (await import('./json/fieldOpt.countryGovDistMap.json')).default
return new Map<string, CountryGovDistMapItem>(countryGovDistMapSchema.parse(countryGovDistMapData))
}
const getSubDistricts: MockAPIHandler<'fieldOpt', 'getSubDistricts'> = async (id) => {
const data = (await import('./json/fieldOpt.getSubDistricts.json')).default
const filtered = data.filter(({ parentId }) => parentId === id)
const formatted = filtered.map(({ parentId, ...data }) => data)
Expand Down Expand Up @@ -173,7 +170,7 @@ export const fieldOpt = {
}),
countryGovDistMap: getTRPCMock({
path: ['fieldOpt', 'countryGovDistMap'],
response: countryGovDistMap,
response: async () => await countryGovDistMap(),
}),
getSubDistricts: getTRPCMock({
path: ['fieldOpt', 'getSubDistricts'],
Expand All @@ -195,10 +192,10 @@ export const fieldOpt = {
}
return isMatch
})
const formatted = filtered.map(({ name, slug, iso, countryId, parentId, ...data }) => data)
const formatted = filtered.map(({ parentId, ...data }) => data)
return formatted
}
const formatted = data.map(({ name, slug, iso, countryId, parentId, ...data }) => data)
const formatted = data.map(({ parentId, ...data }) => data)
return formatted
},
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/mockData/json/fieldOpt.getSubDistricts.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ui/mockData/json/fieldOpt.govDists.json

Large diffs are not rendered by default.

0 comments on commit 1649045

Please sign in to comment.