robert2206 8 éve
szülő
commit
32f9a08815

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

@@ -1,6 +1,6 @@
 import { Component } from '@angular/core';
 import { NavController, NavParams, MenuController } from 'ionic-angular';
-import { DownloadService } from "../../services/download-service";
+import { SyncService } from "../../services/sync-service";
 
 @Component({
 	selector: 'page-home',
@@ -12,15 +12,15 @@ export class HomePage {
 		public navCtrl: NavController,
 		public navParams: NavParams,
 		public menuCtrl: MenuController,
-		public download: DownloadService
+		public sync: SyncService
 	) { }
 
   	ionViewDidLoad() {
 		console.log('ionViewDidLoad HomePage');
 		this.menuCtrl.enable(true);
 
-		this.download.do().subscribe(model => { 
-			console.log(model);
+		this.sync.do().subscribe(result => { 
+			console.log(result);
 			
 		}, error => console.error(error));	
   	}

+ 5 - 1
src/services/auth-service.ts

@@ -45,9 +45,13 @@ export class AuthService {
             .concatMap(userInformation => this.storeUserInformation(userInformation));
     }
 
+    /**
+     *
+     */
     skipSignin(): Observable<any> {
         return this.pouch.getAll('user').concatMap(result => this.playUsersInformations(result));
     }
+
     /**
      *
      */
@@ -59,7 +63,7 @@ export class AuthService {
      *
      */
     private normalizeUserInformation(response: any, password: string): Observable<any> {
-        return Observable.create((observer: Observer<any>) => { 
+        return Observable.create((observer: Observer<any>) => {
             observer.next(Object.assign(response, { password: password }));
         });
     }

+ 24 - 8
src/services/download-service.ts

@@ -3,35 +3,51 @@ 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 odoo: OdooService,
+        public pouch: PouchService
+    ) { 
+    }
 
+    /**
+     *
+     */
     do(): Observable<any> {
-        return Observable.from(getMetadataStorage().getModels()).concatMap(item => this.odoo.searchRead(item.model.name, [[[]]]));
+        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 fetch(model: string): Observable<any> {
-        return null;
+    protected login(user: any): Observable<any> {
+        this.odoo.initialize("http://localhost:8100");
+        
+        return this.odoo.login(user.db, user.username, user.password);
     }
 
     /**
      *
      */
-    protected store(model: string): Observable<any> {
-
-        return null;
+    protected store(data: any): Observable<any> {
+        let type = data.type;
+        let records = data.records;
+        
+        return Observable.from(records).concatMap(item => this.pouch.save(Object.assign(item, { type: type })));
     }
 }

+ 30 - 0
src/services/odoo-service.ts

@@ -11,6 +11,36 @@ export class OdooService {
         private odooRPC: OdooRPCService
     ) { }
 
+    /**
+     *
+     */
+    initialize(url: string, port?: number): void {
+        let host = url;
+
+        if (port) {
+            host = host + ":" + port;
+        }
+        
+        this.odooRPC.init({
+            odoo_server: host,
+            http_auth: "username:password"
+        });
+    }
+
+    /**
+     *
+     */
+    login(database: string, username: string, password: string): Observable<any> {
+        return Observable.fromPromise(this.odooRPC.login(database, username, password));
+    }
+
+    /**
+     *
+     */
+    logout(): Observable<any> {
+        return Observable.fromPromise(this.odooRPC.logout());
+    }
+
     /**
      * Example:
      * instance.searchRead('res.partner', [[['is_company', '=', true], ['customer', '=', true]]], { 'fields': ['name', 'comment']})

+ 31 - 3
src/services/pouch-service.ts

@@ -1,11 +1,14 @@
 import { Injectable } from "@angular/core";
 import { Observable } from "rxjs/Observable";
+import { Observer } from "rxjs/Observer";
 import PouchDB from "pouchdb";
 
 import * as PouchFind from "pouchdb-find";
 
 import "rxjs/add/observable/throw";
 import "rxjs/add/observable/fromPromise";
+import "rxjs/add/observable/from";
+import "rxjs/add/operator/concatMap";
 
 PouchDB.plugin(PouchFind);
 
@@ -21,15 +24,16 @@ export class PouchService {
     /**
      *
      */
-    private initialize(): void {
+    private initialize(): Observable<any> {
         this.pouchDB = new PouchDB("odoo", {
             auto_compaction: true
         });
-        this.pouchDB.createIndex({
+
+        return Observable.fromPromise(this.pouchDB.createIndex({
             index: {
                 fields: ['type']
             }
-        }).then(response => console.log(response)).catch(error => console.log(error));
+        }));
     }
 
     /**
@@ -40,6 +44,9 @@ export class PouchService {
             return Observable.throw("Error: Cannot save data without type");
         }
 
+        delete data.__last_update;
+        
+
         return Observable.fromPromise(this.pouchDB.post(data));
     }
 
@@ -54,6 +61,13 @@ export class PouchService {
         return Observable.fromPromise(this.pouchDB.remove(data));
     }
 
+    /**
+     *
+     */
+    removeAll(type: string): Observable<any> {
+        return this.getAll(type).concatMap(result => Observable.from(result.docs)).concatMap(doc => this.remove(Object.assign(doc, { _deleted: true })));
+    }
+
     /**
      *
      */
@@ -77,4 +91,18 @@ export class PouchService {
             }
         }));
     }
+
+    /**
+     *
+     */
+    destroy(): Observable<any> {
+        return Observable.fromPromise(this.pouchDB.destroy());
+    }
+
+    /**
+     *
+     */
+    destroyAndInitialize() {
+        return this.destroy().concat(this.initialize());
+    }
 }

+ 13 - 2
src/services/sync-service.ts

@@ -1,13 +1,19 @@
 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 { getMetadataStorage } from "../odoo/utils/metadata-storage";
+
+import "rxjs/add/operator/concat";
+
 @Injectable()
 export class SyncService {
 
     constructor(
+        public pouch: PouchService,
         public upload: UploadService,
         public download: DownloadService
     ) { }
@@ -16,8 +22,13 @@ export class SyncService {
      *
      */
     do(): Observable<any> {
-        return Observable.create((observer: Observable<any>) => {
+        return this.removeAll().concat(this.download.do());
+    }
 
-        });
+    /**
+     *
+     */
+    removeAll(): Observable<any> {
+        return Observable.from(getMetadataStorage().getModels()).concatMap(item => this.pouch.removeAll(item.model.document));
     }
 }