155426b669
Capture raw request for courier track callbacks regardless of Content-Type and persist contentType/rawBody/query into log_third_party for empty-body diagnosis. Co-authored-by: Cursor <cursoragent@cursor.com>
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { Body, Controller, Param, Post, Req, Res } from '@nestjs/common';
|
|
import type { Request, Response } from 'express';
|
|
import { DeliveryCallbackService } from './delivery-callback.service';
|
|
|
|
type CourierCallbackRequest = Request & { rawBody?: Buffer };
|
|
|
|
@Controller('callbacks')
|
|
export class DeliveryCallbackController {
|
|
constructor(private readonly deliveryCallbackService: DeliveryCallbackService) {}
|
|
|
|
/** 小飞侠/物流路由变化回调(适配器入口) */
|
|
@Post('courier/:provider/track')
|
|
async trackByProvider(
|
|
@Param('provider') provider: string,
|
|
@Body() body: unknown,
|
|
@Req() req: CourierCallbackRequest,
|
|
@Res() res: Response,
|
|
) {
|
|
const result = await this.deliveryCallbackService.handleTrackCallback(
|
|
provider,
|
|
body,
|
|
`/api/v1/callbacks/courier/${provider}/track`,
|
|
this.buildRequestMeta(req),
|
|
);
|
|
// 直出承运商约定结构,避免被全局 { code:0, data } 包装
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
/** 兼容旧路径,默认按小飞侠解析 */
|
|
@Post('delivery/track')
|
|
async trackLegacy(
|
|
@Body() body: unknown,
|
|
@Req() req: CourierCallbackRequest,
|
|
@Res() res: Response,
|
|
) {
|
|
const result = await this.deliveryCallbackService.handleTrackCallback(
|
|
'xfx',
|
|
body,
|
|
'/api/v1/callbacks/delivery/track',
|
|
this.buildRequestMeta(req),
|
|
);
|
|
return res.status(200).json(result);
|
|
}
|
|
|
|
private buildRequestMeta(req: CourierCallbackRequest) {
|
|
const contentType = req.headers['content-type'];
|
|
return {
|
|
contentType: Array.isArray(contentType) ? contentType.join(', ') : contentType || null,
|
|
rawBody: req.rawBody?.toString('utf8') ?? null,
|
|
query:
|
|
req.query && typeof req.query === 'object'
|
|
? (req.query as Record<string, unknown>)
|
|
: undefined,
|
|
};
|
|
}
|
|
}
|