Kaynağa Gözat

added bulk operations for delete and save data in customer module, started auto render list based on data modifications

robert2206 8 yıl önce
ebeveyn
işleme
d2af187eb1

+ 1 - 1
ionic.config.json

@@ -6,7 +6,7 @@
     "proxies": [
         {
             "path": "/",
-            "proxyUrl": "http://192.168.88.123:8069/"
+            "proxyUrl": "http://127.0.0.1:8069/"
         }
     ]
 }

+ 1 - 0
package.json

@@ -33,6 +33,7 @@
     "pouchdb-find": "^0.10.5",
     "rxjs": "5.0.0-beta.12",
     "sw-toolbox": "3.4.0",
+    "transform-pouch": "^1.1.3",
     "zone.js": "0.6.26"
   },
   "devDependencies": {

+ 54 - 11
src/base/base-details-view.ts

@@ -3,24 +3,67 @@ import { PouchService } from "../services/pouch-service";
 import { Observable } from "rxjs/Observable";
 
 export abstract class BaseDetailsView<T> extends BaseView<T> {
-    
 
-    constructor(c: { new (): T; }) {
+    item: T;
+    action: string;
+
+    constructor(c: { new (): T; }, item?: T) {
         super(c, PouchService);
+        
+        this.item = item;
+        this.action = item ? "to_update" : "to_create";
+    }
+
+    /**
+     * 
+     */
+    getItem(): T {
+        return this.item;
+    }
+
+    /**
+     * 
+     * @param item 
+     */
+    setItem(item: T) {
+        this.item = item;
+    }
+
+    /**
+     * 
+     */
+    getAction(): string {
+        return this.action;
+    }
+
+    /**
+     * 
+     * @param action 
+     */
+    setAction(action: string): void {
+        this.action = action;
+    }
+
+    /**
+     * 
+     */
+    itemExists(): boolean {
+        return !!Object.keys(this.getItem()).length;
+    }
+
+    /**
+     * 
+     */
+    cleanItem(): void {
+        this.setItem(Object.create(null));
     }
 
     /**
      * 
      * @param data 
      */
-    save(data: T): Observable<any> {
-        let storableData = {
-            odoo_model: super.getModelName(),
-            records: [
-                data
-            ]
-        };
-
-        return super.getInjectable(PouchService).save(storableData);
+    performSave(): Observable<any> {
+        this.setItem(Object.assign(this.getItem(), { odoo_model: super.getModelName(), odoo_status: this.getAction() }));
+        return super.getInjectable(PouchService).save(this.getItem());
     }
 }

+ 17 - 7
src/base/base-list-view.ts

@@ -1,5 +1,6 @@
 import { BaseView } from "./base-view";
 import { PouchService } from "../services/pouch-service";
+import { Observable } from "rxjs/Observable";
 
 export abstract class BaseListView<T> extends BaseView<T>{
 
@@ -21,17 +22,18 @@ export abstract class BaseListView<T> extends BaseView<T>{
      * 
      */
     initialize() {
-        super.getInjectable(PouchService).getAll(this.getModelName()).subscribe(result => { 
-            let obj = result.docs.shift();
+        let pouch = super.getInjectable(PouchService);
 
-            if (!obj) {
-                return;
-            }
-
-            this.applyFilter(obj.records);
+        pouch.getAll(this.getModelName()).subscribe(result => {
+            this.applyFilter(result.docs);            
         }, error => { 
             console.log(error);
         });
+        
+        pouch.changes().subscribe(result => {
+            console.log("esto es un cambio");
+            console.log(result);
+        }, error => console.log(error));
     }
 
     /**
@@ -80,6 +82,14 @@ export abstract class BaseListView<T> extends BaseView<T>{
             return eval(expressions.join("&&"));
         });
     }
+    
+    /**
+     * 
+     */
+    performDelete(): Observable<any> {
+        // return Observable.empty();
+        return super.getInjectable(PouchService).remove(this.getSelectedItem());
+    }
 
     /**
      * 

+ 17 - 1
src/base/base-view.ts

@@ -1,5 +1,6 @@
 import { ReflectiveInjector } from "@angular/core";
 import { getMetadataStorage } from "../odoo/utils/metadata-storage";
+import { Events } from "ionic-angular";
 
 export class BaseView<T> {
 
@@ -11,10 +12,25 @@ export class BaseView<T> {
     constructor(c: { new (): T; }, ...injectables: any[]) {
         this.type = new c();
         this.model = getMetadataStorage().models.filterByTarget(this.type.constructor).items.shift();
-        this.injector = ReflectiveInjector.resolveAndCreate(injectables);
+        this.injector = ReflectiveInjector.resolveAndCreate(injectables.concat(Events));
         this.title = "Sin título";
     }
 
+    /**
+     * 
+     * @param data 
+     */
+    publishEvent(data: any): void {
+
+    }
+
+    /**
+     * 
+     */
+    subscribeToEvent(): void {
+
+    }
+
     /**
      * 
      * @param injectable 

+ 3 - 0
src/odoo/models/res.partner.ts

@@ -52,4 +52,7 @@ export class Partner {
 
     @OdooField(FieldTypes.CHAR)
     phone: string;
+
+    @OdooField(FieldTypes.CHAR)
+    street: string;
 }

+ 15 - 6
src/pages/customer/customer.ts

@@ -18,16 +18,24 @@ export class CustomerPage extends BaseDetailsView<Partner>{
         public navParams: NavParams,
         public formBuilder: FormBuilder
     ) {
-        super(Partner);
+        super(Partner, navParams.data); 
 
         this.customerForm = this.formBuilder.group({
+            _id: null,
+            _rev: null,
             name: ["", Validators.required],
             city: ["", Validators.maxLength(35)],
             street: ["", Validators.maxLength(35)],
-            mobile: ["", Validators.pattern("^\\d+$")],
-            phone: ["", Validators.pattern("^\\d+$")],
+            // mobile: ["", Validators.pattern("^\\d+$")],
+            // phone: ["", Validators.pattern("^\\d+$")],
+            mobile: ["", Validators.maxLength(13)],
+            phone: ["", Validators.maxLength(13)],
             email: ["", Validators.pattern("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$")]
         });
+
+        if (super.itemExists()) {
+            this.customerForm.patchValue(super.getItem());
+        }
     }
 
     /**
@@ -45,10 +53,11 @@ export class CustomerPage extends BaseDetailsView<Partner>{
         if (this.customerForm.invalid) {
             return;
         }
-        data.customer = true;
 
-        super.save(data).subscribe(result => { 
-            console.log(result);
+        super.setItem(Object.assign(this.getItem(), data, { customer: true }));
+
+        super.performSave().subscribe(result => {
+            this.navCtrl.pop();
         }, error => {
             console.log(error);
         });

+ 26 - 12
src/pages/customers/customers.ts

@@ -1,5 +1,5 @@
 import { Component } from "@angular/core";
-import { NavController, NavParams, ActionSheetController } from "ionic-angular";
+import { NavController, NavParams, ActionSheetController, AlertController } from "ionic-angular";
 
 import { BaseListView } from "../../base/base-list-view";
 import { Partner } from "../../odoo/models/res.partner";
@@ -11,12 +11,11 @@ import { CustomerPage } from "../customer/customer";
 })
 export class CustomersPage extends BaseListView<Partner> {
 
-
-
     constructor(
         public navCtrl: NavController,
         public navParams: NavParams,
-        public actionSheetCtrl: ActionSheetController
+        public actionSheetCtrl: ActionSheetController,
+        public alertController: AlertController
     ) {
         super(Partner, ["customer", "=", true]);
      }
@@ -31,10 +30,6 @@ export class CustomersPage extends BaseListView<Partner> {
      */
     openOptions(item: Partner): void {
         this.setSelectedItem(item);
-
-        console.log(this.getSelectedIndex());
-        console.log(this.getSelectedItem());
-        
         
         this.actionSheetCtrl.create({
             title: "Opciones",
@@ -43,7 +38,7 @@ export class CustomersPage extends BaseListView<Partner> {
                     text: "Abrir",
                     icon: "open",
                     handler: () => {
-
+                        this.goToDetails();
                     }
                 },
                 {
@@ -65,7 +60,7 @@ export class CustomersPage extends BaseListView<Partner> {
                     icon: "close",
                     role: "destructive",
                     handler: () => {
-
+                        this.askIfDelete();
                     }
                 },
                 {
@@ -84,8 +79,27 @@ export class CustomersPage extends BaseListView<Partner> {
      * 
      */
     goToDetails(): void {
-        console.log("Details");
+        this.navCtrl.push(CustomerPage, super.getSelectedItem());
+    }
 
-        this.navCtrl.push(CustomerPage);
+    /**
+     * 
+     */
+    askIfDelete(): void {
+        this.alertController.create({
+            title: "Confirmar",
+            message: "Quieres eliminar este cliente?",
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: () => {
+                        super.performDelete().subscribe(result => console.log(result), error => console.log(error));
+                    }
+                }
+            ]
+        }).present();
     }
 }

+ 34 - 64
src/services/pouch-service.ts

@@ -5,8 +5,6 @@ import PouchDB from "pouchdb";
 
 import * as PouchFind from "pouchdb-find";
 
-import { UUID } from "angular2-uuid";
-
 import "rxjs/add/observable/throw";
 import "rxjs/add/observable/fromPromise";
 import "rxjs/add/observable/from";
@@ -14,12 +12,6 @@ import "rxjs/add/operator/concatMap";
 
 PouchDB.plugin(PouchFind);
 
-export enum SaveAction {
-    Add = 1,
-    Update = 2,
-    Delete = 3
-}
-
 @Injectable()
 export class PouchService {
 
@@ -33,6 +25,7 @@ export class PouchService {
      *
      */
     private initialize(destroy?: boolean): Observable<any> {
+        // PouchDB.debug.enable('pouchdb:api');
         this.pouchDB = new PouchDB("odoo", {
             auto_compaction: true
         });
@@ -44,67 +37,30 @@ export class PouchService {
         }));
     }
 
-
     /**
      *
      */
-    save(data: any, action?: SaveAction): Observable<any> {
+    save(data: any): Observable<any> {
         if (!data.odoo_model) {
             return Observable.throw("Error: Cannot save data without model name");
         }
 
-        return this.getAll(data.odoo_model).concatMap(result => this.pouchDB.post(result.docs.length !== 0 ? Object.assign(result.docs.shift(), { records: data.records }) : data ));
-        // return this.getAll(data.odoo_model).concatMap(r => this.pouchDB.post(this.alignData(r.docs.shift(), data, action)));
+        return Observable.fromPromise(this.pouchDB.post(data));
     }
 
     /**
      * 
-     * @param dataStored 
-     * @param newData 
-     * @param action 
+     * @param data 
+     * @param model 
      */
-    protected alignData(dataStored: any, data: any, action?: SaveAction) {
-        if (!dataStored) {
-            return this.fillRecordsWithUUID(data);
-        }
-
-        if (!action) {
-            dataStored.records = data.records;
-            return this.fillRecordsWithUUID(dataStored);
-        }
-
-        if (action === SaveAction.Add) {
-            dataStored.records = this.fillRecordsWithUUID(data).records.concat(dataStored.records);
-            return dataStored;
-        }
-
-        if (action === SaveAction.Update) {
-
-        }
-
-        if (action === SaveAction.Delete) {
-
-        }
+    bulkSave(data: Array<any>): Observable<any> {
+        return Observable.fromPromise(this.pouchDB.bulkDocs(data));
     }
 
     /**
      * 
      * @param data 
      */
-    protected fillRecordsWithUUID(data: any): any {
-        data.records = data.records.map(record => {
-            record.odoo_id = record.id;
-            record.id = UUID.UUID();
-
-            return record;
-        });
-
-        return data;
-    }
-
-    /**
-     *
-     */
     remove(data: any): Observable<any> {
         if (!data._id || !data._rev) {
             return Observable.throw("Error: Cannot remove data without id or rev");
@@ -114,24 +70,22 @@ export class PouchService {
     }
 
     /**
-     *
+     * 
+     * @param data 
      */
-    removeAll(type: string): Observable<any> {
-        return this.getAll(type).concatMap(result => this.cleanDoc(result.docs.shift()));
+    bulkRemove(data: Array<any>): Observable<any> {
+        return this.bulkSave(data.map(item => {
+            item._deleted = true;
+
+            return item;
+        }));
     }
 
     /**
-     * 
-     * @param doc 
+     *
      */
-    private cleanDoc(doc: any): Observable<any> {
-        if (!doc || !doc.records) {
-            return Observable.empty();
-        }
-
-        doc.records = [];
-        
-        return this.save(doc);
+    removeAll(type: string): Observable<any> {
+        return this.getAll(type).concatMap(result => this.bulkRemove(result.docs));
     }
 
     /**
@@ -171,4 +125,20 @@ export class PouchService {
     destroyAndInitialize() {
         return this.destroy().concatMap(response => this.initialize(response.ok));
     }
+
+    /**
+     * 
+     */
+    changes(): Observable<any> {
+        return Observable.create((o: Observer<any>) => {
+            this.pouchDB.changes({
+                since: "now",
+                live: true
+            }).on("change", change => {
+                o.next(change);
+            }).on("error", error => { 
+                o.next(error);
+            });
+        });
+    }
 }

+ 22 - 7
src/services/sync-service.ts

@@ -61,23 +61,38 @@ export class SyncService {
      *
      */
     removeAll(): Observable<any> {
-        return this.getModels().concatMap(item => this.pouch.removeAll(item.model.name));
+        // return this.getModels().concatMap(item => this.pouch.removeAll(item.model.name));
+        return Observable.empty();
     }
 
     /**
      *
      */
     protected download(): Observable<any> {
+        // return this.getModels()
+        //     .concatMap(item => this.odoo.searchRead(item.model.name, item.model.domain, item.fields.getNames())
+        //     .map(r => {
+        //         delete r.length;
+
+        //         return Object.assign(r, {
+        //             odoo_model: item.model.name,
+        //         });
+        //     }))
+        //     .concatMap(data => this.pouch.save(data));
         return this.getModels()
             .concatMap(item => this.odoo.searchRead(item.model.name, item.model.domain, item.fields.getNames())
-            .map(r => {
-                delete r.length;
-
-                return Object.assign(r, {
-                    odoo_model: item.model.name,
+            .map(response => {
+                return response.records.map(record => {
+                    record.odoo_id = record.id;
+                    record.odoo_model = item.model.name;
+                    record.odoo_status = "to_none";
+
+                    delete record.id;
+                    
+                    return record;
                 });
             }))
-            .concatMap(data => this.pouch.save(data));
+            .concatMap(data => this.pouch.bulkSave(data));
     }
 
     /**