sysmgr-nest/src/users/users.service.ts
2026-10-10 21:13:35 +08:00

40 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Injectable } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import { BaseCrudService } from '../common/base-crud.service.js';
import { PrismaService } from '../prisma/prisma.service.js';
import { getRequestContext } from '../common/context/request-context.js';
import { CreateUserDto, ResetPasswordDto, UpdateUserDto } from './dto.js';
import { PageQuery } from '../common/page.dto.js';
@Injectable()
export class UsersService extends BaseCrudService {
constructor(prisma: PrismaService) {
super(prisma, 'user');
}
async createUser(dto: CreateUserDto) {
const passwordHash = await bcrypt.hash(dto.password, 10);
const { password: _p, ...rest } = dto;
return this.create({ ...rest, passwordHash });
}
async listUsers(q: PageQuery) {
const ctx = getRequestContext();
const where: Record<string, unknown> = {};
// 数据权限:orgCodes 为空表示「全部可见」,否则限制在可见部门集合
if (ctx && ctx.orgCodes.length > 0) {
where.orgCode = { in: ctx.orgCodes };
}
return this.paginate(q.page, q.pageSize, where);
}
async updateUser(id: string, dto: UpdateUserDto) {
return this.update(id, { ...dto });
}
async resetPassword(id: string, dto: ResetPasswordDto) {
const passwordHash = await bcrypt.hash(dto.password, 10);
return this.update(id, { passwordHash });
}
}