-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fieldOpt * geo * misc
- Loading branch information
Showing
23 changed files
with
253 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 20 additions & 18 deletions
38
packages/api/router/fieldOpt/query.attributesByCategory.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
import flush from 'just-flush' | ||
import { type SetOptional } from 'type-fest' | ||
|
||
import { prisma } from '@weareinreach/db' | ||
import { type AttributesByCategory } from '@weareinreach/db/client' | ||
import { type FieldAttributes } from '@weareinreach/db/zod_util/attributeSupplement' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TAttributesByCategorySchema } from './query.attributesByCategory.schema' | ||
import { fieldAttributesSchema, type TAttributesByCategorySchema } from './query.attributesByCategory.schema' | ||
|
||
export const attributesByCategory = async ({ input }: TRPCHandlerParams<TAttributesByCategorySchema>) => { | ||
const where = Array.isArray(input) | ||
? { categoryName: { in: input } } | ||
: typeof input === 'string' | ||
? { categoryName: input } | ||
: undefined | ||
console.log(input) | ||
const result = await prisma.attributesByCategory.findMany({ | ||
where, | ||
where: { | ||
categoryName: Array.isArray(input?.categoryName) ? { in: input.categoryName } : input?.categoryName, | ||
canAttachTo: input?.canAttachTo?.length ? { hasSome: input.canAttachTo } : undefined, | ||
}, | ||
orderBy: [{ categoryName: 'asc' }, { attributeName: 'asc' }], | ||
}) | ||
|
||
const flushedResults = result.map((item) => | ||
flush<FlushedAttributesByCategory>(item) | ||
) as FlushedAttributesByCategory[] | ||
const flushedResults = result.map((item) => { | ||
const { dataSchema, ...rest } = item | ||
|
||
const parsedDataSchema = fieldAttributesSchema.safeParse(dataSchema) | ||
|
||
return { | ||
...rest, | ||
dataSchema: parsedDataSchema.success | ||
? (parsedDataSchema.data as FieldAttributes[] | FieldAttributes[][]) | ||
: null, | ||
} | ||
}) | ||
return flushedResults | ||
} | ||
type FlushedAttributesByCategory = SetOptional< | ||
AttributesByCategory, | ||
'interpolationValues' | 'icon' | 'iconBg' | 'badgeRender' | 'dataSchema' | 'dataSchemaName' | ||
> | ||
export default attributesByCategory |
53 changes: 50 additions & 3 deletions
53
packages/api/router/fieldOpt/query.attributesByCategory.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,55 @@ | ||
import { z } from 'zod' | ||
|
||
import { FieldType } from '@weareinreach/db/zod_util/attributeSupplement' | ||
|
||
export const ZAttributesByCategorySchema = z | ||
.string() | ||
.or(z.string().array()) | ||
.object({ | ||
categoryName: z.string().or(z.string().array()).optional().describe('categoryName'), | ||
canAttachTo: z.enum(['LOCATION', 'ORGANIZATION', 'SERVICE', 'USER']).array().optional(), | ||
}) | ||
.optional() | ||
.describe('categoryName') | ||
export type TAttributesByCategorySchema = z.infer<typeof ZAttributesByCategorySchema> | ||
|
||
const fieldTypeSchema = z.nativeEnum(FieldType) | ||
const baseFieldAttributesSchema = z.object({ | ||
key: z.string(), | ||
label: z.string(), | ||
name: z.string(), | ||
type: fieldTypeSchema, | ||
required: z.boolean().optional(), | ||
}) | ||
|
||
const textFieldAttributesSchema = baseFieldAttributesSchema.extend({ | ||
type: z.literal(FieldType.text), | ||
}) | ||
|
||
const selectFieldAttributesSchema = baseFieldAttributesSchema.extend({ | ||
type: z.literal(FieldType.select), | ||
options: z.array( | ||
z.object({ | ||
value: z.string(), | ||
label: z.string(), | ||
}) | ||
), | ||
}) | ||
|
||
const numberFieldAttributesSchema = baseFieldAttributesSchema.extend({ | ||
type: z.literal(FieldType.number), | ||
}) | ||
|
||
const currencyFieldAttributesSchema = baseFieldAttributesSchema.extend({ | ||
type: z.literal(FieldType.currency), | ||
}) | ||
|
||
const fieldAttributesObject = z | ||
.union([ | ||
textFieldAttributesSchema, | ||
selectFieldAttributesSchema, | ||
numberFieldAttributesSchema, | ||
currencyFieldAttributesSchema, | ||
]) | ||
.brand('FieldAttributes') | ||
.array() | ||
export const fieldAttributesSchema = fieldAttributesObject.or(fieldAttributesObject.array()) | ||
|
||
export type TFieldAttributesSchema = z.infer<typeof fieldAttributesSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ export const phoneTypes = async () => { | |
}) | ||
return result | ||
} | ||
export default phoneTypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ export const userTitle = async () => { | |
}) | ||
return results | ||
} | ||
export default userTitle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
import { defineRouter, publicProcedure } from '~api/lib/trpc' | ||
import { defineRouter, importHandler, publicProcedure } from '~api/lib/trpc' | ||
|
||
import * as schema from './schemas' | ||
|
||
const HandlerCache: Partial<GeoHandlerCache> = {} | ||
const NAMESPACE = 'geo' | ||
|
||
type GeoHandlerCache = { | ||
autocomplete: typeof import('./query.autocomplete.handler').autocomplete | ||
geoByPlaceId: typeof import('./query.geoByPlaceId.handler').geoByPlaceId | ||
} | ||
const namespaced = (s: string) => `${NAMESPACE}.${s}` | ||
|
||
export const geoRouter = defineRouter({ | ||
autocomplete: publicProcedure.input(schema.ZAutocompleteSchema).query(async ({ ctx, input }) => { | ||
if (!HandlerCache.autocomplete) | ||
HandlerCache.autocomplete = await import('./query.autocomplete.handler').then((mod) => mod.autocomplete) | ||
if (!HandlerCache.autocomplete) throw new Error('Failed to load handler') | ||
return HandlerCache.autocomplete({ ctx, input }) | ||
autocomplete: publicProcedure.input(schema.ZAutocompleteSchema).query(async (opts) => { | ||
const handler = await importHandler( | ||
namespaced('autocomplete'), | ||
() => import('./query.autocomplete.handler') | ||
) | ||
return handler(opts) | ||
}), | ||
geoByPlaceId: publicProcedure.input(schema.ZGeoByPlaceIdSchema).query(async ({ ctx, input }) => { | ||
if (!HandlerCache.geoByPlaceId) | ||
HandlerCache.geoByPlaceId = await import('./query.geoByPlaceId.handler').then((mod) => mod.geoByPlaceId) | ||
if (!HandlerCache.geoByPlaceId) throw new Error('Failed to load handler') | ||
return HandlerCache.geoByPlaceId({ ctx, input }) | ||
geoByPlaceId: publicProcedure.input(schema.ZGeoByPlaceIdSchema).query(async (opts) => { | ||
const handler = await importHandler( | ||
namespaced('geoByPlaceId'), | ||
() => import('./query.geoByPlaceId.handler') | ||
) | ||
return handler(opts) | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.