zksu
/
API
forked from yoone/API
1
0
Fork 0

Compare commits

...

9 Commits

Author SHA1 Message Date
tikkhun 5d7e0090aa style: 修复代码格式问题,包括空格和空行 2026-01-10 15:17:08 +08:00
tikkhun ecdedcc041 fix: 修复订单服务中产品属性和组件处理的问题
处理产品属性为空的情况,避免空指针异常
为产品组件查询添加关联关系
在订单销售记录创建时增加对空产品的过滤
添加新的品牌判断逻辑
2026-01-10 15:16:29 +08:00
tikkhun b2ee61e47d refactor: 移除未使用的导入和注释掉的生命周期钩子 2026-01-10 15:16:29 +08:00
tikkhun 64c1d1afe5 refactor(订单服务): 移除冗余的订单可编辑性检查注释
注释说明检查应在 save 方法中进行
2026-01-10 15:14:12 +08:00
tikkhun 4eb45af452 feat(订单): 增强订单相关功能及数据模型
- 在订单实体中添加orderItems和orderSales字段
- 优化QueryOrderSalesDTO,增加排序字段和更多描述信息
- 重构saveOrderSale方法,使用产品属性自动设置品牌和强度
- 在订单查询中返回关联的orderItems和orderSales数据
- 添加getAttributesObject方法处理产品属性
2026-01-10 15:14:12 +08:00
tikkhun a8d12a695e fix(product.service): 支持英文分号和逗号分隔siteSkus字段
修改siteSkus字段的分隔符处理逻辑,使其同时支持英文分号和逗号作为分隔符,提高数据兼容性
2026-01-10 15:09:52 +08:00
zhuotianyuan a00a95c9a3 style: 修复 typeorm 配置缩进问题 2026-01-10 07:07:24 +00:00
zhuotianyuan 82c8640f0c fix(config): 将数据库配置更改为本地开发环境
更新数据库连接配置为本地开发环境,包括主机、端口和密码
移除自动同步数据库的配置项
2026-01-10 07:07:24 +00:00
zhuotianyuan cb00076bd3 feat(webhook): 添加对shoppy平台webhook的支持
- 在site.entity.ts中添加webhookUrl字段
- 在auth.middleware.ts中添加/shoppy路由到白名单
- 在webhook.controller.ts中实现shoppy平台webhook处理逻辑

fix(webhook): 更新webhook控制器中的密钥值

refactor(entity): 将可选字段明确标记为可选类型

feat(adapter): 公开映射方法以支持统一接口调用

将各适配器中的私有映射方法改为公开,并在接口中定义统一方法签名
修改webhook控制器以使用适配器映射方法处理订单数据

feat: 添加订单支付日期字段并支持国家筛选

- 在ShopyyOrder接口中添加date_paid字段
- 在OrderStatisticsParams中添加country数组字段用于国家筛选
- 修改统计服务以支持按国家筛选订单数据
- 更新数据库配置和同步设置
- 优化订单服务中的类型定义和查询条件

refactor(webhook): 移除未使用的shoppy webhook处理逻辑

fix(订单服务): 修复订单内容括号处理并添加同步日志

添加订单同步过程的调试日志
修复订单内容中括号内容的处理逻辑
修正控制器方法名拼写错误
2026-01-10 07:07:24 +00:00
13 changed files with 3012 additions and 291 deletions

View File

