brf/server/routes/api/roles.ts

154 lines
3.8 KiB
TypeScript

import _ from 'lodash'
import * as z from 'zod'
import { type FastifyPluginCallbackZod } from 'fastify-type-provider-zod'
// import knex from '../../lib/knex.ts'
// import Queries from '../../services/roles/queries.ts'
import { RoleSchema } from '../../schemas/db.ts'
// export const RoleFullSchema = Type.Object({
// id: Type.Number(),
// name: Type.String(),
// createdAt: Type.String(),
// createdById: Type.Number(),
// modifiedAt: Type.String(),
// modifiedById: Type.Number(),
// })
// console.log(RoleFullSchema)
// export const RoleSchema = Type.Pick(RoleFullSchema, ['id', 'name'])
// export const RoleVariableSchema = Type.Pick(RoleFullSchema, ['name', 'createdById'])
const rolesPlugin: FastifyPluginCallbackZod = (fastify, _options, done) => {
// const queries = Queries({ knex })
const { db } = fastify
// fastify.addHook('onRequest', fastify.auth)
fastify.route({
url: '/',
method: 'GET',
schema: {
querystring: RoleSchema.partial().extend({
limit: z.number().optional(),
sort: z.keyof(RoleSchema).optional(),
offset: z.number().optional(),
}),
response: {
200: z.array(RoleSchema),
},
},
handler(request) {
// if (!client) {
// // TODO figure out if better, eg instanceof, check is possible
// if (select && select.andWhereNotBetween) {
// client = select
// select = null
// } else {
// client = kysely
// }
// }
const { offset, limit, sort, ...query } = request.query
let builder = db.selectFrom('role').selectAll()
// if (!_.isEmpty(query)) {
// builder = where(builder, _.pick(query, columns))
// }
for (const [key, value] of Object.entries(query)) {
if (value === null) {
builder = builder.where(key, 'is', null)
} else if (Array.isArray(value)) {
builder = builder.where(key, 'in', value)
} else {
builder = builder.where(key, '=', value)
}
}
builder = builder.orderBy(sort || 'id', sort?.startsWith('-') ? 'desc' : 'asc')
if (limit) {
builder = builder.limit(limit)
}
if (offset) {
builder = builder.offset(offset)
}
return builder.execute()
},
})
// fastify.route({
// url: '/',
// method: 'POST',
// schema: {
// body: RoleVariableSchema,
// response: {
// 201: RoleFullSchema,
// },
// },
// async handler(request, reply) {
// const newRole = request.session.userId
// ? {
// ...request.body,
// createdById: request.session.userId,
// }
// : request.body
// return queries.create(newRole).then((row) => {
// return reply.header('Location', `${request.url}/${row.id}`).status(201).send(row)
// })
// },
// })
// fastify.route({
// url: '/:id',
// method: 'DELETE',
// schema: {
// params: Type.Object({
// id: Type.Number(),
// }),
// response: {
// 204: {},
// 404: {},
// },
// },
// handler(request, reply) {
// return queries.removeById(request.params.id).then((count) => reply.status(count > 0 ? 204 : 404).send())
// },
// })
// fastify.route({
// url: '/:id',
// method: 'PATCH',
// schema: {
// params: Type.Object({
// id: Type.Number(),
// }),
// body: RoleVariableSchema,
// response: {
// 204: {},
// },
// },
// async handler(request) {
// const patch = request.session.userId
// ? {
// ...request.body,
// modifiedById: request.session.userId,
// }
// : request.body
// return queries.update(request.params.id, patch)
// },
// })
done()
}
export default rolesPlugin