|
@@ -5,6 +5,8 @@ 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";
|
|
@@ -12,6 +14,12 @@ import "rxjs/add/operator/concatMap";
|
|
|
|
|
|
PouchDB.plugin(PouchFind);
|
|
|
|
|
|
+export enum SaveAction {
|
|
|
+ Add = 1,
|
|
|
+ Update = 2,
|
|
|
+ Delete = 3
|
|
|
+}
|
|
|
+
|
|
|
@Injectable()
|
|
|
export class PouchService {
|
|
|
|
|
@@ -36,15 +44,62 @@ export class PouchService {
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- save(data: any): Observable<any> {
|
|
|
+ save(data: any, action?: SaveAction): Observable<any> {
|
|
|
if (!data.odoo_model) {
|
|
|
return Observable.throw("Error: Cannot save data without model name");
|
|
|
}
|
|
|
|
|
|
- return Observable.fromPromise(this.pouchDB.post(data));
|
|
|
+ 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)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param dataStored
|
|
|
+ * @param newData
|
|
|
+ * @param action
|
|
|
+ */
|
|
|
+ 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) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -62,16 +117,21 @@ export class PouchService {
|
|
|
*
|
|
|
*/
|
|
|
removeAll(type: string): Observable<any> {
|
|
|
- // return this.getAll(type).concatMap(result => Observable.from(result.docs)).concatMap(doc => this.remove(Object.assign(doc, { _deleted: true })));
|
|
|
- return this.getAll(type).concatMap(result => this.test(result.docs.shift()));
|
|
|
+ return this.getAll(type).concatMap(result => this.cleanDoc(result.docs.shift()));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param doc
|
|
|
+ */
|
|
|
+ private cleanDoc(doc: any): Observable<any> {
|
|
|
+ if (!doc || !doc.records) {
|
|
|
+ return Observable.empty();
|
|
|
+ }
|
|
|
|
|
|
- private test(doc: any): Observable<any> {
|
|
|
- return Observable.create((o: Observer<any>) => {
|
|
|
- o.next(doc);
|
|
|
- o.complete();
|
|
|
- });
|
|
|
+ doc.records = [];
|
|
|
+
|
|
|
+ return this.save(doc);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -89,9 +149,6 @@ export class PouchService {
|
|
|
*
|
|
|
*/
|
|
|
getAll(odoo_model: string): Observable<any> {
|
|
|
- console.log(odoo_model);
|
|
|
-
|
|
|
-
|
|
|
return Observable.fromPromise(this.pouchDB.find({
|
|
|
selector: {
|
|
|
odoo_model: {
|