城市合伙人端的修改(后台)
This commit is contained in:
@@ -1,114 +1,182 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PartnerStaffRole } from '@dukang/shared-types';
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
import { CreatePartnerStaffDto, UpdatePartnerStaffDto } from './dto/partner-staff.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PartnerStaffService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async listStaff(parentAccountId: bigint) {
|
||||
const rows = await this.prisma.partnerAccount.findMany({
|
||||
where: { parentAccountId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
return rows.map((row) => this.toStaffItem(row));
|
||||
}
|
||||
|
||||
async createStaff(parentAccountId: bigint, dto: CreatePartnerStaffDto) {
|
||||
const parent = await this.prisma.partnerAccount.findUniqueOrThrow({
|
||||
where: { id: parentAccountId },
|
||||
});
|
||||
if (parent.isPrimary !== 1) {
|
||||
throw new BadRequestException('仅主账号可添加子账号');
|
||||
}
|
||||
|
||||
const phone = dto.phone.trim();
|
||||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||||
throw new BadRequestException('请输入正确的手机号码');
|
||||
}
|
||||
|
||||
const existing = await this.prisma.partnerAccount.findUnique({ where: { phone } });
|
||||
if (existing) throw new BadRequestException('该手机号已被使用');
|
||||
|
||||
const name = dto.name.trim();
|
||||
if (!name) throw new BadRequestException('请填写真实姓名');
|
||||
|
||||
const account = await this.prisma.partnerAccount.create({
|
||||
data: {
|
||||
partnerId: parent.partnerId,
|
||||
phone,
|
||||
name,
|
||||
staffRole: (dto.staffRole as PartnerStaffRole | undefined) ?? PartnerStaffRole.INTERNAL,
|
||||
isPrimary: 0,
|
||||
parentAccountId: parent.id,
|
||||
status: 'DISABLED',
|
||||
},
|
||||
});
|
||||
|
||||
return this.toStaffItem(account);
|
||||
}
|
||||
|
||||
async updateStaff(parentAccountId: bigint, staffId: bigint, dto: UpdatePartnerStaffDto) {
|
||||
const staff = await this.assertStaffOwned(parentAccountId, staffId);
|
||||
const data: Record<string, unknown> = {};
|
||||
if (dto.name !== undefined) {
|
||||
const name = dto.name.trim();
|
||||
if (!name) throw new BadRequestException('请填写真实姓名');
|
||||
data.name = name;
|
||||
}
|
||||
if (dto.staffRole !== undefined) {
|
||||
data.staffRole = dto.staffRole as PartnerStaffRole;
|
||||
}
|
||||
if (dto.status !== undefined) {
|
||||
data.status = dto.status;
|
||||
}
|
||||
const updated = await this.prisma.partnerAccount.update({
|
||||
where: { id: staff.id },
|
||||
data,
|
||||
});
|
||||
return this.toStaffItem(updated);
|
||||
}
|
||||
|
||||
async deleteStaff(parentAccountId: bigint, staffId: bigint) {
|
||||
const staff = await this.assertStaffOwned(parentAccountId, staffId);
|
||||
await this.prisma.partnerAccount.delete({ where: { id: staff.id } });
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
private async assertStaffOwned(parentAccountId: bigint, staffId: bigint) {
|
||||
const staff = await this.prisma.partnerAccount.findFirst({
|
||||
where: { id: staffId, parentAccountId },
|
||||
});
|
||||
if (!staff) throw new NotFoundException('子账号不存在');
|
||||
return staff;
|
||||
}
|
||||
|
||||
private toStaffItem(row: {
|
||||
id: bigint;
|
||||
name: string;
|
||||
phone: string;
|
||||
staffRole: string | null;
|
||||
status: string;
|
||||
lastLoginAt: Date | null;
|
||||
}) {
|
||||
return serializeBigInt({
|
||||
id: row.id.toString(),
|
||||
name: row.name,
|
||||
phone: this.maskPhone(row.phone),
|
||||
staffRole: row.staffRole ?? PartnerStaffRole.INTERNAL,
|
||||
status: row.status,
|
||||
lastLoginAt: row.lastLoginAt?.toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
private maskPhone(phone: string): string {
|
||||
if (phone.length !== 11) return phone;
|
||||
return `${phone.slice(0, 3)} **** ${phone.slice(7)}`;
|
||||
}
|
||||
}
|
||||
import {
|
||||
|
||||
BadRequestException,
|
||||
|
||||
Injectable,
|
||||
|
||||
NotFoundException,
|
||||
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { PartnerStaffRole } from '@dukang/shared-types';
|
||||
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
|
||||
import type { AuthUser } from '../../common/guards/jwt-auth.guard';
|
||||
|
||||
import { AnalyticsService } from '../analytics/analytics.service';
|
||||
|
||||
import { CreatePartnerStaffDto, UpdatePartnerStaffDto } from './dto/partner-staff.dto';
|
||||
|
||||
|
||||
|
||||
@Injectable()
|
||||
|
||||
export class PartnerStaffService {
|
||||
|
||||
constructor(
|
||||
|
||||
private readonly prisma: PrismaService,
|
||||
|
||||
private readonly analytics: AnalyticsService,
|
||||
|
||||
) {}
|
||||
|
||||
|
||||
|
||||
async listStaff(parentAccountId: bigint) {
|
||||
|
||||
const rows = await this.prisma.partnerAccount.findMany({
|
||||
|
||||
where: { parentAccountId },
|
||||
|
||||
orderBy: { createdAt: 'desc' },
|
||||
|
||||
});
|
||||
|
||||
return rows.map((row) => this.toStaffItem(row));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async createStaff(actor: AuthUser, dto: CreatePartnerStaffDto) {
|
||||
|
||||
const parentAccountId = actor.actorId;
|
||||
|
||||
const parent = await this.prisma.partnerAccount.findUniqueOrThrow({
|
||||
|
||||
where: { id: parentAccountId },
|
||||
|
||||
});
|
||||
|
||||
if (parent.isPrimary !== 1) {
|
||||
|
||||
throw new BadRequestException('仅主账号可添加子账号');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const phone = dto.phone.trim();
|
||||
|
||||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||||
|
||||
throw new BadRequestException('请输入正确的手机号码');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
const existing = await this.prisma.partnerAccount.findUnique({ where: { phone } });
|
||||
|
||||
if (existing) throw new BadRequestException('该手机号已被使用');
|
||||
|
||||
|
||||
|
||||
const name = dto.name.trim();
|
||||
|
||||
if (!name) throw new BadRequestException('请填写真实姓名');
|
||||
|
||||
|
||||
|
||||
const staffRole = (dto.staffRole as PartnerStaffRole | undefined) ?? PartnerStaffRole.INTERNAL;
|
||||
|
||||
|
||||
|
||||
const account = await this.prisma.partnerAccount.create({
|
||||
|
||||
data: {
|
||||
|
||||
phone,
|
||||
|
||||
name,
|
||||
|
||||
staffRole,
|
||||
|
||||
permissions: dto.permissions ?? undefined,
|
||||
|
||||
isPrimary: 0,
|
||||
|
||||
parentAccountId: parent.id,
|
||||
|
||||
status: 'DISABLED',
|
||||
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
this.trackStaffEvent(actor, parent.id, 'partner_staff_create', account.id, {
|
||||
|
||||
name,
|
||||
|
||||
phone: this.maskPhone(phone),
|
||||
|
||||
staffRole,
|
||||
|
||||
status: account.status,
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
return this.toStaffItem(account);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async updateStaff(actor: AuthUser, staffId: bigint, dto: UpdatePartnerStaffDto) {
|
||||
|
||||
const parentAccountId = actor.actorId;
|
||||
|
||||
const staff = await this.assertStaffOwned(parentAccountId, staffId);
|
||||
|
||||
const before = {
|
||||
|
||||
name: staff.name,
|
||||
|
||||
staffRole: staff.staffRole,
|
||||
|
||||
status: staff.status,
|
||||
|
||||
};
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
|
||||
if (dto.name !== undefined) {
|
||||
|
||||
const name = dto.name.trim();
|
||||
|
||||
if (!name) throw new BadRequestException('请填写真实姓名');
|
||||
|
||||
data.name = name;
|
||||
|
||||
}
|
||||
|
||||
if (dto.staffRole !== undefined) {
|
||||
|
||||
data.staffRole = dto.staffRole as PartnerStaffRole;
|
||||
|
||||
}
|
||||
|
||||
if (dto.permissions !== undefined) {
|
||||
|
||||
data.permissions = dto.permissions;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user