Files
certd/packages/ui/certd-server/src/controller/pipeline/history-controller.ts
T

215 lines
6.9 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { ALL, Body, Controller, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
2024-10-03 22:03:49 +08:00
import { CommonException, Constants, CrudController, PermissionException } from '@certd/lib-server';
import { PipelineEntity } from '../../modules/pipeline/entity/pipeline.js';
import { HistoryService } from '../../modules/pipeline/service/history-service.js';
import { HistoryLogService } from '../../modules/pipeline/service/history-log-service.js';
import { HistoryEntity } from '../../modules/pipeline/entity/history.js';
import { HistoryLogEntity } from '../../modules/pipeline/entity/history-log.js';
import { PipelineService } from '../../modules/pipeline/service/pipeline-service.js';
2023-06-27 22:45:27 +08:00
import * as fs from 'fs';
2024-11-04 16:39:02 +08:00
import { logger } from '@certd/basic';
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
2024-10-03 22:03:49 +08:00
import { SysSettingsService } from '@certd/lib-server';
2024-10-04 00:52:19 +08:00
import { In } from 'typeorm';
2023-01-29 13:44:19 +08:00
/**
* 证书
*/
@Provide()
@Controller('/api/pi/history')
2023-05-23 18:01:20 +08:00
export class HistoryController extends CrudController<HistoryService> {
2023-01-29 13:44:19 +08:00
@Inject()
service: HistoryService;
@Inject()
2023-06-27 22:45:27 +08:00
pipelineService: PipelineService;
@Inject()
2023-01-29 13:44:19 +08:00
logService: HistoryLogService;
@Inject()
authService: AuthService;
@Inject()
sysSettingsService: SysSettingsService;
2024-10-03 22:03:49 +08:00
getService(): HistoryService {
2023-01-29 13:44:19 +08:00
return this.service;
}
2023-06-27 09:29:43 +08:00
@Post('/page', { summary: Constants.per.authOnly })
2024-10-04 00:52:19 +08:00
async page(@Body(ALL) body: any) {
const isAdmin = await this.authService.isAdmin(this.ctx);
const publicSettings = await this.sysSettingsService.getPublicSettings();
2024-10-04 00:52:19 +08:00
const pipelineQuery: any = {};
if (!(publicSettings.managerOtherUserPipeline && isAdmin)) {
2024-10-10 02:15:05 +08:00
body.query.userId = this.getUserId();
pipelineQuery.userId = this.getUserId();
}
2024-10-04 00:52:19 +08:00
let pipelineIds: any = null;
const pipelineTitle = body.query?.pipelineTitle;
delete body.query.pipelineTitle;
2024-10-04 00:52:19 +08:00
if (pipelineTitle) {
2024-10-14 00:19:55 +08:00
const pipelines = await this.pipelineService.list({
query: pipelineQuery,
buildQuery: qb => {
qb.andWhere('title like :title', { title: `%${pipelineTitle}%` });
2024-10-14 00:19:55 +08:00
},
2024-10-04 00:52:19 +08:00
});
pipelineIds = pipelines.map(p => p.id);
}
const buildQuery = qb => {
if (pipelineIds) {
qb.andWhere({
2024-10-04 00:52:19 +08:00
pipelineId: In(pipelineIds),
});
}
};
2024-10-14 00:19:55 +08:00
const res = await this.service.page({
query: body.query,
page: body.page,
2024-10-14 14:00:24 +08:00
sort: body.sort,
2024-10-14 00:19:55 +08:00
buildQuery,
});
2024-12-03 00:43:43 +08:00
2024-10-04 00:52:19 +08:00
return this.ok(res);
2023-01-29 13:44:19 +08:00
}
2023-06-27 09:29:43 +08:00
@Post('/list', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async list(@Body(ALL) body) {
const isAdmin = await this.authService.isAdmin(this.ctx);
if (!isAdmin) {
2024-12-09 15:59:14 +08:00
body.userId = this.getUserId();
}
2023-01-29 13:44:19 +08:00
if (body.pipelineId == null) {
return this.ok([]);
}
const buildQuery = qb => {
qb.limit(10);
};
2024-10-14 00:19:55 +08:00
const listRet = await this.getService().list({
query: body,
2024-10-14 14:00:24 +08:00
sort: { prop: 'id', asc: false },
2024-10-14 00:19:55 +08:00
buildQuery,
});
2024-12-03 10:32:47 +08:00
for (const item of listRet) {
if (!item.pipeline) {
continue;
}
const json = JSON.parse(item.pipeline);
delete json.stages;
item.pipeline = json;
}
2023-01-29 13:44:19 +08:00
return this.ok(listRet);
}
2023-06-27 09:29:43 +08:00
@Post('/add', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async add(@Body(ALL) bean: PipelineEntity) {
2024-10-10 02:15:05 +08:00
bean.userId = this.getUserId();
2023-01-29 13:44:19 +08:00
return super.add(bean);
}
2023-06-27 09:29:43 +08:00
@Post('/update', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async update(@Body(ALL) bean) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
delete bean.userId;
2023-01-29 13:44:19 +08:00
return super.update(bean);
}
2023-06-27 09:29:43 +08:00
@Post('/save', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async save(@Body(ALL) bean: HistoryEntity) {
2024-10-10 02:15:05 +08:00
bean.userId = this.getUserId();
2023-01-29 13:44:19 +08:00
if (bean.id > 0) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
2023-01-29 13:44:19 +08:00
}
await this.service.save(bean);
return this.ok(bean.id);
}
2023-06-27 09:29:43 +08:00
@Post('/saveLog', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async saveLog(@Body(ALL) bean: HistoryLogEntity) {
2024-10-10 02:15:05 +08:00
bean.userId = this.getUserId();
2023-01-29 13:44:19 +08:00
if (bean.id > 0) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
2023-01-29 13:44:19 +08:00
}
await this.logService.save(bean);
return this.ok(bean.id);
}
2023-06-27 09:29:43 +08:00
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
await super.delete(id);
return this.ok();
}
@Post('/deleteByIds', { summary: Constants.per.authOnly })
async deleteByIds(@Body(ALL) body: any) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), body.ids);
const isAdmin = await this.authService.isAdmin(this.ctx);
2024-10-10 02:15:05 +08:00
const userId = isAdmin ? null : this.getUserId();
await this.getService().deleteByIds(body.ids, userId);
return this.ok();
2023-01-29 13:44:19 +08:00
}
2023-06-27 09:29:43 +08:00
@Post('/detail', { summary: Constants.per.authOnly })
async detail(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
2023-01-29 13:44:19 +08:00
const detail = await this.service.detail(id);
return this.ok(detail);
}
2023-06-27 09:29:43 +08:00
@Post('/logs', { summary: Constants.per.authOnly })
async logs(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.logService, id);
2023-01-29 13:44:19 +08:00
const logInfo = await this.logService.info(id);
return this.ok(logInfo);
}
2023-06-27 22:45:27 +08:00
@Post('/files', { summary: Constants.per.authOnly })
async files(@Query('pipelineId') pipelineId: number, @Query('historyId') historyId: number) {
await this.authService.checkEntityUserId(this.ctx, this.service, historyId);
2023-06-27 22:45:27 +08:00
const files = await this.getFiles(historyId, pipelineId);
return this.ok(files);
}
private async getFiles(historyId, pipelineId) {
let history = null;
if (historyId != null) {
// nothing
history = await this.service.info(historyId);
} else if (pipelineId != null) {
history = await this.service.getLastHistory(pipelineId);
}
if (history == null) {
throw new CommonException('historyId is null');
}
2024-10-10 02:15:05 +08:00
if (history.userId !== this.getUserId()) {
2023-06-27 22:45:27 +08:00
throw new PermissionException();
}
return await this.service.getFiles(history);
}
@Get('/download', { summary: Constants.per.authOnly })
async download(@Query('pipelineId') pipelineId: number, @Query('historyId') historyId: number, @Query('fileId') fileId: string) {
await this.authService.checkEntityUserId(this.ctx, this.service, historyId);
2023-06-27 22:45:27 +08:00
const files = await this.getFiles(historyId, pipelineId);
const file = files.find(f => f.id === fileId);
if (file == null) {
throw new CommonException('file not found');
}
// koa send file
// 下载文件的名称
// const filename = file.filename;
// 要下载的文件的完整路径
const path = file.path;
2024-05-30 10:12:48 +08:00
logger.info(`download:${path}`);
2023-06-27 22:45:27 +08:00
// 以流的形式下载文件
this.ctx.attachment(path);
this.ctx.set('Content-Type', 'application/octet-stream');
return fs.createReadStream(path);
}
2023-01-29 13:44:19 +08:00
}