feat: update export automation scripts
This commit is contained in:
@@ -44,14 +44,24 @@ function extractReportId(reportInfo) {
|
||||
);
|
||||
}
|
||||
|
||||
function extractReportInfo(input) {
|
||||
if (!input.payload) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return ensureObject(input.payload, 'payload');
|
||||
}
|
||||
|
||||
function validateAndNormalizeReportInput(input) {
|
||||
const reportInfo = ensureObject(input, 'body');
|
||||
const reportId = extractReportId(reportInfo);
|
||||
const body = ensureObject(input, 'body');
|
||||
const reportId = extractReportId(body);
|
||||
|
||||
if (!reportId) {
|
||||
throw new ValidationError('report_id is required in response info');
|
||||
}
|
||||
|
||||
const reportInfo = extractReportInfo(body);
|
||||
|
||||
return {
|
||||
reportId,
|
||||
reportInfo,
|
||||
@@ -68,6 +78,7 @@ function toDatabaseRecord(normalizedReport) {
|
||||
module.exports = {
|
||||
ValidationError,
|
||||
extractReportId,
|
||||
extractReportInfo,
|
||||
validateAndNormalizeReportInput,
|
||||
toDatabaseRecord,
|
||||
};
|
||||
|
||||
@@ -18,6 +18,31 @@ function createResponseInfo() {
|
||||
};
|
||||
}
|
||||
|
||||
function createPersistRequest() {
|
||||
return {
|
||||
reportId: 'report-001',
|
||||
sourceType: 'MANUAL_CAPTURE',
|
||||
sourceReportId: null,
|
||||
aadvid: '1648829117232140',
|
||||
payload: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('validateAndNormalizeReportInput accepts persist request with payload', () => {
|
||||
const input = createPersistRequest();
|
||||
|
||||
const result = validateAndNormalizeReportInput(input);
|
||||
|
||||
assert.equal(result.reportId, 'report-001');
|
||||
assert.deepEqual(result.reportInfo, input.payload);
|
||||
});
|
||||
|
||||
test('validateAndNormalizeReportInput accepts response info with data.reportId', () => {
|
||||
const input = createResponseInfo();
|
||||
|
||||
@@ -74,3 +99,20 @@ test('toDatabaseRecord maps response info into segmented_market_reports columns'
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('toDatabaseRecord maps persist request payload into segmented_market_reports report_info', () => {
|
||||
const normalized = validateAndNormalizeReportInput(createPersistRequest());
|
||||
|
||||
const record = toDatabaseRecord(normalized);
|
||||
|
||||
assert.deepEqual(record, {
|
||||
report_id: 'report-001',
|
||||
report_info: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,22 @@ function createResponseInfo() {
|
||||
};
|
||||
}
|
||||
|
||||
function createPersistRequest() {
|
||||
return {
|
||||
reportId: 'report-001',
|
||||
sourceType: 'MANUAL_CAPTURE',
|
||||
sourceReportId: null,
|
||||
aadvid: '1648829117232140',
|
||||
payload: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function withServer(repository, callback) {
|
||||
const app = createApp({
|
||||
repository,
|
||||
@@ -131,3 +147,43 @@ test('POST /api/reports persists raw response info and returns created response'
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('POST /api/reports persists persist request payload as report_info', async () => {
|
||||
let savedRecord = null;
|
||||
|
||||
await withServer(
|
||||
{
|
||||
async save(record) {
|
||||
savedRecord = record;
|
||||
return {
|
||||
reportId: record.report_id,
|
||||
created: true,
|
||||
};
|
||||
},
|
||||
},
|
||||
async (baseUrl) => {
|
||||
const persistRequest = createPersistRequest();
|
||||
const response = await fetch(`${baseUrl}/api/reports`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(persistRequest),
|
||||
});
|
||||
const body = await response.json();
|
||||
|
||||
assert.equal(response.status, 201);
|
||||
assert.deepEqual(body, {
|
||||
success: true,
|
||||
data: {
|
||||
reportId: 'report-001',
|
||||
created: true,
|
||||
},
|
||||
});
|
||||
assert.deepEqual(savedRecord, {
|
||||
report_id: 'report-001',
|
||||
report_info: persistRequest.payload,
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -199,19 +199,53 @@ test("buildAutoCopyPayload deep clones payload, shifts top-level dates, and appe
|
||||
assert.notEqual(copied.nested, original.nested);
|
||||
});
|
||||
|
||||
test("buildPersistRequest keeps the response info unchanged for backend parsing", () => {
|
||||
const responseInfo = {
|
||||
code: 0,
|
||||
message: "success",
|
||||
data: {
|
||||
reportId: "report-001",
|
||||
status: "SUCCESS",
|
||||
},
|
||||
test("buildPersistRequest sends create payload and metadata to the backend", () => {
|
||||
const payload = {
|
||||
name: "测试报告",
|
||||
startTime: "2025-03-01",
|
||||
endTime: "2026-02-28",
|
||||
categories: [],
|
||||
analysisDims: ["MARKETOVERVIEW"],
|
||||
};
|
||||
|
||||
const request = api.buildPersistRequest(responseInfo);
|
||||
const request = api.buildPersistRequest({
|
||||
payload,
|
||||
aadvid: "1648829117232140",
|
||||
reportId: "report-001",
|
||||
sourceType: "MANUAL_CAPTURE",
|
||||
sourceReportId: null,
|
||||
});
|
||||
|
||||
assert.deepEqual(request, responseInfo);
|
||||
assert.notEqual(request, responseInfo);
|
||||
assert.notEqual(request.data, responseInfo.data);
|
||||
assert.deepEqual(request, {
|
||||
reportId: "report-001",
|
||||
sourceType: "MANUAL_CAPTURE",
|
||||
sourceReportId: null,
|
||||
aadvid: "1648829117232140",
|
||||
payload: {
|
||||
name: "测试报告",
|
||||
startTime: "2025-03-01",
|
||||
endTime: "2026-02-28",
|
||||
categories: [],
|
||||
analysisDims: ["MARKETOVERVIEW"],
|
||||
},
|
||||
});
|
||||
assert.notEqual(request.payload, payload);
|
||||
});
|
||||
|
||||
test("buildPersistRequest records auto-copy source report id", () => {
|
||||
const request = api.buildPersistRequest({
|
||||
payload: {
|
||||
name: "同比报告",
|
||||
startTime: "2024-03-01",
|
||||
endTime: "2025-02-28",
|
||||
},
|
||||
aadvid: "1648829117232140",
|
||||
reportId: "report-002",
|
||||
sourceType: "AUTO_COPY",
|
||||
sourceReportId: "report-001",
|
||||
});
|
||||
|
||||
assert.equal(request.reportId, "report-002");
|
||||
assert.equal(request.sourceType, "AUTO_COPY");
|
||||
assert.equal(request.sourceReportId, "report-001");
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// @namespace https://yuntu.oceanengine.com/
|
||||
// @version 0.1.0
|
||||
// @description 记录最近一次成功创建的云图报告,并一键复制同比报告
|
||||
// @author wangxi
|
||||
// @author wangxuesheng
|
||||
// @match https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketList*
|
||||
// @match https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketcreation*
|
||||
// @match https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketDetail/*
|
||||
@@ -162,8 +162,20 @@
|
||||
return cloned;
|
||||
}
|
||||
|
||||
function buildPersistRequest(responseJson) {
|
||||
return deepCloneJson(responseJson);
|
||||
function buildPersistRequest({
|
||||
payload,
|
||||
aadvid,
|
||||
reportId,
|
||||
sourceType,
|
||||
sourceReportId,
|
||||
}) {
|
||||
return {
|
||||
reportId: String(reportId),
|
||||
sourceType,
|
||||
sourceReportId: sourceReportId ? String(sourceReportId) : null,
|
||||
aadvid: String(aadvid),
|
||||
payload: deepCloneJson(payload),
|
||||
};
|
||||
}
|
||||
|
||||
function parseJson(text) {
|
||||
@@ -320,7 +332,7 @@
|
||||
let lastError = null;
|
||||
|
||||
for (const base of bases) {
|
||||
try {
|
||||
try {
|
||||
if (typeof GM_xmlhttpRequest !== "function") {
|
||||
const response = await fetch(`${base}/api/reports`, {
|
||||
method: "POST",
|
||||
@@ -424,6 +436,7 @@
|
||||
try {
|
||||
const nextPayload = buildAutoCopyPayload(payload);
|
||||
const requestUrl = getCreateRequestUrl(meta);
|
||||
const sourceReportId = meta.reportId || null;
|
||||
const { responseJson } = await createReportThroughPage(requestUrl, nextPayload);
|
||||
const reportId = extractReportId(responseJson);
|
||||
|
||||
@@ -440,7 +453,15 @@
|
||||
};
|
||||
|
||||
writeStoredData(nextPayload, nextMeta);
|
||||
await persistToLocalServer(buildPersistRequest(responseJson));
|
||||
await persistToLocalServer(
|
||||
buildPersistRequest({
|
||||
payload: nextPayload,
|
||||
aadvid: nextMeta.aadvid,
|
||||
reportId,
|
||||
sourceType: "AUTO_COPY",
|
||||
sourceReportId,
|
||||
}),
|
||||
);
|
||||
|
||||
showToast(`同比报告创建成功:${reportId}`, "success");
|
||||
} catch (error) {
|
||||
@@ -486,7 +507,15 @@
|
||||
writeStoredData(payload, meta);
|
||||
updateButtonState();
|
||||
|
||||
await persistToLocalServer(buildPersistRequest(responseJson));
|
||||
await persistToLocalServer(
|
||||
buildPersistRequest({
|
||||
payload,
|
||||
aadvid,
|
||||
reportId,
|
||||
sourceType: "MANUAL_CAPTURE",
|
||||
sourceReportId: null,
|
||||
}),
|
||||
);
|
||||
|
||||
showToast(`已记录报告配置:${reportId}`, "success");
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user