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)
@ApiOperation({ summary: 'create order ' })
createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) {
console.log(userId)
return this.orderService.createOrderAsUser(userId, body);
}
@@ -8,7 +8,7 @@ import { IAttribute } from '../interface/order.interface';
@Entity({ tableName: 'order_items' })
@Index({ properties: ['order'] })
export class OrderItem extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint
@@ -18,8 +18,8 @@ export class OrderItem extends BaseEntity {
@ManyToOne(() => Product)
product!: Product;
@Property({ type: 'json' })
attributes: IAttribute[]
@Property({ type: 'json', nullable: true })
attributes?: IAttribute[]
@Property({ type: 'int' })
quantity!: number;
+14 -14
View File
@@ -74,14 +74,14 @@ export class Order {
// @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true })
// subTotal?: number;
@Formula(alias => `
(
SELECT COALESCE(SUM(oi.sub_total), 0)
FROM order_items oi
WHERE oi.order_id = ${alias}.id
)
`)
subTotal!: number;
@Formula(alias => `
(
SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi
WHERE oi.order_id = ${alias}.id
)
`)
subTotal!: number;
// @Property({ type: 'decimal', precision: 10, scale: 0, default: null, nullable: true })
@@ -91,7 +91,7 @@ export class Order {
CASE
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
WHERE oi.order_id = ${alias}.id
) * 0.1
@@ -108,7 +108,7 @@ export class Order {
@Formula(alias => `
(
(
SELECT COALESCE(SUM(oi.sub_total), 0)
SELECT COALESCE(SUM(COALESCE(oi.unit_price, 0) * oi.quantity), 0)
FROM order_items oi
WHERE oi.order_id = ${alias}.id
)
@@ -122,7 +122,7 @@ export class Order {
CASE
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
WHERE oi.order_id = ${alias}.id
) * 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
WHERE oi.order_id = ${alias}.id
)
@@ -172,7 +172,7 @@ export class Order {
CASE
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
WHERE oi.order_id = ${alias}.id
) * 0.1
@@ -227,7 +227,7 @@ export class Order {
const maxOrder = await em.findOne(
Order,
{},
{ id: { $ne: null } },
{
orderBy: { orderNumber: 'DESC' },
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({
creator: admin,
@@ -96,6 +96,7 @@ export class OrderService {
em.persist(order)
const productIds = items.map(item => item.productId)
const products = await this.productRepository.find({
id: { $in: productIds }
})
@@ -104,19 +105,18 @@ export class OrderService {
}
for (const item of items) {
this.persistOrderItem(order, item)
await this.persistOrderItem(order, item)
}
// TODO : calculation must be done after create order
// await this.calculateOrder(order)
// await em.flush()
await em.flush()
return order
})
return order
}
@@ -154,7 +154,7 @@ export class OrderService {
order.attachments = attachments
}
if (enableTax != undefined) {
if (enableTax != undefined) {
order.enableTax = enableTax
}
@@ -178,7 +178,7 @@ export class OrderService {
async updateOrderAsAdmin(orderId: string, dto: IUpdateOrder) {
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)
@@ -230,7 +230,7 @@ export class OrderService {
if (found) {
throw new BadRequestException(`Product already exists`)
}
console.log(productId)
const product = await this.productRepository.findOne({ id: productId })
if (!product) {
throw new BadRequestException(`Product not found`)