Browse Source

auth rewrited and sync engine fused in the same file

robert2206 8 years ago
parent
commit
723e168c4d

+ 0 - 4
src/app/app.module.ts

@@ -15,8 +15,6 @@ import { AuthService } from "../services/auth-service";
 import { OdooService } from "../services/odoo-service";
 import { PouchService } from "../services/pouch-service";
 import { SyncService } from "../services/sync-service";
-import { DownloadService } from "../services/download-service";
-import { UploadService } from "../services/upload-service";
 
 // Pipes
 import { MenuPipe } from "../pipes/menu";
@@ -50,8 +48,6 @@ import { MenuPipe } from "../pipes/menu";
         OdooService,
         PouchService,
         SyncService,
-        DownloadService,
-        UploadService,
         {
             provide: ErrorHandler,
             useClass: IonicErrorHandler

+ 1 - 0
src/enums/doc-status.ts

@@ -1,4 +1,5 @@
 export const enum DocStatus {
+    None = 0,
     Created = 1,
     Updated = 2,
     Deleted = 3

+ 1 - 1
src/pages/home/home.ts

@@ -19,7 +19,7 @@ export class HomePage {
 
   	ionViewDidLoad() {
 		console.log('ionViewDidLoad HomePage');
-		this.menuCtrl.enable(true);
+		this.menuCtrl.enable(true);	
 
 		this.sync.do().subscribe(result => { 
 			// result.count().subscribe(x => {

+ 6 - 6
src/pages/login/login.ts

@@ -42,11 +42,9 @@ export class LoginPage {
 	ionViewDidLoad() {
 		this.menuCtrl.enable(false);
 
-		this.auth.skipSignin().subscribe(result => { 
-			if (result.count > 0) {
-				this.navCtrl.setRoot(HomePage);
-			}
-		}, error => console.log(error));
+		if (this.auth.skipSignin()) {
+			this.navCtrl.setRoot(HomePage);
+		}
 	}
 	
 	/**
@@ -64,8 +62,10 @@ export class LoginPage {
 	 *
 	 */
 	signinSuccess(data: any): void {
-		this.loader.dismiss();
 		console.log(data);
+
+		this.loader.dismiss();
+		this.navCtrl.setRoot(HomePage);
 	}
 
 	/**

+ 9 - 30
src/services/auth-service.ts

@@ -7,7 +7,8 @@ import { PouchService } from "./pouch-service";
 import { SyncService } from "./sync-service";
 
 import "rxjs/add/observable/fromPromise";
-import "rxjs/add/operator/concatMap";
+import "rxjs/add/observable/empty";
+import "rxjs/add/operator/concat"
 
 @Injectable()
 export class AuthService {
@@ -41,15 +42,14 @@ export class AuthService {
         this.initialize(loginInformation.host);
 
         return Observable.fromPromise(this.odooRPC.login(loginInformation.database, loginInformation.username, loginInformation.password))
-            .concatMap(response => this.normalizeUserInformation(response, loginInformation.password))
-            .concatMap(userInformation => this.storeUserInformation(userInformation));
+            .concat(this.store(loginInformation));
     }
 
     /**
      *
      */
-    skipSignin(): Observable<any> {
-        return this.pouch.getAll('user').concatMap(result => this.playUsersInformations(result));
+    skipSignin(): boolean {
+        return !!localStorage.getItem("odoo");
     }
 
     /**
@@ -62,30 +62,9 @@ export class AuthService {
     /**
      *
      */
-    private normalizeUserInformation(response: any, password: string): Observable<any> {
-        return Observable.create((observer: Observer<any>) => {
-            observer.next(Object.assign(response, { password: password }));
-        });
-    }
-
-    /**
-     *
-     */
-    private storeUserInformation(userInformation: any): Observable<any> {
-        return this.pouch.save(Object.assign(userInformation, { type: 'user' }));
-    }
-
-    /**
-     *
-     */
-    private playUsersInformations(result: any): Observable<any> {
-        let users: any = {
-            count: result.docs.length,
-            users: result.docs
-        }
-
-        return Observable.create((observer: Observer<any>) => { 
-            observer.next(users);
-        });
+    private store(loginInformation: any): Observable<any> {
+        localStorage.setItem("odoo", JSON.stringify(loginInformation));
+        
+        return Observable.empty();
     }
 }

+ 0 - 58
src/services/download-service.ts

@@ -1,58 +0,0 @@
-import { Injectable } from "@angular/core";
-import { Observable } from "rxjs/Observable";
-import { Observer } from "rxjs/Observer";
-
-import { OdooService } from "./odoo-service";
-import { PouchService } from "./pouch-service";
-
-import { getMetadataStorage } from "../odoo/utils/metadata-storage";
-
-import "rxjs/add/observable/from";
-import "rxjs/add/operator/concatMap";
-import "rxjs/add/operator/map";
-
-@Injectable()
-export class DownloadService {
-
-    constructor(
-        public odoo: OdooService,
-        public pouch: PouchService
-    ) { 
-    }
-
-    /**
-     *
-     */
-    do(): Observable<any> {
-        return this.pouch.getAll("user")
-            .concatMap(users => this.login(users.docs.shift()))
-            .concatMap(logged => Observable.from(getMetadataStorage().getModels()))
-            .concatMap(item => this.odoo.searchRead(item.model.name, []).map(r => {
-                return Object.assign(r, { type: item.model.document });
-            })).concatMap(data => this.store(data));
-    }
-
-    /**
-     *
-     */
-    protected login(user: any): Observable<any> {
-        this.odoo.initialize("http://localhost:8100");
-        
-        return this.odoo.login(user.db, user.username, user.password);
-    }
-
-    /**
-     *
-     */
-    protected store(data: any): Observable<any> {
-        let type = data.type;
-        let records = data.records;
-        
-        return Observable.from(records).map((r: any) => {
-            r.remote_id = r.id;
-            delete r.id;
-            
-            return r;
-        }).concatMap(item => this.pouch.save(Object.assign(item, { type: type, doc_state: "none" })));
-    }
-}

+ 102 - 8
src/services/sync-service.ts

@@ -2,34 +2,128 @@ import { Injectable } from "@angular/core";
 import { Observable } from "rxjs/Observable";
 
 import { PouchService } from "./pouch-service";
-import { UploadService } from "../services/upload-service";
-import { DownloadService } from "../services/download-service";
+import { OdooService } from "../services/odoo-service";
+
+import { DocStatus } from "../enums/doc-status";
 
 import { getMetadataStorage } from "../odoo/utils/metadata-storage";
 
+import "rxjs/add/observable/from";
+import "rxjs/add/operator/filter";
+import "rxjs/add/operator/concatMap";
 import "rxjs/add/operator/concat";
+import "rxjs/add/operator/map";
+import "rxjs/add/operator/groupBy";
+import "rxjs/add/operator/mergeAll";
 
 @Injectable()
 export class SyncService {
 
     constructor(
         public pouch: PouchService,
-        public upload: UploadService,
-        public download: DownloadService
+        public odoo: OdooService
     ) { }
 
     /**
      *
      */
     do(): Observable<any> {
-        // return this.removeAll().concat(this.download.do());
-        return this.upload.do();
+        return this.login().concat(this.removeAll()).concat(this.download());
+    }
+
+    /**
+     *
+     */
+    protected login(): Observable<any> {
+        let loginInformation = JSON.parse(localStorage.getItem("odoo"));
+
+        this.odoo.initialize(loginInformation.host);
+        return this.odoo.login(loginInformation.database, loginInformation.username, loginInformation.password);
+    }
+
+    /**
+     *
+     */
+    protected logout(): Observable<any> {
+        return this.odoo.logout();
+    }
+
+    /**
+     *
+     */
+    protected getModels(): Observable<any> {
+        return Observable.from(getMetadataStorage().getModels());
+    }
+
+    /**
+     *
+     */
+    protected removeAll(): Observable<any> {
+        return this.getModels().concatMap(item => this.pouch.removeAll(item.model.document));
+    }
+
+    /**
+     *
+     */
+    protected download(): Observable<any> {
+        return this.getModels()
+            .concatMap(item => this.odoo.searchRead(item.model.name, []).map(r => {
+                return Object.assign(r, { type: item.model.document });
+            }))
+            .concatMap(data => this.store(data));
+    }
+
+    /**
+     *
+     */
+    protected store(data: any): Observable<any> {
+        let type = data.type;
+        let records = data.records;
+        
+        return Observable.from(records).map((r: any) => {
+            r.remote_id = r.id;
+            delete r.id;
+            
+            return r;
+        }).concatMap(item => this.pouch.save(Object.assign(item, {
+            type: type,
+            doc_status: DocStatus.None
+        })));
+    }
+
+    /**
+     *
+     */
+    protected upload(): Observable<any> {
+        return this.getModels()
+            .concatMap(item => this.pouch.getAll(item.model.document))
+            .concatMap(result => Observable.from(result.docs).filter((r: any) => {
+                return r.doc_status != DocStatus.None;
+            }).groupBy((r: any) => {
+                return r.doc_status;
+            }))
+            .mergeAll()
+            .concatMap(doc => this.send(doc));
     }
 
     /**
      *
      */
-    removeAll(): Observable<any> {
-        return Observable.from(getMetadataStorage().getModels()).concatMap(item => this.pouch.removeAll(item.model.document));
+    protected send(data: any): Observable<any> {
+        let id: number = data.remote_id;
+        let model: string = data.type;
+
+        delete data.type;
+        delete data.remote_id;
+
+        if (data.doc_state === "created") {
+            return this.odoo.create(model, data);
+        }
+        
+        if (data.doc_state === "updated") {
+            return this.odoo.write(model, [[id], data]);
+        } 
+
+        return this.odoo.unlink(model, [[id]]);
     }
 }

+ 0 - 64
src/services/upload-service.ts

@@ -1,64 +0,0 @@
-import { Injectable } from "@angular/core";
-import { Observable } from "rxjs/Observable";
-import { Observer } from "rxjs/Observer";
-
-import { PouchService } from "../services/pouch-service";
-import { OdooService } from "../services/odoo-service";
-
-import { getMetadataStorage } from "../odoo/utils/metadata-storage";
-
-import "rxjs/add/operator/filter";
-import "rxjs/add/observable/from";
-import "rxjs/add/operator/concatMap";
-import "rxjs/add/operator/map";
-import "rxjs/add/operator/groupBy";
-import "rxjs/add/operator/mergeAll";
-
-@Injectable()
-export class UploadService {
-
-    constructor(
-        public pouch: PouchService,
-        public odoo: OdooService
-    ) { }
-
-    do(): Observable<any> {
-        return this.pouch.getAll("user")
-            .concatMap(users => this.login(users.docs.shift()))
-            .concatMap(logged => Observable.from(getMetadataStorage().getModels()))
-            .concatMap(item => this.pouch.getAll(item.model.document))
-            .concatMap(result => Observable.from(result.docs).filter((r: any) => {
-                return r.doc_state == "none";
-            }).groupBy((r: any) => {
-                return r.doc_state;
-            })).mergeAll().concatMap(doc => this.processDocument(doc));    
-    }
-    
-    /**
-     *
-     */
-    private login(user: any): Observable<any> {
-        this.odoo.initialize("http://localhost:8100");
-        
-        return this.odoo.login(user.db, user.username, user.password);
-    }
-
-    /**
-     *
-     */
-    protected processDocument(doc: any): Observable<any> {
-        let id: number = doc.remote_id;
-        let model: string = doc.type;
-
-        delete doc.type;
-        delete doc.remote_id;
-
-        if (doc.doc_state === "created") {
-            this.odoo.create(model, doc);
-        } else if (doc.doc_state === "updated") {
-            return this.odoo.write(model, [[id], doc]);
-        } 
-
-        return this.odoo.unlink(model, [[id]]);
-    }
-}