mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 13:11:17 +00:00
feat(backend): add location router with tRPC factory pattern
- Add createLocationRouter with location.update mutation - Refactor tRPC to factory pattern (createTRPC, createTRPCRouter) - Protected procedure by default (all routes require auth) - Replace zod with arktype for input validation - Wire location router in main() with dependency injection Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
33
apps/aris-backend/src/location/router.ts
Normal file
33
apps/aris-backend/src/location/router.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { TRPCError } from "@trpc/server"
|
||||
import { type } from "arktype"
|
||||
|
||||
import { UserNotFoundError } from "../lib/error.ts"
|
||||
import type { TRPC } from "../trpc/router.ts"
|
||||
import type { LocationService } from "./service.ts"
|
||||
|
||||
const locationInput = type({
|
||||
lat: "number",
|
||||
lng: "number",
|
||||
accuracy: "number",
|
||||
timestamp: "Date",
|
||||
})
|
||||
|
||||
export function createLocationRouter(t: TRPC, { locationService }: { locationService: LocationService }) {
|
||||
return t.router({
|
||||
update: t.procedure.input(locationInput).mutation(({ input, ctx }) => {
|
||||
try {
|
||||
locationService.updateUserLocation(ctx.user.id, {
|
||||
lat: input.lat,
|
||||
lng: input.lng,
|
||||
accuracy: input.accuracy,
|
||||
timestamp: input.timestamp,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof UserNotFoundError) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: error.message })
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user