1 | "use strict";
|
2 | var __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 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var __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 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | const common_1 = require("@nestjs/common");
|
24 | const typeorm_1 = require("@nestjs/typeorm");
|
25 | const typeorm_2 = require("typeorm");
|
26 | const func_entity_1 = require("../model/func.entity");
|
27 | const module_entity_1 = require("../model/module.entity");
|
28 | const role_entity_1 = require("../model/role.entity");
|
29 | let RoleService = class RoleService {
|
30 | constructor(funcRepository, roleRepository, moduleRepository) {
|
31 | this.funcRepository = funcRepository;
|
32 | this.roleRepository = roleRepository;
|
33 | this.moduleRepository = moduleRepository;
|
34 | }
|
35 | createRole(moduleToken, name, score) {
|
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.roleRepository.findOne({ name, moduleToken });
|
42 | if (exist) {
|
43 | throw new common_1.HttpException("指定模块token=" + moduleToken + "下,指定名称name=" + name + "角色已经存在", 420);
|
44 | }
|
45 | const role = this.roleRepository.create({ name, score, module });
|
46 | try {
|
47 | yield this.roleRepository.save(role);
|
48 | }
|
49 | catch (err) {
|
50 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
51 | }
|
52 | });
|
53 | }
|
54 | updateRole(id, name, score) {
|
55 | return __awaiter(this, void 0, void 0, function* () {
|
56 | const role = yield this.roleRepository.findOneById(id);
|
57 | if (!role) {
|
58 | throw new common_1.HttpException("指定id=" + id + "角色不存在", 421);
|
59 | }
|
60 | if (name !== role.name) {
|
61 | const exist = yield this.roleRepository.findOne({ name, moduleToken: role.moduleToken });
|
62 | if (exist) {
|
63 | throw new common_1.HttpException("指定模块token=" + role.moduleToken + "下,指定名称name=" + name + "角色已经存在", 420);
|
64 | }
|
65 | }
|
66 | try {
|
67 | role.name = name;
|
68 | role.score = score;
|
69 | yield this.roleRepository.save(role);
|
70 | }
|
71 | catch (err) {
|
72 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
73 | }
|
74 | });
|
75 | }
|
76 | deleteRole(id) {
|
77 | return __awaiter(this, void 0, void 0, function* () {
|
78 | const role = yield this.roleRepository.findOneById(id);
|
79 | if (!role) {
|
80 | throw new common_1.HttpException("指定id=" + id + "角色不存在", 421);
|
81 | }
|
82 | try {
|
83 | yield this.roleRepository.remove(role);
|
84 | }
|
85 | catch (err) {
|
86 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
87 | }
|
88 | });
|
89 | }
|
90 | setFuncs(id, funcIds) {
|
91 | return __awaiter(this, void 0, void 0, function* () {
|
92 | const role = yield this.roleRepository.findOneById(id);
|
93 | if (!role) {
|
94 | throw new common_1.HttpException("指定id=" + id + "角色不存在", 421);
|
95 | }
|
96 | const funcs = yield this.funcRepository.findByIds(funcIds);
|
97 | funcIds.forEach(funcId => {
|
98 | const find = funcs.find(func => {
|
99 | return func.id === funcId;
|
100 | });
|
101 | if (!find) {
|
102 | throw new common_1.HttpException("指定id=" + funcId + "功能不存在", 422);
|
103 | }
|
104 | if (find.moduleToken !== role.moduleToken) {
|
105 | throw new common_1.HttpException("指定角色、功能必须属于同一个模块", 423);
|
106 | }
|
107 | });
|
108 | try {
|
109 | role.funcs = funcs;
|
110 | yield this.roleRepository.save(role);
|
111 | }
|
112 | catch (err) {
|
113 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
114 | }
|
115 | });
|
116 | }
|
117 | };
|
118 | RoleService = __decorate([
|
119 | common_1.Component(),
|
120 | __param(0, typeorm_1.InjectRepository(func_entity_1.Func)),
|
121 | __param(1, typeorm_1.InjectRepository(role_entity_1.Role)),
|
122 | __param(2, typeorm_1.InjectRepository(module_entity_1.Module)),
|
123 | __metadata("design:paramtypes", [typeorm_2.Repository,
|
124 | typeorm_2.Repository,
|
125 | typeorm_2.Repository])
|
126 | ], RoleService);
|
127 | exports.RoleService = RoleService;
|