Files
fabric_trade_server/dist/admin-users/admin-users.service.js
T
mohmdsdgandCursor 63646b9f01 Replace demo admins with a single login and drop fake inbox seed.
Only 09128081392 / 123456 remains as admin; remove seeded internal messages and add a reset-admin script for production databases.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-09 16:25:37 +03:30

162 lines
6.4 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminUsersService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const bcrypt = __importStar(require("bcryptjs"));
const prisma_service_1 = require("../prisma/prisma.service");
const phone_1 = require("../common/utils/phone");
const DEFAULT_PASSWORD = '123456';
let AdminUsersService = class AdminUsersService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async findAll() {
const users = await this.prisma.adminUser.findMany({
orderBy: { createdAt: 'desc' },
});
return users.map((u) => this.map(u));
}
async findOne(id) {
const user = await this.prisma.adminUser.findUnique({ where: { id } });
if (!user)
throw new common_1.NotFoundException('کاربر ادمین یافت نشد');
return this.map(user);
}
async create(dto) {
const phone = (0, phone_1.normalizePhone)((0, phone_1.toEnglishDigits)(dto.phone));
if (!(0, phone_1.isValidPhone)(phone)) {
throw new common_1.BadRequestException('شماره موبایل نامعتبر است');
}
const passwordHash = await bcrypt.hash(dto.password?.trim() || DEFAULT_PASSWORD, 10);
try {
const user = await this.prisma.adminUser.create({
data: {
fullName: dto.fullName.trim(),
phone,
email: dto.email.trim().toLowerCase(),
passwordHash,
role: dto.role ?? client_1.AdminRole.admin,
isActive: dto.isActive ?? true,
},
});
return this.map(user);
}
catch (e) {
if (e instanceof client_1.Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002') {
throw new common_1.ConflictException('شماره یا ایمیل تکراری است');
}
throw e;
}
}
async update(id, dto) {
await this.ensure(id);
const phone = dto.phone !== undefined
? (0, phone_1.normalizePhone)((0, phone_1.toEnglishDigits)(dto.phone))
: undefined;
if (phone !== undefined && !(0, phone_1.isValidPhone)(phone)) {
throw new common_1.BadRequestException('شماره موبایل نامعتبر است');
}
const data = {
fullName: dto.fullName?.trim(),
phone,
email: dto.email?.trim().toLowerCase(),
role: dto.role,
isActive: dto.isActive,
};
if (dto.password?.trim()) {
data.passwordHash = await bcrypt.hash(dto.password.trim(), 10);
}
try {
const user = await this.prisma.adminUser.update({
where: { id },
data,
});
return this.map(user);
}
catch (e) {
if (e instanceof client_1.Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002') {
throw new common_1.ConflictException('شماره یا ایمیل تکراری است');
}
throw e;
}
}
async setActive(id, isActive) {
await this.ensure(id);
const user = await this.prisma.adminUser.update({
where: { id },
data: { isActive },
});
return this.map(user);
}
async ensure(id) {
const user = await this.prisma.adminUser.findUnique({ where: { id } });
if (!user)
throw new common_1.NotFoundException('کاربر ادمین یافت نشد');
return user;
}
map(user) {
return {
id: user.id,
fullName: user.fullName,
phone: user.phone,
email: user.email,
role: user.role,
isActive: user.isActive,
lastLoginAt: user.lastLoginAt?.toISOString() ?? null,
createdAt: user.createdAt.toISOString(),
updatedAt: user.updatedAt.toISOString(),
};
}
};
exports.AdminUsersService = AdminUsersService;
exports.AdminUsersService = AdminUsersService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
], AdminUsersService);
//# sourceMappingURL=admin-users.service.js.map