Skip to content

Commit

Permalink
sonarlint issues - api
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Apr 24, 2024
1 parent 8678210 commit 427d210
Show file tree
Hide file tree
Showing 79 changed files with 400 additions and 347 deletions.
8 changes: 5 additions & 3 deletions packages/api/router/attribute/query.map.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { prisma } from '@weareinreach/db'
import { handleError } from '~api/lib/errorHandler'
import { type TRPCHandlerParams } from '~api/types/handler'

export const map = async ({ ctx: _ }: TRPCHandlerParams<undefined>) => {
export const map = async ({ ctx: _ }: TRPCHandlerParams) => {
try {
const result = await prisma.attribute.findMany({
where: {
Expand All @@ -25,7 +25,9 @@ export const map = async ({ ctx: _ }: TRPCHandlerParams<undefined>) => {
requireText: true,
},
})
if (!result) return null
if (!result) {
return null
}
const byId = new Map<string, MapById>(
result.map(({ id, ...rest }) => [id, filterObj(rest, (_key, value) => value !== null) as MapById])
)
Expand All @@ -34,7 +36,7 @@ export const map = async ({ ctx: _ }: TRPCHandlerParams<undefined>) => {
)
return { byId, byTag }
} catch (error) {
handleError(error)
return handleError(error)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ export const EditModeBarDelete = async ({
})
return result
}
default: {
throw new Error('Invalid input')
}
}
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default EditModeBarDelete
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ export const EditModeBarPublish = async ({
})
return result
}
default: {
throw new Error('Invalid input')
}
}
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default EditModeBarPublish
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const EditModeBarReverify = async ({
})
return reverify
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default EditModeBarReverify
7 changes: 5 additions & 2 deletions packages/api/router/component/query.EditModeBar.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type TRPCHandlerParams } from '~api/types/handler'

import { type TEditModeBarSchema } from './query.EditModeBar.schema'

export const EditModeBar = async ({ ctx, input }: TRPCHandlerParams<TEditModeBarSchema>) => {
export const EditModeBar = async ({ input }: TRPCHandlerParams<TEditModeBarSchema>) => {
try {
const { orgLocationId, orgServiceId, slug } = input

Expand All @@ -30,9 +30,12 @@ export const EditModeBar = async ({ ctx, input }: TRPCHandlerParams<TEditModeBar
})
return { ...result, lastVerified: null }
}
default: {
throw new Error('Invalid input')
}
}
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default EditModeBar
4 changes: 2 additions & 2 deletions packages/api/router/component/query.ServiceSelect.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { prisma } from '@weareinreach/db'
import { handleError } from '~api/lib/errorHandler'
import { type TRPCHandlerParams } from '~api/types/handler'

