144 lines
5.5 KiB
JavaScript
144 lines
5.5 KiB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
import { BadRequestException, Body, Controller, Delete, Get, HttpCode, Injectable, Module, Param, Post, Put, } from "@nestjs/common";
|
|
import { userCreateSchema, userUpdateSchema, } from "@imen/shared";
|
|
import { AdminAuth, CurrentUser } from "../common/auth.js";
|
|
import { AuditService } from "../common/common.module.js";
|
|
import { IdPipe, ZodPipe } from "../common/zod.pipe.js";
|
|
import { AuthModule } from "../auth/auth.controller.js";
|
|
import { AuthService, hashPassword, toUserDto } from "../auth/auth.service.js";
|
|
import { PrismaService } from "../prisma/prisma.service.js";
|
|
let UsersService = class UsersService {
|
|
prisma;
|
|
auth;
|
|
audit;
|
|
constructor(prisma, auth, audit) {
|
|
this.prisma = prisma;
|
|
this.auth = auth;
|
|
this.audit = audit;
|
|
}
|
|
async list() {
|
|
const users = await this.prisma.adminUser.findMany({ orderBy: { id: "asc" } });
|
|
return users.map(toUserDto);
|
|
}
|
|
async create(input, actor) {
|
|
const user = await this.prisma.adminUser.create({
|
|
data: {
|
|
phone: input.phone,
|
|
name: input.name,
|
|
role: input.role,
|
|
passwordHash: await hashPassword(input.password),
|
|
},
|
|
});
|
|
this.audit.log(actor.id, "create", "user", user.id, user.phone);
|
|
return toUserDto(user);
|
|
}
|
|
async update(id, input, actor) {
|
|
if (id === actor.id && (input.role !== "ADMIN" || !input.isActive)) {
|
|
throw new BadRequestException("نمیتوانید نقش یا وضعیت حساب خودتان را تغییر دهید");
|
|
}
|
|
const user = await this.prisma.adminUser.update({
|
|
where: { id },
|
|
data: {
|
|
name: input.name,
|
|
role: input.role,
|
|
isActive: input.isActive,
|
|
...(input.password ? { passwordHash: await hashPassword(input.password) } : {}),
|
|
},
|
|
});
|
|
if (input.password || !input.isActive)
|
|
await this.auth.revokeAll(id);
|
|
this.audit.log(actor.id, "update", "user", id, user.phone);
|
|
return toUserDto(user);
|
|
}
|
|
async remove(id, actor) {
|
|
if (id === actor.id)
|
|
throw new BadRequestException("نمیتوانید حساب خودتان را حذف کنید");
|
|
const user = await this.prisma.adminUser.delete({ where: { id } });
|
|
this.audit.log(actor.id, "delete", "user", id, user.phone);
|
|
}
|
|
};
|
|
UsersService = __decorate([
|
|
Injectable(),
|
|
__metadata("design:paramtypes", [PrismaService,
|
|
AuthService,
|
|
AuditService])
|
|
], UsersService);
|
|
export { UsersService };
|
|
let UsersController = class UsersController {
|
|
users;
|
|
constructor(users) {
|
|
this.users = users;
|
|
}
|
|
list() {
|
|
return this.users.list();
|
|
}
|
|
create(body, actor) {
|
|
return this.users.create(body, actor);
|
|
}
|
|
update(id, body, actor) {
|
|
return this.users.update(id, body, actor);
|
|
}
|
|
async remove(id, actor) {
|
|
await this.users.remove(id, actor);
|
|
}
|
|
};
|
|
__decorate([
|
|
Get(),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], UsersController.prototype, "list", null);
|
|
__decorate([
|
|
Post(),
|
|
__param(0, Body(new ZodPipe(userCreateSchema))),
|
|
__param(1, CurrentUser()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], UsersController.prototype, "create", null);
|
|
__decorate([
|
|
Put(":id"),
|
|
__param(0, Param("id", IdPipe)),
|
|
__param(1, Body(new ZodPipe(userUpdateSchema))),
|
|
__param(2, CurrentUser()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], UsersController.prototype, "update", null);
|
|
__decorate([
|
|
Delete(":id"),
|
|
HttpCode(204),
|
|
__param(0, Param("id", IdPipe)),
|
|
__param(1, CurrentUser()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "remove", null);
|
|
UsersController = __decorate([
|
|
AdminAuth("ADMIN"),
|
|
Controller("admin/users"),
|
|
__metadata("design:paramtypes", [UsersService])
|
|
], UsersController);
|
|
export { UsersController };
|
|
let UsersModule = class UsersModule {
|
|
};
|
|
UsersModule = __decorate([
|
|
Module({
|
|
imports: [AuthModule],
|
|
controllers: [UsersController],
|
|
providers: [UsersService],
|
|
})
|
|
], UsersModule);
|
|
export { UsersModule };
|
|
//# sourceMappingURL=users.controller.js.map
|