diff --git a/integration_test/booking_test_firebase.dart b/integration_test/booking_test_firebase.dart new file mode 100644 index 0000000..1c0bc98 --- /dev/null +++ b/integration_test/booking_test_firebase.dart @@ -0,0 +1,33 @@ +import 'package:flutter_test/flutter_test.dart'; + +import 'package:integration_test/integration_test.dart'; + +import 'package:mera_aadhar/main.dart' as app; +import 'package:firebase_core/firebase_core.dart'; + +import 'package:mera_aadhar/fixtures/booking_fixture.dart'; +import 'package:mera_aadhar/models/booking_model.dart'; +import 'package:mera_aadhar/firebase/booking_db.dart'; +import 'package:flutter/foundation.dart'; + +Future addDelay(int ms) async { + await Future.delayed(Duration(milliseconds: ms)); +} + + +void main() { + + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + testWidgets('Authentication Testing', (WidgetTester tester) async { + await Firebase.initializeApp(); + + debugPrint("init nwo dummy"); + Booking dummy = BookingFixture.dummyBooking(); + + BookingDB bdb = new BookingDB(); + await bdb.addNewBooking(dummy); + debugPrint("Successfully added booking!"); + }); + +} \ No newline at end of file diff --git a/lib/firebase/booking_db.dart b/lib/firebase/booking_db.dart new file mode 100644 index 0000000..d3dd252 --- /dev/null +++ b/lib/firebase/booking_db.dart @@ -0,0 +1,36 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:mera_aadhar/models/booking_model.dart'; +import 'package:firebase_auth/firebase_auth.dart'; + +class BookingDB { + +CollectionReference _collectionRef = + FirebaseFirestore.instance.collection('bookings'); + + Future addNewBooking(Booking booking) { + return _collectionRef + .add(booking.toJson()) + .then((value) => print("Booking entry added")) + .catchError((error) => print("Failed to add entry: $error")); + } + + Future getMyCurrentBooking() async { + FirebaseAuth auth = FirebaseAuth.instance; + + print("my ph "+(auth.currentUser?.phoneNumber)!); + + QuerySnapshot snap = await _collectionRef + .where('phoneNo', isEqualTo: auth.currentUser?.phoneNumber) + .orderBy('timestamp', descending: true) + .limit(1) + .get(); + + if(snap.size != 0){ + Booking b = Booking.fromJson(snap.docs[0].data() as Map); + if(b.bookingStatus! != "Completed") return b; + else return null; + } + else return null; + } + +} \ No newline at end of file diff --git a/lib/firebase/operator_db.dart b/lib/firebase/operator_db.dart new file mode 100644 index 0000000..1c1c6b9 --- /dev/null +++ b/lib/firebase/operator_db.dart @@ -0,0 +1,49 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:mera_aadhar/models/operator_model.dart'; +import 'package:firebase_auth/firebase_auth.dart'; + +class OperatorDB { + +CollectionReference _collectionRef = + FirebaseFirestore.instance.collection('operators'); + + Future addNewOperator(Operator booking) { + // TODO: UNSAFE/TESTING ONLY + return _collectionRef + .add(booking.toJson()) + .then((value) => print("Operator entry added")) + .catchError((error) => print("Failed to add entry: $error")); + } + + Future getOperatorById(int operatorId) async { + QuerySnapshot snap = await _collectionRef + .where('operatorId', isEqualTo: operatorId) + .limit(1) + .get(); + + if(snap.size != 0){ + Operator b = Operator.fromJson(snap.docs[0].data() as Map); + if(b.isOperatorActive! != false) return b; + else return null; + } + else return null; + } + + Future> getOperatorsByLatLong(String latlong) async { + QuerySnapshot snap = await _collectionRef + .where('centerLocation', isEqualTo: latlong) + .get(); + + List operators = []; + + snap.docs.forEach((doc){ + Operator op = Operator.fromJson(doc.data() as Map); + if(op.isOperatorActive!){ + operators.add(op); + } + }); + + return operators; + } + +} \ No newline at end of file diff --git a/lib/fixtures/booking_fixture.dart b/lib/fixtures/booking_fixture.dart new file mode 100644 index 0000000..bb0cf99 --- /dev/null +++ b/lib/fixtures/booking_fixture.dart @@ -0,0 +1,18 @@ +import 'package:mera_aadhar/models/booking_model.dart'; +import 'dart:convert'; + +class BookingFixture { + static Booking dummyBooking(){ + Map data = jsonDecode('{"bookingId":42,"operatorId":22451,"bookingType":0,"phoneNo":"+918872276957","bookingLocation":{"lat":23.25564,"lon":21.22134},"userdata":{"phoneNo":"8872276957","type":0,"locationText":"Hostel O, Tiet"},"paydata":{"type":"CASH","receipt":null,"status":"Success"},"bookingStatus":"Completed","timestamp":1660465314}'); + final Booking bookin = Booking.fromJson(data); + return bookin; + } + + Future fixture_booking_getMyCurrentBooking() async { + return dummyBooking(); + } + + Future fixture_nobooking_getMyCurrentBooking() async { + return null; + } +} diff --git a/lib/fixtures/operator_fixture.dart b/lib/fixtures/operator_fixture.dart new file mode 100644 index 0000000..7867596 --- /dev/null +++ b/lib/fixtures/operator_fixture.dart @@ -0,0 +1,11 @@ +import 'package:mera_aadhar/models/operator_model.dart'; +import 'dart:convert'; + +class OperatorFixture { + static Operator dummySurabhi(){ + Map data = jsonDecode('{"operatorId":42254,"centerLocation":"31.66525645-23.2554126","name":"Surabhi Misra","picture":"","age":18,"gender":"Female","phoneNo":"+91123456789","email":"surabhi@gmail.com","ratings":"4.5","reviews":["Good work","She is very hardworking operator","quickly resolved my services"],"isOperatorActive":true,"timestamp":1660465314}'); + final Operator surabhi = Operator.fromJson(data); + return surabhi; + } + +} diff --git a/lib/models/booking_model.dart b/lib/models/booking_model.dart new file mode 100644 index 0000000..d4117d2 --- /dev/null +++ b/lib/models/booking_model.dart @@ -0,0 +1,122 @@ +class Booking { + int? bookingId; + int? operatorId; + int? bookingType; + String? phoneNo; + BookingLocation? bookingLocation; + Userdata? userdata; + Paydata? paydata; + String? bookingStatus; + int? timestamp; + + Booking( + {this.bookingId, + this.operatorId, + this.bookingType, + this.phoneNo, + this.bookingLocation, + this.userdata, + this.paydata, + this.bookingStatus, + this.timestamp}); + + Booking.fromJson(Map json) { + bookingId = json['bookingId']; + operatorId = json['operatorId']; + bookingType = json['bookingType']; + phoneNo = json['phoneNo']; + bookingLocation = json['bookingLocation'] != null + ? new BookingLocation.fromJson(json['bookingLocation']) + : null; + userdata = json['userdata'] != null + ? new Userdata.fromJson(json['userdata']) + : null; + paydata = + json['paydata'] != null ? new Paydata.fromJson(json['paydata']) : null; + bookingStatus = json['bookingStatus']; + timestamp = json['timestamp']; + } + + Map toJson() { + final Map data = new Map(); + data['bookingId'] = this.bookingId; + data['operatorId'] = this.operatorId; + data['bookingType'] = this.bookingType; + data['phoneNo'] = this.phoneNo; + if (this.bookingLocation != null) { + data['bookingLocation'] = this.bookingLocation!.toJson(); + } + if (this.userdata != null) { + data['userdata'] = this.userdata!.toJson(); + } + if (this.paydata != null) { + data['paydata'] = this.paydata!.toJson(); + } + data['bookingStatus'] = this.bookingStatus; + data['timestamp'] = this.timestamp; + return data; + } +} + +class BookingLocation { + double? lat; + double? lon; + + BookingLocation({this.lat, this.lon}); + + BookingLocation.fromJson(Map json) { + lat = json['lat']; + lon = json['lon']; + } + + Map toJson() { + final Map data = new Map(); + data['lat'] = this.lat; + data['lon'] = this.lon; + return data; + } +} + +class Userdata { + String? phoneNo; + int? type; + String? locationText; + + Userdata({this.phoneNo, this.type, this.locationText}); + + Userdata.fromJson(Map json) { + phoneNo = json['phoneNo']; + type = json['type']; + locationText = json['locationText']; + } + + Map toJson() { + final Map data = new Map(); + data['phoneNo'] = this.phoneNo; + data['type'] = this.type; + data['locationText'] = this.locationText; + return data; + } +} + +class Paydata { + String? type; + Null? receipt; + String? status; + + Paydata({this.type, this.receipt, this.status}); + + Paydata.fromJson(Map json) { + type = json['type']; + receipt = json['receipt']; + status = json['status']; + } + + Map toJson() { + final Map data = new Map(); + data['type'] = this.type; + data['receipt'] = this.receipt; + data['status'] = this.status; + return data; + } +} diff --git a/lib/models/operator_model.dart b/lib/models/operator_model.dart new file mode 100644 index 0000000..955453a --- /dev/null +++ b/lib/models/operator_model.dart @@ -0,0 +1,60 @@ +class Operator { + int? operatorId; + String? centerLocation; + String? name; + String? picture; + int? age; + String? gender; + String? phoneNo; + String? email; + String? ratings; + List? reviews; + bool? isOperatorActive; + int? timestamp; + + Operator( + {this.operatorId, + this.centerLocation, + this.name, + this.picture, + this.age, + this.gender, + this.phoneNo, + this.email, + this.ratings, + this.reviews, + this.isOperatorActive, + this.timestamp}); + + Operator.fromJson(Map json) { + operatorId = json['operatorId']; + centerLocation = json['centerLocation']; + name = json['name']; + picture = json['picture']; + age = json['age']; + gender = json['gender']; + phoneNo = json['phoneNo']; + email = json['email']; + ratings = json['ratings']; + reviews = json['reviews'].cast(); + isOperatorActive = json['isOperatorActive']; + timestamp = json['timestamp']; + } + + Map toJson() { + final Map data = new Map(); + data['operatorId'] = this.operatorId; + data['centerLocation'] = this.centerLocation; + data['name'] = this.name; + data['picture'] = this.picture; + data['age'] = this.age; + data['gender'] = this.gender; + data['phoneNo'] = this.phoneNo; + data['email'] = this.email; + data['ratings'] = this.ratings; + data['reviews'] = this.reviews; + data['isOperatorActive'] = this.isOperatorActive; + data['timestamp'] = this.timestamp; + return data; + } +} diff --git a/lib/screens/home_page.dart b/lib/screens/home_page.dart index 6c0c552..190eec1 100644 --- a/lib/screens/home_page.dart +++ b/lib/screens/home_page.dart @@ -8,6 +8,8 @@ import 'package:mera_aadhar/utilities/constants.dart'; import 'package:mera_aadhar/screens/map_basic.dart'; import 'package:provider/provider.dart'; +import 'package:mera_aadhar/testscreens/testfirebase.dart'; + class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); @@ -38,6 +40,14 @@ class HomePage extends StatelessWidget { Provider.of(context,listen: false).logout(context); }, ), + TextButton( + child: Text("Test Firebase"), + onPressed: () { + Navigator.push(context, + MaterialPageRoute(builder: (context) => TestFirescreen())); + }, + ), + ], ), )); diff --git a/lib/testscreens/testfirebase.dart b/lib/testscreens/testfirebase.dart new file mode 100644 index 0000000..1287e5f --- /dev/null +++ b/lib/testscreens/testfirebase.dart @@ -0,0 +1,92 @@ +import 'package:flutter/src/foundation/key.dart'; +import 'package:flutter/src/widgets/framework.dart'; +import 'package:flutter/material.dart'; +import 'package:mera_aadhar/screens/login_type.dart'; +import 'package:mera_aadhar/screens/verification.dart'; +import 'package:mera_aadhar/services/auth/otp_signin.dart'; +import 'package:mera_aadhar/utilities/constants.dart'; +import 'package:mera_aadhar/screens/map_basic.dart'; +import 'package:provider/provider.dart'; + +import 'package:firebase_core/firebase_core.dart'; + +import 'package:mera_aadhar/fixtures/booking_fixture.dart'; +import 'package:mera_aadhar/models/booking_model.dart'; +import 'package:mera_aadhar/firebase/booking_db.dart'; + +import 'package:mera_aadhar/fixtures/operator_fixture.dart'; +import 'package:mera_aadhar/models/operator_model.dart'; +import 'package:mera_aadhar/firebase/operator_db.dart'; + +import 'package:flutter/foundation.dart'; + +class TestFirescreen extends StatelessWidget { + const TestFirescreen({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: SafeArea( + child: Column( + children: [ + TextButton( + child: Text("Add dummy booking for ph 8872276957"), + onPressed: () { + debugPrint("init nwo dummy"); + Booking dummy = BookingFixture.dummyBooking(); + + BookingDB bdb = new BookingDB(); + bdb.addNewBooking(dummy).then((value){ + debugPrint("Successfully added booking!"); + }); + + }, + ), + TextButton( + child: Text("Get current bookin"), + onPressed: () { + BookingDB bdb = new BookingDB(); + bdb.getMyCurrentBooking().then((value){ + debugPrint("What!"); + print(value); + }); + }, + ), + TextButton( + child: Text("install surabhi as operator"), + onPressed: () { + OperatorDB odb = new OperatorDB(); + odb.addNewOperator(OperatorFixture.dummySurabhi()).then((value){ + debugPrint("Added surabhi"); + }); + }, + ), + TextButton( + child: Text("get operators (multiple bruuuh) from center lat/long"), + onPressed: () { + String centerLatlong = "31.66525645-23.2554126"; + OperatorDB odp = new OperatorDB(); + odp.getOperatorsByLatLong(centerLatlong).then((lst){ + print("Length lst " + lst.length.toString()); + }); + }, + ), + TextButton( + child: Text("get operator from operator id"), + onPressed: () { + int surabhiId = 42254; // DEBUG SURABHI ID IS THISSSS LETS MAKE HER WORK HARD AS OPERATOR + + OperatorDB odb = new OperatorDB(); + odb.getOperatorById(surabhiId).then((value){ + debugPrint("Yea surab!"); + print(value); + }); + + }, + ), + ], + ), + )); + + } +} diff --git a/pubspec.lock b/pubspec.lock index 4bd3205..83641ff 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -8,6 +8,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.0.4" + archive: + dependency: transitive + description: + name: archive + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.11" async: dependency: transitive description: @@ -71,6 +78,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" cupertino_icons: dependency: "direct main" description: @@ -85,6 +99,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.2" firebase_auth: dependency: "direct main" description: @@ -127,6 +148,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.7.1" + firebase_database: + dependency: "direct main" + description: + name: firebase_database + url: "https://pub.dartlang.org" + source: hosted + version: "9.1.1" + firebase_database_platform_interface: + dependency: transitive + description: + name: firebase_database_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.2+1" + firebase_database_web: + dependency: transitive + description: + name: firebase_database_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.1+3" firebase_messaging: dependency: "direct main" description: @@ -153,6 +195,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_driver: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" flutter_lints: dependency: "direct dev" description: @@ -205,6 +252,11 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "10.1.0" + fuchsia_remote_debug_protocol: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" http: dependency: transitive description: @@ -219,6 +271,11 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.0.1" + integration_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" intl: dependency: transitive description: @@ -352,6 +409,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.0.0" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" plugin_platform_interface: dependency: transitive description: @@ -373,6 +437,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" proj4dart: dependency: transitive description: @@ -427,6 +498,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + sync_http: + dependency: transitive + description: + name: sync_http + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.0" term_glyph: dependency: transitive description: @@ -454,7 +532,7 @@ packages: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.1" + version: "1.3.0" unicode: dependency: transitive description: @@ -469,6 +547,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + vm_service: + dependency: transitive + description: + name: vm_service + url: "https://pub.dartlang.org" + source: hosted + version: "8.2.2" + webdriver: + dependency: transitive + description: + name: webdriver + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" wkt_parser: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5a981b8..ed0b8b0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -45,8 +45,11 @@ dependencies: location: ^4.4.0 provider: ^6.0.3 flutter_map_marker_popup: any + firebase_database: ^9.1.1 dev_dependencies: + integration_test: + sdk: flutter flutter_test: sdk: flutter