feat: improve yuntu report sync flow

This commit is contained in:
wxs
2026-04-13 21:08:30 +08:00
parent c8b5d27078
commit bcc5afa291
8 changed files with 383 additions and 346 deletions
@@ -6,90 +6,71 @@ const {
toDatabaseRecord,
} = require('../src/report-service');
function createManualPayload() {
function createResponseInfo() {
return {
reportId: 'report-001',
sourceType: 'MANUAL_CAPTURE',
sourceReportId: null,
aadvid: '1648829117232140',
name: '测试报告',
price: ['1,100', '101,100000'],
rules: [{ keywords: ['奶粉'], op: 'INCLUDE' }],
analysisDims: ['MARKETOVERVIEW'],
categories: [{ id: '20028', name: '奶粉类目' }],
channels: ['ALL'],
startTime: '2025-03-01',
endTime: '2026-02-28',
periodType: 'MONTH',
userName: 'tester@example.com',
payload: {
code: 0,
message: 'success',
data: {
reportId: 'report-001',
status: 'SUCCESS',
name: '测试报告',
startTime: '2025-03-01',
endTime: '2026-02-28',
},
};
}
test('validateAndNormalizeReportInput accepts a valid manual report payload', () => {
const input = createManualPayload();
test('validateAndNormalizeReportInput accepts response info with data.reportId', () => {
const input = createResponseInfo();
const result = validateAndNormalizeReportInput(input);
assert.equal(result.reportId, 'report-001');
assert.equal(result.sourceType, 'MANUAL_CAPTURE');
assert.equal(result.sourceReportId, null);
assert.equal(result.aadvid, '1648829117232140');
assert.equal(result.startTime, '2025-03-01');
assert.equal(result.endTime, '2026-02-28');
assert.deepEqual(result.reportInfo, input);
});
test('validateAndNormalizeReportInput rejects AUTO_COPY without sourceReportId', () => {
test('validateAndNormalizeReportInput accepts response info with data.report_id', () => {
const input = {
...createManualPayload(),
reportId: 'report-002',
sourceType: 'AUTO_COPY',
sourceReportId: '',
code: 0,
data: {
report_id: 'report-002',
},
};
const result = validateAndNormalizeReportInput(input);
assert.equal(result.reportId, 'report-002');
assert.deepEqual(result.reportInfo, input);
});
test('validateAndNormalizeReportInput rejects response info without a report id', () => {
assert.throws(
() => validateAndNormalizeReportInput(input),
() =>
validateAndNormalizeReportInput({
code: 0,
data: {},
}),
(error) => {
assert.equal(error.code, 'VALIDATION_ERROR');
assert.match(error.message, /sourceReportId/i);
assert.match(error.message, /report[_ ]?id/i);
return true;
},
);
});
test('toDatabaseRecord maps API payload fields into database-ready values', () => {
const normalized = validateAndNormalizeReportInput({
...createManualPayload(),
reportId: 'report-003',
sourceType: 'AUTO_COPY',
sourceReportId: 'report-001',
});
test('toDatabaseRecord maps response info into segmented_market_reports columns', () => {
const normalized = validateAndNormalizeReportInput(createResponseInfo());
const record = toDatabaseRecord(normalized);
assert.deepEqual(record, {
report_id: 'report-003',
source_type: 'AUTO_COPY',
source_report_id: 'report-001',
aadvid: '1648829117232140',
name: '测试报告',
price: ['1,100', '101,100000'],
rules: [{ keywords: ['奶粉'], op: 'INCLUDE' }],
analysis_dims: ['MARKETOVERVIEW'],
categories: [{ id: '20028', name: '奶粉类目' }],
transaction_channels: ['ALL'],
start_time: '2025-03-01',
end_time: '2026-02-28',
period_type: 'MONTH',
user_name: 'tester@example.com',
payload: {
name: '测试报告',
startTime: '2025-03-01',
endTime: '2026-02-28',
report_id: 'report-001',
report_info: {
code: 0,
message: 'success',
data: {
reportId: 'report-001',
status: 'SUCCESS',
name: '测试报告',
},
},
});
});
@@ -3,26 +3,13 @@ const assert = require('node:assert/strict');
const { createApp } = require('../src/server');
function createManualPayload() {
function createResponseInfo() {
return {
reportId: 'report-001',
sourceType: 'MANUAL_CAPTURE',
sourceReportId: null,
aadvid: '1648829117232140',
name: '测试报告',
price: ['1,100', '101,100000'],
rules: [{ keywords: ['奶粉'], op: 'INCLUDE' }],
analysisDims: ['MARKETOVERVIEW'],
categories: [{ id: '20028', name: '奶粉类目' }],
channels: ['ALL'],
startTime: '2025-03-01',
endTime: '2026-02-28',
periodType: 'MONTH',
userName: 'tester@example.com',
payload: {
name: '测试报告',
startTime: '2025-03-01',
endTime: '2026-02-28',
code: 0,
message: 'success',
data: {
reportId: 'report-001',
status: 'SUCCESS',
},
};
}
@@ -78,7 +65,7 @@ test('GET /health returns ok status', async () => {
);
});
test('POST /api/reports returns 400 for invalid auto copy payload', async () => {
test('POST /api/reports returns 400 when response info does not contain a report id', async () => {
await withServer(
{
async save() {
@@ -92,9 +79,8 @@ test('POST /api/reports returns 400 for invalid auto copy payload', async () =>
'content-type': 'application/json',
},
body: JSON.stringify({
...createManualPayload(),
sourceType: 'AUTO_COPY',
sourceReportId: '',
code: 0,
data: {},
}),
});
const body = await response.json();
@@ -106,7 +92,7 @@ test('POST /api/reports returns 400 for invalid auto copy payload', async () =>
);
});
test('POST /api/reports persists a valid payload and returns created response', async () => {
test('POST /api/reports persists raw response info and returns created response', async () => {
let savedRecord = null;
await withServer(
@@ -114,19 +100,19 @@ test('POST /api/reports persists a valid payload and returns created response',
async save(record) {
savedRecord = record;
return {
id: 12,
reportId: record.report_id,
created: true,
};
},
},
async (baseUrl) => {
const responseInfo = createResponseInfo();
const response = await fetch(`${baseUrl}/api/reports`, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(createManualPayload()),
body: JSON.stringify(responseInfo),
});
const body = await response.json();
@@ -134,13 +120,14 @@ test('POST /api/reports persists a valid payload and returns created response',
assert.deepEqual(body, {
success: true,
data: {
id: 12,
reportId: 'report-001',
created: true,
},
});
assert.equal(savedRecord.report_id, 'report-001');
assert.equal(savedRecord.source_type, 'MANUAL_CAPTURE');
assert.deepEqual(savedRecord, {
report_id: 'report-001',
report_info: responseInfo,
});
},
);
});