refactor(api): 统一接口参数为对象形式并支持多条件查询
重构所有接口方法,将直接传递id参数改为接受where条件对象 支持通过id、sku、email等多条件查询实体 优化产品服务逻辑,支持通过sku直接查询产品 统一各适配器实现,确保接口一致性
This commit is contained in:
parent
d549227e03
commit
f867f256ae
|
|
@ -135,8 +135,11 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return data
|
||||
}
|
||||
|
||||
async getCustomer(id: string | number): Promise<UnifiedCustomerDTO> {
|
||||
const customer = await this.shopyyService.getCustomer(this.site, id);
|
||||
async getCustomer(where: {id?: string | number,email?: string,phone?: string}): Promise<UnifiedCustomerDTO> {
|
||||
if(!where.id && !where.email && !where.phone){
|
||||
throw new Error('必须传入 id 或 email 或 phone')
|
||||
}
|
||||
const customer = await this.shopyyService.getCustomer(this.site, where.id);
|
||||
return this.mapPlatformToUnifiedCustomer(customer);
|
||||
}
|
||||
|
||||
|
|
@ -162,13 +165,13 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return this.mapPlatformToUnifiedCustomer(createdCustomer);
|
||||
}
|
||||
|
||||
async updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
|
||||
const updatedCustomer = await this.shopyyService.updateCustomer(this.site, id, data);
|
||||
async updateCustomer(where: {id: string | number}, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
|
||||
const updatedCustomer = await this.shopyyService.updateCustomer(this.site, where.id, data);
|
||||
return this.mapPlatformToUnifiedCustomer(updatedCustomer);
|
||||
}
|
||||
|
||||
async deleteCustomer(id: string | number): Promise<boolean> {
|
||||
return await this.shopyyService.deleteCustomer(this.site, id);
|
||||
async deleteCustomer(where: {id: string | number}): Promise<boolean> {
|
||||
return await this.shopyyService.deleteCustomer(this.site, where.id);
|
||||
}
|
||||
|
||||
batchProcessCustomers?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> {
|
||||
|
|
@ -221,13 +224,13 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return this.mapPlatformToUnifiedMedia(createdMedia);
|
||||
}
|
||||
|
||||
async updateMedia(id: string | number, data: any): Promise<UnifiedMediaDTO> {
|
||||
const updatedMedia = await this.shopyyService.updateMedia(this.site, id, data);
|
||||
async updateMedia(where: {id: string | number}, data: any): Promise<UnifiedMediaDTO> {
|
||||
const updatedMedia = await this.shopyyService.updateMedia(this.site, where.id, data);
|
||||
return this.mapPlatformToUnifiedMedia(updatedMedia);
|
||||
}
|
||||
|
||||
async deleteMedia(id: string | number): Promise<boolean> {
|
||||
return await this.shopyyService.deleteMedia(this.site, id);
|
||||
async deleteMedia(where: {id: string | number}): Promise<boolean> {
|
||||
return await this.shopyyService.deleteMedia(this.site, where.id);
|
||||
}
|
||||
|
||||
mapMediaSearchParams(params: UnifiedSearchParamsDTO): any {
|
||||
|
|
@ -543,8 +546,8 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return params;
|
||||
}
|
||||
|
||||
async getOrder(id: string | number): Promise<UnifiedOrderDTO> {
|
||||
const data = await this.shopyyService.getOrder(this.site.id, String(id));
|
||||
async getOrder(where: {id: string | number}): Promise<UnifiedOrderDTO> {
|
||||
const data = await this.shopyyService.getOrder(this.site.id, String(where.id));
|
||||
return this.mapPlatformToUnifiedOrder(data);
|
||||
}
|
||||
|
||||
|
|
@ -602,10 +605,10 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return this.mapPlatformToUnifiedOrder(createdOrder);
|
||||
}
|
||||
|
||||
async updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
|
||||
async updateOrder(where: {id: string | number}, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
|
||||
// 使用映射方法转换参数
|
||||
const requestParams = this.mapUpdateOrderParams(data);
|
||||
return await this.shopyyService.updateOrder(this.site, String(id), requestParams);
|
||||
return await this.shopyyService.updateOrder(this.site, String(where.id), requestParams);
|
||||
}
|
||||
|
||||
async deleteOrder(where: {id: string | number}): Promise<boolean> {
|
||||
|
|
@ -890,10 +893,18 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return params;
|
||||
}
|
||||
|
||||
async getProduct(id: string | number): Promise<UnifiedProductDTO> {
|
||||
// 使用ShopyyService获取单个产品
|
||||
const product = await this.shopyyService.getProduct(this.site, id);
|
||||
return this.mapPlatformToUnifiedProduct(product);
|
||||
async getProduct(where: {id?: string | number, sku?: string}): Promise<UnifiedProductDTO> {
|
||||
if(!where.id && !where.sku){
|
||||
throw new Error('必须传入 id 或 sku')
|
||||
}
|
||||
if (where.id) {
|
||||
// 使用ShopyyService获取单个产品
|
||||
const product = await this.shopyyService.getProduct(this.site, where.id);
|
||||
return this.mapPlatformToUnifiedProduct(product);
|
||||
} else if (where.sku) {
|
||||
// 通过sku获取产品
|
||||
return this.getProductBySku(where.sku);
|
||||
}
|
||||
}
|
||||
|
||||
async getProducts(
|
||||
|
|
@ -951,24 +962,50 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return this.mapPlatformToUnifiedProduct(res);
|
||||
}
|
||||
|
||||
async updateProduct(id: string | number, data: Partial<UnifiedProductDTO>): Promise<boolean> {
|
||||
// Shopyy update returns boolean?
|
||||
// shopyyService.updateProduct returns boolean.
|
||||
// So I can't return the updated product.
|
||||
// I have to fetch it again or return empty/input.
|
||||
// Since getProduct is missing, I'll return input data as UnifiedProductDTO (mock).
|
||||
async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
|
||||
let productId: string;
|
||||
if (where.id) {
|
||||
productId = String(where.id);
|
||||
} else if (where.sku) {
|
||||
// 通过sku获取产品ID
|
||||
const product = await this.getProductBySku(where.sku);
|
||||
productId = String(product.id);
|
||||
} else {
|
||||
throw new Error('必须提供id或sku参数');
|
||||
}
|
||||
// 使用映射方法转换参数
|
||||
const requestParams = this.mapUpdateProductParams(data);
|
||||
await this.shopyyService.updateProduct(this.site, String(id), requestParams);
|
||||
await this.shopyyService.updateProduct(this.site, productId, requestParams);
|
||||
return true;
|
||||
}
|
||||
|
||||
async deleteProduct(id: string | number): Promise<boolean> {
|
||||
async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
|
||||
let productId: string | number;
|
||||
if (where.id) {
|
||||
productId = where.id;
|
||||
} else if (where.sku) {
|
||||
// 通过sku获取产品ID
|
||||
const product = await this.getProductBySku(where.sku);
|
||||
productId = product.id;
|
||||
} else {
|
||||
throw new Error('必须提供id或sku参数');
|
||||
}
|
||||
// Use batch delete
|
||||
await this.shopyyService.batchProcessProducts(this.site, { delete: [id] });
|
||||
await this.shopyyService.batchProcessProducts(this.site, { delete: [productId] });
|
||||
return true;
|
||||
}
|
||||
|
||||
// 通过sku获取产品详情的私有方法
|
||||
private async getProductBySku(sku: string): Promise<UnifiedProductDTO> {
|
||||
// 使用Shopyy API的搜索功能通过sku查询产品
|
||||
const response = await this.shopyyService.getProducts(this.site, 1, 100);
|
||||
const product = response.items.find((item: any) => item.sku === sku);
|
||||
if (!product) {
|
||||
throw new Error(`未找到sku为${sku}的产品`);
|
||||
}
|
||||
return this.mapPlatformToUnifiedProduct(product);
|
||||
}
|
||||
|
||||
async batchProcessProducts(
|
||||
data: { create?: any[]; update?: any[]; delete?: Array<string | number> }
|
||||
): Promise<any> {
|
||||
|
|
@ -980,9 +1017,6 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 评论映射方法 ==========
|
||||
mapPlatformToUnifiedReview(data: any): UnifiedReviewDTO {
|
||||
return data
|
||||
}
|
||||
|
||||
mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) {
|
||||
return data
|
||||
|
|
@ -1020,19 +1054,19 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
|
||||
async createReview(data: any): Promise<UnifiedReviewDTO> {
|
||||
const createdReview = await this.shopyyService.createReview(this.site, data);
|
||||
return this.mapReview(createdReview);
|
||||
return this.mapPlatformToUnifiedReview(createdReview);
|
||||
}
|
||||
|
||||
async updateReview(id: string | number, data: any): Promise<UnifiedReviewDTO> {
|
||||
const updatedReview = await this.shopyyService.updateReview(this.site, id, data);
|
||||
return this.mapReview(updatedReview);
|
||||
async updateReview(where: {id: string | number}, data: any): Promise<UnifiedReviewDTO> {
|
||||
const updatedReview = await this.shopyyService.updateReview(this.site, where.id, data);
|
||||
return this.mapPlatformToUnifiedReview(updatedReview);
|
||||
}
|
||||
|
||||
async deleteReview(id: string | number): Promise<boolean> {
|
||||
return await this.shopyyService.deleteReview(this.site, id);
|
||||
async deleteReview(where: {id: string | number}): Promise<boolean> {
|
||||
return await this.shopyyService.deleteReview(this.site, where.id);
|
||||
}
|
||||
|
||||
mapReview(review: any): UnifiedReviewDTO {
|
||||
mapPlatformToUnifiedReview(review: any): UnifiedReviewDTO {
|
||||
// 将ShopYY评论数据映射到统一评论DTO格式
|
||||
return {
|
||||
id: review.id || review.review_id,
|
||||
|
|
@ -1187,9 +1221,7 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== Webhook映射方法 ==========
|
||||
mapPlatformToUnifiedWebhook(data: any): UnifiedWebhookDTO {
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) {
|
||||
return data
|
||||
|
|
@ -1203,9 +1235,9 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return data
|
||||
}
|
||||
|
||||
async getWebhook(id: string | number): Promise<UnifiedWebhookDTO> {
|
||||
const webhook = await this.shopyyService.getWebhook(this.site, id);
|
||||
return this.mapWebhook(webhook);
|
||||
async getWebhook(where: {id: string | number}): Promise<UnifiedWebhookDTO> {
|
||||
const webhook = await this.shopyyService.getWebhook(this.site, where.id);
|
||||
return this.mapPlatformToUnifiedWebhook(webhook);
|
||||
}
|
||||
|
||||
async getWebhooks(params: UnifiedSearchParamsDTO): Promise<UnifiedWebhookPaginationDTO> {
|
||||
|
|
@ -1238,7 +1270,7 @@ export class ShopyyAdapter implements ISiteAdapter {
|
|||
return await this.shopyyService.deleteWebhook(this.site, where.id);
|
||||
}
|
||||
|
||||
mapWebhook(item: ShopyyWebhook): UnifiedWebhookDTO {
|
||||
mapPlatformToUnifiedWebhook(item: ShopyyWebhook): UnifiedWebhookDTO {
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.webhook_name || `Webhook-${item.id}`,
|
||||
|
|
|
|||
|
|
@ -46,6 +46,9 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
this.mapPlatformToUnifiedOrder = this.mapPlatformToUnifiedOrder.bind(this);
|
||||
this.mapPlatformToUnifiedWebhook = this.mapPlatformToUnifiedWebhook.bind(this);
|
||||
}
|
||||
mapUnifiedToPlatformCustomer(data: Partial<UnifiedCustomerDTO>) {
|
||||
return data
|
||||
}
|
||||
batchProcessProducts?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
|
@ -57,14 +60,8 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 客户映射方法 ==========
|
||||
mapPlatformToUnifiedCustomer(data: any): UnifiedCustomerDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformCustomer(data: Partial<UnifiedCustomerDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
||||
mapCustomer(item: WooCustomer): UnifiedCustomerDTO {
|
||||
mapPlatformToUnifiedCustomer(item: WooCustomer): UnifiedCustomerDTO {
|
||||
// 将 WooCommerce 客户数据映射为统一客户DTO
|
||||
// 包含基础信息地址信息与时间信息
|
||||
return {
|
||||
|
|
@ -153,10 +150,10 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// 客户操作方法
|
||||
async getCustomer(id: string | number): Promise<UnifiedCustomerDTO> {
|
||||
async getCustomer(where: {id: string | number}): Promise<UnifiedCustomerDTO> {
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.get(`customers/${id}`);
|
||||
return this.mapCustomer(res.data);
|
||||
const res = await api.get(`customers/${where.id}`);
|
||||
return this.mapPlatformToUnifiedCustomer(res.data);
|
||||
}
|
||||
|
||||
async getCustomers(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedCustomerDTO>> {
|
||||
|
|
@ -167,7 +164,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
requestParams
|
||||
);
|
||||
return {
|
||||
items: items.map((i: any) => this.mapCustomer(i)),
|
||||
items: items.map((i: any) => this.mapPlatformToUnifiedCustomer(i)),
|
||||
total,
|
||||
totalPages,
|
||||
page,
|
||||
|
|
@ -184,36 +181,33 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
const requestParams = this.mapCustomerSearchParams(params || {});
|
||||
|
||||
const customers = await this.wpService.sdkGetAll(api, 'customers', requestParams);
|
||||
return customers.map((customer: any) => this.mapCustomer(customer));
|
||||
return customers.map((customer: any) => this.mapPlatformToUnifiedCustomer(customer));
|
||||
}
|
||||
|
||||
async createCustomer(data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.post('customers', data);
|
||||
return this.mapCustomer(res.data);
|
||||
return this.mapPlatformToUnifiedCustomer(res.data);
|
||||
}
|
||||
|
||||
async updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
|
||||
async updateCustomer(where: {id: string | number}, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.put(`customers/${id}`, data);
|
||||
return this.mapCustomer(res.data);
|
||||
const res = await api.put(`customers/${where.id}`, data);
|
||||
return this.mapPlatformToUnifiedCustomer(res.data);
|
||||
}
|
||||
|
||||
async deleteCustomer(id: string | number): Promise<boolean> {
|
||||
async deleteCustomer(where: {id: string | number}): Promise<boolean> {
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
await api.delete(`customers/${id}`, { force: true });
|
||||
await api.delete(`customers/${where.id}`, { force: true });
|
||||
return true;
|
||||
}
|
||||
|
||||
// ========== 媒体映射方法 ==========
|
||||
mapPlatformToUnifiedMedia(data: any): UnifiedMediaDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformMedia(data: Partial<UnifiedMediaDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
||||
mapMedia(item: WpMedia): UnifiedMediaDTO {
|
||||
mapPlatformToUnifiedMedia(item: WpMedia): UnifiedMediaDTO {
|
||||
// 将 WordPress 媒体数据映射为统一媒体DTO
|
||||
// 兼容不同字段命名的时间信息
|
||||
return {
|
||||
|
|
@ -238,7 +232,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
params
|
||||
);
|
||||
return {
|
||||
items: items.map(this.mapMedia.bind(this)),
|
||||
items: items.map(this.mapPlatformToUnifiedMedia.bind(this)),
|
||||
total,
|
||||
totalPages,
|
||||
page,
|
||||
|
|
@ -250,21 +244,21 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
// 使用sdkGetAll获取所有媒体数据,不受分页限制
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const media = await this.wpService.sdkGetAll(api, 'media', params);
|
||||
return media.map((mediaItem: any) => this.mapMedia(mediaItem));
|
||||
return media.map((mediaItem: any) => this.mapPlatformToUnifiedMedia(mediaItem));
|
||||
}
|
||||
|
||||
createMedia(file: any): Promise<UnifiedMediaDTO> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
async updateMedia(id: string | number, data: any): Promise<any> {
|
||||
async updateMedia(where: {id: string | number}, data: any): Promise<any> {
|
||||
// 更新媒体信息
|
||||
return await this.wpService.updateMedia(Number(this.site.id), Number(id), data);
|
||||
return await this.wpService.updateMedia(Number(this.site.id), Number(where.id), data);
|
||||
}
|
||||
|
||||
async deleteMedia(id: string | number): Promise<boolean> {
|
||||
async deleteMedia(where: {id: string | number}): Promise<boolean> {
|
||||
// 删除媒体资源
|
||||
await this.wpService.deleteMedia(Number(this.site.id), Number(id), true);
|
||||
await this.wpService.deleteMedia(Number(this.site.id), Number(where.id), true);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -275,9 +269,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 订单映射方法 ==========
|
||||
mapPlatformToUnifiedOrder(data: any): UnifiedOrderDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformOrder(data: Partial<UnifiedOrderDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
|
@ -373,7 +364,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
].filter(Boolean).join(', ');
|
||||
}
|
||||
|
||||
mapOrder(item: WooOrder): UnifiedOrderDTO {
|
||||
mapPlatformToUnifiedOrder(item: WooOrder): UnifiedOrderDTO {
|
||||
// 将 WooCommerce 订单数据映射为统一订单DTO
|
||||
// 包含账单地址与收货地址以及创建与更新时间
|
||||
|
||||
|
|
@ -427,11 +418,11 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// 订单操作方法
|
||||
async getOrder(id: string | number): Promise<UnifiedOrderDTO> {
|
||||
async getOrder(where: {id: string | number}): Promise<UnifiedOrderDTO> {
|
||||
// 获取单个订单详情
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.get(`orders/${id}`);
|
||||
return this.mapOrder(res.data);
|
||||
const res = await api.get(`orders/${where.id}`);
|
||||
return this.mapPlatformToUnifiedOrder(res.data);
|
||||
}
|
||||
|
||||
async getOrders(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedOrderDTO>> {
|
||||
|
|
@ -461,7 +452,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
);
|
||||
|
||||
return {
|
||||
items: ordersWithFulfillments.map(this.mapOrder),
|
||||
items: ordersWithFulfillments.map(this.mapPlatformToUnifiedOrder),
|
||||
total,
|
||||
totalPages,
|
||||
page,
|
||||
|
|
@ -473,7 +464,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
// 使用sdkGetAll获取所有订单数据,不受分页限制
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const orders = await this.wpService.sdkGetAll(api, 'orders', params);
|
||||
return orders.map((order: any) => this.mapOrder(order));
|
||||
return orders.map((order: any) => this.mapPlatformToUnifiedOrder(order));
|
||||
}
|
||||
|
||||
async countOrders(where: Record<string, any>): Promise<number> {
|
||||
|
|
@ -492,18 +483,18 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
// 创建订单并返回统一订单DTO
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.post('orders', data);
|
||||
return this.mapOrder(res.data);
|
||||
return this.mapPlatformToUnifiedOrder(res.data);
|
||||
}
|
||||
|
||||
async updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
|
||||
async updateOrder(where: {id: string | number}, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
|
||||
// 更新订单并返回布尔结果
|
||||
return await this.wpService.updateOrder(this.site, String(id), data as any);
|
||||
return await this.wpService.updateOrder(this.site, String(where.id), data as any);
|
||||
}
|
||||
|
||||
async deleteOrder(id: string | number): Promise<boolean> {
|
||||
async deleteOrder(where: {id: string | number}): Promise<boolean> {
|
||||
// 删除订单
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
await api.delete(`orders/${id}`, { force: true });
|
||||
await api.delete(`orders/${where.id}`, { force: true });
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -608,9 +599,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 产品映射方法 ==========
|
||||
mapPlatformToUnifiedProduct(data: any): UnifiedProductDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformProduct(data: Partial<UnifiedProductDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
|
@ -712,7 +700,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
return mapped;
|
||||
}
|
||||
|
||||
mapProduct(item: WooProduct): UnifiedProductDTO {
|
||||
mapPlatformToUnifiedProduct(item: WooProduct): UnifiedProductDTO {
|
||||
// 将 WooCommerce 产品数据映射为统一产品DTO
|
||||
// 保留常用字段与时间信息以便前端统一展示
|
||||
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#product-properties
|
||||
|
|
@ -800,11 +788,22 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
raw: item,
|
||||
};
|
||||
}
|
||||
|
||||
async getProduct({id, sku}: {id?: string, sku?:string}){
|
||||
if(id) return this.getProductById(id);
|
||||
if(sku) return this.getProductBySku(sku)
|
||||
return this.getProductById(id || sku || '');
|
||||
}
|
||||
async getProductBySku(sku: string){
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const res = await api.get(`products?sku=${sku}`);
|
||||
const product = res.data[0];
|
||||
return this.mapPlatformToUnifiedProduct(product);
|
||||
}
|
||||
// 产品操作方法
|
||||
async getProduct(id: string | number): Promise<UnifiedProductDTO> {
|
||||
async getProductById(id: string | number): Promise<UnifiedProductDTO> {
|
||||
// 获取单个产品详情并映射为统一产品DTO
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
|
||||
const res = await api.get(`products/${id}`);
|
||||
const product = res.data;
|
||||
|
||||
|
|
@ -824,7 +823,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
return this.mapProduct(product);
|
||||
return this.mapPlatformToUnifiedProduct(product);
|
||||
}
|
||||
|
||||
async getProducts(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedProductDTO>> {
|
||||
|
|
@ -865,6 +864,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
);
|
||||
|
||||
return {
|
||||
items: productsWithVariations.map(this.mapPlatformToUnifiedProduct),
|
||||
items: productsWithVariations.map(this.mapPlatformToUnifiedProduct),
|
||||
total,
|
||||
totalPages,
|
||||
|
|
@ -901,21 +901,34 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
})
|
||||
);
|
||||
|
||||
return productsWithVariations.map((product: any) => this.mapProduct(product));
|
||||
return productsWithVariations.map((product: any) => this.mapPlatformToUnifiedProduct(product));
|
||||
}
|
||||
|
||||
async createProduct(data: Partial<UnifiedProductDTO>): Promise<UnifiedProductDTO> {
|
||||
// 创建产品并返回统一产品DTO
|
||||
const res = await this.wpService.createProduct(this.site, data);
|
||||
return this.mapPlatformToUnifiedProduct(res);
|
||||
return this.mapPlatformToUnifiedProduct(res);
|
||||
}
|
||||
|
||||
async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
|
||||
async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
|
||||
// 更新产品并返回统一产品DTO
|
||||
const res = await this.wpService.updateProduct(this.site, String(id), data as any);
|
||||
let productId: string;
|
||||
if (where.id) {
|
||||
productId = String(where.id);
|
||||
} else if (where.sku) {
|
||||
// 通过sku获取产品ID
|
||||
const product = await this.getProductBySku(where.sku);
|
||||
productId = String(product.id);
|
||||
} else {
|
||||
throw new Error('必须提供id或sku参数');
|
||||
}
|
||||
const res = await this.wpService.updateProduct(this.site, productId, data as any);
|
||||
return res;
|
||||
}
|
||||
|
||||
async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
|
||||
async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
|
||||
// 删除产品
|
||||
let productId: string;
|
||||
|
|
@ -928,8 +941,19 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
} else {
|
||||
throw new Error('必须提供id或sku参数');
|
||||
}
|
||||
let productId: string;
|
||||
if (where.id) {
|
||||
productId = String(where.id);
|
||||
} else if (where.sku) {
|
||||
// 通过sku获取产品ID
|
||||
const product = await this.getProductBySku(where.sku);
|
||||
productId = String(product.id);
|
||||
} else {
|
||||
throw new Error('必须提供id或sku参数');
|
||||
}
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
try {
|
||||
await api.delete(`products/${productId}`, { force: true });
|
||||
await api.delete(`products/${productId}`, { force: true });
|
||||
return true;
|
||||
} catch (e) {
|
||||
|
|
@ -938,9 +962,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 评论映射方法 ==========
|
||||
mapPlatformToUnifiedReview(data: any): UnifiedReviewDTO {
|
||||
return data;
|
||||
}
|
||||
|
||||
mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
|
@ -952,7 +974,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
return data;
|
||||
}
|
||||
|
||||
mapReview(item: any): UnifiedReviewDTO & { raw: any } {
|
||||
mapPlatformToUnifiedReview(item: any): UnifiedReviewDTO & { raw: any } {
|
||||
// 将 WooCommerce 评论数据映射为统一评论DTO
|
||||
return {
|
||||
id: item.id,
|
||||
|
|
@ -984,6 +1006,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
requestParams
|
||||
);
|
||||
return {
|
||||
items: items.map(this.mapPlatformToUnifiedReview.bind(this)),
|
||||
items: items.map(this.mapPlatformToUnifiedReview.bind(this)),
|
||||
total,
|
||||
totalPages,
|
||||
|
|
@ -997,31 +1020,33 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const reviews = await this.wpService.sdkGetAll(api, 'products/reviews', params);
|
||||
return reviews.map((review: any) => this.mapPlatformToUnifiedReview(review));
|
||||
return reviews.map((review: any) => this.mapPlatformToUnifiedReview(review));
|
||||
}
|
||||
|
||||
async createReview(data: any): Promise<UnifiedReviewDTO> {
|
||||
const res = await this.wpService.createReview(this.site, data);
|
||||
return this.mapPlatformToUnifiedReview(res);
|
||||
return this.mapPlatformToUnifiedReview(res);
|
||||
}
|
||||
|
||||
async updateReview(where: {id: number}, data: any): Promise<UnifiedReviewDTO> {
|
||||
const res = await this.wpService.updateReview(this.site, where.id, data);
|
||||
return this.mapPlatformToUnifiedReview(res);
|
||||
async updateReview(where: {id: number}, data: any): Promise<UnifiedReviewDTO> {
|
||||
const res = await this.wpService.updateReview(this.site, where.id, data);
|
||||
return this.mapPlatformToUnifiedReview(res);
|
||||
}
|
||||
|
||||
async deleteReview(id: number): Promise<boolean> {
|
||||
return await this.wpService.deleteReview(this.site, id);
|
||||
async deleteReview(where: {id: number}): Promise<boolean> {
|
||||
return await this.wpService.deleteReview(this.site, where.id);
|
||||
}
|
||||
|
||||
// ========== 订阅映射方法 ==========
|
||||
mapPlatformToUnifiedSubscription(data: any): UnifiedSubscriptionDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformSubscription(data: Partial<UnifiedSubscriptionDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
||||
mapSubscription(item: WooSubscription): UnifiedSubscriptionDTO {
|
||||
mapPlatformToUnifiedSubscription(item: WooSubscription): UnifiedSubscriptionDTO {
|
||||
// 将 WooCommerce 订阅数据映射为统一订阅DTO
|
||||
// 若缺少创建时间则回退为开始时间
|
||||
return {
|
||||
|
|
@ -1062,7 +1087,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
params
|
||||
);
|
||||
return {
|
||||
items: items.map(this.mapSubscription),
|
||||
items: items.map(this.mapPlatformToUnifiedSubscription),
|
||||
total,
|
||||
totalPages,
|
||||
page,
|
||||
|
|
@ -1077,7 +1102,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
// 使用sdkGetAll获取所有订阅数据,不受分页限制
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const subscriptions = await this.wpService.sdkGetAll(api, 'subscriptions', params);
|
||||
return subscriptions.map((subscription: any) => this.mapSubscription(subscription));
|
||||
return subscriptions.map((subscription: any) => this.mapPlatformToUnifiedSubscription(subscription));
|
||||
}
|
||||
|
||||
// ========== 变体映射方法 ==========
|
||||
|
|
@ -1322,9 +1347,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// ========== 网络钩子映射方法 ==========
|
||||
mapPlatformToUnifiedWebhook(data: any): UnifiedWebhookDTO {
|
||||
return data;
|
||||
}
|
||||
mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) {
|
||||
return data;
|
||||
}
|
||||
|
|
@ -1337,7 +1359,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
}
|
||||
|
||||
// 映射 WooCommerce webhook 到统一格式
|
||||
mapWebhook(webhook: WooWebhook): UnifiedWebhookDTO {
|
||||
mapPlatformToUnifiedWebhook(webhook: WooWebhook): UnifiedWebhookDTO {
|
||||
return {
|
||||
id: webhook.id.toString(),
|
||||
name: webhook.name,
|
||||
|
|
@ -1359,12 +1381,12 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
const result = await this.wpService.getWebhooks(this.site, params);
|
||||
|
||||
return {
|
||||
items: (result.items as WooWebhook[]).map(this.mapWebhook),
|
||||
total: result.total,
|
||||
page: Number(params.page || 1),
|
||||
per_page: Number(params.per_page || 20),
|
||||
totalPages: result.totalPages,
|
||||
};
|
||||
items: (result.items as WooWebhook[]).map(this.mapPlatformToUnifiedWebhook),
|
||||
total: result.total,
|
||||
page: Number(params.page || 1),
|
||||
per_page: Number(params.per_page || 20),
|
||||
totalPages: result.totalPages,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get webhooks: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
|
|
@ -1375,17 +1397,17 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
try {
|
||||
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
|
||||
const webhooks = await this.wpService.sdkGetAll(api, 'webhooks', params);
|
||||
return webhooks.map((webhook: any) => this.mapWebhook(webhook));
|
||||
return webhooks.map((webhook: any) => this.mapPlatformToUnifiedWebhook(webhook));
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get all webhooks: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取单个 webhook 详情
|
||||
async getWebhook(id: string | number): Promise<UnifiedWebhookDTO> {
|
||||
async getWebhook(where: {id: string | number}): Promise<UnifiedWebhookDTO> {
|
||||
try {
|
||||
const result = await this.wpService.getWebhook(this.site, id);
|
||||
return this.mapWebhook(result as WooWebhook);
|
||||
const result = await this.wpService.getWebhook(this.site, where.id);
|
||||
return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to get webhook: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
|
|
@ -1403,14 +1425,14 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
api_version: data.api_version || 'wp/v2',
|
||||
};
|
||||
const result = await this.wpService.createWebhook(this.site, params);
|
||||
return this.mapWebhook(result as WooWebhook);
|
||||
return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create webhook: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新现有的 webhook
|
||||
async updateWebhook(id: string | number, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
|
||||
async updateWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
|
||||
try {
|
||||
const params = {
|
||||
...(data.name ? { name: data.name } : {}),
|
||||
|
|
@ -1420,17 +1442,17 @@ export class WooCommerceAdapter implements ISiteAdapter {
|
|||
...(data.secret ? { secret: data.secret } : {}),
|
||||
...(data.api_version ? { api_version: data.api_version } : {}),
|
||||
};
|
||||
const result = await this.wpService.updateWebhook(this.site, id, params);
|
||||
return this.mapWebhook(result as WooWebhook);
|
||||
const result = await this.wpService.updateWebhook(this.site, where.id, params);
|
||||
return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to update webhook: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除指定的 webhook
|
||||
async deleteWebhook(id: string | number): Promise<boolean> {
|
||||
async deleteWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<boolean> {
|
||||
try {
|
||||
await this.wpService.deleteWebhook(this.site, id);
|
||||
await this.wpService.deleteWebhook(this.site, where.id);
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to delete webhook: ${error instanceof Error ? error.message : String(error)}`);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import {
|
|||
import { UnifiedPaginationDTO, UnifiedSearchParamsDTO } from '../dto/api.dto';
|
||||
import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
|
||||
|
||||
export interface ISiteAdapter {
|
||||
export interface ISiteAdapter {
|
||||
// ========== 客户映射方法 ==========
|
||||
/**
|
||||
* 将平台客户数据转换为统一客户数据格式
|
||||
|
|
@ -39,7 +39,7 @@ export interface ISiteAdapter {
|
|||
* 获取单个客户
|
||||
* 获取单个客户
|
||||
*/
|
||||
getCustomer(id: string | number): Promise<UnifiedCustomerDTO>;
|
||||
getCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>): Promise<UnifiedCustomerDTO>;
|
||||
|
||||
/**
|
||||
* 获取客户列表
|
||||
|
|
@ -60,12 +60,12 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 更新客户
|
||||
*/
|
||||
updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO>;
|
||||
updateCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO>;
|
||||
|
||||
/**
|
||||
* 删除客户
|
||||
*/
|
||||
deleteCustomer(id: string | number): Promise<boolean>;
|
||||
deleteCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 批量处理客户
|
||||
|
|
@ -152,7 +152,7 @@ export interface ISiteAdapter {
|
|||
* 获取单个订单
|
||||
* 获取单个订单
|
||||
*/
|
||||
getOrder(id: string | number): Promise<UnifiedOrderDTO>;
|
||||
getOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>): Promise<UnifiedOrderDTO>;
|
||||
|
||||
/**
|
||||
* 获取订单列表
|
||||
|
|
@ -172,7 +172,7 @@ export interface ISiteAdapter {
|
|||
* 获取订单总数
|
||||
* 获取订单总数
|
||||
*/
|
||||
countOrders(params: Record<string,any>): Promise<number>;
|
||||
countOrders(params: Record<string, any>): Promise<number>;
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
|
|
@ -185,13 +185,13 @@ export interface ISiteAdapter {
|
|||
* 更新订单
|
||||
* 更新订单
|
||||
*/
|
||||
updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean>;
|
||||
updateOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>, data: Partial<UnifiedOrderDTO>): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 删除订单
|
||||
* 删除订单
|
||||
*/
|
||||
deleteOrder(id: string | number): Promise<boolean>;
|
||||
deleteOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 获取订单备注
|
||||
|
|
@ -288,7 +288,7 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 获取单个产品
|
||||
*/
|
||||
getProduct(id: string | number): Promise<UnifiedProductDTO>;
|
||||
getProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>): Promise<UnifiedProductDTO>;
|
||||
|
||||
/**
|
||||
* 获取产品列表
|
||||
|
|
@ -308,12 +308,12 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 更新产品
|
||||
*/
|
||||
updateProduct(id: string | number, data: Partial<UnifiedProductDTO>): Promise<boolean>;
|
||||
updateProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>, data: Partial<UnifiedProductDTO>): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 删除产品
|
||||
*/
|
||||
deleteProduct(id: string | number): Promise<boolean>;
|
||||
deleteProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 批量处理产品
|
||||
|
|
@ -367,12 +367,12 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 更新评论
|
||||
*/
|
||||
updateReview(id: number, data: UpdateReviewDTO): Promise<UnifiedReviewDTO>;
|
||||
updateReview(where: Partial<Pick<UnifiedReviewDTO, 'id'>>, data: UpdateReviewDTO): Promise<UnifiedReviewDTO>;
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*/
|
||||
deleteReview(id: number): Promise<boolean>;
|
||||
deleteReview(where: Partial<Pick<UnifiedReviewDTO, 'id'>>): Promise<boolean>;
|
||||
|
||||
// ========== 订阅映射方法 ==========
|
||||
/**
|
||||
|
|
@ -490,7 +490,7 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 获取单个webhook
|
||||
*/
|
||||
getWebhook(id: string | number): Promise<UnifiedWebhookDTO>;
|
||||
getWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<UnifiedWebhookDTO>;
|
||||
|
||||
/**
|
||||
* 获取webhooks列表
|
||||
|
|
@ -510,16 +510,16 @@ export interface ISiteAdapter {
|
|||
/**
|
||||
* 更新webhook
|
||||
*/
|
||||
updateWebhook(id: string | number, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO>;
|
||||
updateWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO>;
|
||||
|
||||
/**
|
||||
* 删除webhook
|
||||
*/
|
||||
deleteWebhook(id: string | number): Promise<boolean>;
|
||||
deleteWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<boolean>;
|
||||
|
||||
// ========== 站点/其他方法 ==========
|
||||
/**
|
||||
* 获取站点链接列表
|
||||
*/
|
||||
getLinks(): Promise<Array<{title: string, url: string}>>;
|
||||
getLinks(): Promise<Array<{ title: string, url: string }>>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue