import { Injectable } from '@angular/core'; import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from '@angular/fire/firestore'; import { Myorder } from './services/myorder'; import { ServiceService } from './../config/service.service'; import * as firebase from 'firebase'; import { Observable, Subject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class MyordersService { product: Myorder[] = []; riderId: string; orders: Myorder[]; selItem: Myorder; constructor( public afs: AngularFirestore, public service: ServiceService ) { this.riderId = 'LVIMCEdUOGMzYxISL5OkMUPD35Q2'; const users = this.service.get('user').then((data) => { if (data) { data = JSON.parse(data); this.riderId = data.uid; if (this.riderId !== undefined) { this.orderList(this.riderId); } } else { this.riderId = 'LVIMCEdUOGMzYxISL5OkMUPD35Q2'; } }); } public async orderList(riderId) { console.log('called'); console.log(riderId); const This = this; riderId = 'qbTKza18mWVzYG9NLIbmjMbrYjG2'; const orderRef: AngularFirestoreCollection<any> = this.afs.collection('orders', ref => ref.where('riderId', '==', riderId) .where('orderStatus', '==', 2) .orderBy('bookDate', 'desc')); orderRef.valueChanges().subscribe((value) => { this.orders = []; const res = value; if (res.length > 0) { res.forEach((item) => { console.log(item); const order: Myorder = { amount: item.amount, status: item.status, orderStatus: item.orderStatus, bookDate: item.bookDate, deliveryLocation: item.deliveryLocation, deliveryAddress: item.deliveryAddress, pickupAddress: item.pickupAddress, pickupLocation: item.pickupLocation, riderName: 'John', orderId: item.orderId, orderCode: item.orderCode, product: this.service.key2Array(item.product), time: '60' }; this.orders.push(order); }); console.log(this.orders); } else { // alert('No Orders Found'); } }); } public async trackOrder(order) { this.selItem = order; } public async rideStatus(status: number) { this.afs.collection('orders').doc(this.selItem.orderId).update({orderStatus: status}).then(() => { console.log('Booking Successfully'); }); } public async rideChange(riderId: string, state: number) { this.riderId = 'qbTKza18mWVzYG9NLIbmjMbrYjG2'; if (state === 1) { const change = {currOrder: riderId, rideState: 1}; this.afs.collection('riders').doc(this.riderId).update(change).then(() => { console.log('Rider Set Booking'); }); } else { const change = {currOrder: '', rideState: 0}; this.afs.collection('riders').doc(this.riderId).update(change).then(() => { console.log('Rider Set Booking'); }); } } public getStatus(orderId): Observable<any> { const orderRef: AngularFirestoreCollection<any> = this.afs.collection('orders', ref => ref.where('orderId', '==', orderId)); return orderRef.valueChanges(); } public async rideReject(riderId: string) { this.afs.collection('orders').doc(riderId).update({riderId: ''}).then(() => { console.log('Booking Rejected'); }); } }