Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 适应区块新方案 #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 50 additions & 32 deletions app/controller/material-center/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
*
*/
import { Controller } from 'egg';
import { throwApiError } from '../../lib/ApiError';
import { E_ErrorCode, E_MaterialErrorCode, E_TASK_STATUS, E_TASK_TYPE, E_Public } from '../../lib/enum';
import { apiError } from '../../lib/ApiError';
import { createBlockRule, updateBlockRule, buildBlockRule } from '../../validate/material-center/block';
import { apiError, throwApiError } from '../../lib/ApiError';
import { E_ErrorCode, E_MaterialErrorCode, E_Public } from '../../lib/enum';
import { buildBlockRule, createBlockRule, updateBlockRule } from '../../validate/material-center/block';


export default class BlockController extends Controller {
Expand All @@ -30,7 +29,7 @@ export default class BlockController extends Controller {
}
async list() {
const { query } = this.ctx.request;
const {appId} = query;
const { appId } = query;
if (appId && !/^[1-9]+[0-9]*$/.test(appId)) {
delete query.appId;
}
Expand All @@ -40,7 +39,7 @@ export default class BlockController extends Controller {
async getBlocks() {
this.ctx.body = await this.service.materialCenter.block.getBlocks(this.ctx.queries);
}

async find() {
this.ctx.body = await this.service.materialCenter.block.find(this.ctx.queries);
}
Expand All @@ -52,9 +51,9 @@ export default class BlockController extends Controller {
async findById() {
const { id } = this.ctx.params;
this.ctx.validate({
id: 'id'
id: 'id'
},
{id}
{ id }
);
this.ctx.body = await this.service.materialCenter.block.findById(id);
}
Expand Down Expand Up @@ -91,7 +90,7 @@ export default class BlockController extends Controller {
this.ctx.body = this.ctx.helper.getResponseData(null, error);
}
}

async create() {
const { body } = this.ctx.request;
this.ctx.validate(
Expand Down Expand Up @@ -130,7 +129,7 @@ export default class BlockController extends Controller {
// }
this.ctx.body = await this.service.materialCenter.block.create(createParam);
}

async update() {
const { id } = this.ctx.params;
const { body } = this.ctx.request;
Expand Down Expand Up @@ -181,10 +180,10 @@ export default class BlockController extends Controller {
async build() {
// post参数校验二次丰富
this.ctx.validate(buildBlockRule);
const { deploy_info: message, block, version, needToSave = false } = this.ctx.request.body;
const { materialCenter, task } = this.ctx.service;
const { deploy_info: message, block, version } = this.ctx.request.body;
const { materialCenter } = this.ctx.service;
const { content } = block;
Comment on lines +183 to 185
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add validation for 'block' and 'version' in the 'build' method

The block and version parameters are destructured from this.ctx.request.body without validation. If either block or version is undefined, it could lead to runtime errors when accessing their properties.

Apply this diff to add validation:

+ this.ctx.validate({
+   block: 'object',
+   version: 'string',
+ }, this.ctx.request.body);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { deploy_info: message, block, version } = this.ctx.request.body;
const { materialCenter } = this.ctx.service;
const { content } = block;
this.ctx.validate({
block: 'object',
version: 'string',
}, this.ctx.request.body);
const { deploy_info: message, block, version } = this.ctx.request.body;
const { materialCenter } = this.ctx.service;
const { content } = block;


if (!content) {
throwApiError('', Number(E_ErrorCode.BadRequest), E_MaterialErrorCode.CM204);
}
Expand All @@ -202,29 +201,48 @@ export default class BlockController extends Controller {
block.screenshot = '';
// 更新i18n 信息
block.i18n = await materialCenter.block.getBlockI18n(id);
// 如果有未完成的任务直接返回该任务信息
let taskInfo: any = await task.getUnfinishedTask(E_TASK_TYPE.BLOCK_BUILD, id);
if (taskInfo?.data) {
this.ctx.body = taskInfo;
return;
}

const newTask = {
taskTypeId: E_TASK_TYPE.BLOCK_BUILD,
taskStatus: E_TASK_STATUS.INIT,
uniqueId: id
// 新模式不执行构建,直接更新数据库
// 新增区块历史
const { screenshot, path, description, label } = block;
const build_info = {
result: true,
versions: [version],
};
taskInfo = await task.create(newTask);
// 执行构建
const body = { message, block, version, needToSave };
await this.service.materialCenter.blockBuilder.start(id, taskInfo.data.id, body )
this.ctx.body = taskInfo;

const assets = {
material: [],
scripts: [],
styles: [],
};
const historyParam = {
message,
content,
assets,
build_info,
screenshot,
path,
description,
label,
version,
block_id: block.id,
i18n: block.i18n ?? null,
created_app: block.created_app,
};
const history = await materialCenter.blockHistory.create(historyParam);
// 更新区块
const { data: oldBlock } = await materialCenter.block.findById(block.id);
block.last_build_info = build_info;
block.assets = assets;
const blockResult = await materialCenter.block.update({
...block,
histories: [...oldBlock.histories.map(({ id }) => id), history.data.id]
});
this.ctx.body = blockResult;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure 'oldBlock.histories' is defined before mapping

In line 238, the code assumes that oldBlock.histories is defined when mapping to extract ids. If oldBlock.histories is undefined, this will cause a runtime error.

Apply this diff to handle undefined histories:

- histories: [...oldBlock.histories.map(({ id }) => id), history.data.id]
+ const existingHistories = oldBlock.histories ? oldBlock.histories.map(({ id }) => id) : [];
+ histories: [...existingHistories, history.data.id]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { data: oldBlock } = await materialCenter.block.findById(block.id);
block.last_build_info = build_info;
block.assets = assets;
const blockResult = await materialCenter.block.update({
...block,
histories: [...oldBlock.histories.map(({ id }) => id), history.data.id]
});
this.ctx.body = blockResult;
const { data: oldBlock } = await materialCenter.block.findById(block.id);
block.last_build_info = build_info;
block.assets = assets;
const blockResult = await materialCenter.block.update({
...block,
const existingHistories = oldBlock.histories ? oldBlock.histories.map(({ id }) => id) : [];
histories: [...existingHistories, history.data.id]
});
this.ctx.body = blockResult;

}

private async ensureBlockId(blockData): Promise<number | string> {
const { auth, materialCenter } = this.ctx.service;
const { id, label, framework } = blockData;
const { id, label, framework } = blockData;
if (id) {
return id;
}
Expand All @@ -244,5 +262,5 @@ export default class BlockController extends Controller {
const newBlock = await materialCenter.block.create(blockData);
return newBlock.data.id ?? 0;
}

}
Loading