????

Your IP : 216.73.216.64


Current Path : C:/inetpub/vhost/qnquyhoach.nextform.vn/api/dist/apps/ht-cay-xanhs/
Upload File :
Current File : C:/inetpub/vhost/qnquyhoach.nextform.vn/api/dist/apps/ht-cay-xanhs/log.interceptor.js

"use strict";
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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogCayXanhInterceptor = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const rxjs_1 = require("rxjs");
const log_decorator_1 = require("./log.decorator");
const typeorm_1 = require("@nestjs/typeorm");
const log_cay_xanh_entity_1 = require("../log-cay-xanhs/entities/log-cay-xanh.entity");
const typeorm_2 = require("typeorm");
const permission_type_1 = require("../permissions/permission-type");
const user_entity_1 = require("../users/entities/user.entity");
const ht_xanhs_service_1 = require("./ht-xanhs.service");
let LogCayXanhInterceptor = class LogCayXanhInterceptor {
    constructor(reflector, logCayXanhRepo, userRepo, htCayXanhService) {
        this.reflector = reflector;
        this.logCayXanhRepo = logCayXanhRepo;
        this.userRepo = userRepo;
        this.htCayXanhService = htCayXanhService;
    }
    async intercept(context, next) {
        const logOperation = this.reflector.get(log_decorator_1.SYS_LOG_KEY, context.getHandler());
        if (!logOperation) {
            return next.handle();
        }
        const request = context.switchToHttp().getRequest();
        const userId = request.userId;
        const user = await this.userRepo.findOne({
            where: { id: userId },
            relations: ['roles'],
            select: [],
        });
        const permissionSet = new Set();
        for (const role of user.roles) {
            role.permissions.forEach((p) => permissionSet.add(p));
        }
        const isAuditLog = Array.from(permissionSet).some((permission) => permission === permission_type_1.Permission.AuditLog);
        const { id } = request.params;
        const data = request.body;
        const method = request.method;
        const logCayXanh = {
            SoThuTu: data.SoThuTu,
            DoCao: data.DoCao,
            DuongKinhGoc: data.DuongKinhGoc,
            TinhTrang: data.TinhTrang,
            GhiChu: data.GhiChu,
            TenLoaiCay: data.TenLoaiCay,
            MaDuong: data.MaDuong,
            NamCapNhat: data.NamCapNhat,
            gidCayXanh: ['PUT', 'PATCH'].includes(method) ? +id : null,
            createdBy: userId,
            action: method,
        };
        if (id) {
            const existingLogCayXanhPending = await this.logCayXanhRepo.findOne({
                where: { gidCayXanh: +id, status: 'pending' },
            });
            const countLog = await this.logCayXanhRepo.count({ where: { gidCayXanh: +id } });
            if (countLog === 0) {
                const htCayXanhInit = await this.htCayXanhService.findOne({ where: { id: +id } });
                const logCayXanhInit = new log_cay_xanh_entity_1.LogCayXanh();
                logCayXanhInit.SoThuTu = htCayXanhInit.SoThuTu;
                logCayXanhInit.DoCao = htCayXanhInit.DoCao;
                logCayXanhInit.DuongKinhGoc = htCayXanhInit.DuongKinhGoc;
                logCayXanhInit.TinhTrang = htCayXanhInit.TinhTrang;
                logCayXanhInit.GhiChu = htCayXanhInit.GhiChu;
                logCayXanhInit.TenLoaiCay = htCayXanhInit.TenLoaiCay;
                logCayXanhInit.MaDuong = htCayXanhInit.MaDuong;
                logCayXanhInit.NamCapNhat = htCayXanhInit.NamCapNhat;
                logCayXanhInit.gidCayXanh = +id;
                logCayXanhInit.createdBy = userId;
                logCayXanhInit.action = 'POST';
                logCayXanhInit.status = 'initiated';
                await this.logCayXanhRepo.save(logCayXanhInit);
            }
            if (isAuditLog) {
                if (existingLogCayXanhPending) {
                    return (0, rxjs_1.of)({ success: false, message: 'Vui lòng duyệt dữ liệu trước khi cập nhật' });
                }
                await this.logCayXanhRepo.save({ ...logCayXanh, status: 'modified' });
                return next.handle();
            }
            if (existingLogCayXanhPending) {
                await this.logCayXanhRepo.update(existingLogCayXanhPending.id, logCayXanh);
                return (0, rxjs_1.of)({ success: true, message: 'Cập nhật dữ liệu xét duyệt thành công' });
            }
        }
        await this.logCayXanhRepo.save(logCayXanh);
        const responseData = {
            success: true,
            message: 'Gửi xét duyệt dữ liệu thành công',
        };
        return (0, rxjs_1.of)(responseData);
    }
};
exports.LogCayXanhInterceptor = LogCayXanhInterceptor;
exports.LogCayXanhInterceptor = LogCayXanhInterceptor = __decorate([
    (0, common_1.Injectable)(),
    __param(1, (0, typeorm_1.InjectRepository)(log_cay_xanh_entity_1.LogCayXanh)),
    __param(2, (0, typeorm_1.InjectRepository)(user_entity_1.User)),
    __metadata("design:paramtypes", [core_1.Reflector,
        typeorm_2.Repository,
        typeorm_2.Repository,
        ht_xanhs_service_1.HTCayXanhsService])
], LogCayXanhInterceptor);
//# sourceMappingURL=log.interceptor.js.map