2
Fork 0
definma-ui/src/app/models/sample.model.ts

69 lines
1.9 KiB
TypeScript

import pick from 'lodash/pick';
import cloneDeep from 'lodash/cloneDeep';
import {IdModel} from './id.model';
import {MaterialModel} from './material.model';
import {MeasurementModel} from './measurement.model';
import {BaseModel} from './base.model';
export class SampleModel extends BaseModel {
_id: IdModel = null;
color = '';
number = '';
type = '';
batch = '';
condition: {condition_template: string, [prop: string]: string} = {condition_template: null};
material_id: IdModel = null;
material: MaterialModel;
measurements: MeasurementModel[] = [];
note_id: IdModel = null;
user_id: IdModel = null;
selected = false;
notes: {
comment: string,
sample_references: {sample_id: IdModel, relation: string}[],
custom_fields: {[prop: string]: string}
} = {comment: '', sample_references: [], custom_fields: {}};
status = '';
added: Date = null;
deserialize(input: any): this {
Object.assign(this, input);
if (input.hasOwnProperty('material')) {
this.material = new MaterialModel().deserialize(input.material);
this.material_id = input.material._id;
}
if (input.hasOwnProperty('measurements')) {
this.measurements = input.measurements.map(e => new MeasurementModel().deserialize(e));
}
if (input.hasOwnProperty('added')) {
this.added = new Date(input.added);
}
return this;
}
sendFormat(pickCondition = true) {
const pickFields = ['color', 'type', 'batch', 'material_id', 'notes'];
if (pickCondition) {
pickFields.push('condition');
}
const tmp = pick(this.conditionTemplateCheck(), pickFields);
Object.keys(tmp).forEach(key => {
if (tmp[key] === undefined) {
delete tmp[key];
}
});
if (this.material && this.material.name === undefined) {
delete tmp.material_id;
}
return tmp;
}
private conditionTemplateCheck() {
const res = cloneDeep(this);
if (res.condition.condition_template === null) {
res.condition = {};
}
return res;
}
}