mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 13:11:17 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aff9464245 | |||
| 0db6cae82b | |||
| faad9e9736 | |||
| da2c1b9ee7 |
@@ -12,9 +12,12 @@
|
||||
"@aris/core": "workspace:*",
|
||||
"@aris/source-location": "workspace:*",
|
||||
"@aris/source-weatherkit": "workspace:*",
|
||||
"@hono/trpc-server": "^0.3",
|
||||
"@trpc/server": "^11",
|
||||
"better-auth": "^1",
|
||||
"hono": "^4",
|
||||
"pg": "^8"
|
||||
"pg": "^8",
|
||||
"zod": "^3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/pg": "^8"
|
||||
|
||||
8
apps/aris-backend/src/lib/error.ts
Normal file
8
apps/aris-backend/src/lib/error.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export class UserNotFoundError extends Error {
|
||||
constructor(
|
||||
public readonly userId: string,
|
||||
message?: string,
|
||||
) {
|
||||
super(message ? `${message}: user not found: ${userId}` : `User not found: ${userId}`)
|
||||
}
|
||||
}
|
||||
111
apps/aris-backend/src/location/service.test.ts
Normal file
111
apps/aris-backend/src/location/service.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
|
||||
import { UserNotFoundError } from "../lib/error.ts"
|
||||
import { LocationService } from "./service.ts"
|
||||
|
||||
describe("LocationService", () => {
|
||||
test("feedSourceForUser creates source on first call", () => {
|
||||
const service = new LocationService()
|
||||
const source = service.feedSourceForUser("user-1")
|
||||
|
||||
expect(source).toBeDefined()
|
||||
expect(source.id).toBe("location")
|
||||
})
|
||||
|
||||
test("feedSourceForUser returns same source for same user", () => {
|
||||
const service = new LocationService()
|
||||
const source1 = service.feedSourceForUser("user-1")
|
||||
const source2 = service.feedSourceForUser("user-1")
|
||||
|
||||
expect(source1).toBe(source2)
|
||||
})
|
||||
|
||||
test("feedSourceForUser returns different sources for different users", () => {
|
||||
const service = new LocationService()
|
||||
const source1 = service.feedSourceForUser("user-1")
|
||||
const source2 = service.feedSourceForUser("user-2")
|
||||
|
||||
expect(source1).not.toBe(source2)
|
||||
})
|
||||
|
||||
test("updateUserLocation updates the source", () => {
|
||||
const service = new LocationService()
|
||||
const source = service.feedSourceForUser("user-1")
|
||||
const location = {
|
||||
lat: 51.5074,
|
||||
lng: -0.1278,
|
||||
accuracy: 10,
|
||||
timestamp: new Date(),
|
||||
}
|
||||
|
||||
service.updateUserLocation("user-1", location)
|
||||
|
||||
expect(source.lastLocation).toEqual(location)
|
||||
})
|
||||
|
||||
test("updateUserLocation throws if source does not exist", () => {
|
||||
const service = new LocationService()
|
||||
const location = {
|
||||
lat: 51.5074,
|
||||
lng: -0.1278,
|
||||
accuracy: 10,
|
||||
timestamp: new Date(),
|
||||
}
|
||||
|
||||
expect(() => service.updateUserLocation("user-1", location)).toThrow(UserNotFoundError)
|
||||
})
|
||||
|
||||
test("lastUserLocation returns null for unknown user", () => {
|
||||
const service = new LocationService()
|
||||
|
||||
expect(service.lastUserLocation("unknown")).toBeNull()
|
||||
})
|
||||
|
||||
test("lastUserLocation returns last location", () => {
|
||||
const service = new LocationService()
|
||||
service.feedSourceForUser("user-1")
|
||||
const location1 = {
|
||||
lat: 51.5074,
|
||||
lng: -0.1278,
|
||||
accuracy: 10,
|
||||
timestamp: new Date(),
|
||||
}
|
||||
const location2 = {
|
||||
lat: 52.0,
|
||||
lng: -0.2,
|
||||
accuracy: 5,
|
||||
timestamp: new Date(),
|
||||
}
|
||||
|
||||
service.updateUserLocation("user-1", location1)
|
||||
service.updateUserLocation("user-1", location2)
|
||||
|
||||
expect(service.lastUserLocation("user-1")).toEqual(location2)
|
||||
})
|
||||
|
||||
test("removeUser removes the source", () => {
|
||||
const service = new LocationService()
|
||||
service.feedSourceForUser("user-1")
|
||||
const location = {
|
||||
lat: 51.5074,
|
||||
lng: -0.1278,
|
||||
accuracy: 10,
|
||||
timestamp: new Date(),
|
||||
}
|
||||
|
||||
service.updateUserLocation("user-1", location)
|
||||
service.removeUser("user-1")
|
||||
|
||||
expect(service.lastUserLocation("user-1")).toBeNull()
|
||||
})
|
||||
|
||||
test("removeUser allows new source to be created", () => {
|
||||
const service = new LocationService()
|
||||
const source1 = service.feedSourceForUser("user-1")
|
||||
|
||||
service.removeUser("user-1")
|
||||
const source2 = service.feedSourceForUser("user-1")
|
||||
|
||||
expect(source1).not.toBe(source2)
|
||||
})
|
||||
})
|
||||
55
apps/aris-backend/src/location/service.ts
Normal file
55
apps/aris-backend/src/location/service.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { LocationSource, type Location } from "@aris/source-location"
|
||||
|
||||
import { UserNotFoundError } from "../lib/error.ts"
|
||||
|
||||
/**
|
||||
* Manages LocationSource instances per user.
|
||||
*/
|
||||
export class LocationService {
|
||||
private sources = new Map<string, LocationSource>()
|
||||
|
||||
/**
|
||||
* Get or create a LocationSource for a user.
|
||||
* @param userId - The user's unique identifier
|
||||
* @returns The user's LocationSource instance
|
||||
*/
|
||||
feedSourceForUser(userId: string): LocationSource {
|
||||
let source = this.sources.get(userId)
|
||||
if (!source) {
|
||||
source = new LocationSource()
|
||||
this.sources.set(userId, source)
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
/**
|
||||
* Update location for a user.
|
||||
* @param userId - The user's unique identifier
|
||||
* @param location - The new location data
|
||||
* @throws {UserNotFoundError} If no source exists for the user
|
||||
*/
|
||||
updateUserLocation(userId: string, location: Location): void {
|
||||
const source = this.sources.get(userId)
|
||||
if (!source) {
|
||||
throw new UserNotFoundError(userId)
|
||||
}
|
||||
source.pushLocation(location)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last known location for a user.
|
||||
* @param userId - The user's unique identifier
|
||||
* @returns The last location, or null if none exists
|
||||
*/
|
||||
lastUserLocation(userId: string): Location | null {
|
||||
return this.sources.get(userId)?.lastLocation ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a user's LocationSource.
|
||||
* @param userId - The user's unique identifier
|
||||
*/
|
||||
removeUser(userId: string): void {
|
||||
this.sources.delete(userId)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
import { trpcServer } from "@hono/trpc-server"
|
||||
import { Hono } from "hono"
|
||||
|
||||
import { registerAuthHandlers } from "./auth/http.ts"
|
||||
import { createContext } from "./trpc/context.ts"
|
||||
import { appRouter } from "./trpc/router.ts"
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
@@ -8,6 +11,14 @@ app.get("/health", (c) => c.json({ status: "ok" }))
|
||||
|
||||
registerAuthHandlers(app)
|
||||
|
||||
app.use(
|
||||
"/trpc/*",
|
||||
trpcServer({
|
||||
router: appRouter,
|
||||
createContext,
|
||||
}),
|
||||
)
|
||||
|
||||
export default {
|
||||
port: 3000,
|
||||
fetch: app.fetch,
|
||||
|
||||
14
apps/aris-backend/src/trpc/context.ts
Normal file
14
apps/aris-backend/src/trpc/context.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"
|
||||
|
||||
import { auth } from "../auth/index.ts"
|
||||
|
||||
export async function createContext(opts: FetchCreateContextFnOptions) {
|
||||
const session = await auth.api.getSession({ headers: opts.req.headers })
|
||||
|
||||
return {
|
||||
user: session?.user ?? null,
|
||||
session: session?.session ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
export type Context = Awaited<ReturnType<typeof createContext>>
|
||||
5
apps/aris-backend/src/trpc/router.ts
Normal file
5
apps/aris-backend/src/trpc/router.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { router } from "./trpc.ts"
|
||||
|
||||
export const appRouter = router({})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
22
apps/aris-backend/src/trpc/trpc.ts
Normal file
22
apps/aris-backend/src/trpc/trpc.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { initTRPC, TRPCError } from "@trpc/server"
|
||||
|
||||
import type { Context } from "./context.ts"
|
||||
|
||||
const t = initTRPC.context<Context>().create()
|
||||
|
||||
export const router = t.router
|
||||
export const publicProcedure = t.procedure
|
||||
|
||||
const isAuthed = t.middleware(({ ctx, next }) => {
|
||||
if (!ctx.user || !ctx.session) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" })
|
||||
}
|
||||
return next({
|
||||
ctx: {
|
||||
user: ctx.user,
|
||||
session: ctx.session,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
export const protectedProcedure = t.procedure.use(isAuthed)
|
||||
15
bun.lock
15
bun.lock
@@ -20,9 +20,12 @@
|
||||
"@aris/core": "workspace:*",
|
||||
"@aris/source-location": "workspace:*",
|
||||
"@aris/source-weatherkit": "workspace:*",
|
||||
"@hono/trpc-server": "^0.3",
|
||||
"@trpc/server": "^11",
|
||||
"better-auth": "^1",
|
||||
"hono": "^4",
|
||||
"pg": "^8",
|
||||
"zod": "^3",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/pg": "^8",
|
||||
@@ -91,6 +94,8 @@
|
||||
|
||||
"@better-fetch/fetch": ["@better-fetch/fetch@1.1.21", "", {}, "sha512-/ImESw0sskqlVR94jB+5+Pxjf+xBwDZF/N5+y2/q4EqD7IARUTSpPfIo8uf39SYpCxyOCtbyYpUrZ3F/k0zT4A=="],
|
||||
|
||||
"@hono/trpc-server": ["@hono/trpc-server@0.3.4", "", { "peerDependencies": { "@trpc/server": "^10.10.0 || >11.0.0-rc", "hono": ">=4.*" } }, "sha512-xFOPjUPnII70FgicDzOJy1ufIoBTu8eF578zGiDOrYOrYN8CJe140s9buzuPkX+SwJRYK8LjEBHywqZtxdm8aA=="],
|
||||
|
||||
"@noble/ciphers": ["@noble/ciphers@2.1.1", "", {}, "sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw=="],
|
||||
|
||||
"@noble/hashes": ["@noble/hashes@2.0.1", "", {}, "sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw=="],
|
||||
@@ -129,6 +134,8 @@
|
||||
|
||||
"@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],
|
||||
|
||||
"@trpc/server": ["@trpc/server@11.8.1", "", { "peerDependencies": { "typescript": ">=5.7.2" } }, "sha512-P4rzZRpEL7zDFgjxK65IdyH0e41FMFfTkQkuq0BA5tKcr7E6v9/v38DEklCpoDN6sPiB1Sigy/PUEzHENhswDA=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
|
||||
|
||||
"@types/node": ["@types/node@25.0.9", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw=="],
|
||||
@@ -197,6 +204,12 @@
|
||||
|
||||
"xtend": ["xtend@4.0.2", "", {}, "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="],
|
||||
|
||||
"zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
|
||||
"zod": ["zod@3.25.76", "", {}, "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ=="],
|
||||
|
||||
"@better-auth/core/zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
|
||||
|
||||
"better-auth/zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
|
||||
|
||||
"better-call/zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user