@ -367,7 +367,6 @@ export class ShopyyAdapter implements ISiteAdapter {
date_paid: typeof item.pay_at === 'number' date_paid: typeof item.pay_at === 'number'
? item.pay_at === 0 ? null : new Date(item.pay_at * 1000).toISOString() ? item.pay_at === 0 ? null : new Date(item.pay_at * 1000).toISOString()
: null, : null,
refunds: [], refunds: [],
currency_symbol: (currencySymbols[item.currency] || '$') || '', currency_symbol: (currencySymbols[item.currency] || '$') || '',
date_created: date_created:
@ -387,6 +386,7 @@ export class ShopyyAdapter implements ISiteAdapter {
tracking_number: f.tracking_number || '', tracking_number: f.tracking_number || '',
shipping_provider: f.tracking_company || '', shipping_provider: f.tracking_company || '',
shipping_method: f.tracking_company || '', shipping_method: f.tracking_company || '',
date_created: typeof f.created_at === 'number' date_created: typeof f.created_at === 'number'
? new Date(f.created_at * 1000).toISOString() ? new Date(f.created_at * 1000).toISOString()
: f.created_at || '', : f.created_at || '',

View File

@ -79,7 +79,7 @@ export class StatisticsController {
@ApiOkResponse() @ApiOkResponse()
@Get('/orderSource') @Get('/orderSource')
async getOrderSorce(@Query() params) { async getOrderSource(@Query() params) {
try { try {
return successResponse(await this.statisticsService.getOrderSorce(params)); return successResponse(await this.statisticsService.getOrderSorce(params));
} catch (error) { } catch (error) {

View File

@ -14,6 +14,8 @@ import { SiteService } from '../service/site.service';
import { OrderService } from '../service/order.service'; import { OrderService } from '../service/order.service';
import { SiteApiService } from '../service/site-api.service'; import { SiteApiService } from '../service/site-api.service';
@Controller('/webhook') @Controller('/webhook')
export class WebhookController { export class WebhookController {
private secret = 'YOONE24kd$kjcdjflddd'; private secret = 'YOONE24kd$kjcdjflddd';
@ -182,15 +184,10 @@ export class WebhookController {
success: true, success: true,
message: 'Webhook processed successfully', message: 'Webhook processed successfully',
}; };
} else {
return {
code: 403,
success: false,
message: 'Webhook verification failed',
};
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
} }
} }

View File

@ -98,14 +98,10 @@ export class QueryOrderDTO {
} }
export class QueryOrderSalesDTO { export class QueryOrderSalesDTO {
@ApiProperty() @ApiProperty({ description: '是否为原产品还是库存产品' })
@Rule(RuleType.bool().default(false)) @Rule(RuleType.bool().default(false))
isSource: boolean; isSource: boolean;
@ApiProperty()
@Rule(RuleType.bool().default(false))
exceptPackage: boolean;
@ApiProperty({ example: '1', description: '页码' }) @ApiProperty({ example: '1', description: '页码' })
@Rule(RuleType.number()) @Rule(RuleType.number())
current: number; current: number;
@ -114,19 +110,31 @@ export class QueryOrderSalesDTO {
@Rule(RuleType.number()) @Rule(RuleType.number())
pageSize: number; pageSize: number;
@ApiProperty() @ApiProperty({ description: '排序对象,格式如 { productName: "asc", sku: "desc" }',type: 'any', required: false })
@Rule(RuleType.object().allow(null))
orderBy?: Record<string, 'asc' | 'desc'>;
// filter
@ApiProperty({ description: '是否排除套餐' })
@Rule(RuleType.bool().default(false))
exceptPackage: boolean;
@ApiProperty({ description: '站点ID' })
@Rule(RuleType.number()) @Rule(RuleType.number())
siteId: number; siteId: number;
@ApiProperty() @ApiProperty({ description: '名称' })
@Rule(RuleType.string()) @Rule(RuleType.string())
name: string; name: string;
@ApiProperty() @ApiProperty({ description: 'SKU' })
@Rule(RuleType.string())
sku: string;
@ApiProperty({ description: '开始日期' })
@Rule(RuleType.date()) @Rule(RuleType.date())
startDate: Date; startDate: Date;
@ApiProperty() @ApiProperty({ description: '结束日期' })
@Rule(RuleType.date()) @Rule(RuleType.date())
endDate: Date; endDate: Date;
} }

View File

@ -346,6 +346,7 @@ export interface ShopyyOrder {
financial_status?: number; financial_status?: number;
fulfillment_status?: number; fulfillment_status?: number;
// 创建与更新时间可能为时间戳 // 创建与更新时间可能为时间戳
date_paid?: number | string;
created_at?: number | string; created_at?: number | string;
date_added?: string; date_added?: string;
updated_at?: number | string; updated_at?: number | string;

View File

@ -121,7 +121,7 @@ export class UpdateSiteDTO {
skuPrefix?: string; skuPrefix?: string;
// 区域 // 区域
@ApiProperty({ description: '区域' }) @ApiProperty({ description: '区域', required: false })
@Rule(RuleType.array().items(RuleType.string()).optional()) @Rule(RuleType.array().items(RuleType.string()).optional())
areas?: string[]; areas?: string[];
@ -133,6 +133,10 @@ export class UpdateSiteDTO {
@ApiProperty({ description: '站点网站URL', required: false }) @ApiProperty({ description: '站点网站URL', required: false })
@Rule(RuleType.string().optional()) @Rule(RuleType.string().optional())
websiteUrl?: string; websiteUrl?: string;
@ApiProperty({ description: 'Webhook URL', required: false })
@Rule(RuleType.string().optional())
webhookUrl?: string;
} }
export class QuerySiteDTO { export class QuerySiteDTO {

View File

@ -19,6 +19,10 @@ export class OrderStatisticsParams {
@Rule(RuleType.number().allow(null)) @Rule(RuleType.number().allow(null))
siteId?: number; siteId?: number;
@ApiProperty()
@Rule(RuleType.array().allow(null))
country?: any[];
@ApiProperty({ @ApiProperty({
enum: ['all', 'first_purchase', 'repeat_purchase'], enum: ['all', 'first_purchase', 'repeat_purchase'],
default: 'all', default: 'all',

View File

@ -272,6 +272,14 @@ export class Order {
@Expose() @Expose()
updatedAt: Date; updatedAt: Date;
@ApiProperty({ type: 'json', nullable: true, description: '订单项列表' })
@Expose()
orderItems?: any[];
@ApiProperty({ type: 'json', nullable: true, description: '销售项列表' })
@Expose()
orderSales?: any[];
// 在插入或更新前处理用户代理字符串 // 在插入或更新前处理用户代理字符串
@BeforeInsert() @BeforeInsert()
@BeforeUpdate() @BeforeUpdate()

View File

@ -1,8 +1,8 @@
import { ApiProperty } from '@midwayjs/swagger'; import { ApiProperty } from '@midwayjs/swagger';
import { Exclude, Expose } from 'class-transformer'; import { Exclude, Expose } from 'class-transformer';
import { import {
BeforeInsert, // BeforeInsert,
BeforeUpdate, // BeforeUpdate,
Column, Column,
CreateDateColumn, CreateDateColumn,
Entity, Entity,
@ -75,7 +75,7 @@ export class OrderSale {
@ApiProperty({ nullable: true }) @ApiProperty({ nullable: true })
@Column({ type: 'int', nullable: true }) @Column({ type: 'int', nullable: true })
@Expose() @Expose()
size: number | null; size: number | null; // 其实是 strength
@ApiProperty() @ApiProperty()
@Column({ default: false }) @Column({ default: false })
@ -98,24 +98,24 @@ export class OrderSale {
@Expose() @Expose()
updatedAt?: Date; updatedAt?: Date;
// === 自动计算逻辑 === // // === 自动计算逻辑 ===
@BeforeInsert() // @BeforeInsert()
@BeforeUpdate() // @BeforeUpdate()
setFlags() { // setFlags() {
if (!this.name) return; // if (!this.name) return;
const lower = this.name.toLowerCase(); // const lower = this.name.toLowerCase();
this.isYoone = lower.includes('yoone'); // this.isYoone = lower.includes('yoone');
this.isZex = lower.includes('zex'); // this.isZex = lower.includes('zex');
this.isYooneNew = this.isYoone && lower.includes('new'); // this.isYooneNew = this.isYoone && lower.includes('new');
let size: number | null = null; // let size: number | null = null;
const sizes = [3, 6, 9, 12, 15, 18]; // const sizes = [3, 6, 9, 12, 15, 18];
for (const s of sizes) { // for (const s of sizes) {
if (lower.includes(s.toString())) { // if (lower.includes(s.toString())) {
size = s; // size = s;
break; // break;
} // }
} // }
this.size = size; // this.size = size;
} // }
} }

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,7 @@ import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import { UnifiedOrderDTO } from '../dto/site-api.dto'; import { UnifiedOrderDTO } from '../dto/site-api.dto';
import { CustomerService } from './customer.service'; import { CustomerService } from './customer.service';
import { ProductService } from './product.service';
@Provide() @Provide()
export class OrderService { export class OrderService {
@ -110,6 +111,8 @@ export class OrderService {
@Logger() @Logger()
logger; // 注入 Logger 实例 logger; // 注入 Logger 实例
@Inject()
productService: ProductService;
/** /**
* *
@ -278,6 +281,11 @@ export class OrderService {
console.error('更新订单状态失败,原因为:', error) console.error('更新订单状态失败,原因为:', error)
} }
} }
async getOrderByExternalOrderId(siteId: number, externalOrderId: string) {
return await this.orderModel.findOne({
where: { externalOrderId: String(externalOrderId), siteId },
});
}
/** /**
* *
* : * :
@ -301,7 +309,7 @@ export class OrderService {
* @param order * @param order
* @param forceUpdate * @param forceUpdate
*/ */
async syncSingleOrder(siteId: number, order: any, forceUpdate = false) { async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) {
// 从订单数据中解构出各个子项 // 从订单数据中解构出各个子项
let { let {
line_items, line_items,
@ -315,47 +323,28 @@ export class OrderService {
// console.log('同步进单个订单', order) // console.log('同步进单个订单', order)
// 如果订单状态为 AUTO_DRAFT,则跳过处理 // 如果订单状态为 AUTO_DRAFT,则跳过处理
if (order.status === OrderStatus.AUTO_DRAFT) { if (order.status === OrderStatus.AUTO_DRAFT) {
this.logger.debug('订单状态为 AUTO_DRAFT,跳过处理', siteId, order.id)
return; return;
} }
// 检查数据库中是否已存在该订单 // 这里其实不用过滤不可编辑的行为,而是应在 save 中做判断
const existingOrder = await this.orderModel.findOne({ // if(!order.is_editable && !forceUpdate){
where: { externalOrderId: order.id, siteId: siteId }, // this.logger.debug('订单不可编辑,跳过处理', siteId, order.id)
}); // return;
// 自动更新订单状态(如果需要) // }
// 自动转换远程订单的状态(如果需要)
await this.autoUpdateOrderStatus(siteId, order); await this.autoUpdateOrderStatus(siteId, order);
// 这里的 saveOrder 已经包括了创建订单和更新订单
if(existingOrder){ let orderRecord: Order = await this.saveOrder(siteId, orderData);
// 矫正数据库中的订单数据
const updateData: any = { status: order.status };
if (this.canUpdateErpStatus(existingOrder.orderStatus)) {
updateData.orderStatus = this.mapOrderStatus(order.status);
}
// 更新
await this.orderModel.update({ externalOrderId: order.id, siteId: siteId }, updateData);
// 更新 fulfillments 数据
await this.saveOrderFulfillments({
siteId,
orderId: existingOrder.id,
externalOrderId:order.id,
fulfillments: fulfillments,
});
}
const externalOrderId = order.id;
// 如果订单从未完成变为完成状态,则更新库存 // 如果订单从未完成变为完成状态,则更新库存
if ( if (
existingOrder && orderRecord &&
existingOrder.orderStatus !== ErpOrderStatus.COMPLETED && orderRecord.orderStatus !== ErpOrderStatus.COMPLETED &&
orderData.status === OrderStatus.COMPLETED orderData.status === OrderStatus.COMPLETED
) { ) {
this.updateStock(existingOrder); await this.updateStock(orderRecord);
// 不再直接返回,继续执行后续的更新操作 // 不再直接返回,继续执行后续的更新操作
} }
// 如果订单不可编辑且不强制更新,则跳过处理 const externalOrderId = String(order.id);
if (existingOrder && !existingOrder.is_editable && !forceUpdate) {
return;
}
// 保存订单主数据
const orderRecord = await this.saveOrder(siteId, orderData);
const orderId = orderRecord.id; const orderId = orderRecord.id;
// 保存订单项 // 保存订单项
await this.saveOrderItems({ await this.saveOrderItems({
@ -459,7 +448,8 @@ export class OrderService {
* @param order * @param order
* @returns * @returns
*/ */
async saveOrder(siteId: number, order: UnifiedOrderDTO): Promise<Order> { // 这里 omit 是因为处理在外头了 其实 saveOrder 应该包括 savelineitems 等
async saveOrder(siteId: number, order: Omit<UnifiedOrderDTO, 'line_items' | 'refunds'>): Promise<Order> {
// 将外部订单ID转换为字符串 // 将外部订单ID转换为字符串
const externalOrderId = String(order.id) const externalOrderId = String(order.id)
delete order.id delete order.id
@ -708,6 +698,8 @@ export class OrderService {
* *
* @param orderItem * @param orderItem
*/ */
// TODO 这里存的是库存商品实际
// 所以叫做 orderInventoryItems 可能更合适
async saveOrderSale(orderItem: OrderItem) { async saveOrderSale(orderItem: OrderItem) {
const currentOrderSale = await this.orderSaleModel.find({ const currentOrderSale = await this.orderSaleModel.find({
where: { where: {
@ -726,46 +718,47 @@ export class OrderService {
}); });
if (!product) return; if (!product) return;
const componentDetails: { product: Product, quantity: number }[] = product.components?.length > 0 ? await Promise.all(product.components.map(async comp => {
const orderSales: OrderSale[] = []; return {
product: await this.productModel.findOne({
if (product.components && product.components.length > 0) {
for (const comp of product.components) {
const baseProduct = await this.productModel.findOne({
where: { sku: comp.sku }, where: { sku: comp.sku },
}); relations: ['components', 'attributes'],
if (baseProduct) { }),
const orderSaleItem: OrderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId,
siteId: orderItem.siteId,
externalOrderItemId: orderItem.externalOrderItemId,
productId: baseProduct.id,
name: baseProduct.name,
quantity: comp.quantity * orderItem.quantity, quantity: comp.quantity * orderItem.quantity,
sku: comp.sku,
isPackage: orderItem.name.toLowerCase().includes('package'),
});
orderSales.push(orderSaleItem);
} }
} })) : [{ product, quantity: orderItem.quantity }]
} else {
const orderSaleItem: OrderSale = plainToClass(OrderSale, { const orderSales: OrderSale[] = componentDetails.map(componentDetail => {
if (!componentDetail.product) return null
const attrsObj = this.productService.getAttributesObject(product.attributes)
const orderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId, orderId: orderItem.orderId,
siteId: orderItem.siteId, siteId: orderItem.siteId,
externalOrderItemId: orderItem.externalOrderItemId, externalOrderItemId: orderItem.externalOrderItemId,
productId: product.id, productId: componentDetail.product.id,
name: product.name, name: componentDetail.product.name,
quantity: orderItem.quantity, quantity: componentDetail.quantity * orderItem.quantity,
sku: product.sku, sku: componentDetail.product.sku,
isPackage: orderItem.name.toLowerCase().includes('package'), isPackage: componentDetail.product.type === 'bundle',
isYoone: attrsObj?.['brand']?.name === 'yoone',
isZyn: attrsObj?.['brand']?.name === 'zyn',
isZex: attrsObj?.['brand']?.name === 'zex',
isYooneNew: attrsObj?.['brand']?.name === 'yoone' && attrsObj?.['version']?.name === 'new',
size: this.extractNumberFromString(attrsObj?.['strength']?.name) || null,
}); });
orderSales.push(orderSaleItem); return orderSale
} }).filter(v => v !== null)
if (orderSales.length > 0) { if (orderSales.length > 0) {
await this.orderSaleModel.save(orderSales); await this.orderSaleModel.save(orderSales);
} }
} }
extractNumberFromString(str: string): number {
if (!str) return 0;
const num = parseInt(str, 10);
return isNaN(num) ? 0 : num;
}
/** /**
* 退 * 退
@ -1426,7 +1419,7 @@ export class OrderService {
* @param params * @param params
* @returns * @returns
*/ */
async getOrderSales({ siteId, startDate, endDate, current, pageSize, name, exceptPackage }: QueryOrderSalesDTO) { async getOrderSales({ siteId, startDate, endDate, current, pageSize, name, exceptPackage, orderBy }: QueryOrderSalesDTO) {
const nameKeywords = name ? name.split(' ').filter(Boolean) : []; const nameKeywords = name ? name.split(' ').filter(Boolean) : [];
const defaultStart = dayjs().subtract(30, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'); const defaultStart = dayjs().subtract(30, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss');
const defaultEnd = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'); const defaultEnd = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss');
@ -1642,11 +1635,12 @@ export class OrderService {
* @returns * @returns
*/ */
async getOrderItems({ async getOrderItems({
current,
pageSize,
siteId, siteId,
startDate, startDate,
endDate, endDate,
current, sku,
pageSize,
name, name,
}: QueryOrderSalesDTO) { }: QueryOrderSalesDTO) {
const nameKeywords = name ? name.split(' ').filter(Boolean) : []; const nameKeywords = name ? name.split(' ').filter(Boolean) : [];
@ -2645,7 +2639,4 @@ async exportToCsv(data: any[], options: { type?: 'string' | 'buffer'; fileName?:
throw new Error(`导出CSV文件失败: ${error.message}`); throw new Error(`导出CSV文件失败: ${error.message}`);
} }
} }
} }

View File

@ -1466,7 +1466,12 @@ export class ProductService {
price: num(rec.price), price: num(rec.price),
promotionPrice: num(rec.promotionPrice), promotionPrice: num(rec.promotionPrice),
type: val(rec.type), type: val(rec.type),
siteSkus: rec.siteSkus ? String(rec.siteSkus).split(',').map(s => s.trim()).filter(Boolean) : undefined, siteSkus: rec.siteSkus
? String(rec.siteSkus)
.split(/[;,]/) // 支持英文分号或英文逗号分隔
.map(s => s.trim())
.filter(Boolean)
: undefined,
category, // 添加分类字段 category, // 添加分类字段
attributes: attributes.length > 0 ? attributes : undefined, attributes: attributes.length > 0 ? attributes : undefined,
@ -1531,7 +1536,14 @@ export class ProductService {
return dto; return dto;
} }
getAttributesObject(attributes:DictItem[]){
if(!attributes) return {}
const obj:any = {}
attributes.forEach(attr=>{
obj[attr.dict.name] = attr
})
return obj
}
// 将单个产品转换为 CSV 行数组 // 将单个产品转换为 CSV 行数组
transformProductToCsvRow( transformProductToCsvRow(
p: Product, p: Product,

View File

@ -15,8 +15,19 @@ export class StatisticsService {
orderItemRepository: Repository<OrderItem>; orderItemRepository: Repository<OrderItem>;
async getOrderStatistics(params: OrderStatisticsParams) { async getOrderStatistics(params: OrderStatisticsParams) {
const { startDate, endDate, grouping, siteId } = params; const { startDate, endDate, grouping, siteId, country } = params;
// const keywords = keyword ? keyword.split(' ').filter(Boolean) : []; // const keywords = keyword ? keyword.split(' ').filter(Boolean) : [];
let siteIds = []
if (country) {
siteIds = await this.getSiteIds(country)
}
if (siteId) {
siteIds.push(siteId)
}
const start = dayjs(startDate).format('YYYY-MM-DD'); const start = dayjs(startDate).format('YYYY-MM-DD');
const end = dayjs(endDate).add(1, 'd').format('YYYY-MM-DD'); const end = dayjs(endDate).add(1, 'd').format('YYYY-MM-DD');
let sql let sql
@ -54,6 +65,8 @@ export class StatisticsService {
AND o.status IN('processing','completed') AND o.status IN('processing','completed')
`; `;
if (siteId) sql += ` AND o.siteId=${siteId}`; if (siteId) sql += ` AND o.siteId=${siteId}`;
if (siteIds.length) sql += ` AND o.siteId IN (${siteIds.join(',')})`;
sql += ` sql += `
GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source
), ),
@ -247,7 +260,10 @@ export class StatisticsService {
LEFT JOIN order_item oi ON o.id = oi.orderId LEFT JOIN order_item oi ON o.id = oi.orderId
WHERE o.date_paid IS NOT NULL WHERE o.date_paid IS NOT NULL
AND o.date_paid >= '${start}' AND o.date_paid < '${end}' AND o.date_paid >= '${start}' AND o.date_paid < '${end}'
AND o.status IN ('processing','completed') AND o.status IN ('processing','completed')`;
if (siteId) sql += ` AND o.siteId=${siteId}`;
if (siteIds.length) sql += ` AND o.siteId IN (${siteIds.join(',')})`;
sql +=`
GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source
), ),
order_sales_summary AS ( order_sales_summary AS (
@ -440,6 +456,10 @@ export class StatisticsService {
LEFT JOIN order_item oi ON o.id = oi.orderId LEFT JOIN order_item oi ON o.id = oi.orderId
WHERE o.date_paid IS NOT NULL WHERE o.date_paid IS NOT NULL
AND o.date_paid >= '${start}' AND o.date_paid < '${end}' AND o.date_paid >= '${start}' AND o.date_paid < '${end}'
`;
if (siteId) sql += ` AND o.siteId=${siteId}`;
if (siteIds.length) sql += ` AND o.siteId IN (${siteIds.join(',')})`;
sql +=`
AND o.status IN ('processing','completed') AND o.status IN ('processing','completed')
GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source GROUP BY o.id, o.date_paid, o.customer_email, o.total, o.source_type, o.siteId, o.utm_source
), ),
@ -1314,7 +1334,14 @@ export class StatisticsService {
} }
async getOrderSorce(params) { async getOrderSorce(params) {
const sql = ` const { country } = params;
let siteIds = []
if (country) {
siteIds = await this.getSiteIds(country)
}
let sql = `
WITH cutoff_months AS ( WITH cutoff_months AS (
SELECT SELECT
DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 7 MONTH), '%Y-%m') AS start_month, DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 7 MONTH), '%Y-%m') AS start_month,
@ -1326,7 +1353,10 @@ export class StatisticsService {
DATE_FORMAT(MIN(date_paid), '%Y-%m') AS first_order_month, DATE_FORMAT(MIN(date_paid), '%Y-%m') AS first_order_month,
SUM(total) AS first_order_total SUM(total) AS first_order_total
FROM \`order\` FROM \`order\`
WHERE status IN ('processing', 'completed') WHERE status IN ('processing', 'completed')`;
if (siteIds.length!=0) sql += ` AND siteId IN ('${siteIds.join("','")}')`;
else sql += ` AND siteId IS NULL `;
sql += `
GROUP BY customer_email GROUP BY customer_email
), ),
order_months AS ( order_months AS (
@ -1334,7 +1364,10 @@ export class StatisticsService {
customer_email, customer_email,
DATE_FORMAT(date_paid, '%Y-%m') AS order_month DATE_FORMAT(date_paid, '%Y-%m') AS order_month
FROM \`order\` FROM \`order\`
WHERE status IN ('processing', 'completed') WHERE status IN ('processing', 'completed')`;
if (siteIds.length!=0) sql += ` AND siteId IN ('${siteIds.join("','")}')`;
else sql += ` AND siteId IS NULL `;
sql += `
), ),
filtered_orders AS ( filtered_orders AS (
SELECT o.customer_email, o.order_month, u.first_order_month,u.first_order_total, c.start_month SELECT o.customer_email, o.order_month, u.first_order_month,u.first_order_total, c.start_month
@ -1366,7 +1399,7 @@ export class StatisticsService {
ORDER BY order_month DESC, first_order_month_group ORDER BY order_month DESC, first_order_month_group
` `
const inactiveSql = ` let inactiveSql = `
WITH WITH
cutoff_months AS ( cutoff_months AS (
SELECT SELECT
@ -1381,7 +1414,10 @@ export class StatisticsService {
date_paid, date_paid,
total total
FROM \`order\` FROM \`order\`
WHERE status IN ('processing', 'completed') WHERE status IN ('processing', 'completed')`;
if (siteIds.length!=0) inactiveSql += ` AND siteId IN ('${siteIds.join("','")}')`;
else inactiveSql += ` AND siteId IS NULL `;
inactiveSql += `
), ),
filtered_users AS ( filtered_users AS (
@ -1524,4 +1560,13 @@ export class StatisticsService {
} }
async getSiteIds(country: any[]) {
const sql = `
SELECT DISTINCT sa.siteId as site_id FROM area a left join site_areas_area sa on a.id = sa.areaId WHERE a.code IN ('${country.join("','")}')
`
const res = await this.orderRepository.query(sql)
return res.map(item => item.site_id)
}
} }