export const ServiceSelect = async ({ ctx }: TRPCHandlerParams<undefined>) => {
export const ServiceSelect = async ({ ctx: _ctx }: TRPCHandlerParams) => {
try {
const result = await prisma.serviceCategory.findMany({
where: {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const ServiceSelect = async ({ ctx }: TRPCHandlerParams<undefined>) => {
}))
return transformed
} catch (error) {
handleError(error)
return handleError(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/api/router/fieldOpt/query.ccaMap.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ccaMap = async ({ input }: TRPCHandlerParams<TCcaMapSchema>) => {

return { byId, byCCA }
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default ccaMap
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getSubDistricts = async ({ input }: TRPCHandlerParams<TGetSubDistri
})
return results
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default getSubDistricts
2 changes: 1 addition & 1 deletion packages/api/router/fieldOpt/query.govDists.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const govDists = async ({ input }: TRPCHandlerParams<TGovDistsSchema>) =>
})
return results
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default govDists
2 changes: 1 addition & 1 deletion packages/api/router/fieldOpt/query.orgBadges.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const orgBadges = async ({ input }: TRPCHandlerParams<TOrgBadgesSchema>)
})
return badges
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default orgBadges
35 changes: 19 additions & 16 deletions packages/api/router/geo/query.autocomplete.handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* eslint-disable node/no-process-env */
import {
type PlaceAutocompleteRequest,
type PlaceAutocompleteType,
} from '@googlemaps/google-maps-services-js'
import { type PlaceAutocompleteRequest, PlaceAutocompleteType } from '@googlemaps/google-maps-services-js'

import { googleMapsApi } from '~api/google'
import { googleAPIResponseHandler } from '~api/lib/googleHandler'
Expand All @@ -11,26 +8,32 @@ import { type TRPCHandlerParams } from '~api/types/handler'

import { type TAutocompleteSchema } from './query.autocomplete.schema'

const getTypes = (input: TAutocompleteSchema): PlaceAutocompleteType => {
if (input.cityOnly) {
return [PlaceAutocompleteType.cities] as unknown as PlaceAutocompleteType
}
if (input.fullAddress) {
return [PlaceAutocompleteType.address] as unknown as PlaceAutocompleteType
}
return [
'administrative_area_level_2',
'administrative_area_level_3',
'neighborhood',
'locality',
'postal_code',
] as unknown as PlaceAutocompleteType
}

export const autocomplete = async ({ input }: TRPCHandlerParams<TAutocompleteSchema>) => {
const types = input.cityOnly
? ['(cities)']
: input.fullAddress
? ['address']
: ([
'administrative_area_level_2',
'administrative_area_level_3',
'neighborhood',
'locality',
'postal_code',
] as unknown as PlaceAutocompleteType)
const types = getTypes(input)

const { data } = await googleMapsApi.placeAutocomplete({
params: {
key: process.env.GOOGLE_PLACES_API_KEY as string,
input: input.search,
language: input.locale,
types,
locationbias: 'ipbias',
types,
},
} as PlaceAutocompleteRequest)
const parsedData = autocompleteResponse.parse(data)
Expand Down
4 changes: 3 additions & 1 deletion packages/api/router/location/query.forGoogleMaps.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const forGoogleMaps = async ({ input }: TRPCHandlerParams<TForGoogleMapsS
const coordsForBounds: { latitude: number; longitude: number }[] = []

for (const { latitude, longitude } of result) {
if (latitude && longitude) coordsForBounds.push({ latitude, longitude })
if (latitude && longitude) {
coordsForBounds.push({ latitude, longitude })
}
}
const bounds = result.length > 1 ? getBoundary(coordsForBounds) : null
const singleLat = result.at(0)?.latitude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export const forLocationPage = async ({ input }: TRPCHandlerParams<TForLocationP
longitude: true,
latitude: true,
description: globalSelect.freeText(),
attributes,
notVisitable: true,
reviews: {
where: { visible: true, deleted: false },
select: { id: true },
},
attributes,
},
})
return location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const forLocationPageEdits = async ({ input }: TRPCHandlerParams<TForLoca

return formatted
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default forLocationPageEdits
4 changes: 3 additions & 1 deletion packages/api/router/location/query.forVisitCard.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const forVisitCard = async ({ input }: TRPCHandlerParams<TForVisitCardSch
longitude: true,
},
})
if (!result) return null
if (!result) {
return null
}
const { attributes, ...rest } = result
const transformed = {
...rest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { prisma } from '@weareinreach/db'
import { handleError } from '~api/lib/errorHandler'
import { globalWhere } from '~api/selects/global'
import { type TRPCHandlerParams } from '~api/types/handler'

import { type TForVisitCardEditsSchema } from './query.forVisitCardEdits.schema'
Expand Down Expand Up @@ -28,7 +27,9 @@ export const forVisitCardEdits = async ({ input }: TRPCHandlerParams<TForVisitCa
longitude: true,
},
})
if (!result) return null
if (!result) {
return null
}
const { attributes, ...rest } = result
const transformed = {
...rest,
Expand Down
6 changes: 4 additions & 2 deletions packages/api/router/location/query.getAlerts.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export const getAlerts = async ({ input }: TRPCHandlerParams<TGetAlertsSchema>)

const reformatted = compact(
dbResult.flatMap(({ text, attribute }) => {
if (!text?.tsKey) return
if (!text?.tsKey) {
return null
}
return { ...text.tsKey, ...attribute }
})
)
return reformatted
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default getAlerts
4 changes: 3 additions & 1 deletion packages/api/router/location/query.getByOrgId.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const getByOrgId = async ({ ctx, input }: TRPCHandlerParams<TGetByOrgIdSc
id: true,
},
})
if (locations.length === 0) throw new TRPCError({ code: 'NOT_FOUND' })
if (locations.length === 0) {
throw new TRPCError({ code: 'NOT_FOUND' })
}
return locations
}
export default getByOrgId
8 changes: 4 additions & 4 deletions packages/api/router/misc/query.forEditNavbar.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isIdFor, prisma } from '@weareinreach/db'
import { prisma } from '@weareinreach/db'
import { handleError } from '~api/lib/errorHandler'
import { type TRPCHandlerParams } from '~api/types/handler'

