UNPKG

6.41 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 info_item_entity_1 = require("../model/info.item.entity");
27let InfoItemService = class InfoItemService {
28 constructor(infoItemRepository) {
29 this.infoItemRepository = infoItemRepository;
30 }
31 createInfoItem(name, label, description, type, necessary, registerVisible, informationVisible, order) {
32 return __awaiter(this, void 0, void 0, function* () {
33 const exist = yield this.infoItemRepository.findOne({ name });
34 if (exist) {
35 throw new common_1.HttpException("指定名称信息项已存在:" + name, 412);
36 }
37 if (necessary && !registerVisible) {
38 throw new common_1.HttpException("指定名称name=" + name + "必填信息项,注册时必须可见", 412);
39 }
40 const item = this.infoItemRepository.create({
41 name,
42 label,
43 default: false,
44 description,
45 type,
46 necessary,
47 registerVisible,
48 informationVisible,
49 order
50 });
51 try {
52 yield this.infoItemRepository.save(item);
53 }
54 catch (err) {
55 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
56 }
57 });
58 }
59 updateInfoItem(id, name, label, description, type, necessary, registerVisible, informationVisible, order) {
60 return __awaiter(this, void 0, void 0, function* () {
61 const exist = yield this.infoItemRepository.findOneById(id);
62 if (!exist) {
63 throw new common_1.HttpException("指定id=" + id + "信息项不存在", 413);
64 }
65 if (exist.default) {
66 throw new common_1.HttpException("默认信息项不允许更新", 413);
67 }
68 if (necessary && !registerVisible) {
69 throw new common_1.HttpException("指定名称name=" + name + "必填信息项,注册时必须可见", 412);
70 }
71 if (name !== exist.name) {
72 const exist1 = yield this.infoItemRepository.findOne({ name });
73 if (exist1) {
74 throw new common_1.HttpException("指定name=" + name + "信息项已存在", 412);
75 }
76 }
77 exist.name = name;
78 exist.label = label;
79 exist.description = description;
80 exist.type = type;
81 exist.necessary = necessary;
82 exist.registerVisible = registerVisible;
83 exist.informationVisible = informationVisible;
84 exist.order = order;
85 try {
86 yield this.infoItemRepository.save(exist);
87 }
88 catch (err) {
89 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
90 }
91 });
92 }
93 deleteInfoItem(id) {
94 return __awaiter(this, void 0, void 0, function* () {
95 const exist = yield this.infoItemRepository.findOneById(id);
96 if (!exist) {
97 throw new common_1.HttpException("指定id=" + id + "信息项不存在", 413);
98 }
99 if (exist.default) {
100 throw new common_1.HttpException("默认信息项不允许删除", 413);
101 }
102 try {
103 yield this.infoItemRepository.remove(exist);
104 }
105 catch (err) {
106 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
107 }
108 });
109 }
110 deleteInfoItems(ids) {
111 return __awaiter(this, void 0, void 0, function* () {
112 const infoItems = yield this.infoItemRepository.findByIds(ids);
113 ids.forEach(id => {
114 const find = infoItems.find(item => {
115 return item.id === id;
116 });
117 if (!find) {
118 throw new common_1.HttpException("指定id=" + id + "信息项不存在", 413);
119 }
120 if (find.default) {
121 throw new common_1.HttpException("默认信息项不允许删除", 413);
122 }
123 });
124 try {
125 yield this.infoItemRepository.remove(infoItems);
126 }
127 catch (err) {
128 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
129 }
130 });
131 }
132};
133InfoItemService = __decorate([
134 common_1.Component(),
135 __param(0, typeorm_1.InjectRepository(info_item_entity_1.InfoItem)),
136 __metadata("design:paramtypes", [typeorm_2.Repository])
137], InfoItemService);
138exports.InfoItemService = InfoItemService;