refactor: 统一userProject查询参数传递逻辑

This commit is contained in:
xiaojunnuo
2026-06-04 00:00:40 +08:00
parent cdb812ef63
commit 2cbacb4338
30 changed files with 158 additions and 134 deletions
@@ -57,7 +57,7 @@ export abstract class BaseService<T> {
}
protected buildUserProjectQuery(userId: number, projectId?: number) {
const query: { userId: number; projectId?: number } = {
const query: { userId: number; projectId?: number; [key: string]: any } = {
userId,
};
if (projectId != null) {
@@ -282,12 +282,12 @@ export abstract class BaseService<T> {
async batchDelete(ids: number[], userId: number,projectId?:number) {
ids = this.filterIds(ids);
if(userId!=null){
const userProjectQuery = this.buildUserProjectQuery(userId, projectId);
const list = await this.getRepository().find({
where: {
// @ts-ignore
id: In(ids),
userId,
projectId,
...userProjectQuery,
},
})
// @ts-ignore
@@ -225,11 +225,11 @@ export class AccessService extends BaseService<AccessEntity> {
if (userId == null) {
return [];
}
const userProjectQuery = this.buildUserProjectQuery(userId, projectId);
return await this.repository.find({
where: {
id: In(ids),
userId,
projectId,
...userProjectQuery,
},
select: {
id: true,
@@ -96,11 +96,11 @@ export class AddonService extends BaseService<AddonEntity> {
if (userId==null) {
return [];
}
const userProjectQuery = this.buildUserProjectQuery(userId, projectId);
return await this.repository.find({
where: {
id: In(ids),
userId,
projectId
...userProjectQuery,
},
select: {
id: true,
@@ -117,11 +117,11 @@ export class AddonService extends BaseService<AddonEntity> {
async getDefault(userId: number, addonType: string,projectId?:number): Promise<any> {
const userProjectQuery = this.buildUserProjectQuery(userId, projectId);
const res = await this.repository.findOne({
where: {
userId,
addonType,
projectId
...userProjectQuery,
},
order: {
isDefault: "DESC"
@@ -154,27 +154,17 @@ export class AddonService extends BaseService<AddonEntity> {
if (userId==null) {
throw new ValidateException("userId不能为空");
}
await this.repository.update(
{
userId,
addonType,
projectId
},
{
isDefault: false
}
);
await this.repository.update(
{
id,
userId,
addonType,
projectId
},
{
isDefault: true
}
);
const userProjectQuery = this.buildUserProjectQuery(userId, projectId);
const query = {
addonType,
...userProjectQuery,
};
await this.repository.update(query, {
isDefault: false
});
await this.repository.update({ ...query, id }, {
isDefault: true
});
}
async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any,projectId?:number }) {
@@ -202,12 +192,12 @@ export class AddonService extends BaseService<AddonEntity> {
}
async getOneByType(req:{addonType:string,type:string,userId:number,projectId?:number}) {
const userProjectQuery = this.buildUserProjectQuery(req.userId, req.projectId);
return await this.repository.findOne({
where: {
addonType: req.addonType,
type: req.type,
userId: req.userId,
projectId: req.projectId
...userProjectQuery,
}
});
}