136 lines
4.1 KiB
TypeScript
136 lines
4.1 KiB
TypeScript
import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
|
|
import {
|
|
interfaceCatalogStateSchema,
|
|
type InterfaceCatalogState,
|
|
appUserCreateSchema,
|
|
appUserIdParamSchema,
|
|
appUserUpdateSchema,
|
|
bindingIdParamSchema,
|
|
interfaceCatalogQuerySchema,
|
|
userBindingCreateSchema,
|
|
type AppUserCreate,
|
|
type AppUserUpdate,
|
|
type UserBindingCreate,
|
|
} from "@mmapp/contracts/users"
|
|
import {
|
|
addBinding,
|
|
createUser,
|
|
deleteUser,
|
|
getUserById,
|
|
listInterfaceCatalog,
|
|
getInterfaceCatalogState,
|
|
listUsers,
|
|
removeBinding,
|
|
updateUser,
|
|
UsersServiceError,
|
|
} from "../modules/users/service/users-service.js"
|
|
|
|
function sendServiceError(reply: { status: (c: number) => { send: (b: unknown) => unknown } }, err: unknown) {
|
|
if (err instanceof UsersServiceError) {
|
|
return reply.status(err.status).send({ error: err.message })
|
|
}
|
|
throw err
|
|
}
|
|
|
|
export function createUsersRoutes(deps: { getCatalogState?: (serverId: number) => Promise<InterfaceCatalogState> } = {}): FastifyPluginAsyncZod {
|
|
return async (app) => {
|
|
app.get("/users", async (_req, reply) => {
|
|
return reply.send({ users: await listUsers() })
|
|
})
|
|
|
|
app.get("/users/interface-catalog", {
|
|
schema: { querystring: interfaceCatalogQuerySchema },
|
|
}, async (req, reply) => {
|
|
const q = req.query as { serverId: number }
|
|
try {
|
|
return reply.send({ interfaces: await listInterfaceCatalog(q.serverId) })
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
|
|
app.get("/users/interface-catalog-state", {
|
|
schema: { querystring: interfaceCatalogQuerySchema },
|
|
}, async (req, reply) => {
|
|
const { serverId } = req.query as { serverId: number }
|
|
try {
|
|
const raw = await (deps.getCatalogState ?? getInterfaceCatalogState)(serverId)
|
|
const parsed = interfaceCatalogStateSchema.safeParse(raw)
|
|
if (!parsed.success || parsed.data.serverId !== serverId) return reply.status(502).send({ error: "Не подтверждён контекст каталога интерфейсов." })
|
|
return reply.send(parsed.data)
|
|
} catch (error) { return sendServiceError(reply, error) }
|
|
})
|
|
|
|
app.get("/users/:id", {
|
|
schema: { params: appUserIdParamSchema },
|
|
}, async (req, reply) => {
|
|
const { id } = req.params as { id: string }
|
|
const user = await getUserById(id)
|
|
if (!user) return reply.status(404).send({ error: "Пользователь не найден" })
|
|
return reply.send({ user })
|
|
})
|
|
|
|
app.post("/users", {
|
|
schema: { body: appUserCreateSchema },
|
|
}, async (req, reply) => {
|
|
try {
|
|
const user = await createUser(req.body as AppUserCreate)
|
|
return reply.status(201).send({ user })
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
|
|
app.patch("/users/:id", {
|
|
schema: { params: appUserIdParamSchema, body: appUserUpdateSchema },
|
|
}, async (req, reply) => {
|
|
const { id } = req.params as { id: string }
|
|
try {
|
|
const user = await updateUser(id, req.body as AppUserUpdate)
|
|
return reply.send({ user })
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
|
|
app.delete("/users/:id", {
|
|
schema: { params: appUserIdParamSchema },
|
|
}, async (req, reply) => {
|
|
const { id } = req.params as { id: string }
|
|
try {
|
|
await deleteUser(id)
|
|
return reply.status(204).send()
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
|
|
app.post("/users/:id/bindings", {
|
|
schema: { params: appUserIdParamSchema, body: userBindingCreateSchema },
|
|
}, async (req, reply) => {
|
|
const { id } = req.params as { id: string }
|
|
try {
|
|
const binding = await addBinding(id, req.body as UserBindingCreate)
|
|
return reply.status(201).send({ binding })
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
|
|
app.delete("/users/:id/bindings/:bindingId", {
|
|
schema: { params: bindingIdParamSchema },
|
|
}, async (req, reply) => {
|
|
const { id, bindingId } = req.params as { id: string; bindingId: string }
|
|
try {
|
|
await removeBinding(id, bindingId)
|
|
return reply.status(204).send()
|
|
} catch (err) {
|
|
return sendServiceError(reply, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default createUsersRoutes()
|