certd/packages/ui/certd-server/src/modules/monitor/service/cert-info-service.ts

63 lines
1.5 KiB
TypeScript

import { Provide } from '@midwayjs/core';
import { BaseService, PageReq } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CertInfoEntity } from '../entity/cert-info.js';
@Provide()
export class CertInfoService extends BaseService<CertInfoEntity> {
@InjectEntityModel(CertInfoEntity)
repository: Repository<CertInfoEntity>;
//@ts-ignore
getRepository() {
return this.repository;
}
async page(pageReq: PageReq<CertInfoEntity>) {
return await super.page(pageReq);
}
async getUserDomainCount(userId: number) {
if (!userId) {
throw new Error('userId is required');
}
return await this.repository.sum('domainCount', {
userId,
});
}
async updateDomains(pipelineId: number, userId: number, domains: string[]) {
const found = await this.repository.findOne({
where: {
pipelineId,
userId,
},
});
const bean = new CertInfoEntity();
if (found) {
//bean
bean.id = found.id;
} else {
//create
bean.pipelineId = pipelineId;
bean.userId = userId;
if (!domains || domains.length === 0) {
return;
}
}
if (!domains || domains.length === 0) {
bean.domain = '';
bean.domains = '';
bean.domainCount = 0;
} else {
bean.domain = domains[0];
bean.domains = domains.join(',');
bean.domainCount = domains.length;
}
await this.addOrUpdate(bean);
}
}