Initial commit
This commit is contained in:
37
apps/backend/src/modules/complex/complex.routes.ts
Normal file
37
apps/backend/src/modules/complex/complex.routes.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { createComplexSchema, updateComplexSchema } from '@repo/api-contract'
|
||||
import { Hono } from 'hono'
|
||||
import { z } from 'zod'
|
||||
import { requireAuth } from '@/middlewares/require-auth.middleware'
|
||||
import { createComplexHandler } from '@/modules/complex/handlers/create-complex.handler'
|
||||
import { getComplexByIdHandler } from '@/modules/complex/handlers/get-complex-by-id.handler'
|
||||
import { getComplexBySlugHandler } from '@/modules/complex/handlers/get-complex-by-slug.handler'
|
||||
import { listMyComplexesHandler } from '@/modules/complex/handlers/list-my-complexes.handler'
|
||||
import { updateComplexHandler } from '@/modules/complex/handlers/update-complex.handler'
|
||||
import type { AppEnv } from '@/types/hono'
|
||||
|
||||
export const complexRoutes = new Hono<AppEnv>()
|
||||
const complexIdParamsSchema = z.object({ id: z.uuid() })
|
||||
const complexSlugParamsSchema = z.object({ slug: z.string().trim().min(1) })
|
||||
|
||||
complexRoutes.use('*', requireAuth)
|
||||
|
||||
complexRoutes.post('/', zValidator('json', createComplexSchema), createComplexHandler)
|
||||
complexRoutes.get('/mine', listMyComplexesHandler)
|
||||
complexRoutes.get(
|
||||
'/slug/:slug',
|
||||
zValidator('param', complexSlugParamsSchema),
|
||||
getComplexBySlugHandler,
|
||||
)
|
||||
|
||||
complexRoutes.get(
|
||||
'/:id',
|
||||
zValidator('param', complexIdParamsSchema),
|
||||
getComplexByIdHandler,
|
||||
)
|
||||
complexRoutes.patch(
|
||||
'/:id',
|
||||
zValidator('param', complexIdParamsSchema),
|
||||
zValidator('json', updateComplexSchema),
|
||||
updateComplexHandler,
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { CreateComplexInput } from '@repo/api-contract'
|
||||
import type { AppContext } from '@/types/hono'
|
||||
import { createComplex } from '@/modules/complex/services/complex.service'
|
||||
|
||||
export async function createComplexHandler(c: AppContext) {
|
||||
const payload = c.req.valid('json' as never) as CreateComplexInput
|
||||
|
||||
const authUser = c.get('authUser')
|
||||
const adminEmail = authUser.email
|
||||
|
||||
if (!adminEmail) {
|
||||
return c.json({ message: 'El usuario autenticado no tiene email.' }, 400)
|
||||
}
|
||||
|
||||
const complex = await createComplex({
|
||||
complexName: payload.complexName,
|
||||
physicalAddress: payload.physicalAddress,
|
||||
adminEmail,
|
||||
planCode: payload.planCode,
|
||||
})
|
||||
|
||||
return c.json(complex, 201)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { AppContext } from '@/types/hono'
|
||||
import { getComplexById } from '@/modules/complex/services/complex.service'
|
||||
|
||||
type ComplexIdParams = { id: string }
|
||||
|
||||
export async function getComplexByIdHandler(c: AppContext) {
|
||||
const { id } = c.req.valid('param' as never) as ComplexIdParams
|
||||
|
||||
const complex = await getComplexById(id)
|
||||
|
||||
if (!complex) {
|
||||
return c.json({ message: 'Complejo no encontrado.' }, 404)
|
||||
}
|
||||
|
||||
return c.json(complex)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { AppContext } from '@/types/hono'
|
||||
import { getComplexBySlug } from '@/modules/complex/services/complex.service'
|
||||
|
||||
type ComplexSlugParams = { slug: string }
|
||||
|
||||
export async function getComplexBySlugHandler(c: AppContext) {
|
||||
const { slug } = c.req.valid('param' as never) as ComplexSlugParams
|
||||
|
||||
const complex = await getComplexBySlug(slug)
|
||||
|
||||
if (!complex) {
|
||||
return c.json({ message: 'Complejo no encontrado.' }, 404)
|
||||
}
|
||||
|
||||
return c.json(complex)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import type { AppContext } from '@/types/hono'
|
||||
import { listMyComplexes } from '@/modules/complex/services/complex.service'
|
||||
|
||||
export async function listMyComplexesHandler(c: AppContext) {
|
||||
const appUserId = c.get('appUserId')
|
||||
const complexes = await listMyComplexes(appUserId)
|
||||
return c.json(complexes)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { UpdateComplexInput } from '@repo/api-contract'
|
||||
import { Prisma } from '@/generated/prisma/client'
|
||||
import type { AppContext } from '@/types/hono'
|
||||
import { getComplexById, updateComplex } from '@/modules/complex/services/complex.service'
|
||||
|
||||
type ComplexIdParams = { id: string }
|
||||
|
||||
export async function updateComplexHandler(c: AppContext) {
|
||||
const { id } = c.req.valid('param' as never) as ComplexIdParams
|
||||
const payload = c.req.valid('json' as never) as UpdateComplexInput
|
||||
|
||||
const existing = await getComplexById(id)
|
||||
|
||||
if (!existing) {
|
||||
return c.json({ message: 'Complejo no encontrado.' }, 404)
|
||||
}
|
||||
|
||||
try {
|
||||
const complex = await updateComplex(id, payload)
|
||||
return c.json(complex)
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
return c.json({ message: 'No se pudo actualizar el complejo.' }, 409)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
132
apps/backend/src/modules/complex/services/complex.service.ts
Normal file
132
apps/backend/src/modules/complex/services/complex.service.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { v7 as uuidv7 } from 'uuid'
|
||||
import { Prisma } from '@/generated/prisma/client'
|
||||
import { db } from '@/lib/prisma'
|
||||
|
||||
export type CreateComplexInput = {
|
||||
complexName: string
|
||||
physicalAddress: string
|
||||
adminEmail: string
|
||||
planCode?: string
|
||||
}
|
||||
|
||||
export type UpdateComplexInput = {
|
||||
complexName?: string
|
||||
physicalAddress?: string | null
|
||||
complexSlug?: string
|
||||
adminEmail?: string
|
||||
planCode?: string | null
|
||||
}
|
||||
|
||||
function slugify(value: string): string {
|
||||
return value
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
}
|
||||
|
||||
async function buildUniqueSlug(
|
||||
source: string,
|
||||
excludeComplexId?: string,
|
||||
): Promise<string> {
|
||||
const base = slugify(source)
|
||||
const fallback = base.length > 0 ? base : `complex-${uuidv7().slice(0, 8)}`
|
||||
|
||||
let candidate = fallback
|
||||
let index = 1
|
||||
|
||||
while (true) {
|
||||
const existing = await db.complex.findFirst({
|
||||
where: {
|
||||
complexSlug: candidate,
|
||||
...(excludeComplexId
|
||||
? {
|
||||
id: {
|
||||
not: excludeComplexId,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (!existing) return candidate
|
||||
|
||||
index += 1
|
||||
candidate = `${fallback}-${index}`
|
||||
}
|
||||
}
|
||||
|
||||
export async function createComplex(input: CreateComplexInput) {
|
||||
const complexSlug = await buildUniqueSlug(input.complexName)
|
||||
|
||||
return db.complex.create({
|
||||
data: {
|
||||
id: uuidv7(),
|
||||
complexName: input.complexName,
|
||||
physicalAddress: input.physicalAddress.trim(),
|
||||
complexSlug,
|
||||
adminEmail: input.adminEmail,
|
||||
planCode: input.planCode,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getComplexById(id: string) {
|
||||
return db.complex.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export async function getComplexBySlug(slug: string) {
|
||||
return db.complex.findUnique({
|
||||
where: { complexSlug: slug },
|
||||
})
|
||||
}
|
||||
|
||||
export async function listMyComplexes(appUserId: string) {
|
||||
const complexUsers = await db.complexUser.findMany({
|
||||
where: { userId: appUserId },
|
||||
include: {
|
||||
complex: true,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
},
|
||||
})
|
||||
|
||||
return complexUsers.map((complexUser) => complexUser.complex)
|
||||
}
|
||||
|
||||
export async function updateComplex(id: string, input: UpdateComplexInput) {
|
||||
const data: Record<string, unknown> = {}
|
||||
|
||||
if (input.complexName) {
|
||||
data.complexName = input.complexName
|
||||
data.complexSlug = await buildUniqueSlug(input.complexName, id)
|
||||
}
|
||||
|
||||
if (input.complexSlug) {
|
||||
data.complexSlug = await buildUniqueSlug(input.complexSlug, id)
|
||||
}
|
||||
|
||||
if (input.adminEmail) {
|
||||
data.adminEmail = input.adminEmail
|
||||
}
|
||||
|
||||
if (input.planCode !== undefined) {
|
||||
data.planCode = input.planCode
|
||||
}
|
||||
|
||||
if (input.physicalAddress !== undefined) {
|
||||
data.physicalAddress = input.physicalAddress?.trim() ?? null
|
||||
}
|
||||
|
||||
return db.complex.update({
|
||||
where: { id },
|
||||
data: data as Prisma.ComplexUncheckedUpdateInput,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user