UNPKG

6.22 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 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;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15 return new (P || (P = Promise))(function (resolve, reject) {
16 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
19 step((generator = generator.apply(thisArg, _arguments || [])).next());
20 });
21};
22Object.defineProperty(exports, "__esModule", { value: true });
23const common_1 = require("@nestjs/common");
24const typeorm_1 = require("@nestjs/typeorm");
25const typeorm_2 = require("typeorm");
26const func_entity_1 = require("../model/func.entity");
27const module_entity_1 = require("../model/module.entity");
28const permission_entity_1 = require("../model/permission.entity");
29let FuncService = class FuncService {
30 constructor(funcRepository, moduleRepository, permissionRepository) {
31 this.funcRepository = funcRepository;
32 this.moduleRepository = moduleRepository;
33 this.permissionRepository = permissionRepository;
34 }
35 createFunc(moduleToken, name) {
36 return __awaiter(this, void 0, void 0, function* () {
37 const module = yield this.moduleRepository.findOneById(moduleToken);
38 if (!module) {
39 throw new common_1.HttpException("指定模块token=" + moduleToken + "不存在", 415);
40 }
41 const exist = yield this.funcRepository.findOne({ name, moduleToken });
42 if (exist) {
43 throw new common_1.HttpException("指定模块token=" + moduleToken + "下,指定名称name=" + name + "功能已经存在", 416);
44 }
45 const func = this.funcRepository.create({ name, module });
46 try {
47 yield this.funcRepository.save(func);
48 }
49 catch (err) {
50 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
51 }
52 });
53 }
54 updateFunc(id, name) {
55 return __awaiter(this, void 0, void 0, function* () {
56 const func = yield this.funcRepository.findOneById(id);
57 if (!func) {
58 throw new common_1.HttpException("指定id=" + id + "功能不存在", 417);
59 }
60 if (name !== func.name) {
61 const exist = yield this.funcRepository.findOne({ name, moduleToken: func.moduleToken });
62 if (exist) {
63 throw new common_1.HttpException("指定模块token=" + func.moduleToken + "下,指定名称name=" + name + "功能已经存在", 416);
64 }
65 try {
66 func.name = name;
67 yield this.funcRepository.save(func);
68 }
69 catch (err) {
70 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
71 }
72 }
73 });
74 }
75 deleteFunc(id) {
76 return __awaiter(this, void 0, void 0, function* () {
77 const func = yield this.funcRepository.findOneById(id);
78 if (!func) {
79 throw new common_1.HttpException("指定id=" + id + "功能不存在", 417);
80 }
81 try {
82 yield this.funcRepository.remove(func);
83 }
84 catch (err) {
85 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
86 }
87 });
88 }
89 setPermissions(id, permissionIds) {
90 return __awaiter(this, void 0, void 0, function* () {
91 const func = yield this.funcRepository.findOneById(id, { relations: ["permissions"] });
92 if (!func) {
93 throw new common_1.HttpException("指定id=" + id + "功能不存在", 417);
94 }
95 const pers = yield this.permissionRepository.findByIds(permissionIds, { relations: ["module"] });
96 permissionIds.forEach(permissionId => {
97 const find = pers.find(per => {
98 return per.id === permissionId;
99 });
100 if (!find) {
101 throw new common_1.HttpException("指定id=" + permissionId + "权限不存在", 418);
102 }
103 if (find.moduleToken !== func.moduleToken) {
104 throw new common_1.HttpException("指定功能、权限只能属于同一个模块", 419);
105 }
106 });
107 try {
108 func.permissions = pers;
109 yield this.funcRepository.save(func);
110 }
111 catch (err) {
112 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
113 }
114 });
115 }
116};
117FuncService = __decorate([
118 common_1.Component(),
119 __param(0, typeorm_1.InjectRepository(func_entity_1.Func)),
120 __param(1, typeorm_1.InjectRepository(module_entity_1.Module)),
121 __param(2, typeorm_1.InjectRepository(permission_entity_1.Permission)),
122 __metadata("design:paramtypes", [typeorm_2.Repository,
123 typeorm_2.Repository,
124 typeorm_2.Repository])
125], FuncService);
126exports.FuncService = FuncService;