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 organization_entity_1 = require("../model/organization.entity");
|
27 | const user_entity_1 = require("../model/user.entity");
|
28 | let OrganizationService = class OrganizationService {
|
29 | constructor(userRepository, organizationRepository) {
|
30 | this.userRepository = userRepository;
|
31 | this.organizationRepository = organizationRepository;
|
32 | }
|
33 | getRoots() {
|
34 | return __awaiter(this, void 0, void 0, function* () {
|
35 | const os = yield this.organizationRepository.createQueryBuilder("o").leftJoinAndSelect("o.parent", "parent").getMany();
|
36 | return os.filter(o => !o.parent);
|
37 | });
|
38 | }
|
39 | getChildren(id) {
|
40 | return __awaiter(this, void 0, void 0, function* () {
|
41 | const o = yield this.organizationRepository.findOneById(id, { relations: ["children"] });
|
42 | if (!o) {
|
43 | throw new common_1.HttpException("指定父组织id=" + id + "不存在", 402);
|
44 | }
|
45 | return o.children;
|
46 | });
|
47 | }
|
48 | getAll() {
|
49 | return __awaiter(this, void 0, void 0, function* () {
|
50 | return this.organizationRepository.find();
|
51 | });
|
52 | }
|
53 | createOrganization(name, parentId) {
|
54 | return __awaiter(this, void 0, void 0, function* () {
|
55 | let parent;
|
56 | if (parentId !== undefined && parentId !== null) {
|
57 | parent = yield this.organizationRepository.findOneById(parentId);
|
58 | if (!parent) {
|
59 | throw new common_1.HttpException("指定父组织id=" + parentId + "不存在", 402);
|
60 | }
|
61 | }
|
62 | const exist = yield this.organizationRepository.findOne({ name });
|
63 | if (exist) {
|
64 | throw new common_1.HttpException("指定名称name=" + name + "组织已存在", 403);
|
65 | }
|
66 | const organization = this.organizationRepository.create({ name, parent });
|
67 | try {
|
68 | yield this.organizationRepository.save(organization);
|
69 | }
|
70 | catch (err) {
|
71 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
72 | }
|
73 | });
|
74 | }
|
75 | updateOrganization(id, name, parentId) {
|
76 | return __awaiter(this, void 0, void 0, function* () {
|
77 | const exist = yield this.organizationRepository.findOneById(id);
|
78 | if (!exist) {
|
79 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 404);
|
80 | }
|
81 | if (name !== exist.name) {
|
82 | const exist = yield this.organizationRepository.findOne({ name });
|
83 | if (exist) {
|
84 | throw new common_1.HttpException("指定name=" + name + "组织已存在", 404);
|
85 | }
|
86 | }
|
87 | let parent;
|
88 | if (parentId !== undefined && parentId !== null) {
|
89 | parent = yield this.organizationRepository.findOneById(parentId);
|
90 | if (!parent) {
|
91 | throw new common_1.HttpException("指定父组织id=" + parentId + "不存在", 402);
|
92 | }
|
93 | }
|
94 | try {
|
95 | exist.name = name;
|
96 | exist.parent = parent;
|
97 | yield this.organizationRepository.save(exist);
|
98 | }
|
99 | catch (err) {
|
100 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
101 | }
|
102 | });
|
103 | }
|
104 | deleteOrganization(id) {
|
105 | return __awaiter(this, void 0, void 0, function* () {
|
106 | const exist = yield this.organizationRepository.findOneById(id, { relations: ["children"] });
|
107 | if (!exist) {
|
108 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 404);
|
109 | }
|
110 | if (exist.children && exist.children.length > 0) {
|
111 | throw new common_1.HttpException("指定组织存在子组织,无法删除", 404);
|
112 | }
|
113 | try {
|
114 | yield this.organizationRepository.remove(exist);
|
115 | }
|
116 | catch (err) {
|
117 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
118 | }
|
119 | });
|
120 | }
|
121 | getUsersInOrganization(id) {
|
122 | return __awaiter(this, void 0, void 0, function* () {
|
123 | const o = yield this.organizationRepository.findOneById(id, { relations: ["users"] });
|
124 | if (!o) {
|
125 | throw new common_1.HttpException("指定id=" + id + "父组织不存在", 402);
|
126 | }
|
127 | return o.users.filter(user => {
|
128 | return !user.recycle;
|
129 | });
|
130 | });
|
131 | }
|
132 | addUserToOrganization(id, userId) {
|
133 | return __awaiter(this, void 0, void 0, function* () {
|
134 | const o = yield this.organizationRepository.findOneById(id, { relations: ["users"] });
|
135 | if (!o) {
|
136 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 402);
|
137 | }
|
138 | const user = yield this.userRepository.findOneById(userId);
|
139 | if (!user) {
|
140 | throw new common_1.HttpException("指定id=" + userId + "用户不存在", 402);
|
141 | }
|
142 | const exist = o.users.find(user => {
|
143 | return user.id === userId;
|
144 | });
|
145 | if (exist) {
|
146 | throw new common_1.HttpException("指定用户id=" + userId + "已存在于指定组织id=" + id + "中", 402);
|
147 | }
|
148 | o.users.push(user);
|
149 | try {
|
150 | yield this.organizationRepository.save(o);
|
151 | }
|
152 | catch (err) {
|
153 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
154 | }
|
155 | });
|
156 | }
|
157 | addUsersToOrganization(id, userIds) {
|
158 | return __awaiter(this, void 0, void 0, function* () {
|
159 | const o = yield this.organizationRepository.findOneById(id, { relations: ["users"] });
|
160 | if (!o) {
|
161 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 402);
|
162 | }
|
163 | const users = yield this.userRepository.findByIds(userIds);
|
164 | userIds.forEach(id => {
|
165 | const find = users.find(user => {
|
166 | return user.id === id;
|
167 | });
|
168 | if (!find) {
|
169 | throw new common_1.HttpException("指定id=" + id + "用户不存在", 402);
|
170 | }
|
171 | });
|
172 | o.users.forEach(user => {
|
173 | const match = userIds.find(id => {
|
174 | return id === user.id;
|
175 | });
|
176 | if (match) {
|
177 | throw new common_1.HttpException("指定用户id=" + user.id + "已存在于指定组织id=" + id + "中", 402);
|
178 | }
|
179 | });
|
180 | o.users.push(...users);
|
181 | try {
|
182 | yield this.organizationRepository.save(o);
|
183 | }
|
184 | catch (err) {
|
185 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
186 | }
|
187 | });
|
188 | }
|
189 | removeUserFromOrganization(id, userId) {
|
190 | return __awaiter(this, void 0, void 0, function* () {
|
191 | const o = yield this.organizationRepository.findOneById(id, { relations: ["users"] });
|
192 | if (!o) {
|
193 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 402);
|
194 | }
|
195 | const user = yield this.userRepository.findOneById(userId);
|
196 | if (!user) {
|
197 | throw new common_1.HttpException("指定id=" + userId + "用户不存在", 402);
|
198 | }
|
199 | const index = o.users.findIndex(user => {
|
200 | return user.id === userId;
|
201 | });
|
202 | if (index < 0) {
|
203 | throw new common_1.HttpException("指定用户id=" + userId + "不存在于指定组织id=" + id + "中", 402);
|
204 | }
|
205 | o.users.splice(index, 1);
|
206 | try {
|
207 | yield this.organizationRepository.save(o);
|
208 | }
|
209 | catch (err) {
|
210 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
211 | }
|
212 | });
|
213 | }
|
214 | removeUsersFromOrganization(id, userIds) {
|
215 | return __awaiter(this, void 0, void 0, function* () {
|
216 | const o = yield this.organizationRepository.findOneById(id, { relations: ["users"] });
|
217 | if (!o) {
|
218 | throw new common_1.HttpException("指定id=" + id + "组织不存在", 402);
|
219 | }
|
220 | const users = yield this.userRepository.findByIds(userIds);
|
221 | userIds.forEach(userId => {
|
222 | const find = users.find(user => {
|
223 | return user.id === userId;
|
224 | });
|
225 | if (!find) {
|
226 | throw new common_1.HttpException("指定id=" + userId + "用户不存在", 402);
|
227 | }
|
228 | const index = o.users.findIndex(user => {
|
229 | return user.id === userId;
|
230 | });
|
231 | if (index < 0) {
|
232 | throw new common_1.HttpException("指定用户id=" + userId + "不存在于指定组织id=" + id + "中", 402);
|
233 | }
|
234 | o.users.splice(index, 1);
|
235 | });
|
236 | try {
|
237 | yield this.organizationRepository.save(o);
|
238 | }
|
239 | catch (err) {
|
240 | throw new common_1.HttpException("数据库错误" + err.toString(), 401);
|
241 | }
|
242 | });
|
243 | }
|
244 | };
|
245 | OrganizationService = __decorate([
|
246 | common_1.Component(),
|
247 | __param(0, typeorm_1.InjectRepository(user_entity_1.User)),
|
248 | __param(1, typeorm_1.InjectRepository(organization_entity_1.Organization)),
|
249 | __metadata("design:paramtypes", [typeorm_2.Repository,
|
250 | typeorm_2.Repository])
|
251 | ], OrganizationService);
|
252 | exports.OrganizationService = OrganizationService;
|