87 lines
3.0 KiB
TypeScript
87 lines
3.0 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class TenantService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async list(): Promise<any[]> {
|
|
const tenants = await this.prisma.tenant.findMany({
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
_count: { select: { groups: true, admins: true, messages: true, tenantBots: true } },
|
|
tenantBots: { include: { account: { select: { jid: true, displayName: true, status: true } } } },
|
|
},
|
|
});
|
|
return tenants.map((t) => ({
|
|
id: t.id,
|
|
slug: t.slug,
|
|
name: t.name,
|
|
isActive: t.isActive,
|
|
isForwardingPaused: t.isForwardingPaused,
|
|
createdAt: t.createdAt.toISOString(),
|
|
stats: {
|
|
groups: t._count.groups,
|
|
admins: t._count.admins,
|
|
messages: t._count.messages,
|
|
bots: t._count.tenantBots,
|
|
},
|
|
bot: t.tenantBots[0]
|
|
? { jid: t.tenantBots[0].account.jid, displayName: t.tenantBots[0].account.displayName, status: t.tenantBots[0].account.status }
|
|
: null,
|
|
}));
|
|
}
|
|
|
|
async get(id: string): Promise<any> {
|
|
const t = await this.prisma.tenant.findUnique({
|
|
where: { id },
|
|
include: {
|
|
_count: { select: { groups: true, admins: true, messages: true, rules: true, syncRoutes: true } },
|
|
tenantBots: { include: { account: { select: { id: true, jid: true, displayName: true, status: true, createdAt: true } } } },
|
|
admins: { select: { id: true, email: true, role: true, createdAt: true } },
|
|
},
|
|
});
|
|
if (!t) throw new NotFoundException('Tenant not found');
|
|
return {
|
|
id: t.id,
|
|
slug: t.slug,
|
|
name: t.name,
|
|
isActive: t.isActive,
|
|
isForwardingPaused: t.isForwardingPaused,
|
|
createdAt: t.createdAt.toISOString(),
|
|
stats: {
|
|
groups: t._count.groups,
|
|
admins: t._count.admins,
|
|
messages: t._count.messages,
|
|
rules: t._count.rules,
|
|
routes: t._count.syncRoutes,
|
|
},
|
|
bot: t.tenantBots[0]
|
|
? { id: t.tenantBots[0].account.id, jid: t.tenantBots[0].account.jid, displayName: t.tenantBots[0].account.displayName, status: t.tenantBots[0].account.status, linkedSince: t.tenantBots[0].createdAt.toISOString() }
|
|
: null,
|
|
admins: t.admins,
|
|
};
|
|
}
|
|
|
|
async update(id: string, data: { isActive?: boolean; isForwardingPaused?: boolean }): Promise<any> {
|
|
const t = await this.prisma.tenant.findUnique({ where: { id } });
|
|
if (!t) throw new NotFoundException('Tenant not found');
|
|
|
|
const updated = await this.prisma.tenant.update({
|
|
where: { id },
|
|
data: {
|
|
...(data.isActive !== undefined && { isActive: data.isActive }),
|
|
...(data.isForwardingPaused !== undefined && { isForwardingPaused: data.isForwardingPaused }),
|
|
},
|
|
});
|
|
|
|
return {
|
|
id: updated.id,
|
|
slug: updated.slug,
|
|
name: updated.name,
|
|
isActive: updated.isActive,
|
|
isForwardingPaused: updated.isForwardingPaused,
|
|
};
|
|
}
|
|
}
|