juice-shop

Форк
0
/
accounting.component.ts 
129 строк · 4.5 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { ProductService } from '../Services/product.service'
7
import { type AfterViewInit, Component, type OnDestroy, ViewChild } from '@angular/core'
8
import { MatPaginator } from '@angular/material/paginator'
9
import { type Subscription } from 'rxjs'
10
import { MatTableDataSource } from '@angular/material/table'
11
import { QuantityService } from '../Services/quantity.service'
12
import { library } from '@fortawesome/fontawesome-svg-core'
13
import { faCheck } from '@fortawesome/free-solid-svg-icons'
14
import { OrderHistoryService } from '../Services/order-history.service'
15
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
16

17
library.add(faCheck)
18

19
interface Order {
20
  id: string
21
  orderId: string
22
  totalPrice: number
23
  delivered: boolean
24
}
25

26
@Component({
27
  selector: 'app-accounting',
28
  templateUrl: './accounting.component.html',
29
  styleUrls: ['./accounting.component.scss']
30
})
31
export class AccountingComponent implements AfterViewInit, OnDestroy {
32
  public orderHistoryColumns = ['OrderId', 'Price', 'Status', 'StatusButton']
33
  @ViewChild('paginatorOrderHistory', { static: true }) paginatorOrderHistory: MatPaginator
34
  public orderData: Order[]
35
  public orderSource
36
  public displayedColumns = ['Product', 'Price', 'Quantity']
37
  public tableData: any[]
38
  public dataSource
39
  @ViewChild('paginator', { static: true }) paginator: MatPaginator
40
  private productSubscription: Subscription
41
  private quantitySubscription: Subscription
42
  public quantityMap: any
43
  constructor (private readonly productService: ProductService, private readonly quantityService: QuantityService, private readonly orderHistoryService: OrderHistoryService, private readonly snackBarHelperService: SnackBarHelperService) { }
44

45
  ngAfterViewInit () {
46
    this.loadQuantity()
47
    this.loadProducts()
48
    this.loadOrders()
49
  }
50

51
  loadQuantity () {
52
    this.quantitySubscription = this.quantityService.getAll().subscribe((stock) => {
53
      this.quantityMap = {}
54
      stock.forEach((item) => {
55
        this.quantityMap[item.ProductId] = {
56
          id: item.id,
57
          quantity: item.quantity
58
        }
59
      })
60
    }, (err) => { console.log(err) })
61
  }
62

63
  loadProducts () {
64
    this.productSubscription = this.productService.search('').subscribe((tableData: any) => {
65
      this.tableData = tableData
66
      this.dataSource = new MatTableDataSource<Element>(this.tableData)
67
      this.dataSource.paginator = this.paginator
68
    }, (err) => { console.log(err) })
69
  }
70

71
  loadOrders () {
72
    this.orderHistoryService.getAll().subscribe((orders) => {
73
      this.orderData = []
74
      for (const order of orders) {
75
        this.orderData.push({
76
          id: order._id,
77
          orderId: order.orderId,
78
          totalPrice: order.totalPrice,
79
          delivered: order.delivered
80
        })
81
      }
82
      this.orderSource = new MatTableDataSource<Order>(this.orderData)
83
      this.orderSource.paginator = this.paginatorOrderHistory
84
    }, (err) => { console.log(err) })
85
  }
86

87
  ngOnDestroy () {
88
    if (this.productSubscription) {
89
      this.productSubscription.unsubscribe()
90
    }
91
    if (this.quantitySubscription) {
92
      this.quantitySubscription.unsubscribe()
93
    }
94
  }
95

96
  modifyQuantity (id, value) {
97
    this.quantityService.put(id, { quantity: value < 0 ? 0 : value }).subscribe((quantity) => {
98
      const product = this.tableData.find((product) => {
99
        return product.id === quantity.ProductId
100
      })
101
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
102
      this.snackBarHelperService.open(`Quantity for ${product.name} has been updated.`, 'confirmBar')
103
      this.loadQuantity()
104
    }, (err) => {
105
      this.snackBarHelperService.open(err.error, 'errorBar')
106
      console.log(err)
107
    })
108
  }
109

110
  modifyPrice (id, value) {
111
    this.productService.put(id, { price: value < 0 ? 0 : value }).subscribe((product) => {
112
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
113
      this.snackBarHelperService.open(`Price for ${product.name} has been updated.`, 'confirmBar')
114
      this.loadProducts()
115
    }, (err) => {
116
      this.snackBarHelperService.open(err.error, 'errorBar')
117
      console.log(err)
118
    })
119
  }
120

121
  changeDeliveryStatus (deliveryStatus, orderId) {
122
    this.orderHistoryService.toggleDeliveryStatus(orderId, { deliveryStatus }).subscribe(() => {
123
      this.loadOrders()
124
    }, (err) => {
125
      this.snackBarHelperService.open(err, 'errorBar')
126
      console.log(err)
127
    })
128
  }
129
}
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.