2026-01-25 22:47:53 +00:00
|
|
|
import { TRPCError } from "@trpc/server"
|
|
|
|
|
import { type } from "arktype"
|
|
|
|
|
|
|
|
|
|
import type { TRPC } from "../trpc/router.ts"
|
|
|
|
|
import type { LocationService } from "./service.ts"
|
|
|
|
|
|
2026-02-13 19:16:33 +00:00
|
|
|
import { UserNotFoundError } from "../lib/error.ts"
|
|
|
|
|
|
2026-01-25 22:47:53 +00:00
|
|
|
const locationInput = type({
|
|
|
|
|
lat: "number",
|
|
|
|
|
lng: "number",
|
|
|
|
|
accuracy: "number",
|
|
|
|
|
timestamp: "Date",
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-13 19:16:33 +00:00
|
|
|
export function createLocationRouter(
|
|
|
|
|
t: TRPC,
|
|
|
|
|
{ locationService }: { locationService: LocationService },
|
|
|
|
|
) {
|
2026-01-25 22:47:53 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
}
|