mirror of
https://github.com/certd/certd.git
synced 2026-07-15 02:07:38 +08:00
chore: 优化证书参数模版功能
This commit is contained in:
@@ -135,6 +135,5 @@ export class MainConfiguration {
|
||||
|
||||
logger.info("当前环境:", this.app.getEnv()); // prod
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+17
-2
@@ -14,13 +14,26 @@ export class CertApplyTemplateController extends CrudController<CertApplyTemplat
|
||||
return this.service;
|
||||
}
|
||||
|
||||
private removeContent(data: any) {
|
||||
const records = Array.isArray(data) ? data : data?.records;
|
||||
if (!records) {
|
||||
return data;
|
||||
}
|
||||
for (const item of records) {
|
||||
delete item.content;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Post("/page", { description: Constants.per.authOnly, summary: "查询证书申请参数模版分页列表" })
|
||||
async page(@Body(ALL) body: any) {
|
||||
const { projectId, userId } = await this.getProjectUserIdRead();
|
||||
body.query = body.query ?? {};
|
||||
body.query.projectId = projectId;
|
||||
body.query.userId = userId;
|
||||
return super.page(body);
|
||||
const res = await super.page(body);
|
||||
this.removeContent(res.data);
|
||||
return res;
|
||||
}
|
||||
|
||||
@Post("/list", { description: Constants.per.authOnly, summary: "查询证书申请参数模版列表" })
|
||||
@@ -30,7 +43,9 @@ export class CertApplyTemplateController extends CrudController<CertApplyTemplat
|
||||
body.query.projectId = projectId;
|
||||
body.query.userId = userId;
|
||||
body.query.disabled = false;
|
||||
return super.list(body);
|
||||
const res = await super.list(body);
|
||||
this.removeContent(res.data);
|
||||
return res;
|
||||
}
|
||||
|
||||
@Post("/add", { description: Constants.per.authOnly, summary: "添加证书申请参数模版" })
|
||||
|
||||
+58
-14
@@ -3,23 +3,31 @@ import { CertApplyTemplateService } from "./cert-apply-template-service.js";
|
||||
|
||||
function createService(list: any[]) {
|
||||
const service = new CertApplyTemplateService();
|
||||
function matchesWhere(item: any, where: any) {
|
||||
for (const key of Object.keys(where)) {
|
||||
const expected = where[key];
|
||||
if (expected?._type === "isNull") {
|
||||
if (item[key] != null) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (expected != null && item[key] !== expected) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
(service as any).repository = {
|
||||
async findOne({ where }: any) {
|
||||
return list.find(item => {
|
||||
if (where.id != null && item.id !== where.id) {
|
||||
return false;
|
||||
return list.find(item => matchesWhere(item, where));
|
||||
},
|
||||
async update(where: any, patch: any) {
|
||||
for (const item of list) {
|
||||
if (matchesWhere(item, where)) {
|
||||
Object.assign(item, patch);
|
||||
}
|
||||
if (where.userId != null && item.userId !== where.userId) {
|
||||
return false;
|
||||
}
|
||||
if (where.projectId != null && item.projectId !== where.projectId) {
|
||||
return false;
|
||||
}
|
||||
if (where.isDefault != null && item.isDefault !== where.isDefault) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
return service;
|
||||
@@ -149,4 +157,40 @@ describe("CertApplyTemplateService", () => {
|
||||
privateKeyType: "rsa_4096",
|
||||
});
|
||||
});
|
||||
|
||||
it("sets default for templates with null project id", async () => {
|
||||
const list = [
|
||||
{
|
||||
id: 1,
|
||||
userId: 10,
|
||||
projectId: null,
|
||||
isDefault: true,
|
||||
disabled: false,
|
||||
content: "{}",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
userId: 10,
|
||||
projectId: null,
|
||||
isDefault: false,
|
||||
disabled: false,
|
||||
content: "{}",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
userId: 10,
|
||||
projectId: 20,
|
||||
isDefault: true,
|
||||
disabled: false,
|
||||
content: "{}",
|
||||
},
|
||||
];
|
||||
const service = createService(list);
|
||||
|
||||
await service.setDefault(2, 10, null);
|
||||
|
||||
assert.equal(list[0].isDefault, false);
|
||||
assert.equal(list[1].isDefault, true);
|
||||
assert.equal(list[2].isDefault, true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { InjectEntityModel } from "@midwayjs/typeorm";
|
||||
import { BaseService, ValidateException } from "@certd/lib-server";
|
||||
import { Repository } from "typeorm";
|
||||
import { IsNull, Repository } from "typeorm";
|
||||
import { CertApplyTemplateEntity } from "../entity/cert-apply-template.js";
|
||||
import { CertApplyTemplateParams, pickCertApplyCustomParams, pickCertApplyTemplateParams } from "./cert-apply-template-fields.js";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user