2023-01-29 13:44:19 +08:00
|
|
|
import {
|
|
|
|
|
ALL,
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
2023-06-27 22:45:27 +08:00
|
|
|
Get,
|
2023-01-29 13:44:19 +08:00
|
|
|
Inject,
|
|
|
|
|
Post,
|
|
|
|
|
Provide,
|
|
|
|
|
Query,
|
|
|
|
|
} from '@midwayjs/decorator';
|
|
|
|
|
import { CrudController } from '../../../basic/crud-controller';
|
|
|
|
|
import { PipelineEntity } from '../entity/pipeline';
|
|
|
|
|
import { HistoryService } from '../service/history-service';
|
|
|
|
|
import { HistoryLogService } from '../service/history-log-service';
|
|
|
|
|
import { HistoryEntity } from '../entity/history';
|
|
|
|
|
import { HistoryLogEntity } from '../entity/history-log';
|
2023-06-27 22:45:27 +08:00
|
|
|
import { Constants } from '../../../basic/constants';
|
|
|
|
|
import { PipelineService } from '../service/pipeline-service';
|
|
|
|
|
import { CommonException } from '../../../basic/exception/common-exception';
|
|
|
|
|
import { PermissionException } from '../../../basic/exception/permission-exception';
|
|
|
|
|
import * as fs from 'fs';
|
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;
|
|
|
|
|
|
|
|
|
|
getService() {
|
|
|
|
|
return this.service;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/page', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async page(@Body(ALL) body) {
|
|
|
|
|
body.query.userId = this.ctx.user.id;
|
|
|
|
|
return super.page(body);
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
body.userId = this.ctx.user.id;
|
|
|
|
|
if (body.pipelineId == null) {
|
|
|
|
|
return this.ok([]);
|
|
|
|
|
}
|
|
|
|
|
const buildQuery = qb => {
|
|
|
|
|
qb.limit(10);
|
|
|
|
|
};
|
|
|
|
|
const listRet = await this.getService().list(
|
|
|
|
|
body,
|
|
|
|
|
{ prop: 'id', asc: false },
|
|
|
|
|
buildQuery
|
|
|
|
|
);
|
|
|
|
|
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) {
|
|
|
|
|
bean.userId = this.ctx.user.id;
|
|
|
|
|
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.service.checkUserId(bean.id, this.ctx.user.id);
|
|
|
|
|
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) {
|
|
|
|
|
bean.userId = this.ctx.user.id;
|
|
|
|
|
if (bean.id > 0) {
|
|
|
|
|
await this.service.checkUserId(bean.id, this.ctx.user.id);
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
bean.userId = this.ctx.user.id;
|
|
|
|
|
if (bean.id > 0) {
|
|
|
|
|
await this.service.checkUserId(bean.id, this.ctx.user.id);
|
|
|
|
|
}
|
|
|
|
|
await this.logService.save(bean);
|
|
|
|
|
return this.ok(bean.id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/delete', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async delete(@Query('id') id) {
|
|
|
|
|
await this.service.checkUserId(id, this.ctx.user.id);
|
|
|
|
|
return super.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/detail', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async detail(@Query('id') id) {
|
|
|
|
|
await this.service.checkUserId(id, this.ctx.user.id);
|
|
|
|
|
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 })
|
2023-01-29 13:44:19 +08:00
|
|
|
async logs(@Query('id') id) {
|
|
|
|
|
await this.logService.checkUserId(id, this.ctx.user.id);
|
|
|
|
|
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, @Query('historyId') historyId) {
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
if (history.userId !== this.ctx.user.id) {
|
|
|
|
|
throw new PermissionException();
|
|
|
|
|
}
|
|
|
|
|
return await this.service.getFiles(history);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get('/download', { summary: Constants.per.authOnly })
|
|
|
|
|
async download(
|
|
|
|
|
@Query('pipelineId') pipelineId,
|
|
|
|
|
@Query('historyId') historyId,
|
|
|
|
|
@Query('fileId') fileId
|
|
|
|
|
) {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 以流的形式下载文件
|
|
|
|
|
this.ctx.attachment(path);
|
|
|
|
|
this.ctx.set('Content-Type', 'application/octet-stream');
|
|
|
|
|
|
|
|
|
|
return fs.createReadStream(path);
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|