This commit is contained in:
2026-01-27 11:46:07 +03:30
parent 75f2c4b73e
commit 935f905e29
4 changed files with 25 additions and 24 deletions
@@ -23,6 +23,7 @@ export class OrderController {
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiOperation({ summary: 'create order ' }) @ApiOperation({ summary: 'create order ' })
createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) { createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) {
console.log(userId)
return this.orderService.createOrderAsUser(userId, body); return this.orderService.createOrderAsUser(userId, body);
} }
@@ -8,7 +8,7 @@ import { IAttribute } from '../interface/order.interface';
@Entity({ tableName: 'order_items' }) @Entity({ tableName: 'order_items' })
@Index({ properties: ['order'] }) @Index({ properties: ['order'] })
export class OrderItem extends BaseEntity { export class OrderItem extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true }) @PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint id: bigint
@@ -18,8 +18,8 @@ export class OrderItem extends BaseEntity {
@ManyToOne(() => Product) @ManyToOne(() => Product)
product!: Product; product!: Product;
@Property({ type: 'json' }) @Property({ type: 'json', nullable: true })
attributes: IAttribute[] attributes?: IAttribute[]
@Property({ type: 'int' }) @Property({ type: 'int' })
quantity!: number; quantity!: number;
+14 -14
View File
@@ -74,14 +74,14 @@ export class Order {
// @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true }) // @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true })
// subTotal?: number; // subTotal?: number;
@Formula(alias => ` @Formula(alias => `
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) )
`) `)
subTotal!: number; subTotal!: number;
// @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true }) // @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true })
@@ -91,7 +91,7 @@ export class Order {
CASE CASE
WHEN ${alias}.enable_tax = true THEN WHEN ${alias}.enable_tax = true THEN
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) * 0.1 ) * 0.1
@@ -108,7 +108,7 @@ export class Order {
@Formula(alias => ` @Formula(alias => `
( (
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) )
@@ -122,7 +122,7 @@ export class Order {
CASE CASE
WHEN ${alias}.enable_tax = true THEN WHEN ${alias}.enable_tax = true THEN
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) * 0.1 ) * 0.1
@@ -158,7 +158,7 @@ export class Order {
( (
( (
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) )
@@ -172,7 +172,7 @@ export class Order {
CASE CASE
WHEN ${alias}.enable_tax = true THEN WHEN ${alias}.enable_tax = true THEN
( (
SELECT COALESCE(SUM(oi.sub_total), 0) SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi FROM order_items oi
WHERE oi.order_id = ${alias}.id WHERE oi.order_id = ${alias}.id
) * 0.1 ) * 0.1
@@ -227,7 +227,7 @@ export class Order {
const maxOrder = await em.findOne( const maxOrder = await em.findOne(
Order, Order,
{}, { id: { $ne: null } },
{ {
orderBy: { orderNumber: 'DESC' }, orderBy: { orderNumber: 'DESC' },
fields: ['orderNumber'], fields: ['orderNumber'],
+7 -7
View File
@@ -80,7 +80,7 @@ export class OrderService {
} }
} }
const order = await this.em.transactional(async (em) => { return this.em.transactional(async (em) => {
const order = this.orderRepository.create({ const order = this.orderRepository.create({
creator: admin, creator: admin,
@@ -96,6 +96,7 @@ export class OrderService {
em.persist(order) em.persist(order)
const productIds = items.map(item => item.productId) const productIds = items.map(item => item.productId)
const products = await this.productRepository.find({ const products = await this.productRepository.find({
id: { $in: productIds } id: { $in: productIds }
}) })
@@ -104,19 +105,18 @@ export class OrderService {
} }
for (const item of items) { for (const item of items) {
this.persistOrderItem(order, item) await this.persistOrderItem(order, item)
} }
// TODO : calculation must be done after create order // TODO : calculation must be done after create order
// await this.calculateOrder(order) // await this.calculateOrder(order)
// await em.flush() await em.flush()
return order return order
}) })
return order
} }
@@ -154,7 +154,7 @@ export class OrderService {
order.attachments = attachments order.attachments = attachments
} }
if (enableTax != undefined) { if (enableTax != undefined) {
order.enableTax = enableTax order.enableTax = enableTax
} }
@@ -178,7 +178,7 @@ export class OrderService {
async updateOrderAsAdmin(orderId: string, dto: IUpdateOrder) { async updateOrderAsAdmin(orderId: string, dto: IUpdateOrder) {
const order = await this.findOneOrFail(orderId) const order = await this.findOneOrFail(orderId)
const updateOrder= await this.updateOrder(order, dto) const updateOrder = await this.updateOrder(order, dto)
// const updateOrder = await this.calculateOrder(order) // const updateOrder = await this.calculateOrder(order)
@@ -230,7 +230,7 @@ export class OrderService {
if (found) { if (found) {
throw new BadRequestException(`Product already exists`) throw new BadRequestException(`Product already exists`)
} }
console.log(productId)
const product = await this.productRepository.findOne({ id: productId }) const product = await this.productRepository.findOne({ id: productId })
if (!product) { if (!product) {
throw new BadRequestException(`Product not found`) throw new BadRequestException(`Product not found`)