Initial SuperAdmin API (NestJS + Prisma)
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma, ProductType, Tenant } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
import { CreateTenantDto } from './dto/create-tenant.dto';
|
||||
import { UpdateTenantDto } from './dto/update-tenant.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TenantsService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly audit: AuditService,
|
||||
) {}
|
||||
|
||||
async findAll(product?: ProductType) {
|
||||
return this.prisma.tenant.findMany({
|
||||
where: product ? { product } : undefined,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
product: true,
|
||||
slug: true,
|
||||
nameFa: true,
|
||||
nameEn: true,
|
||||
domain: true,
|
||||
avatarUrl: true,
|
||||
primaryColor: true,
|
||||
secondaryColor: true,
|
||||
backgroundColor: true,
|
||||
nodeServer: true,
|
||||
nodeWs: true,
|
||||
appUrl: true,
|
||||
adminUrl: true,
|
||||
isActive: true,
|
||||
notes: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const tenant = await this.prisma.tenant.findUnique({ where: { id } });
|
||||
if (!tenant) {
|
||||
throw new NotFoundException('نسخه یافت نشد');
|
||||
}
|
||||
return this.sanitize(tenant);
|
||||
}
|
||||
|
||||
async create(dto: CreateTenantDto, userId?: string) {
|
||||
try {
|
||||
const tenant = await this.prisma.tenant.create({
|
||||
data: {
|
||||
product: dto.product,
|
||||
slug: dto.slug.toLowerCase(),
|
||||
nameFa: dto.nameFa,
|
||||
nameEn: dto.nameEn,
|
||||
domain: this.normalizeDomain(dto.domain),
|
||||
avatarUrl: dto.avatarUrl,
|
||||
primaryColor: dto.primaryColor,
|
||||
secondaryColor: dto.secondaryColor,
|
||||
backgroundColor: dto.backgroundColor,
|
||||
nodeServer: dto.nodeServer,
|
||||
nodeWs: dto.nodeWs,
|
||||
appUrl: dto.appUrl,
|
||||
adminUrl: dto.adminUrl,
|
||||
remoteApiToken: dto.remoteApiToken,
|
||||
appUiConfig: dto.appUiConfig
|
||||
? (dto.appUiConfig as Prisma.InputJsonValue)
|
||||
: undefined,
|
||||
isActive: dto.isActive ?? true,
|
||||
notes: dto.notes,
|
||||
},
|
||||
});
|
||||
|
||||
await this.audit.log({
|
||||
action: 'tenant.create',
|
||||
entityType: 'tenant',
|
||||
entityId: tenant.id,
|
||||
tenantId: tenant.id,
|
||||
userId,
|
||||
after: this.sanitize(tenant) as unknown as Prisma.InputJsonValue,
|
||||
});
|
||||
|
||||
return this.sanitize(tenant);
|
||||
} catch (error) {
|
||||
this.handleUnique(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateTenantDto, userId?: string) {
|
||||
const before = await this.prisma.tenant.findUnique({ where: { id } });
|
||||
if (!before) {
|
||||
throw new NotFoundException('نسخه یافت نشد');
|
||||
}
|
||||
|
||||
try {
|
||||
const tenant = await this.prisma.tenant.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(dto.product !== undefined && { product: dto.product }),
|
||||
...(dto.slug !== undefined && { slug: dto.slug.toLowerCase() }),
|
||||
...(dto.nameFa !== undefined && { nameFa: dto.nameFa }),
|
||||
...(dto.nameEn !== undefined && { nameEn: dto.nameEn }),
|
||||
...(dto.domain !== undefined && {
|
||||
domain: this.normalizeDomain(dto.domain),
|
||||
}),
|
||||
...(dto.avatarUrl !== undefined && { avatarUrl: dto.avatarUrl }),
|
||||
...(dto.primaryColor !== undefined && {
|
||||
primaryColor: dto.primaryColor,
|
||||
}),
|
||||
...(dto.secondaryColor !== undefined && {
|
||||
secondaryColor: dto.secondaryColor,
|
||||
}),
|
||||
...(dto.backgroundColor !== undefined && {
|
||||
backgroundColor: dto.backgroundColor,
|
||||
}),
|
||||
...(dto.nodeServer !== undefined && { nodeServer: dto.nodeServer }),
|
||||
...(dto.nodeWs !== undefined && { nodeWs: dto.nodeWs }),
|
||||
...(dto.appUrl !== undefined && { appUrl: dto.appUrl }),
|
||||
...(dto.adminUrl !== undefined && { adminUrl: dto.adminUrl }),
|
||||
...(dto.remoteApiToken !== undefined && {
|
||||
remoteApiToken: dto.remoteApiToken,
|
||||
}),
|
||||
...(dto.appUiConfig !== undefined && {
|
||||
appUiConfig: dto.appUiConfig as Prisma.InputJsonValue,
|
||||
}),
|
||||
...(dto.isActive !== undefined && { isActive: dto.isActive }),
|
||||
...(dto.notes !== undefined && { notes: dto.notes }),
|
||||
},
|
||||
});
|
||||
|
||||
await this.audit.log({
|
||||
action: 'tenant.update',
|
||||
entityType: 'tenant',
|
||||
entityId: tenant.id,
|
||||
tenantId: tenant.id,
|
||||
userId,
|
||||
before: this.sanitize(before) as unknown as Prisma.InputJsonValue,
|
||||
after: this.sanitize(tenant) as unknown as Prisma.InputJsonValue,
|
||||
});
|
||||
|
||||
return this.sanitize(tenant);
|
||||
} catch (error) {
|
||||
this.handleUnique(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async remove(id: string, userId?: string) {
|
||||
const before = await this.prisma.tenant.findUnique({ where: { id } });
|
||||
if (!before) {
|
||||
throw new NotFoundException('نسخه یافت نشد');
|
||||
}
|
||||
|
||||
await this.prisma.tenant.delete({ where: { id } });
|
||||
|
||||
await this.audit.log({
|
||||
action: 'tenant.delete',
|
||||
entityType: 'tenant',
|
||||
entityId: id,
|
||||
tenantId: id,
|
||||
userId,
|
||||
before: this.sanitize(before) as unknown as Prisma.InputJsonValue,
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
private sanitize(tenant: Tenant) {
|
||||
const { remoteApiToken, ...rest } = tenant;
|
||||
return {
|
||||
...rest,
|
||||
hasRemoteApiToken: Boolean(remoteApiToken),
|
||||
};
|
||||
}
|
||||
|
||||
private normalizeDomain(domain: string) {
|
||||
return domain
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/^https?:\/\//, '')
|
||||
.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
private handleUnique(error: unknown) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2002'
|
||||
) {
|
||||
throw new ConflictException('slug یا domain تکراری است');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user