refactor(backend): 统一使用buildUserProjectQuery处理用户和项目查询条件

This commit is contained in:
xiaojunnuo
2026-06-03 23:32:14 +08:00
parent ea010f8c9b
commit cdb812ef63
8 changed files with 30 additions and 10 deletions
@@ -80,7 +80,7 @@ const development = {
type: "better-sqlite3",
database: "./data/db.sqlite",
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true
logging: false,
logging: true,
highlightSql: false,
// 配置实体模型 或者 entities: '/entity',
@@ -134,5 +134,6 @@ export class MainConfiguration {
});
logger.info("当前环境:", this.app.getEnv()); // prod
}
}
@@ -3,6 +3,7 @@ import { CertApplyTemplateService } from "./cert-apply-template-service.js";
function createService(list: any[]) {
const service = new CertApplyTemplateService();
const updateWhereList: any[] = [];
function matchesWhere(item: any, where: any) {
for (const key of Object.keys(where)) {
const expected = where[key];
@@ -23,6 +24,7 @@ function createService(list: any[]) {
return list.find(item => matchesWhere(item, where));
},
async update(where: any, patch: any) {
updateWhereList.push({ ...where });
for (const item of list) {
if (matchesWhere(item, where)) {
Object.assign(item, patch);
@@ -30,6 +32,7 @@ function createService(list: any[]) {
}
},
};
(service as any).updateWhereList = updateWhereList;
return service;
}
@@ -158,7 +161,7 @@ describe("CertApplyTemplateService", () => {
});
});
it("sets default for templates with null project id", async () => {
it("does not include project id condition when setting default without project id", async () => {
const list = [
{
id: 1,
@@ -189,8 +192,12 @@ describe("CertApplyTemplateService", () => {
await service.setDefault(2, 10, null);
assert.deepEqual((service as any).updateWhereList, [
{ userId: 10 },
{ userId: 10, id: 2 },
]);
assert.equal(list[0].isDefault, false);
assert.equal(list[1].isDefault, true);
assert.equal(list[2].isDefault, true);
assert.equal(list[2].isDefault, false);
});
});
@@ -47,16 +47,16 @@ export class CertApplyTemplateService extends BaseService<CertApplyTemplateEntit
if (entity.disabled) {
throw new ValidateException("禁用的模版不能设为默认");
}
await this.repository.update({ userId, projectId }, { isDefault: false });
await this.repository.update({ id: entity.id, userId, projectId }, { isDefault: true });
const query = this.buildUserProjectQuery(userId, projectId);
await this.repository.update(query, { isDefault: false });
await this.repository.update({ ...query, id: entity.id }, { isDefault: true });
return entity;
}
async getDefault(userId: number, projectId?: number) {
return await this.repository.findOne({
where: {
userId,
projectId,
...this.buildUserProjectQuery(userId, projectId),
isDefault: true,
disabled: false,
},
@@ -87,8 +87,7 @@ export class CertApplyTemplateService extends BaseService<CertApplyTemplateEntit
const template = await this.repository.findOne({
where: {
id,
userId,
projectId,
...this.buildUserProjectQuery(userId, projectId),
},
});
if (!template) {