-
-
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.
- Loading branch information
Showing
41 changed files
with
894 additions
and
9,173 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
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { z } from 'zod' | ||
|
||
export const ZCountriesSchema = z | ||
.object({ | ||
where: z.object({ activeForOrgs: z.boolean(), cca2: z.string() }).partial(), | ||
// includeGeo: z.object({ wkt: z.boolean(), json: z.boolean() }), | ||
}) | ||
.object({ activeForOrgs: z.boolean(), cca2: z.string() }) | ||
.partial() | ||
.optional() | ||
.transform((val) => (val ? { where: val } : undefined)) | ||
export type TCountriesSchema = z.infer<typeof ZCountriesSchema> |
32 changes: 32 additions & 0 deletions
32
packages/api/router/fieldOpt/query.getSubDistricts.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { prisma } from '@weareinreach/db' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TGetSubDistrictsSchema } from './query.getSubDistricts.schema' | ||
|
||
export const getSubDistricts = async ({ input }: TRPCHandlerParams<TGetSubDistrictsSchema>) => { | ||
try { | ||
const results = await prisma.govDist.findMany({ | ||
where: { | ||
parentId: input, | ||
}, | ||
select: { | ||
id: true, | ||
tsKey: true, | ||
tsNs: true, | ||
abbrev: true, | ||
country: { select: { cca2: true } }, | ||
parent: { select: { tsKey: true, tsNs: true } }, | ||
govDistType: { | ||
select: { tsKey: true, tsNs: true }, | ||
}, | ||
}, | ||
orderBy: { | ||
name: 'asc', | ||
}, | ||
}) | ||
return results | ||
} catch (error) { | ||
handleError(error) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { type z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZGetSubDistrictsSchema = prefixedId('govDist') | ||
export type TGetSubDistrictsSchema = z.infer<typeof ZGetSubDistrictsSchema> |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { prisma } from '@weareinreach/db' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TGovDistsSchema } from './query.govDists.schema' | ||
|
||
export const govDists = async ({ input }: TRPCHandlerParams<TGovDistsSchema>) => { | ||
try { | ||
const results = await prisma.govDist.findMany({ | ||
where: input, | ||
select: { | ||
id: true, | ||
tsKey: true, | ||
tsNs: true, | ||
abbrev: true, | ||
country: { select: { cca2: true } }, | ||
govDistType: { select: { tsKey: true, tsNs: true } }, | ||
}, | ||
}) | ||
return results | ||
} catch (error) { | ||
handleError(error) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZGovDistsSchema = z | ||
.object({ | ||
id: prefixedId('govDist'), | ||
name: z.string(), | ||
slug: z.string(), | ||
iso: z.string(), | ||
abbrev: z.string(), | ||
countryId: prefixedId('country'), | ||
parentsOnly: z.boolean().default(true), | ||
}) | ||
.partial() | ||
.optional() | ||
.transform((val) => { | ||
if (!val) return val | ||
const { parentsOnly, ...rest } = val | ||
return { ...rest, ...(parentsOnly ? { parentId: null } : {}) } | ||
}) | ||
export type TGovDistsSchema = z.infer<typeof ZGovDistsSchema> |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defineRouter, publicProcedure } from '~api/lib/trpc' | ||
|
||
import * as schema from './schemas' | ||
|
||
export const HandlerCache: Partial<HandlerCache> = {} | ||
export const serviceAreaRouter = defineRouter({ | ||
getServiceArea: publicProcedure.input(schema.ZGetServiceAreaSchema).query(async ({ ctx, input }) => { | ||
if (!HandlerCache.getServiceArea) { | ||
HandlerCache.getServiceArea = await import('./query.getServiceArea.handler').then( | ||
(mod) => mod.getServiceArea | ||
) | ||
} | ||
if (!HandlerCache.getServiceArea) throw new Error('Failed to load handler') | ||
return HandlerCache.getServiceArea({ ctx, input }) | ||
}), | ||
update: publicProcedure.input(schema.ZUpdateSchema).mutation(async ({ ctx, input }) => { | ||
if (!HandlerCache.update) { | ||
HandlerCache.update = await import('./mutation.update.handler').then((mod) => mod.update) | ||
} | ||
if (!HandlerCache.update) throw new Error('Failed to load handler') | ||
return HandlerCache.update({ ctx, input }) | ||
}), | ||
}) | ||
|
||
type HandlerCache = { | ||
getServiceArea: typeof import('./query.getServiceArea.handler').getServiceArea | ||
update: typeof import('./mutation.update.handler').update | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/api/router/serviceArea/mutation.update.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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { prisma } from '@weareinreach/db' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TUpdateSchema } from './mutation.update.schema' | ||
|
||
export const update = async ({ input }: TRPCHandlerParams<TUpdateSchema>) => { | ||
try { | ||
const { id: serviceAreaId, districts, countries } = input | ||
|
||
const txn = await prisma.$transaction(async (tx) => { | ||
const results = { | ||
countries: { | ||
created: 0, | ||
deleted: 0, | ||
}, | ||
districts: { | ||
created: 0, | ||
deleted: 0, | ||
}, | ||
} | ||
if (districts.createdVals) { | ||
const { count } = await tx.serviceAreaDist.createMany({ | ||
data: districts.createdVals.map((govDistId) => ({ serviceAreaId, govDistId })), | ||
skipDuplicates: true, | ||
}) | ||
results.districts.created = count | ||
} | ||
if (districts.deletedVals) { | ||
const { count } = await tx.serviceAreaDist.deleteMany({ | ||
where: { serviceAreaId, govDistId: { in: districts.deletedVals } }, | ||
}) | ||
results.districts.deleted = count | ||
} | ||
if (countries.createdVals) { | ||
const { count } = await tx.serviceAreaCountry.createMany({ | ||
data: countries.createdVals.map((countryId) => ({ serviceAreaId, countryId })), | ||
skipDuplicates: true, | ||
}) | ||
results.countries.created = count | ||
} | ||
if (countries.deletedVals) { | ||
const { count } = await tx.serviceAreaCountry.deleteMany({ | ||
where: { serviceAreaId, countryId: { in: countries.deletedVals } }, | ||
}) | ||
results.countries.deleted = count | ||
} | ||
return results | ||
}) | ||
return txn | ||
} catch (error) { | ||
handleError(error) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZUpdateSchema = z.object({ | ||
id: prefixedId('serviceArea'), | ||
districts: z.object({ | ||
createdVals: prefixedId('govDist').array().nullable(), | ||
deletedVals: prefixedId('govDist').array().nullable(), | ||
}), | ||
countries: z.object({ | ||
createdVals: prefixedId('country').array().nullable(), | ||
deletedVals: prefixedId('country').array().nullable(), | ||
}), | ||
}) | ||
export type TUpdateSchema = z.infer<typeof ZUpdateSchema> |
50 changes: 50 additions & 0 deletions
50
packages/api/router/serviceArea/query.getServiceArea.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { prisma } from '@weareinreach/db' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TGetServiceAreaSchema } from './query.getServiceArea.schema' | ||
|
||
export const getServiceArea = async ({ ctx, input }: TRPCHandlerParams<TGetServiceAreaSchema>) => { | ||
try { | ||
const result = await prisma.serviceArea.findUnique({ | ||
where: { | ||
id: input, | ||
}, | ||
select: { | ||
id: true, | ||
countries: { | ||
select: { | ||
country: { | ||
select: { cca2: true, id: true }, | ||
}, | ||
}, | ||
}, | ||
districts: { | ||
select: { | ||
govDist: { | ||
select: { | ||
id: true, | ||
parent: { select: { tsKey: true, tsNs: true } }, | ||
country: { select: { cca2: true } }, | ||
tsKey: true, | ||
tsNs: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!result) return result | ||
|
||
const { id, districts, countries } = result | ||
const formatted = { | ||
id, | ||
districts: districts.map(({ govDist }) => govDist), | ||
countries: countries.map(({ country }) => country), | ||
} | ||
return formatted | ||
} catch (error) { | ||
handleError(error) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/api/router/serviceArea/query.getServiceArea.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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { type z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZGetServiceAreaSchema = prefixedId('serviceArea') | ||
export type TGetServiceAreaSchema = z.infer<typeof ZGetServiceAreaSchema> |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// codegen:start {preset: barrel, include: ./*.schema.ts} | ||
export * from './mutation.update.schema' | ||
export * from './query.getServiceArea.schema' | ||
// codegen:end |
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
Oops, something went wrong.