diff --git a/src/app/components/dashboard/dashboard.component.ts b/src/app/components/dashboard/dashboard.component.ts index 012b238..fdd4ca8 100644 --- a/src/app/components/dashboard/dashboard.component.ts +++ b/src/app/components/dashboard/dashboard.component.ts @@ -10,6 +10,7 @@ import { resolve } from 'path'; import { forkJoin } from 'rxjs'; import { UserService } from 'src/app/shared/service/user.service'; import { FirebaseService } from 'src/app/shared/service/firebase.service'; +import { UserInfo } from 'src/app/shared/tables/user'; @Component({ selector: 'app-dashboard', @@ -18,9 +19,11 @@ import { FirebaseService } from 'src/app/shared/service/firebase.service'; }) export class DashboardComponent implements OnInit, AfterViewInit { //Log-in + token = localStorage.getItem("jwt-token") + userInfo: UserInfo; isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); + loggedId: number; + loggedRole: string; shopId: number; //Shop Properties @@ -71,17 +74,20 @@ export class DashboardComponent implements OnInit, AfterViewInit { ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - // this.shopId = Number(localStorage.getItem('shop-id')) + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + if (userInfo.userRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; - const shopIdPromise = new Promise((resolve) => { - const shopId = Number(localStorage.getItem('shop-id')); - resolve(shopId); - }) + // this.shopId = Number(localStorage.getItem('shop-id')) - shopIdPromise.then((shopId) => { - this.shopId = shopId; + // const shopIdPromise = new Promise((resolve) => { + // const shopId = Number(localStorage.getItem('shop-id')); + // resolve(shopId); + // }) + + // shopIdPromise.then((shopId) => { + // this.shopId = shopId; this.productService.getProductsByShopIdNoPageable(this.shopId).subscribe((products) => { let i = 0; @@ -106,44 +112,47 @@ export class DashboardComponent implements OnInit, AfterViewInit { this.doughtnutData(data.products); }) - this.orderService.getOrdersByShopId(this.shopId, this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + this.orderService.getOrdersByShopId(userInfo.shopId, this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) .subscribe(this.processResult()); - this.shopService.getShopRevenue(this.shopId, 30).subscribe((res) => { + this.shopService.getShopRevenue(userInfo.shopId, 30).subscribe((res) => { this.totalRevenue = res; }); this.valueOfShopDistrict(); - }) - } - else { - //ADMIN DO HERE + // }) - this.userService.countNewUser(this.day).subscribe((res) => { - this.totalUser = res; - }) - this.productService.getProductsNoPagination().subscribe((products) => { - let i = 0; - products.forEach(product => { - this.totalReviewCount = this.totalReviewCount + product.reviewCount; - this.totalProducts = this.totalProducts + product.sold; - if (product.averageRating != 0) { - this.totalAverageRating = this.totalAverageRating + product.averageRating; - i++; - } - }); - this.totalAverageRating = this.totalAverageRating / i; - }) + } + else { + //ADMIN DO HERE - this.productService.getProductsPaginationAndSort(0, 5, 'sold', 'desc').subscribe((data) => { - this.products = data.products; - this.doughtnutData(data.products) - }) + this.userService.countNewUser(this.day).subscribe((res) => { + this.totalUser = res; + }) + this.productService.getProductsNoPagination().subscribe((products) => { + let i = 0; + products.forEach(product => { + this.totalReviewCount = this.totalReviewCount + product.reviewCount; + this.totalProducts = this.totalProducts + product.sold; + if (product.averageRating != 0) { + this.totalAverageRating = this.totalAverageRating + product.averageRating; + i++; + } + }); + this.totalAverageRating = this.totalAverageRating / i; + }) - this.orderService - .getOrdersPagination(this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) - .subscribe(this.processResult()); + this.productService.getProductsPaginationAndSort(0, 5, 'sold', 'desc').subscribe((data) => { + this.products = data.products; + this.doughtnutData(data.products) + }) - this.valueOfAdminDistrict(); - } + this.orderService + .getOrdersPagination(this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.processResult()); + + this.valueOfAdminDistrict(); + } + + }) } processResult() { @@ -155,10 +164,6 @@ export class DashboardComponent implements OnInit, AfterViewInit { }; } - - - - // doughnut 2 public view = chartData.view; public doughnutChartColorScheme = chartData.doughnutChartcolorScheme; diff --git a/src/app/components/products/physical/add-product/add-product.component.ts b/src/app/components/products/physical/add-product/add-product.component.ts index 8f5e356..d8b97ae 100644 --- a/src/app/components/products/physical/add-product/add-product.component.ts +++ b/src/app/components/products/physical/add-product/add-product.component.ts @@ -15,6 +15,8 @@ import { ProductImageService } from 'src/app/shared/service/product-image.servic import { ProductImage } from 'src/app/shared/tables/product-image'; import { ToastrService } from 'ngx-toastr'; import { error } from 'console'; +import { UserService } from 'src/app/shared/service/user.service'; +import { userInfo } from 'os'; @Component({ @@ -29,9 +31,8 @@ export class AddProductComponent implements OnInit { public Editor = ClassicEditor; //Log-in + token: string = localStorage.getItem("jwt-token"); isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); shopId: number; adminImg = environment.adminImg; @@ -54,7 +55,8 @@ export class AddProductComponent implements OnInit { private productImageService: ProductImageService, private modalService: BsModalService, private storage: AngularFireStorage, - private toastService: ToastrService) { + private toastService: ToastrService, + private userService: UserService) { this.productForm = this.fb.group({ name: new FormControl("", [Validators.required, Validators.minLength(2)]), price: new FormControl("", [Validators.required, Validators.pattern("^[0-9]*$")]), @@ -66,12 +68,14 @@ export class AddProductComponent implements OnInit { } ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - } - this.productForm.patchValue({ - shopId: this.shopId + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + if (userInfo.userRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; + this.productForm.patchValue({ + shopId: this.shopId + }) + } }) } @@ -138,25 +142,6 @@ export class AddProductComponent implements OnInit { this.modalRef = this.modalService.show(this.errorShopModal, { class: 'modal-sm' }); } }) - - // Promise.all(uploadPromises).then(() => { - - // this.productService.addProduct(product).pipe( - // switchMap((product) => { - // this.createdId = product.id; - // return this.imageUrls; - // }), - // concatMap((url) => { - // const img = new ProductImage(); - // img.id = 0; - // img.imageUrl = url; - // img.productId = this.createdId; - // return this.productImageService.addProductImage(img, img.productId); - // }) - - - // resolve(); - // }) }) } diff --git a/src/app/components/products/physical/product-list/product-list.component.ts b/src/app/components/products/physical/product-list/product-list.component.ts index ea2d68e..b2c6ac0 100644 --- a/src/app/components/products/physical/product-list/product-list.component.ts +++ b/src/app/components/products/physical/product-list/product-list.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit, TemplateRef } from '@angular/core'; import { Router } from '@angular/router'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; import { ProductService } from "../../../../shared/service/product.service"; +import { UserService } from 'src/app/shared/service/user.service'; @Component({ selector: 'app-product-list', @@ -11,8 +12,8 @@ import { ProductService } from "../../../../shared/service/product.service"; export class ProductListComponent implements OnInit { //Login Info - loggedId: number = Number(localStorage.getItem('user-id')); - loggedRole: string = localStorage.getItem('user-role'); + token: string = localStorage.getItem("jwt-token"); + loggedRole: string; isShop: boolean = false; shopId: number; @@ -27,16 +28,20 @@ export class ProductListComponent implements OnInit { theTotalElements = 0; constructor(private productService: ProductService, + private userService: UserService, private modalService: BsModalService, private router: Router) { } ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - } - this.listProduct(); + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.loggedRole = userInfo.userRole; + if (userInfo.userRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; + } + this.listProduct(); + }) } listProduct() { diff --git a/src/app/components/sales/orders/orders.component.ts b/src/app/components/sales/orders/orders.component.ts index e03a47d..1568a1e 100644 --- a/src/app/components/sales/orders/orders.component.ts +++ b/src/app/components/sales/orders/orders.component.ts @@ -10,6 +10,7 @@ import { OrderService } from "src/app/shared/service/order.service"; import { BsModalRef, BsModalService } from "ngx-bootstrap/modal"; import { Shipper } from "src/app/shared/tables/shipper"; import { ShipperService } from "src/app/shared/service/shipper.service"; +import { UserService } from "src/app/shared/service/user.service"; @Component({ selector: "app-orders", @@ -19,9 +20,9 @@ import { ShipperService } from "src/app/shared/service/shipper.service"; }) export class OrdersComponent implements OnInit { //Log-in + token: string = localStorage.getItem("jwt-token"); isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); + loggedRole: string; shopId: number; //Required properties @@ -56,15 +57,19 @@ export class OrdersComponent implements OnInit { orderStatuses = ["Chờ xác nhận", "Đã xác nhận", "Đang giao hàng", "Giao thành công", "Đã huỷ đơn", "Không nhận hàng"]; constructor(private orderService: OrderService, + private userService: UserService, private shipperService: ShipperService, private modalService: BsModalService) { } ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - } - this.listOrder(); + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.loggedRole = userInfo.userRole; + if (this.loggedRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId + } + this.listOrder(); + }) } listOrder() { diff --git a/src/app/components/sales/sales-routing.module.ts b/src/app/components/sales/sales-routing.module.ts index 3e571e2..11a7bf5 100644 --- a/src/app/components/sales/sales-routing.module.ts +++ b/src/app/components/sales/sales-routing.module.ts @@ -3,6 +3,7 @@ import { Routes, RouterModule } from "@angular/router"; import { DetailOrderComponent } from "./detail-order/detail-order.component"; import { OrdersComponent } from "./orders/orders.component"; import { TransactionsComponent } from "./transactions/transactions.component"; +import { ScamOrdersComponent } from "./scam-orders/scam-orders.component"; const routes: Routes = [ { @@ -16,6 +17,14 @@ const routes: Routes = [ breadcrumb: "Danh sách", }, }, + { + path: "scam-orders", + component: ScamOrdersComponent, + data: { + title: "Đơn hàng bị báo cáo", + breadcrumb: "Danh sách", + }, + }, { path: "detail-order/user/:userId/order/:id", component: DetailOrderComponent, diff --git a/src/app/components/sales/sales.module.ts b/src/app/components/sales/sales.module.ts index 60a0524..3517205 100644 --- a/src/app/components/sales/sales.module.ts +++ b/src/app/components/sales/sales.module.ts @@ -8,12 +8,13 @@ import { FormsModule } from '@angular/forms'; import { Ng2SearchPipeModule } from 'ng2-search-filter'; import { SharedModule } from 'src/app/shared/shared.module'; import { DetailOrderComponent } from './detail-order/detail-order.component'; +import { ScamOrdersComponent } from './scam-orders/scam-orders.component'; @NgModule({ - declarations: [OrdersComponent, TransactionsComponent, DetailOrderComponent], + declarations: [OrdersComponent, TransactionsComponent, DetailOrderComponent, ScamOrdersComponent], imports: [ CommonModule, SalesRoutingModule, diff --git a/src/app/components/sales/scam-orders/scam-orders.component.html b/src/app/components/sales/scam-orders/scam-orders.component.html new file mode 100644 index 0000000..a96d2c1 --- /dev/null +++ b/src/app/components/sales/scam-orders/scam-orders.component.html @@ -0,0 +1,200 @@ + +
+
+
+
+ +
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Mã vận đơnThời gianKhách hàngShipperĐịa chỉPhương thứcTrạng tháiSố tiền
{{ order.orderTrackingNumber }}{{ order.orderTime }}{{ order.user.fullName }}{{ order?.shipper?.user?.fullName + }} + Loading... + {{ order.address }} + + {{ order.paymentMethod === "CASH" + ? "Tiền mặt" + : order.paymentMethod === "ZALO PAY" + ? "Zalo Pay" + : "" + }} + + + + {{ + order.status === "AWAITING" + ? "Chờ xác nhận" + : order.status === "CONFIRMED" + ? "Đã xác nhận" + : order.status === "SHIPPING" + ? "Đang giao hàng" + : order.status === "COMPLETED" + ? "Giao thành công" + : order.status === "CANCELED" + ? "Đã huỷ đơn" + : order.status === "REJECT_DELIVERY" + ? "Không nhận hàng" + : "" + }} + + {{ order.total | currency : "VND" }} + + +   + + + + + +   + + + +
+
+ + + +       + + +
+ +
+ +
+ +
+

Không tìm thấy đơn hàng

+
+
+
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/app/components/sales/scam-orders/scam-orders.component.scss b/src/app/components/sales/scam-orders/scam-orders.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/sales/scam-orders/scam-orders.component.spec.ts b/src/app/components/sales/scam-orders/scam-orders.component.spec.ts new file mode 100644 index 0000000..a6a764e --- /dev/null +++ b/src/app/components/sales/scam-orders/scam-orders.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ScamOrdersComponent } from './scam-orders.component'; + +describe('ScamOrdersComponent', () => { + let component: ScamOrdersComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ ScamOrdersComponent ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ScamOrdersComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/components/sales/scam-orders/scam-orders.component.ts b/src/app/components/sales/scam-orders/scam-orders.component.ts new file mode 100644 index 0000000..4fb44c4 --- /dev/null +++ b/src/app/components/sales/scam-orders/scam-orders.component.ts @@ -0,0 +1,237 @@ +import { Component, TemplateRef } from '@angular/core'; +import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; +import { OrderService } from 'src/app/shared/service/order.service'; +import { ShipperService } from 'src/app/shared/service/shipper.service'; +import { UserService } from 'src/app/shared/service/user.service'; +import { Order } from 'src/app/shared/tables/order-list'; +import { Shipper } from 'src/app/shared/tables/shipper'; + +@Component({ + selector: 'app-scam-orders', + templateUrl: './scam-orders.component.html', + styleUrls: ['./scam-orders.component.scss'] +}) +export class ScamOrdersComponent { + //Log-in + token: string = localStorage.getItem("jwt-token"); + isShop: boolean = false; + loggedRole: string; + shopId: number; + + //Required properties + oldid: number = 1; + userId: number; + orderId: number; + + orders: Order[] = []; + + //Pagination Properties + thePageNumber = 1; + thePageSize = 10; + sortBy = "orderTime"; + sortDir = "desc"; + theTotalElements = 0; + + order: Order; + layer1: BsModalRef; + layer2: BsModalRef; + + selectedStatus: string; + isHaveShipper: boolean = false; + shipper: Shipper = undefined; + shippers: Shipper[] = []; + searchName: string = ''; + + //Event + refreshInterval = 5000; + refreshTimeout; + totalOrders = 0; + + orderStatuses = ["Chờ xác nhận", "Đã xác nhận", "Đang giao hàng", "Giao thành công", "Đã huỷ đơn", "Không nhận hàng"]; + + constructor(private orderService: OrderService, + private userService: UserService, + private shipperService: ShipperService, + private modalService: BsModalService) { } + + ngOnInit() { + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.loggedRole = userInfo.userRole; + if (this.loggedRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId + } + this.listOrder(); + }) + } + + listOrder() { + if (this.isShop) { + this.orderService.getOrdersByShopId(this.shopId, this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.processResult()); + + this.shipperService.findFreeShopShipper(this.shopId).subscribe((shippers) => { + this.shippers = shippers; + }) + } + else { + this.orderService + .getOrdersPagination(this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.processResult()); + } + + this.refreshTimeout = setTimeout(() => { + this.refreshOrder(); + }, this.refreshInterval); + } + + refreshOrder() { + if (this.isShop) { + this.orderService.getOrdersByShopId(this.shopId, this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.refreshResult()); + + this.shipperService.findFreeShopShipper(this.shopId).subscribe((shippers) => { + this.shippers = shippers; + }) + } + else { + this.orderService + .getOrdersPagination(this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.refreshResult()); + } + + this.refreshTimeout = setTimeout(() => { + this.refreshOrder(); + }, this.refreshInterval); + } + + searchOrder() { + if (this.searchName.trim() !== '') { + this.orderService.findOrdersByTrackingNumber(this.searchName, this.thePageNumber - 1, this.thePageSize, this.sortBy, this.sortDir) + .subscribe(this.processResult()); + } + else { + this.listOrder() + } + } + + processResult() { + return (data: any) => { + this.orders = data.orders; + this.thePageNumber = data.page.pageNo + 1; + this.thePageSize = data.page.pageSize; + this.theTotalElements = data.page.totalElements; + this.totalOrders = data.page.totalElements; + }; + } + + refreshResult() { + return (data: any) => { + this.orders = data.orders; + this.thePageNumber = data.page.pageNo + 1; + this.thePageSize = data.page.pageSize; + this.theTotalElements = data.page.totalElements; + if (this.totalOrders != this.theTotalElements) { + this.totalOrders = this.theTotalElements; + } + }; + } + + // Change status order modal + openStatusModal(confirmBoxChangeStatus: TemplateRef, userId: number, orderId: number, shipper: Shipper) { + this.isHaveShipper = false; + if (shipper != undefined) { + this.isHaveShipper = true + } + + this.orderService.getOrderById(userId, orderId).subscribe( + (order: Order) => { + this.userId = userId; + this.orderId = orderId; + this.selectedStatus = order.status; + this.layer1 = this.modalService.show(confirmBoxChangeStatus, { class: "modal-sm" }); + }, + (error) => { + console.error(error); // handle error + } + ); + } + + confirmBoxChangeStatus(successChangeStatus: TemplateRef) { + if (this.shipper) { + this.isHaveShipper = true; + } + + // console.log(this.isHaveShipper) + if (this.isHaveShipper) { + this.orderService.updateOrderShipper(this.userId, this.orderId, this.shipper.id).subscribe(() => { + this.orderService.updateOrderStatus(this.userId, this.orderId, this.selectedStatus).subscribe((res) => { }); + this.layer1.hide(); + this.layer1 = this.modalService.show(successChangeStatus, { class: "modal-sm" }); + }) + } + else { + this.orderService.updateOrderStatus(this.userId, this.orderId, this.selectedStatus).subscribe((res) => { }); + this.layer1.hide(); + this.layer1 = this.modalService.show(successChangeStatus, { class: "modal-sm" }); + } + + + // if (!this.isHaveShipper) { + // if (this.shipper != undefined) { + // this.orderService.updateOrderStatus(this.userId, this.orderId, this.selectedStatus).subscribe((res) => { }); + // this.layer1.hide(); + // this.layer1 = this.modalService.show(successChangeStatus, { class: "modal-sm" }); + // } + // else { + // this.orderService.updateOrderShipper(this.userId, this.orderId, this.shipper.id).subscribe(() => { + // this.orderService.updateOrderStatus(this.userId, this.orderId, this.selectedStatus).subscribe((res) => { }); + // this.layer1.hide(); + // this.layer1 = this.modalService.show(successChangeStatus, { class: "modal-sm" }); + // }); + // } + // } + // else { + // this.orderService.updateOrderStatus(this.userId, this.orderId, this.selectedStatus).subscribe((res) => { }); + // this.layer1.hide(); + // this.layer1 = this.modalService.show(successChangeStatus, { class: "modal-sm" }); + // } + } + + decline() { + this.shipper = undefined; + this.layer1.hide(); + } + + successChangeStatus() { + this.listOrder(); + this.layer1.hide(); + } + + + // Delete order modal + openDeleteModal(confirmBoxDelete: TemplateRef, userId: number, orderId: number) { + this.userId = userId + this.orderId = orderId + + this.layer1 = this.modalService.show(confirmBoxDelete, { class: "modal-sm" }); + } + + confirmBoxDelete(userId: number, orderId: number, successDelete: TemplateRef) { + this.orderService.deleteOrderById(this.userId, this.orderId).subscribe(() => { + this.listOrder() + }) + this.layer1.hide() + this.layer1 = this.modalService.show(successDelete, { class: "modal-sm" }); + } + + successDelete() { + this.listOrder(); + this.layer1.hide(); + } + + ngOnDestroy() { + // Xóa timeout khi component bị destroy + clearTimeout(this.refreshTimeout); + } +} diff --git a/src/app/components/setting/profile/profile.component.ts b/src/app/components/setting/profile/profile.component.ts index d76af6e..94c082d 100644 --- a/src/app/components/setting/profile/profile.component.ts +++ b/src/app/components/setting/profile/profile.component.ts @@ -1,4 +1,5 @@ import { Component, OnInit } from '@angular/core'; +import { userInfo } from 'os'; import { UserService } from 'src/app/shared/service/user.service'; import { User } from 'src/app/shared/tables/user'; @@ -9,7 +10,7 @@ import { User } from 'src/app/shared/tables/user'; }) export class ProfileComponent implements OnInit { public active = 1; - loggedId: number = Number(localStorage.getItem('user-id')) + token: string = localStorage.getItem("jwt-token"); user: User; constructor( @@ -17,8 +18,10 @@ export class ProfileComponent implements OnInit { ) { } ngOnInit() { - this.userService.getUserById(this.loggedId).subscribe((user) => { - this.user = user; + this.userService.getUserByToken(this.token).subscribe(userInfo => { + this.userService.getUserById(userInfo.userId).subscribe((user) => { + this.user = user; + }) }) } diff --git a/src/app/components/shippers/create-shipper/create-shipper.component.ts b/src/app/components/shippers/create-shipper/create-shipper.component.ts index a19d232..0bf602e 100644 --- a/src/app/components/shippers/create-shipper/create-shipper.component.ts +++ b/src/app/components/shippers/create-shipper/create-shipper.component.ts @@ -5,6 +5,7 @@ import { Router } from '@angular/router'; import { rejects } from 'assert'; import { url } from 'inspector'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; +import { userInfo } from 'os'; import { resolve } from 'path'; import { finalize, mergeMap, Observable, of } from 'rxjs'; import { switchMap } from 'rxjs-compat/operator/switchMap'; @@ -28,9 +29,8 @@ export class CreateShipperComponent implements OnInit { public active = 1; //Log-in + token: string = localStorage.getItem("jwt-token"); isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); shopId: number; imageFile: File; @@ -57,8 +57,6 @@ export class CreateShipperComponent implements OnInit { this.createPermissionForm(); } - - createPermissionForm() { this.permissionForm = this.formBuilder.group({}); } @@ -84,13 +82,15 @@ export class CreateShipperComponent implements OnInit { } ); - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - this.accountForm.patchValue({ - shopId: this.shopId - }) - } + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + if (userInfo.userRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; + this.accountForm.patchValue({ + shopId: this.shopId + }) + } + }) } // Validation for password and confirm password diff --git a/src/app/components/shippers/list-shipper/list-shipper.component.ts b/src/app/components/shippers/list-shipper/list-shipper.component.ts index 337db0d..5c0433c 100644 --- a/src/app/components/shippers/list-shipper/list-shipper.component.ts +++ b/src/app/components/shippers/list-shipper/list-shipper.component.ts @@ -1,8 +1,10 @@ import { Component, OnInit, TemplateRef } from '@angular/core'; import { Router } from '@angular/router'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; +import { userInfo } from 'os'; import { FirebaseService } from 'src/app/shared/service/firebase.service'; import { ShipperService } from 'src/app/shared/service/shipper.service'; +import { UserService } from 'src/app/shared/service/user.service'; import { Shipper } from 'src/app/shared/tables/shipper'; @Component({ @@ -12,9 +14,9 @@ import { Shipper } from 'src/app/shared/tables/shipper'; }) export class ListShipperComponent { //Log-in properties + token: string = localStorage.getItem("jwt-token"); isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); + loggedRole: string; shopId: number; searchName: string = ''; @@ -33,17 +35,21 @@ export class ListShipperComponent { email: string; constructor(private shipperService: ShipperService, + private userService: UserService, private modalService: BsModalService, private router: Router, private firebaseService: FirebaseService) { } ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - } - this.listAllShipper(); + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.loggedRole = userInfo.userRole; + if (this.loggedRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; + } + this.listAllShipper(); + }) } listAllShipper() { diff --git a/src/app/components/vendors/edit-vendor/edit-vendor.component.html b/src/app/components/vendors/edit-vendor/edit-vendor.component.html index 2286b72..a357904 100644 --- a/src/app/components/vendors/edit-vendor/edit-vendor.component.html +++ b/src/app/components/vendors/edit-vendor/edit-vendor.component.html @@ -61,8 +61,10 @@
- + +
Vui lòng không được bỏ @@ -85,8 +87,7 @@
Vui lòng không được - bỏ - trống ngày sinh.
+ bỏ trống ngày sinh.
@@ -97,8 +98,10 @@
- + +
Vui lòng không được @@ -117,8 +120,10 @@
- + +
Vui lòng không diff --git a/src/app/components/vendors/edit-vendor/edit-vendor.component.ts b/src/app/components/vendors/edit-vendor/edit-vendor.component.ts index 34c5160..a62d35e 100644 --- a/src/app/components/vendors/edit-vendor/edit-vendor.component.ts +++ b/src/app/components/vendors/edit-vendor/edit-vendor.component.ts @@ -21,7 +21,8 @@ import { Ward } from 'src/app/shared/tables/ward'; styleUrls: ['./edit-vendor.component.scss'] }) export class EditVendorComponent implements OnInit { - roleName = localStorage.getItem('user-role'); + token: string = localStorage.getItem("jwt-token"); + roleName: string; //ids userId: number; @@ -55,6 +56,7 @@ export class EditVendorComponent implements OnInit { wards: Ward[] = []; //Shop + isShop: boolean = false; isStudent: boolean = true; isEnabled: boolean = true; userImg: string; @@ -86,8 +88,13 @@ export class EditVendorComponent implements OnInit { } ngOnInit() { + this.userService.getUserByToken(this.token).subscribe(userInfo => { + this.roleName = userInfo.userRole; + if (userInfo.userRole != "ROLE_ADMIN") { + this.isShop = true; + } + }) const shopId = +this.route.snapshot.paramMap.get("id")!; - console.log(shopId); this.getAllDistrict().then(() => { this.shopService.getShopById(shopId).subscribe((data => this.fillFormToUpdate(data))) }).catch(error => { }); diff --git a/src/app/shared/components/footer/footer.component.ts b/src/app/shared/components/footer/footer.component.ts index 9ebe388..1c43638 100644 --- a/src/app/shared/components/footer/footer.component.ts +++ b/src/app/shared/components/footer/footer.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core'; import { OrderService } from '../../service/order.service'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; import { Router } from '@angular/router'; +import { UserService } from '../../service/user.service'; @Component({ selector: 'app-footer', @@ -10,8 +11,8 @@ import { Router } from '@angular/router'; }) export class FooterComponent implements OnInit { //Log-in - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); + token = localStorage.getItem('jwt-token') + loggedRole: string; shopId: number; //Pagination Properties @@ -31,15 +32,18 @@ export class FooterComponent implements OnInit { @ViewChild('new_order') newOrderTemplate: TemplateRef; constructor( + private userService: UserService, private orderService: OrderService, private modalService: BsModalService, private router: Router) { } ngOnInit() { - if (this.loggedRole == 'ROLE_SHOP') { - this.shopId = Number(localStorage.getItem('shop-id')) - this.countShopOrder(); - } + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + if (userInfo.userRole == 'ROLE_SHOP') { + this.shopId = userInfo.shopId; + this.countShopOrder(); + } + }) } countShopOrder() { diff --git a/src/app/shared/components/header/header.component.ts b/src/app/shared/components/header/header.component.ts index 85226ee..3696302 100644 --- a/src/app/shared/components/header/header.component.ts +++ b/src/app/shared/components/header/header.component.ts @@ -18,9 +18,10 @@ export class HeaderComponent implements OnInit { public user: User; //Log-in + token: string = localStorage.getItem("jwt-token"); isShop: boolean = false; - loggedId: number = Number(localStorage.getItem('user-id')) - loggedRole = localStorage.getItem('user-role'); + loggedId: number; + loggedRole: string; shopId: number; @Output() rightSidebarEvent = new EventEmitter(); @@ -29,9 +30,7 @@ export class HeaderComponent implements OnInit { public navServices: NavService, private userService: UserService, private firebaseService: FirebaseService) { - this.userService.getUserById(Number(localStorage.getItem('user-id'))).subscribe((user) => { - this.user = user; - }) + } collapseSidebar() { @@ -49,10 +48,18 @@ export class HeaderComponent implements OnInit { ngOnInit() { - if (this.loggedRole != 'ROLE_ADMIN') { - this.isShop = true; - this.shopId = Number(localStorage.getItem('shop-id')) - } + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.loggedId = userInfo.userId; + this.loggedRole = userInfo.userRole; + + if (this.loggedRole != 'ROLE_ADMIN') { + this.isShop = true; + this.shopId = userInfo.shopId; + } + this.userService.getUserById(userInfo.userId).subscribe((user) => { + this.user = user; + }) + }) } logOut() { diff --git a/src/app/shared/components/sidebar/sidebar.component.ts b/src/app/shared/components/sidebar/sidebar.component.ts index d9be684..800e3f0 100644 --- a/src/app/shared/components/sidebar/sidebar.component.ts +++ b/src/app/shared/components/sidebar/sidebar.component.ts @@ -15,34 +15,65 @@ export class SidebarComponent { public menuItems: Menu[]; public url: any; public fileurl: any; + token = localStorage.getItem('jwt-token') user: User; constructor(private router: Router, public navServices: NavService, private userService: UserService) { - this.userService.getUserById(Number(localStorage.getItem('user-id'))).subscribe((user) => { - this.user = user; + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.userService.getUserById(userInfo.userId).subscribe((user) => { + this.user = user; + }) + + if (userInfo.userRole == 'ROLE_ADMIN') { + this.navServices.adminItems.subscribe(menuItems => { + this.menuItems = menuItems + this.router.events.subscribe((event) => { + if (event instanceof NavigationEnd) { + menuItems.filter(items => { + if (items.path === event.url) + this.setNavActive(items) + if (!items.children) return false + items.children.filter(subItems => { + if (subItems.path === event.url) + this.setNavActive(subItems) + if (!subItems.children) return false + subItems.children.filter(subSubItems => { + if (subSubItems.path === event.url) + this.setNavActive(subSubItems) + }) + }) + }) + } + }) + }) + } + else { + this.navServices.shopItems.subscribe(menuItems => { + this.menuItems = menuItems + this.router.events.subscribe((event) => { + if (event instanceof NavigationEnd) { + menuItems.filter(items => { + if (items.path === event.url) + this.setNavActive(items) + if (!items.children) return false + items.children.filter(subItems => { + if (subItems.path === event.url) + this.setNavActive(subItems) + if (!subItems.children) return false + subItems.children.filter(subSubItems => { + if (subSubItems.path === event.url) + this.setNavActive(subSubItems) + }) + }) + }) + } + }) + }) + } }) - this.navServices.items.subscribe(menuItems => { - this.menuItems = menuItems - this.router.events.subscribe((event) => { - if (event instanceof NavigationEnd) { - menuItems.filter(items => { - if (items.path === event.url) - this.setNavActive(items) - if (!items.children) return false - items.children.filter(subItems => { - if (subItems.path === event.url) - this.setNavActive(subItems) - if (!subItems.children) return false - subItems.children.filter(subSubItems => { - if (subSubItems.path === event.url) - this.setNavActive(subSubItems) - }) - }) - }) - } - }) - }) + + } // Active Nave state diff --git a/src/app/shared/guard/admin-guard.service.ts b/src/app/shared/guard/admin-guard.service.ts index 11937ee..58315c2 100644 --- a/src/app/shared/guard/admin-guard.service.ts +++ b/src/app/shared/guard/admin-guard.service.ts @@ -1,19 +1,27 @@ import { Injectable } from '@angular/core'; import { CanActivate, Router } from '@angular/router'; import { FirebaseService } from '../service/firebase.service'; +import { UserService } from '../service/user.service'; @Injectable({ providedIn: 'root' }) export class AdminGuard implements CanActivate { + token: string = localStorage.getItem('jwt-token'); + role: string; constructor( private firebaseService: FirebaseService, + private userService: UserService, private router: Router - ) { } + ) { + this.userService.getUserByToken(this.token).subscribe((userInfo) => { + this.role = userInfo.userRole; + }) + } canActivate(): boolean { - if (this.firebaseService.isAdmin()) { + if (this.role == 'ROLE_ADMIN') { return true; } else { diff --git a/src/app/shared/service/firebase.service.ts b/src/app/shared/service/firebase.service.ts index 2f9b4d0..a140187 100644 --- a/src/app/shared/service/firebase.service.ts +++ b/src/app/shared/service/firebase.service.ts @@ -10,6 +10,7 @@ import { environment } from 'src/environments/environment'; import { StringBoolObject } from '../tables/string-bool-object'; import { Observable } from 'rxjs'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; +import { UserInfo } from '../tables/user'; @Injectable({ providedIn: 'root' @@ -58,21 +59,14 @@ export class FirebaseService { this.userService.getUserByEmailOrPhoneNumber(email).subscribe((user) => { if (user.role.roleName == 'ROLE_ADMIN') { this.loggedIn = true; - localStorage.setItem('user-role', user.role.roleName); - localStorage.setItem('email', user.email); - localStorage.setItem('user-id', user.id.toString()); localStorage.setItem('is-logged', JSON.stringify(this.loggedIn)); this.router.navigate(['/dashboard/default']); resolve(true); // trả về true nếu đăng nhập thành công } else if (user.role.roleName == 'ROLE_SHOP') { - localStorage.setItem('user-role', user.role.roleName); - localStorage.setItem('user-email', user.email); - localStorage.setItem('user-id', user.id.toString()); this.shopService.getShopByUserId(user.id).subscribe((shop) => { if (shop.isEnabled) { this.loggedIn = true; localStorage.setItem('is-logged', JSON.stringify(this.loggedIn)) - localStorage.setItem('shop-id', shop.id.toString()); this.router.navigate(['/dashboard/default']); resolve(true); // trả về true nếu đăng nhập thành công } else { @@ -125,11 +119,14 @@ export class FirebaseService { } } - isAdmin() { - const role = localStorage.getItem('user-role'); - if (role == 'ROLE_ADMIN') { - return true; - } + isAdmin(): boolean { + this.userService.getUserByToken(localStorage.getItem("jwt-token")).subscribe((userInfo) => { + const role = userInfo.userRole; + if (role == 'ROLE_ADMIN') { + return true; + } + return false; + }) return false; } diff --git a/src/app/shared/service/nav.service.ts b/src/app/shared/service/nav.service.ts index 19f7c4f..7b5d8da 100644 --- a/src/app/shared/service/nav.service.ts +++ b/src/app/shared/service/nav.service.ts @@ -1,6 +1,7 @@ import { HostListener, Inject, Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { WINDOW } from "./windows.service"; +import { UserService } from './user.service'; // Menu export interface Menu { @@ -23,10 +24,13 @@ export class NavService { public screenWidth: any public collapseSidebar: boolean = false - public roleName: string = localStorage.getItem('user-role'); - public items: BehaviorSubject; + public token = localStorage.getItem('jwt-token'); + public roleName: string; + public adminItems: BehaviorSubject; + public shopItems: BehaviorSubject; - constructor(@Inject(WINDOW) private window) { + constructor(@Inject(WINDOW) private window, + private userService: UserService) { this.onResize(); if (this.screenWidth < 991) { this.collapseSidebar = true @@ -35,12 +39,8 @@ export class NavService { } private updateMenuItems() { - if (this.roleName == 'ROLE_ADMIN') { - this.items = new BehaviorSubject(this.MENUITEMS); - } - else { - this.items = new BehaviorSubject(this.SHOP_ITEMS); - } + this.adminItems = new BehaviorSubject(this.MENUITEMS); + this.shopItems = new BehaviorSubject(this.SHOP_ITEMS); } // Windows width @@ -88,6 +88,7 @@ export class NavService { { title: 'Đơn hàng', icon: 'dollar-sign', type: 'sub', active: false, children: [ { path: '/sales/orders', title: 'Danh sách', type: 'link' }, + { path: '/sales/scam-orders', title: 'Bị báo cáo', type: 'link' } // { path: '/sales/transactions', title: 'Giao dịch', type: 'link' }, ] }, @@ -168,6 +169,13 @@ export class NavService { badgeType: 'primary', active: false }, + { + title: 'Đơn hàng', path: '/sales/orders', icon: 'dollar-sign', type: 'link', active: false, + // children: [ + // // { path: '/sales/orders', title: 'Danh sách', type: 'link' }, + // // { path: '/sales/transactions', title: 'Giao dịch', type: 'link' }, + // ] + }, { title: 'Sản phẩm', icon: 'box', type: 'sub', active: false, children: [ { path: '/products/add-product', title: 'Thêm sản phẩm', type: 'link' }, @@ -194,12 +202,7 @@ export class NavService { // {path: '/products/digital/digital-add-product', title: 'Add Product', type: 'link'}, // ] // }, - { - title: 'Đơn hàng', icon: 'dollar-sign', type: 'sub', active: false, children: [ - { path: '/sales/orders', title: 'Danh sách', type: 'link' }, - { path: '/sales/transactions', title: 'Giao dịch', type: 'link' }, - ] - }, + // { // title: 'Coupons', icon: 'tag', type: 'sub', active: false, children: [ // {path: '/coupons/list-coupons', title: 'List Coupons', type: 'link'}, diff --git a/src/app/shared/service/user.service.ts b/src/app/shared/service/user.service.ts index f3f81c6..fc08064 100644 --- a/src/app/shared/service/user.service.ts +++ b/src/app/shared/service/user.service.ts @@ -4,7 +4,8 @@ import { Observable } from 'rxjs-compat'; import { environment } from 'src/environments/environment'; import { Address } from '../tables/address'; import { StringBoolObject } from '../tables/string-bool-object'; -import { User } from '../tables/user'; +import { User, UserInfo } from '../tables/user'; +import { userInfo } from 'os'; @Injectable({ providedIn: 'root' @@ -84,4 +85,18 @@ export class UserService { return this.httpClient.get(this.baseUrl + `/count?day=${day}`); } + //Get User By Token + getUserByToken(token: string) { + return this.httpClient.get(this.baseUrl + `/info?token=${token}`); + } + + //User Info + getUserInfo(): UserInfo { + const token = localStorage.getItem("jwt-token"); + this.httpClient.get(this.baseUrl + `/info?token=${token}`).subscribe((userInfo) => { + console.log(userInfo) + return userInfo; + }) + return null; + } } \ No newline at end of file diff --git a/src/app/shared/tables/user.ts b/src/app/shared/tables/user.ts index 3e14dfd..f98aa32 100644 --- a/src/app/shared/tables/user.ts +++ b/src/app/shared/tables/user.ts @@ -16,4 +16,11 @@ export class User { //Response addresses: Address[] role: Role; +} + +export class UserInfo { + userId: number; + shopId: number; + userRole: string; + userEmail: string; } \ No newline at end of file