Initial commit
This commit is contained in:
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