2020-08-06 08:18:57 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import {TemplateModel} from '../models/template.model';
|
|
|
|
import {ApiService} from './api.service';
|
|
|
|
import {MaterialModel} from '../models/material.model';
|
|
|
|
import {BaseModel} from '../models/base.model';
|
|
|
|
import {UserModel} from '../models/user.model';
|
2020-08-20 10:42:02 +02:00
|
|
|
import {ModelItemModel} from '../models/model-item.model';
|
2020-08-28 09:22:28 +02:00
|
|
|
import {ModelFileModel} from '../models/model-file.model';
|
2020-08-06 08:18:57 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class DataService {
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private api: ApiService
|
|
|
|
) { }
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
private collectionMap = { // List of available collections
|
2020-08-31 16:14:47 +02:00
|
|
|
materials: {path: '/materials?status[]=validated&status[]=new', model: MaterialModel, type: 'idArray'},
|
2020-08-20 10:42:02 +02:00
|
|
|
materialSuppliers: {path: '/material/suppliers', model: null, type: 'idArray'},
|
|
|
|
materialGroups: {path: '/material/groups', model: null, type: 'idArray'},
|
2020-08-10 16:15:17 +02:00
|
|
|
materialTemplates: {path: '/template/materials', model: TemplateModel, type: 'template'},
|
|
|
|
measurementTemplates: {path: '/template/measurements', model: TemplateModel, type: 'template'},
|
|
|
|
conditionTemplates: {path: '/template/conditions', model: TemplateModel, type: 'template'},
|
2020-08-20 10:42:02 +02:00
|
|
|
sampleNotesFields: {path: '/sample/notes/fields', model: TemplateModel, type: 'idArray'},
|
|
|
|
users: {path: '/users', model: UserModel, type: 'idArray'},
|
|
|
|
modelGroups: {path: '/model/groups', model: ModelItemModel, type: 'array'},
|
2020-08-28 09:22:28 +02:00
|
|
|
modelFiles: {path: '/model/files', model: ModelFileModel, type: 'array'},
|
2020-08-10 16:15:17 +02:00
|
|
|
user: {path: '/user', model: UserModel, type: 'string'},
|
|
|
|
userKey: {path: '/user/key', model: BaseModel, type: 'string'}
|
2020-08-06 08:18:57 +02:00
|
|
|
};
|
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
arr: {[key: string]: any[]} = {}; // Array of data
|
|
|
|
latest: {[key: string]: any[]} = {}; // Array of latest template versions
|
|
|
|
id: {[key: string]: {[id: string]: any}} = {}; // Data in format _id: data
|
|
|
|
d: {[key: string]: any} = {}; // Data not in array format
|
2020-08-06 08:18:57 +02:00
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
contact = {name: 'CR/APS1-Lingenfelser', mail: 'dominic.lingenfelser@bosch.com'}; // Global contact data
|
2020-08-20 10:42:02 +02:00
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
load(collection, f = () => {}) { // Load data
|
|
|
|
if (this.arr[collection]) { // Data already loaded
|
2020-08-06 08:18:57 +02:00
|
|
|
f();
|
|
|
|
}
|
2021-01-18 14:26:40 +01:00
|
|
|
else { // Load data
|
2020-08-06 08:18:57 +02:00
|
|
|
this.api.get<any>(this.collectionMap[collection].path, data => {
|
2021-01-18 14:26:40 +01:00
|
|
|
if (this.collectionMap[collection].type !== 'string') { // Array data
|
2020-08-06 08:18:57 +02:00
|
|
|
this.arr[collection] = data
|
2020-09-03 15:51:53 +02:00
|
|
|
.map(
|
|
|
|
e => this.collectionMap[collection].model ?
|
|
|
|
new this.collectionMap[collection].model().deserialize(e) : e
|
|
|
|
);
|
2021-01-18 14:26:40 +01:00
|
|
|
// Load ids
|
2020-08-20 10:42:02 +02:00
|
|
|
if (this.collectionMap[collection].type === 'idArray' || this.collectionMap[collection].type === 'template') {
|
|
|
|
this.idReload(collection);
|
2020-08-10 16:15:17 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
2021-01-18 14:26:40 +01:00
|
|
|
else { // Not array data
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d[collection] = new this.collectionMap[collection].model().deserialize(data);
|
|
|
|
}
|
|
|
|
f();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:23:44 +02:00
|
|
|
|
2021-01-18 14:26:40 +01:00
|
|
|
// Generate id object
|
2020-08-06 15:23:44 +02:00
|
|
|
idReload(collection) {
|
|
|
|
this.id[collection] = this.arr[collection].reduce((s, e) => {s[e._id] = e; return s; }, {});
|
2021-01-18 14:26:40 +01:00
|
|
|
if (this.collectionMap[collection].type === 'template') { // Generate array with latest templates
|
2020-08-20 10:42:02 +02:00
|
|
|
const tmpTemplates = {};
|
|
|
|
this.arr[collection].forEach(template => {
|
2021-01-18 14:26:40 +01:00
|
|
|
if (tmpTemplates[template.first_id]) { // Already found another version
|
2020-08-20 10:42:02 +02:00
|
|
|
if (template.version > tmpTemplates[template.first_id].version) {
|
|
|
|
tmpTemplates[template.first_id] = template;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmpTemplates[template.first_id] = template;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.latest[collection] = Object.values(tmpTemplates);
|
|
|
|
}
|
2020-08-06 15:23:44 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|