Expand All @@ -7,19 +7,19 @@ import { type TForEditNavbarSchema } from './query.forEditNavbar.schema'
export const forEditNavbar = async ({ input }: TRPCHandlerParams<TForEditNavbarSchema>) => {
try {
if (input.slug) {
const result = await prisma.organization.findUniqueOrThrow({
const slugResult = await prisma.organization.findUniqueOrThrow({
where: { slug: input.slug },
select: { published: true, deleted: true, lastVerified: true },
})
return result
return slugResult
}
const result = await prisma.orgLocation.findUniqueOrThrow({
where: { id: input.orgLocationId },
select: { published: true, deleted: true },
})
return { ...result, lastVerified: null }
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default forEditNavbar
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getCountryTranslation = async ({ input }: TRPCHandlerParams<TGetCou
})
return result
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default getCountryTranslation
2 changes: 1 addition & 1 deletion packages/api/router/misc/query.hasContactInfo.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const hasContactInfo = async ({ input }: TRPCHandlerParams<THasContactInf
])
return email + phone + socialMedia + website !== 0 // ? 'true' : 'false'
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default hasContactInfo
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export const locationLink = async ({ ctx, input }: TRPCHandlerParams<TLocationLi
})
return result
}
default: {
throw new Error('Invalid action')
}
}
} catch (error) {
handleError(error)
return handleError(error)
}
}
export default locationLink
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const upsertMany = async ({ ctx, input }: TRPCHandlerParams<TUpsertManySc
const upserts = await prisma.$transaction(
data.map(
({ title, services: servicesArr, locations: locationsArr, description, id: passedId, ...record }) => {
const before = passedId ? existing.find(({ id }) => id === passedId) : undefined
const before = passedId ? existing.find(({ id: existingId }) => existingId === passedId) : undefined
const servicesBefore = before?.services?.map(({ serviceId }) => ({ serviceId })) ?? []
const locationsBefore = before?.locations?.map(({ orgLocationId }) => ({ orgLocationId })) ?? []
const id = passedId ?? ctx.generateId('orgEmail')
Expand Down
19 changes: 2 additions & 17 deletions packages/api/router/orgEmail/query.forContactInfo.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ import { type TRPCHandlerParams } from '~api/types/handler'
import { type TForContactInfoSchema } from './query.forContactInfo.schema'

const isPublic = globalWhere.isPublic()
const whereId = (input: TForContactInfoSchema, isSingleLoc?: boolean): Prisma.OrgEmailWhereInput => {
const whereId = (input: TForContactInfoSchema): Prisma.OrgEmailWhereInput => {
switch (true) {
case isIdFor('organization', input.parentId): {
return { organization: { some: { organization: { id: input.parentId, ...isPublic } } } }
// return isSingleLoc
// ? {
// OR: [
// { organization: { some: { organization: { id: input.parentId, ...isPublic } } } },
// { locations: { some: { location: { organization: { id: input.parentId, ...isPublic } } } } },
// ],
// }
// : { organization: { some: { organization: { id: input.parentId, ...isPublic } } } }
}
case isIdFor('orgLocation', input.parentId): {
return { locations: { some: { location: { id: input.parentId, ...isPublic } } } }
Expand All @@ -31,17 +23,10 @@ const whereId = (input: TForContactInfoSchema, isSingleLoc?: boolean): Prisma.Or
}

export const forContactInfo = async ({ input }: TRPCHandlerParams<TForContactInfoSchema>) => {
const locCount = isIdFor('organization', input.parentId)
? await prisma.orgLocation.count({
where: { organization: { id: input.parentId, ...isPublic }, ...isPublic },
})
: 0
const isSingleLoc = locCount === 1

const result = await prisma.orgEmail.findMany({
where: {
...isPublic,
...whereId(input, isSingleLoc),
...whereId(input),
...(input.locationOnly !== undefined ? { locationOnly: input.locationOnly } : {}),
...(input.serviceOnly !== undefined ? { serviceOnly: input.serviceOnly } : {}),
},
Expand Down
Loading

0 comments on commit 427d210

Please sign in to comment.